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
55 #include <stdio.h> /* For printf(). */
57 #include <errno.h> /* For __mach_errno_addr() prototype. */
60 #include <sys/resource.h>
61 #include <sys/sysctl.h>
62 #include <sys/queue.h>
63 #include <machine/vmparam.h>
64 #include <mach/vm_statistics.h>
66 extern int __unix_conforming
;
67 extern int _pthread_cond_wait(pthread_cond_t
*cond
,
68 pthread_mutex_t
*mutex
,
69 const struct timespec
*abstime
,
72 extern int __sigwait(const sigset_t
*set
, int *sig
);
73 extern int __pthread_sigmask(int, const sigset_t
*, sigset_t
*);
74 extern int __pthread_markcancel(mach_port_t
);
75 extern int __pthread_canceled(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");
84 int _pthread_join(pthread_t thread
, void **value_ptr
, int conforming
,
85 int (*_semwait_signal
)(int, int, int, int, __int64_t
, __int32_t
));
87 #ifndef VARIANT_CANCELABLE
91 _pthread_update_cancel_state(pthread_t thread
, int mask
, int state
)
93 int oldstate
, newstate
;
94 os_atomic_rmw_loop2o(thread
, cancel_state
, oldstate
, newstate
, seq_cst
, {
105 PTHREAD_NOEXPORT_VARIANT
107 pthread_cancel(pthread_t thread
)
110 if (__unix_conforming
== 0)
111 __unix_conforming
= 1;
112 #endif /* __DARWIN_UNIX03 */
114 if (!_pthread_is_valid(thread
, 0, NULL
)) {
118 /* if the thread is a workqueue thread, then return error */
119 if (thread
->wqthread
!= 0) {
123 int state
= os_atomic_or2o(thread
, cancel_state
, _PTHREAD_CANCEL_PENDING
, relaxed
);
124 if (state
& PTHREAD_CANCEL_ENABLE
) {
125 mach_port_t kport
= _pthread_kernel_thread(thread
);
126 if (kport
) __pthread_markcancel(kport
);
128 #else /* __DARWIN_UNIX03 */
129 thread
->cancel_state
|= _PTHREAD_CANCEL_PENDING
;
130 #endif /* __DARWIN_UNIX03 */
136 pthread_testcancel(void)
138 pthread_t self
= pthread_self();
141 if (__unix_conforming
== 0)
142 __unix_conforming
= 1;
143 _pthread_testcancel(self
, 1);
144 #else /* __DARWIN_UNIX03 */
145 _pthread_testcancel(self
, 0);
146 #endif /* __DARWIN_UNIX03 */
149 #ifndef BUILDING_VARIANT /* [ */
151 PTHREAD_NOEXPORT_VARIANT
153 _pthread_exit_if_canceled(int error
)
155 if (((error
& 0xff) == EINTR
) && __unix_conforming
&& (__pthread_canceled(0) == 0)) {
156 pthread_t self
= pthread_self();
158 self
->cancel_error
= error
;
160 pthread_exit(PTHREAD_CANCELED
);
165 PTHREAD_NOEXPORT_VARIANT
167 _pthread_testcancel(pthread_t thread
, int isconforming
)
169 const int flags
= (PTHREAD_CANCEL_ENABLE
|_PTHREAD_CANCEL_PENDING
);
171 int state
= os_atomic_load2o(thread
, cancel_state
, seq_cst
);
172 if ((state
& flags
) == flags
) {
173 pthread_exit(isconforming
? PTHREAD_CANCELED
: 0);
179 _pthread_markcancel_if_canceled(pthread_t thread
, mach_port_t kport
)
181 const int flags
= (PTHREAD_CANCEL_ENABLE
|_PTHREAD_CANCEL_PENDING
);
183 int state
= os_atomic_or2o(thread
, cancel_state
,
184 _PTHREAD_CANCEL_INITIALIZED
, relaxed
);
185 if ((state
& flags
) == flags
&& __unix_conforming
) {
186 __pthread_markcancel(kport
);
192 _pthread_get_exit_value(pthread_t thread
, int conforming
)
194 const int flags
= (PTHREAD_CANCEL_ENABLE
|_PTHREAD_CANCEL_PENDING
);
195 void *value
= thread
->exit_value
;
198 int state
= os_atomic_load2o(thread
, cancel_state
, seq_cst
);
199 if ((state
& flags
) == flags
) {
200 value
= PTHREAD_CANCELED
;
206 /* When a thread exits set the cancellation state to DISABLE and DEFERRED */
209 _pthread_setcancelstate_exit(pthread_t thread
, void *value_ptr
, int conforming
)
211 _pthread_update_cancel_state(thread
,
212 _PTHREAD_CANCEL_STATE_MASK
| _PTHREAD_CANCEL_TYPE_MASK
,
213 PTHREAD_CANCEL_DISABLE
| PTHREAD_CANCEL_DEFERRED
);
214 if (value_ptr
== PTHREAD_CANCELED
) {
215 _PTHREAD_LOCK(thread
->lock
);
216 thread
->detached
|= _PTHREAD_WASCANCEL
; // 4597450
217 _PTHREAD_UNLOCK(thread
->lock
);
221 #endif /* !BUILDING_VARIANT ] */
224 * Query/update the cancelability 'state' of a thread
226 PTHREAD_ALWAYS_INLINE
228 _pthread_setcancelstate_internal(int state
, int *oldstateptr
, int conforming
)
233 case PTHREAD_CANCEL_ENABLE
:
235 __pthread_canceled(1);
238 case PTHREAD_CANCEL_DISABLE
:
240 __pthread_canceled(2);
247 self
= pthread_self();
248 int oldstate
= _pthread_update_cancel_state(self
, _PTHREAD_CANCEL_STATE_MASK
, state
);
250 *oldstateptr
= oldstate
& _PTHREAD_CANCEL_STATE_MASK
;
253 _pthread_testcancel(self
, 0); /* See if we need to 'die' now... */
258 PTHREAD_NOEXPORT_VARIANT
260 pthread_setcancelstate(int state
, int *oldstate
)
263 if (__unix_conforming
== 0) {
264 __unix_conforming
= 1;
266 return (_pthread_setcancelstate_internal(state
, oldstate
, 1));
267 #else /* __DARWIN_UNIX03 */
268 return (_pthread_setcancelstate_internal(state
, oldstate
, 0));
269 #endif /* __DARWIN_UNIX03 */
273 * Query/update the cancelability 'type' of a thread
275 PTHREAD_NOEXPORT_VARIANT
277 pthread_setcanceltype(int type
, int *oldtype
)
282 if (__unix_conforming
== 0)
283 __unix_conforming
= 1;
284 #endif /* __DARWIN_UNIX03 */
286 if ((type
!= PTHREAD_CANCEL_DEFERRED
) &&
287 (type
!= PTHREAD_CANCEL_ASYNCHRONOUS
))
289 self
= pthread_self();
290 int oldstate
= _pthread_update_cancel_state(self
, _PTHREAD_CANCEL_TYPE_MASK
, type
);
292 *oldtype
= oldstate
& _PTHREAD_CANCEL_TYPE_MASK
;
295 _pthread_testcancel(self
, 0); /* See if we need to 'die' now... */
296 #endif /* __DARWIN_UNIX03 */
302 pthread_sigmask(int how
, const sigset_t
* set
, sigset_t
* oset
)
307 if (__pthread_sigmask(how
, set
, oset
) == -1) {
311 #else /* __DARWIN_UNIX03 */
312 return(__pthread_sigmask(how
, set
, oset
));
313 #endif /* __DARWIN_UNIX03 */
316 #ifndef BUILDING_VARIANT /* [ */
319 __posix_join_cleanup(void *arg
)
321 pthread_t thread
= (pthread_t
)arg
;
323 _PTHREAD_LOCK(thread
->lock
);
324 /* leave another thread to join */
325 thread
->joiner
= (struct _pthread
*)NULL
;
326 _PTHREAD_UNLOCK(thread
->lock
);
329 PTHREAD_NOEXPORT PTHREAD_NOINLINE
331 _pthread_join(pthread_t thread
, void **value_ptr
, int conforming
,
332 int (*_semwait_signal
)(int, int, int, int, __int64_t
, __int32_t
))
335 pthread_t self
= pthread_self();
336 kern_return_t kern_res
;
337 semaphore_t joinsem
, death
= (semaphore_t
)os_get_cached_semaphore();
339 if (!_pthread_is_valid(thread
, PTHREAD_IS_VALID_LOCK_THREAD
, NULL
)) {
344 if (thread
->sig
!= _PTHREAD_SIG
) {
346 } else if ((thread
->detached
& PTHREAD_CREATE_DETACHED
) ||
347 !(thread
->detached
& PTHREAD_CREATE_JOINABLE
) ||
348 (thread
->joiner
!= NULL
)) {
350 } else if (thread
== self
|| (self
!= NULL
&& self
->joiner
== thread
)) {
354 _PTHREAD_UNLOCK(thread
->lock
);
358 joinsem
= thread
->joiner_notify
;
359 if (joinsem
== SEMAPHORE_NULL
) {
360 thread
->joiner_notify
= joinsem
= death
;
361 death
= MACH_PORT_NULL
;
363 thread
->joiner
= self
;
364 _PTHREAD_UNLOCK(thread
->lock
);
367 /* Wait for it to signal... */
368 pthread_cleanup_push(__posix_join_cleanup
, (void *)thread
);
370 res
= _semwait_signal(joinsem
, 0, 0, 0, 0, 0);
371 } while ((res
< 0) && (errno
== EINTR
));
372 pthread_cleanup_pop(0);
374 /* Wait for it to signal... */
375 kern_return_t (*_semaphore_wait
)(semaphore_t
) =
376 (void*)_semwait_signal
;
378 kern_res
= _semaphore_wait(joinsem
);
379 } while (kern_res
!= KERN_SUCCESS
);
382 os_put_cached_semaphore((os_semaphore_t
)joinsem
);
383 res
= _pthread_join_cleanup(thread
, value_ptr
, conforming
);
387 os_put_cached_semaphore(death
);
392 #endif /* !BUILDING_VARIANT ] */
393 #endif /* VARIANT_CANCELABLE */
396 * Wait for a thread to terminate and obtain its exit value.
399 pthread_join(pthread_t thread
, void **value_ptr
)
402 if (__unix_conforming
== 0)
403 __unix_conforming
= 1;
405 #ifdef VARIANT_CANCELABLE
406 _pthread_testcancel(pthread_self(), 1);
407 #endif /* VARIANT_CANCELABLE */
408 return _pthread_join(thread
, value_ptr
, 1, __semwait_signal
);
410 return _pthread_join(thread
, value_ptr
, 0, (void*)semaphore_wait
);
411 #endif /* __DARWIN_UNIX03 */
416 pthread_cond_wait(pthread_cond_t
*cond
,
417 pthread_mutex_t
*mutex
)
422 if (__unix_conforming
== 0)
423 __unix_conforming
= 1;
425 #ifdef VARIANT_CANCELABLE
427 #else /* !VARIANT_CANCELABLE */
429 #endif /* VARIANT_CANCELABLE */
430 #else /* __DARWIN_UNIX03 */
432 #endif /* __DARWIN_UNIX03 */
433 return (_pthread_cond_wait(cond
, mutex
, (struct timespec
*)NULL
, 0, conforming
));
437 pthread_cond_timedwait(pthread_cond_t
*cond
,
438 pthread_mutex_t
*mutex
,
439 const struct timespec
*abstime
)
443 if (__unix_conforming
== 0)
444 __unix_conforming
= 1;
446 #ifdef VARIANT_CANCELABLE
448 #else /* !VARIANT_CANCELABLE */
450 #endif /* VARIANT_CANCELABLE */
451 #else /* __DARWIN_UNIX03 */
453 #endif /* __DARWIN_UNIX03 */
455 return (_pthread_cond_wait(cond
, mutex
, abstime
, 0, conforming
));
459 sigwait(const sigset_t
* set
, int * sig
)
464 if (__unix_conforming
== 0)
465 __unix_conforming
= 1;
467 #ifdef VARIANT_CANCELABLE
468 _pthread_testcancel(pthread_self(), 1);
469 #endif /* VARIANT_CANCELABLE */
471 if (__sigwait(set
, sig
) == -1) {
474 #ifdef VARIANT_CANCELABLE
475 _pthread_testcancel(pthread_self(), 1);
476 #endif /* VARIANT_CANCELABLE */
479 * EINTR that isn't a result of pthread_cancel()
480 * is translated to 0.
487 #else /* __DARWIN_UNIX03 */
488 if (__sigwait(set
, sig
) == -1) {
490 * EINTR that isn't a result of pthread_cancel()
491 * is translated to 0.
493 if (errno
!= EINTR
) {
499 #endif /* __DARWIN_UNIX03 */