Don't use .Xo/.Xc. Fix date format.
[netbsd-mini2440.git] / regress / lib / libpthread / sem / sem.c
blobbe0c7dac4f501ab7541f53a5f11d12999f363c52
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>.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
18 * distribution.
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 ****************************************************************************
34 * sem test.
36 * $FreeBSD: src/lib/libpthread/test/sem_d.c,v 1.2 2001/05/20 23:11:09 jasone Exp $
38 ****************************************************************************/
40 #include <assert.h>
41 #include <stdio.h>
42 #include <fcntl.h>
43 #include <errno.h>
44 #include <semaphore.h>
45 #include <pthread.h>
46 #include <unistd.h>
48 #define NTHREADS 10
50 #define _LIBC_R_
52 static void *
53 entry(void * a_arg)
55 pthread_t self = pthread_self();
56 sem_t * sem = (sem_t *) a_arg;
58 #ifdef DEBUG
59 printf("Thread %p waiting for semaphore...\n", self);
60 #endif
61 sem_wait(sem);
62 #ifdef DEBUG
63 printf("Thread %p got semaphore\n", self);
64 #endif
66 return NULL;
69 static void ksem(void);
70 static void usem(void);
72 int
73 main()
76 assert(-1 != sysconf(_SC_SEMAPHORES));
78 #ifdef DEBUG
79 printf("Test begin\n");
80 #endif
81 usem();
82 ksem();
84 #ifdef DEBUG
85 printf("Test end\n");
86 #endif
87 return 0;
90 static void
91 usem()
93 sem_t sem_a, sem_b;
94 pthread_t threads[NTHREADS];
95 unsigned i;
96 int val;
98 /* userland sem */
99 assert(0 == sem_init(&sem_b, 0, 0));
100 assert(0 == sem_getvalue(&sem_b, &val));
101 assert(0 == val);
103 assert(0 == sem_post(&sem_b));
104 assert(0 == sem_getvalue(&sem_b, &val));
105 assert(1 == 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++) {
126 sleep(1);
127 #ifdef DEBUG
128 printf("main loop 1: posting...\n");
129 #endif
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++) {
142 sleep(1);
143 #ifdef DEBUG
144 printf("main loop 2: posting...\n");
145 #endif
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));
157 static void
158 ksem()
160 sem_t *sem;
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"));