1 /* $NetBSD: sem.c,v 1.3 2003/11/19 00:40:03 uwe Exp $ */
3 /****************************************************************************
5 * Copyright (C) 2000 Jason Evans <jasone@freebsd.org>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice(s), this list of conditions and the following disclaimer as
13 * the first lines of this file unmodified other than the possible
14 * addition of one or more copyright notices.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice(s), this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ****************************************************************************
36 * $FreeBSD: src/lib/libpthread/test/sem_d.c,v 1.2 2001/05/20 23:11:09 jasone Exp $
38 ****************************************************************************/
44 #include <semaphore.h>
55 pthread_t self
= pthread_self();
56 sem_t
* sem
= (sem_t
*) a_arg
;
59 printf("Thread %p waiting for semaphore...\n", self
);
63 printf("Thread %p got semaphore\n", self
);
69 static void ksem(void);
70 static void usem(void);
76 assert(-1 != sysconf(_SC_SEMAPHORES
));
79 printf("Test begin\n");
94 pthread_t threads
[NTHREADS
];
99 assert(0 == sem_init(&sem_b
, 0, 0));
100 assert(0 == sem_getvalue(&sem_b
, &val
));
103 assert(0 == sem_post(&sem_b
));
104 assert(0 == sem_getvalue(&sem_b
, &val
));
107 assert(0 == sem_wait(&sem_b
));
108 assert(-1 == sem_trywait(&sem_b
));
109 assert(EAGAIN
== errno
);
110 assert(0 == sem_post(&sem_b
));
111 assert(0 == sem_trywait(&sem_b
));
112 assert(0 == sem_post(&sem_b
));
113 assert(0 == sem_wait(&sem_b
));
114 assert(0 == sem_post(&sem_b
));
117 assert(0 == sem_destroy(&sem_b
));
119 assert(0 == sem_init(&sem_a
, 0, 0));
121 for (i
= 0; i
< NTHREADS
; i
++) {
122 pthread_create(&threads
[i
], NULL
, entry
, (void *) &sem_a
);
125 for (i
= 0; i
< NTHREADS
; i
++) {
128 printf("main loop 1: posting...\n");
130 assert(0 == sem_post(&sem_a
));
133 for (i
= 0; i
< NTHREADS
; i
++) {
134 pthread_join(threads
[i
], NULL
);
137 for (i
= 0; i
< NTHREADS
; i
++) {
138 pthread_create(&threads
[i
], NULL
, entry
, (void *) &sem_a
);
141 for (i
= 0; i
< NTHREADS
; i
++) {
144 printf("main loop 2: posting...\n");
146 assert(0 == sem_post(&sem_a
));
149 for (i
= 0; i
< NTHREADS
; i
++) {
150 pthread_join(threads
[i
], NULL
);
153 assert(0 == sem_destroy(&sem_a
));
162 (void)sem_unlink("/foo");
163 sem
= sem_open("/foo", O_CREAT
| O_EXCL
, 0644, 0);
164 assert(sem
!= SEM_FAILED
);
165 assert(-1 != sem_close(sem
));
166 assert(-1 != sem_unlink("/foo"));