2 * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991
27 * Permission to use, copy, modify, and distribute this software and
28 * its documentation for any purpose and without fee is hereby granted,
29 * provided that the above copyright notice appears in all copies and
30 * that both the copyright notice and this permission notice appear in
31 * supporting documentation.
33 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
34 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35 * FOR A PARTICULAR PURPOSE.
37 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
38 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
39 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
40 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
41 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
49 * POSIX Pthread Library
54 #include <stdio.h> /* For printf(). */
56 #include <errno.h> /* For __mach_errno_addr() prototype. */
59 #include <sys/resource.h>
60 #include <sys/sysctl.h>
61 #include <sys/queue.h>
62 #include <machine/vmparam.h>
63 #include <mach/vm_statistics.h>
65 extern int __unix_conforming
;
66 extern int _pthread_setcancelstate_internal(int state
, int *oldstate
, int conforming
);
67 extern void _pthread_testcancel(pthread_t thread
, int isconforming
);
68 extern int _pthread_cond_wait(pthread_cond_t
*cond
,
69 pthread_mutex_t
*mutex
,
70 const struct timespec
*abstime
,
73 extern int __sigwait(const sigset_t
*set
, int *sig
);
74 extern int __pthread_sigmask(int, const sigset_t
*, sigset_t
*);
75 extern int __pthread_markcancel(int);
77 #ifdef VARIANT_CANCELABLE
78 extern int __semwait_signal(int cond_sem
, int mutex_sem
, int timeout
, int relative
, __int64_t tv_sec
, __int32_t tv_nsec
);
80 extern int __semwait_signal(int cond_sem
, int mutex_sem
, int timeout
, int relative
, __int64_t tv_sec
, __int32_t tv_nsec
) __asm__("___semwait_signal_nocancel");
83 #ifndef VARIANT_CANCELABLE
89 pthread_cancel(pthread_t thread
)
92 if (__unix_conforming
== 0)
93 __unix_conforming
= 1;
94 #endif /* __DARWIN_UNIX03 */
96 if (_pthread_lookup_thread(thread
, NULL
, 0) != 0)
99 /* if the thread is a workqueue thread, then return error */
100 if (thread
->wqthread
!= 0) {
107 state
= thread
->cancel_state
|= _PTHREAD_CANCEL_PENDING
;
108 UNLOCK(thread
->lock
);
109 if (state
& PTHREAD_CANCEL_ENABLE
)
110 __pthread_markcancel(_pthread_kernel_thread(thread
));
111 #else /* __DARWIN_UNIX03 */
112 thread
->cancel_state
|= _PTHREAD_CANCEL_PENDING
;
113 #endif /* __DARWIN_UNIX03 */
118 pthread_testcancel(void)
120 pthread_t self
= pthread_self();
123 if (__unix_conforming
== 0)
124 __unix_conforming
= 1;
125 _pthread_testcancel(self
, 1);
126 #else /* __DARWIN_UNIX03 */
127 _pthread_testcancel(self
, 0);
128 #endif /* __DARWIN_UNIX03 */
132 * Query/update the cancelability 'state' of a thread
135 pthread_setcancelstate(int state
, int *oldstate
)
138 if (__unix_conforming
== 0) {
139 __unix_conforming
= 1;
141 return (_pthread_setcancelstate_internal(state
, oldstate
, 1));
142 #else /* __DARWIN_UNIX03 */
143 return (_pthread_setcancelstate_internal(state
, oldstate
, 0));
144 #endif /* __DARWIN_UNIX03 */
148 * Query/update the cancelability 'type' of a thread
151 pthread_setcanceltype(int type
, int *oldtype
)
156 if (__unix_conforming
== 0)
157 __unix_conforming
= 1;
158 #endif /* __DARWIN_UNIX03 */
160 if ((type
!= PTHREAD_CANCEL_DEFERRED
) &&
161 (type
!= PTHREAD_CANCEL_ASYNCHRONOUS
))
163 self
= pthread_self();
166 *oldtype
= self
->cancel_state
& _PTHREAD_CANCEL_TYPE_MASK
;
167 self
->cancel_state
&= ~_PTHREAD_CANCEL_TYPE_MASK
;
168 self
->cancel_state
|= type
;
171 _pthread_testcancel(self
, 0); /* See if we need to 'die' now... */
172 #endif /* __DARWIN_UNIX03 */
177 pthread_sigmask(int how
, const sigset_t
* set
, sigset_t
* oset
)
182 if (__pthread_sigmask(how
, set
, oset
) == -1) {
186 #else /* __DARWIN_UNIX03 */
187 return(__pthread_sigmask(how
, set
, oset
));
188 #endif /* __DARWIN_UNIX03 */
191 #endif /* VARIANT_CANCELABLE */
196 __posix_join_cleanup(void *arg
)
198 pthread_t thread
= (pthread_t
)arg
;
201 /* leave another thread to join */
202 thread
->joiner
= (struct _pthread
*)NULL
;
203 UNLOCK(thread
->lock
);
206 #endif /* __DARWIN_UNIX03 */
209 * Wait for a thread to terminate and obtain its exit value.
212 pthread_join(pthread_t thread
,
216 pthread_t self
= pthread_self();
220 kern_return_t kern_res
;
224 if (__unix_conforming
== 0)
225 __unix_conforming
= 1;
227 #ifdef VARIANT_CANCELABLE
228 _pthread_testcancel(self
, 1);
229 #endif /* VARIANT_CANCELABLE */
230 #endif /* __DARWIN_UNIX03 */
232 if ((res
= _pthread_lookup_thread(thread
, &kthport
, 1)) != 0)
235 if (thread
->sig
== _PTHREAD_SIG
) {
236 semaphore_t death
= SEMAPHORE_NULL
; /* in case we need it */
237 semaphore_t joinsem
= SEMAPHORE_NULL
;
239 if (thread
->joiner_notify
== SEMAPHORE_NULL
) {
240 death
= (semaphore_t
)os_get_cached_semaphore();
244 if ((thread
->detached
& PTHREAD_CREATE_JOINABLE
) &&
245 (thread
->joiner
== NULL
)) {
246 PTHREAD_ASSERT(_pthread_kernel_thread(thread
) == kthport
);
247 if (thread
!= self
&& (self
== NULL
|| self
->joiner
!= thread
)) {
248 if (thread
->joiner_notify
== SEMAPHORE_NULL
) {
249 thread
->joiner_notify
= death
;
250 death
= SEMAPHORE_NULL
;
252 joinsem
= thread
->joiner_notify
;
253 thread
->joiner
= self
;
254 UNLOCK(thread
->lock
);
256 if (death
!= SEMAPHORE_NULL
) {
257 os_put_cached_semaphore((os_semaphore_t
)death
);
258 death
= SEMAPHORE_NULL
;
261 /* Wait for it to signal... */
262 pthread_cleanup_push(__posix_join_cleanup
, (void *)thread
);
264 res
= __semwait_signal(joinsem
, 0, 0, 0, (int64_t)0, (int32_t)0);
265 } while ((res
< 0) && (errno
== EINTR
));
266 pthread_cleanup_pop(0);
267 #else /* __DARWIN_UNIX03 */
268 /* Wait for it to signal... */
270 kern_res
= semaphore_wait(joinsem
);
271 } while (kern_res
!= KERN_SUCCESS
);
272 #endif /* __DARWIN_UNIX03 */
274 os_put_cached_semaphore((os_semaphore_t
)joinsem
);
275 res
= _pthread_join_cleanup(thread
, value_ptr
, conforming
);
277 UNLOCK(thread
->lock
);
281 UNLOCK(thread
->lock
);
284 if (death
!= SEMAPHORE_NULL
) {
285 os_put_cached_semaphore((os_semaphore_t
)death
);
293 pthread_cond_wait(pthread_cond_t
*cond
,
294 pthread_mutex_t
*mutex
)
299 if (__unix_conforming
== 0)
300 __unix_conforming
= 1;
302 #ifdef VARIANT_CANCELABLE
304 #else /* !VARIANT_CANCELABLE */
306 #endif /* VARIANT_CANCELABLE */
307 #else /* __DARWIN_UNIX03 */
309 #endif /* __DARWIN_UNIX03 */
310 return (_pthread_cond_wait(cond
, mutex
, (struct timespec
*)NULL
, 0, conforming
));
314 pthread_cond_timedwait(pthread_cond_t
*cond
,
315 pthread_mutex_t
*mutex
,
316 const struct timespec
*abstime
)
320 if (__unix_conforming
== 0)
321 __unix_conforming
= 1;
323 #ifdef VARIANT_CANCELABLE
325 #else /* !VARIANT_CANCELABLE */
327 #endif /* VARIANT_CANCELABLE */
328 #else /* __DARWIN_UNIX03 */
330 #endif /* __DARWIN_UNIX03 */
332 return (_pthread_cond_wait(cond
, mutex
, abstime
, 0, conforming
));
336 sigwait(const sigset_t
* set
, int * sig
)
341 if (__unix_conforming
== 0)
342 __unix_conforming
= 1;
344 #ifdef VARIANT_CANCELABLE
345 _pthread_testcancel(pthread_self(), 1);
346 #endif /* VARIANT_CANCELABLE */
348 if (__sigwait(set
, sig
) == -1) {
351 #ifdef VARIANT_CANCELABLE
352 _pthread_testcancel(pthread_self(), 1);
353 #endif /* VARIANT_CANCELABLE */
356 * EINTR that isn't a result of pthread_cancel()
357 * is translated to 0.
364 #else /* __DARWIN_UNIX03 */
365 if (__sigwait(set
, sig
) == -1) {
367 * EINTR that isn't a result of pthread_cancel()
368 * is translated to 0.
370 if (errno
!= EINTR
) {
376 #endif /* __DARWIN_UNIX03 */