2 * Copyright (C) 2005 David Xu <davidxu@freebsd.org>.
3 * Copyright (C) 2000 Jason Evans <jasone@freebsd.org>.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice(s), this list of conditions and the following disclaimer as
11 * the first lines of this file unmodified other than the possible
12 * addition of one or more copyright notices.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice(s), this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * $DragonFly: src/lib/libthread_xu/thread/thr_sem.c,v 1.6 2007/06/26 23:30:05 josepht Exp $
33 #include "namespace.h"
34 #include <machine/tls.h>
35 #include <sys/semaphore.h>
43 #include "un-namespace.h"
44 #include "thr_private.h"
47 * Semaphore definitions.
51 volatile umtx_t count
;
56 #define SEM_MAGIC ((u_int32_t) 0x09fa4012)
62 sem_check_validity(sem_t
*sem
)
65 if ((sem
!= NULL
) && ((*sem
)->magic
== SEM_MAGIC
)) {
74 sem_alloc(unsigned int value
, int pshared
)
79 if (value
> SEM_VALUE_MAX
) {
84 static __thread sem_t sem_base
;
85 static __thread
int sem_count
;
87 if (sem_base
== NULL
) {
88 sem_base
= mmap(NULL
, getpagesize(),
89 PROT_READ
| PROT_WRITE
,
90 MAP_ANON
| MAP_SHARED
,
92 sem_count
= getpagesize() / sizeof(*sem
);
99 sem
= malloc(sizeof(struct sem
));
106 sem
->magic
= SEM_MAGIC
;
107 sem
->count
= (u_int32_t
)value
;
113 _sem_init(sem_t
*sem
, int pshared
, unsigned int value
)
115 (*sem
) = sem_alloc(value
, pshared
);
122 _sem_destroy(sem_t
*sem
)
124 if (sem_check_validity(sem
) != 0)
129 switch ((*sem
)->semid
) {
134 /* memory is left intact */
141 _sem_getvalue(sem_t
* __restrict sem
, int * __restrict sval
)
143 if (sem_check_validity(sem
) != 0)
146 *sval
= (*sem
)->count
;
151 _sem_trywait(sem_t
*sem
)
155 if (sem_check_validity(sem
) != 0)
158 while ((val
= (*sem
)->count
) > 0) {
159 if (atomic_cmpset_int(&(*sem
)->count
, val
, val
- 1))
167 _sem_wait(sem_t
*sem
)
169 struct pthread
*curthread
;
170 int val
, oldcancel
, retval
;
172 if (sem_check_validity(sem
) != 0)
175 curthread
= tls_get_curthread();
176 _pthread_testcancel();
178 while ((val
= (*sem
)->count
) > 0) {
179 if (atomic_cmpset_acq_int(&(*sem
)->count
, val
, val
- 1))
182 oldcancel
= _thr_cancel_enter(curthread
);
183 retval
= _thr_umtx_wait(&(*sem
)->count
, 0, NULL
, 0);
184 _thr_cancel_leave(curthread
, oldcancel
);
185 } while (retval
== 0);
192 _sem_timedwait(sem_t
* __restrict sem
, struct timespec
* __restrict abstime
)
194 struct timespec ts
, ts2
;
195 struct pthread
*curthread
;
196 int val
, oldcancel
, retval
;
198 if (sem_check_validity(sem
) != 0)
201 curthread
= tls_get_curthread();
204 * The timeout argument is only supposed to
205 * be checked if the thread would have blocked.
207 _pthread_testcancel();
209 while ((val
= (*sem
)->count
) > 0) {
210 if (atomic_cmpset_acq_int(&(*sem
)->count
, val
, val
- 1))
213 if (abstime
== NULL
) {
217 clock_gettime(CLOCK_REALTIME
, &ts
);
218 TIMESPEC_SUB(&ts2
, abstime
, &ts
);
219 oldcancel
= _thr_cancel_enter(curthread
);
220 retval
= _thr_umtx_wait(&(*sem
)->count
, 0, &ts2
,
222 _thr_cancel_leave(curthread
, oldcancel
);
223 } while (retval
== 0);
230 _sem_post(sem_t
*sem
)
234 if (sem_check_validity(sem
) != 0)
238 * sem_post() is required to be safe to call from within
239 * signal handlers, these code should work as that.
243 } while (!atomic_cmpset_acq_int(&(*sem
)->count
, val
, val
+ 1));
244 _thr_umtx_wake(&(*sem
)->count
, val
+ 1);
248 __strong_reference(_sem_destroy
, sem_destroy
);
249 __strong_reference(_sem_getvalue
, sem_getvalue
);
250 __strong_reference(_sem_init
, sem_init
);
251 __strong_reference(_sem_trywait
, sem_trywait
);
252 __strong_reference(_sem_wait
, sem_wait
);
254 __strong_reference(_sem_timedwait
, sem_timedwait
);
256 __strong_reference(_sem_post
, sem_post
);