libpthread-301.20.1.tar.gz
[apple/libpthread.git] / src / pthread_cancelable.c
CommitLineData
f1a1da6c
A
1/*
2 * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
a0619f9c 5 *
f1a1da6c
A
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
11 * file.
a0619f9c 12 *
f1a1da6c
A
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.
a0619f9c 20 *
f1a1da6c
A
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
a0619f9c
A
24 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991
25 * All Rights Reserved
26 *
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.
32 *
33 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
34 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35 * FOR A PARTICULAR PURPOSE.
36 *
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.
42 *
f1a1da6c
A
43 */
44/*
45 * MkLinux
46 */
47
48/*
49 * POSIX Pthread Library
50 */
51
a0619f9c 52#include "resolver.h"
f1a1da6c
A
53#include "internal.h"
54
55#include <stdio.h> /* For printf(). */
56#include <stdlib.h>
57#include <errno.h> /* For __mach_errno_addr() prototype. */
58#include <signal.h>
59#include <sys/time.h>
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>
65
66extern int __unix_conforming;
f1a1da6c
A
67extern int _pthread_cond_wait(pthread_cond_t *cond,
68 pthread_mutex_t *mutex,
69 const struct timespec *abstime,
70 int isRelative,
71 int isconforming);
72extern int __sigwait(const sigset_t *set, int *sig);
73extern int __pthread_sigmask(int, const sigset_t *, sigset_t *);
a0619f9c
A
74extern int __pthread_markcancel(mach_port_t);
75extern int __pthread_canceled(int);
f1a1da6c
A
76
77#ifdef VARIANT_CANCELABLE
78extern int __semwait_signal(int cond_sem, int mutex_sem, int timeout, int relative, __int64_t tv_sec, __int32_t tv_nsec);
79#else
80extern 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");
81#endif
82
a0619f9c
A
83PTHREAD_NOEXPORT
84int _pthread_join(pthread_t thread, void **value_ptr, int conforming,
85 int (*_semwait_signal)(int, int, int, int, __int64_t, __int32_t));
86
f1a1da6c
A
87#ifndef VARIANT_CANCELABLE
88
a0619f9c
A
89PTHREAD_ALWAYS_INLINE
90static inline int
91_pthread_update_cancel_state(pthread_t thread, int mask, int state)
92{
93 int oldstate, newstate;
94 os_atomic_rmw_loop2o(thread, cancel_state, oldstate, newstate, seq_cst, {
95 newstate = oldstate;
96 newstate &= ~mask;
97 newstate |= state;
98 });
99 return oldstate;
100}
101
f1a1da6c
A
102/*
103 * Cancel a thread
104 */
a0619f9c 105PTHREAD_NOEXPORT_VARIANT
f1a1da6c
A
106int
107pthread_cancel(pthread_t thread)
108{
109#if __DARWIN_UNIX03
110 if (__unix_conforming == 0)
111 __unix_conforming = 1;
112#endif /* __DARWIN_UNIX03 */
113
a0619f9c 114 if (!_pthread_is_valid(thread, 0, NULL)) {
f1a1da6c 115 return(ESRCH);
a0619f9c 116 }
f1a1da6c
A
117
118 /* if the thread is a workqueue thread, then return error */
119 if (thread->wqthread != 0) {
120 return(ENOTSUP);
121 }
122#if __DARWIN_UNIX03
a0619f9c
A
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);
127 }
f1a1da6c
A
128#else /* __DARWIN_UNIX03 */
129 thread->cancel_state |= _PTHREAD_CANCEL_PENDING;
130#endif /* __DARWIN_UNIX03 */
131 return (0);
132}
133
a0619f9c 134
f1a1da6c
A
135void
136pthread_testcancel(void)
137{
138 pthread_t self = pthread_self();
139
140#if __DARWIN_UNIX03
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 */
147}
148
a0619f9c
A
149#ifndef BUILDING_VARIANT /* [ */
150
151PTHREAD_NOEXPORT_VARIANT
152void
153_pthread_exit_if_canceled(int error)
154{
155 if (((error & 0xff) == EINTR) && __unix_conforming && (__pthread_canceled(0) == 0)) {
156 pthread_t self = pthread_self();
157 if (self != NULL) {
158 self->cancel_error = error;
159 }
160 pthread_exit(PTHREAD_CANCELED);
161 }
162}
163
164
165PTHREAD_NOEXPORT_VARIANT
166void
167_pthread_testcancel(pthread_t thread, int isconforming)
168{
169 const int flags = (PTHREAD_CANCEL_ENABLE|_PTHREAD_CANCEL_PENDING);
170
171 int state = os_atomic_load2o(thread, cancel_state, seq_cst);
172 if ((state & flags) == flags) {
173 pthread_exit(isconforming ? PTHREAD_CANCELED : 0);
174 }
175}
176
177PTHREAD_NOEXPORT
178void
179_pthread_markcancel_if_canceled(pthread_t thread, mach_port_t kport)
180{
181 const int flags = (PTHREAD_CANCEL_ENABLE|_PTHREAD_CANCEL_PENDING);
182
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);
187 }
188}
189
190PTHREAD_NOEXPORT
191void *
192_pthread_get_exit_value(pthread_t thread, int conforming)
193{
194 const int flags = (PTHREAD_CANCEL_ENABLE|_PTHREAD_CANCEL_PENDING);
195 void *value = thread->exit_value;
196
197 if (conforming) {
198 int state = os_atomic_load2o(thread, cancel_state, seq_cst);
199 if ((state & flags) == flags) {
200 value = PTHREAD_CANCELED;
201 }
202 }
203 return value;
204}
205
206/* When a thread exits set the cancellation state to DISABLE and DEFERRED */
207PTHREAD_NOEXPORT
208void
209_pthread_setcancelstate_exit(pthread_t thread, void *value_ptr, int conforming)
210{
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);
218 }
219}
220
221#endif /* !BUILDING_VARIANT ] */
222
f1a1da6c
A
223/*
224 * Query/update the cancelability 'state' of a thread
225 */
a0619f9c
A
226PTHREAD_ALWAYS_INLINE
227static inline int
228_pthread_setcancelstate_internal(int state, int *oldstateptr, int conforming)
229{
230 pthread_t self;
231
232 switch (state) {
233 case PTHREAD_CANCEL_ENABLE:
234 if (conforming) {
235 __pthread_canceled(1);
236 }
237 break;
238 case PTHREAD_CANCEL_DISABLE:
239 if (conforming) {
240 __pthread_canceled(2);
241 }
242 break;
243 default:
244 return EINVAL;
245 }
246
247 self = pthread_self();
248 int oldstate = _pthread_update_cancel_state(self, _PTHREAD_CANCEL_STATE_MASK, state);
249 if (oldstateptr) {
250 *oldstateptr = oldstate & _PTHREAD_CANCEL_STATE_MASK;
251 }
252 if (!conforming) {
253 _pthread_testcancel(self, 0); /* See if we need to 'die' now... */
254 }
255 return 0;
256}
257
258PTHREAD_NOEXPORT_VARIANT
f1a1da6c
A
259int
260pthread_setcancelstate(int state, int *oldstate)
261{
262#if __DARWIN_UNIX03
263 if (__unix_conforming == 0) {
264 __unix_conforming = 1;
265 }
266 return (_pthread_setcancelstate_internal(state, oldstate, 1));
267#else /* __DARWIN_UNIX03 */
268 return (_pthread_setcancelstate_internal(state, oldstate, 0));
269#endif /* __DARWIN_UNIX03 */
270}
271
272/*
273 * Query/update the cancelability 'type' of a thread
274 */
a0619f9c 275PTHREAD_NOEXPORT_VARIANT
f1a1da6c
A
276int
277pthread_setcanceltype(int type, int *oldtype)
278{
279 pthread_t self;
280
281#if __DARWIN_UNIX03
282 if (__unix_conforming == 0)
283 __unix_conforming = 1;
284#endif /* __DARWIN_UNIX03 */
285
286 if ((type != PTHREAD_CANCEL_DEFERRED) &&
287 (type != PTHREAD_CANCEL_ASYNCHRONOUS))
288 return EINVAL;
289 self = pthread_self();
a0619f9c
A
290 int oldstate = _pthread_update_cancel_state(self, _PTHREAD_CANCEL_TYPE_MASK, type);
291 if (oldtype) {
292 *oldtype = oldstate & _PTHREAD_CANCEL_TYPE_MASK;
293 }
f1a1da6c
A
294#if !__DARWIN_UNIX03
295 _pthread_testcancel(self, 0); /* See if we need to 'die' now... */
296#endif /* __DARWIN_UNIX03 */
297 return (0);
298}
299
a0619f9c 300
f1a1da6c
A
301int
302pthread_sigmask(int how, const sigset_t * set, sigset_t * oset)
303{
304#if __DARWIN_UNIX03
305 int err = 0;
306
307 if (__pthread_sigmask(how, set, oset) == -1) {
308 err = errno;
309 }
310 return(err);
311#else /* __DARWIN_UNIX03 */
312 return(__pthread_sigmask(how, set, oset));
313#endif /* __DARWIN_UNIX03 */
314}
315
a0619f9c 316#ifndef BUILDING_VARIANT /* [ */
f1a1da6c
A
317
318static void
319__posix_join_cleanup(void *arg)
320{
321 pthread_t thread = (pthread_t)arg;
322
2546420a 323 _PTHREAD_LOCK(thread->lock);
f1a1da6c
A
324 /* leave another thread to join */
325 thread->joiner = (struct _pthread *)NULL;
2546420a 326 _PTHREAD_UNLOCK(thread->lock);
f1a1da6c
A
327}
328
a0619f9c
A
329PTHREAD_NOEXPORT PTHREAD_NOINLINE
330int
331_pthread_join(pthread_t thread, void **value_ptr, int conforming,
332 int (*_semwait_signal)(int, int, int, int, __int64_t, __int32_t))
f1a1da6c
A
333{
334 int res = 0;
335 pthread_t self = pthread_self();
f1a1da6c 336 kern_return_t kern_res;
a0619f9c 337 semaphore_t joinsem, death = (semaphore_t)os_get_cached_semaphore();
f1a1da6c 338
a0619f9c
A
339 if (!_pthread_is_valid(thread, PTHREAD_IS_VALID_LOCK_THREAD, NULL)) {
340 res = ESRCH;
341 goto out;
342 }
f1a1da6c 343
a0619f9c
A
344 if (thread->sig != _PTHREAD_SIG) {
345 res = ESRCH;
346 } else if ((thread->detached & PTHREAD_CREATE_DETACHED) ||
347 !(thread->detached & PTHREAD_CREATE_JOINABLE) ||
348 (thread->joiner != NULL)) {
349 res = EINVAL;
350 } else if (thread == self || (self != NULL && self->joiner == thread)) {
351 res = EDEADLK;
352 }
353 if (res != 0) {
354 _PTHREAD_UNLOCK(thread->lock);
355 goto out;
356 }
357
358 joinsem = thread->joiner_notify;
359 if (joinsem == SEMAPHORE_NULL) {
360 thread->joiner_notify = joinsem = death;
361 death = MACH_PORT_NULL;
362 }
363 thread->joiner = self;
364 _PTHREAD_UNLOCK(thread->lock);
f1a1da6c 365
a0619f9c
A
366 if (conforming) {
367 /* Wait for it to signal... */
368 pthread_cleanup_push(__posix_join_cleanup, (void *)thread);
369 do {
370 res = _semwait_signal(joinsem, 0, 0, 0, 0, 0);
371 } while ((res < 0) && (errno == EINTR));
372 pthread_cleanup_pop(0);
373 } else {
374 /* Wait for it to signal... */
375 kern_return_t (*_semaphore_wait)(semaphore_t) =
376 (void*)_semwait_signal;
377 do {
378 kern_res = _semaphore_wait(joinsem);
379 } while (kern_res != KERN_SUCCESS);
380 }
f1a1da6c 381
a0619f9c
A
382 os_put_cached_semaphore((os_semaphore_t)joinsem);
383 res = _pthread_join_cleanup(thread, value_ptr, conforming);
f1a1da6c 384
a0619f9c
A
385out:
386 if (death) {
387 os_put_cached_semaphore(death);
388 }
389 return res;
390}
f1a1da6c 391
a0619f9c
A
392#endif /* !BUILDING_VARIANT ] */
393#endif /* VARIANT_CANCELABLE */
394
395/*
396 * Wait for a thread to terminate and obtain its exit value.
397 */
398int
399pthread_join(pthread_t thread, void **value_ptr)
400{
f1a1da6c 401#if __DARWIN_UNIX03
a0619f9c
A
402 if (__unix_conforming == 0)
403 __unix_conforming = 1;
404
405#ifdef VARIANT_CANCELABLE
406 _pthread_testcancel(pthread_self(), 1);
407#endif /* VARIANT_CANCELABLE */
408 return _pthread_join(thread, value_ptr, 1, __semwait_signal);
409#else
410 return _pthread_join(thread, value_ptr, 0, (void*)semaphore_wait);
f1a1da6c
A
411#endif /* __DARWIN_UNIX03 */
412
f1a1da6c
A
413}
414
a0619f9c
A
415int
416pthread_cond_wait(pthread_cond_t *cond,
f1a1da6c
A
417 pthread_mutex_t *mutex)
418{
419 int conforming;
420#if __DARWIN_UNIX03
421
422 if (__unix_conforming == 0)
423 __unix_conforming = 1;
424
425#ifdef VARIANT_CANCELABLE
426 conforming = 1;
427#else /* !VARIANT_CANCELABLE */
428 conforming = -1;
429#endif /* VARIANT_CANCELABLE */
430#else /* __DARWIN_UNIX03 */
431 conforming = 0;
432#endif /* __DARWIN_UNIX03 */
433 return (_pthread_cond_wait(cond, mutex, (struct timespec *)NULL, 0, conforming));
434}
435
a0619f9c
A
436int
437pthread_cond_timedwait(pthread_cond_t *cond,
f1a1da6c
A
438 pthread_mutex_t *mutex,
439 const struct timespec *abstime)
440{
441 int conforming;
442#if __DARWIN_UNIX03
443 if (__unix_conforming == 0)
444 __unix_conforming = 1;
445
446#ifdef VARIANT_CANCELABLE
447 conforming = 1;
448#else /* !VARIANT_CANCELABLE */
449 conforming = -1;
450#endif /* VARIANT_CANCELABLE */
451#else /* __DARWIN_UNIX03 */
452 conforming = 0;
453#endif /* __DARWIN_UNIX03 */
454
455 return (_pthread_cond_wait(cond, mutex, abstime, 0, conforming));
456}
457
458int
459sigwait(const sigset_t * set, int * sig)
460{
461#if __DARWIN_UNIX03
462 int err = 0;
463
464 if (__unix_conforming == 0)
465 __unix_conforming = 1;
466
467#ifdef VARIANT_CANCELABLE
468 _pthread_testcancel(pthread_self(), 1);
469#endif /* VARIANT_CANCELABLE */
470
471 if (__sigwait(set, sig) == -1) {
472 err = errno;
473
474#ifdef VARIANT_CANCELABLE
475 _pthread_testcancel(pthread_self(), 1);
476#endif /* VARIANT_CANCELABLE */
a0619f9c
A
477
478 /*
f1a1da6c
A
479 * EINTR that isn't a result of pthread_cancel()
480 * is translated to 0.
481 */
482 if (err == EINTR) {
483 err = 0;
484 }
485 }
486 return(err);
487#else /* __DARWIN_UNIX03 */
488 if (__sigwait(set, sig) == -1) {
a0619f9c 489 /*
f1a1da6c
A
490 * EINTR that isn't a result of pthread_cancel()
491 * is translated to 0.
492 */
493 if (errno != EINTR) {
494 return -1;
495 }
496 }
497
498 return 0;
499#endif /* __DARWIN_UNIX03 */
500}
a0619f9c 501