2 * Copyright (c) 2000-2003 Apple Computer, 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 Threads - IEEE 1003.1c
57 #include <pthread_impl.h>
62 #ifndef _PTHREAD_ATTR_T
63 #define _PTHREAD_ATTR_T
64 typedef __darwin_pthread_attr_t pthread_attr_t
;
67 #ifndef _PTHREAD_COND_T
68 #define _PTHREAD_COND_T
69 typedef __darwin_pthread_cond_t pthread_cond_t
;
72 #ifndef _PTHREAD_CONDATTR_T
73 #define _PTHREAD_CONDATTR_T
74 typedef __darwin_pthread_condattr_t pthread_condattr_t
;
77 #ifndef _PTHREAD_KEY_T
78 #define _PTHREAD_KEY_T
79 typedef __darwin_pthread_key_t pthread_key_t
;
82 #ifndef _PTHREAD_MUTEX_T
83 #define _PTHREAD_MUTEX_T
84 typedef __darwin_pthread_mutex_t pthread_mutex_t
;
87 #ifndef _PTHREAD_MUTEXATTR_T
88 #define _PTHREAD_MUTEXATTR_T
89 typedef __darwin_pthread_mutexattr_t pthread_mutexattr_t
;
92 #ifndef _PTHREAD_ONCE_T
93 #define _PTHREAD_ONCE_T
94 typedef __darwin_pthread_once_t pthread_once_t
;
97 #ifndef _PTHREAD_RWLOCK_T
98 #define _PTHREAD_RWLOCK_T
99 typedef __darwin_pthread_rwlock_t pthread_rwlock_t
;
102 #ifndef _PTHREAD_RWLOCKATTR_T
103 #define _PTHREAD_RWLOCKATTR_T
104 typedef __darwin_pthread_rwlockattr_t pthread_rwlockattr_t
;
109 typedef __darwin_pthread_t pthread_t
;
112 #if (!defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)) || defined(_DARWIN_C_SOURCE)
116 typedef __darwin_mach_port_t mach_port_t
;
121 typedef __darwin_sigset_t sigset_t
;
124 #endif /* (!_POSIX_C_SOURCE && !_XOPEN_SOURCE) || _DARWIN_C_SOURCE */
127 * These symbols indicate which [optional] features are available
128 * They can be tested at compile time via '#ifdef XXX'
129 * The way to check for pthreads is like so:
131 * #include <unistd.h>
132 * #ifdef _POSIX_THREADS
133 * #include <pthread.h>
138 /* These will be moved to unistd.h */
141 * Note: These data structures are meant to be opaque. Only enough
142 * structure is exposed to support initializers.
143 * All of the typedefs will be moved to <sys/types.h>
146 #include <sys/cdefs.h>
155 * Cancel cleanup handler management. Note, since these are implemented as macros,
156 * they *MUST* occur in matched pairs!
159 #define pthread_cleanup_push(func, val) \
161 struct __darwin_pthread_handler_rec __handler; \
162 pthread_t __self = pthread_self(); \
163 __handler.__routine = func; \
164 __handler.__arg = val; \
165 __handler.__next = __self->__cleanup_stack; \
166 __self->__cleanup_stack = &__handler;
168 #define pthread_cleanup_pop(execute) \
169 /* Note: 'handler' must be in this same lexical context! */ \
170 __self->__cleanup_stack = __handler.__next; \
171 if (execute) (__handler.__routine)(__handler.__arg); \
178 #define PTHREAD_CREATE_JOINABLE 1
179 #define PTHREAD_CREATE_DETACHED 2
181 #define PTHREAD_INHERIT_SCHED 1
182 #define PTHREAD_EXPLICIT_SCHED 2
184 #define PTHREAD_CANCEL_ENABLE 0x01 /* Cancel takes place at next cancellation point */
185 #define PTHREAD_CANCEL_DISABLE 0x00 /* Cancel postponed */
186 #define PTHREAD_CANCEL_DEFERRED 0x02 /* Cancel waits until cancellation point */
187 #define PTHREAD_CANCEL_ASYNCHRONOUS 0x00 /* Cancel occurs immediately */
189 /* Value returned from pthread_join() when a thread is canceled */
190 #define PTHREAD_CANCELED ((void *) 1)
192 /* We only support PTHREAD_SCOPE_SYSTEM */
193 #define PTHREAD_SCOPE_SYSTEM 1
194 #define PTHREAD_SCOPE_PROCESS 2
196 /* We only support PTHREAD_PROCESS_PRIVATE */
197 #define PTHREAD_PROCESS_SHARED 1
198 #define PTHREAD_PROCESS_PRIVATE 2
201 * Mutex protocol attributes
203 #define PTHREAD_PRIO_NONE 0
204 #define PTHREAD_PRIO_INHERIT 1
205 #define PTHREAD_PRIO_PROTECT 2
208 * Mutex type attributes
210 #define PTHREAD_MUTEX_NORMAL 0
211 #define PTHREAD_MUTEX_ERRORCHECK 1
212 #define PTHREAD_MUTEX_RECURSIVE 2
213 #define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
218 #define PTHREAD_RWLOCK_INITIALIZER {_PTHREAD_RWLOCK_SIG_init, {0}}
223 #define PTHREAD_MUTEX_INITIALIZER {_PTHREAD_MUTEX_SIG_init, {0}}
226 * Condition variable attributes
230 * Condition variables
233 #define PTHREAD_COND_INITIALIZER {_PTHREAD_COND_SIG_init, {0}}
236 * Initialization control (once) variables
239 #define PTHREAD_ONCE_INIT {_PTHREAD_ONCE_SIG_init, {0}}
242 * Prototypes for all PTHREAD interfaces
244 int pthread_atfork(void (*)(void), void (*)(void),
246 int pthread_attr_destroy(pthread_attr_t
*);
247 int pthread_attr_getdetachstate(const pthread_attr_t
*,
249 int pthread_attr_getguardsize(const pthread_attr_t
* __restrict
,
250 size_t * __restrict
);
251 int pthread_attr_getinheritsched(const pthread_attr_t
* __restrict
,
253 int pthread_attr_getschedparam(const pthread_attr_t
* __restrict
,
254 struct sched_param
* __restrict
);
255 int pthread_attr_getschedpolicy(const pthread_attr_t
* __restrict
,
257 int pthread_attr_getscope(const pthread_attr_t
* __restrict
, int * __restrict
);
258 int pthread_attr_getstack(const pthread_attr_t
* __restrict
,
259 void ** __restrict
, size_t * __restrict
);
260 int pthread_attr_getstackaddr(const pthread_attr_t
* __restrict
,
262 int pthread_attr_getstacksize(const pthread_attr_t
* __restrict
,
263 size_t * __restrict
);
264 int pthread_attr_init(pthread_attr_t
*);
265 int pthread_attr_setdetachstate(pthread_attr_t
*,
267 int pthread_attr_setguardsize(pthread_attr_t
*, size_t );
268 int pthread_attr_setinheritsched(pthread_attr_t
*,
270 int pthread_attr_setschedparam(pthread_attr_t
* __restrict
,
271 const struct sched_param
* __restrict
);
272 int pthread_attr_setschedpolicy(pthread_attr_t
*,
274 int pthread_attr_setscope(pthread_attr_t
*, int);
275 int pthread_attr_setstack(pthread_attr_t
*,
277 int pthread_attr_setstackaddr(pthread_attr_t
*,
279 int pthread_attr_setstacksize(pthread_attr_t
*, size_t );
280 int pthread_cancel(pthread_t
) __DARWIN_ALIAS(pthread_cancel
);
282 int pthread_cond_broadcast(pthread_cond_t
*);
283 int pthread_cond_destroy(pthread_cond_t
*);
285 #ifndef LIBC_ALIAS_PTHREAD_COND_INIT
287 int pthread_cond_init(pthread_cond_t
* __restrict
,
288 const pthread_condattr_t
* __restrict
) __DARWIN_ALIAS(pthread_cond_init
);
290 #else /* LIBC_ALIAS_PTHREAD_COND_INIT */
291 int pthread_cond_init(pthread_cond_t
* __restrict
,
292 const pthread_condattr_t
* __restrict
) LIBC_ALIAS(pthread_cond_init
);
293 #endif /* !LIBC_ALIAS_PTHREAD_COND_INIT */
295 int pthread_cond_signal(pthread_cond_t
*);
297 #ifndef LIBC_ALIAS_PTHREAD_COND_TIMEDWAIT
299 int pthread_cond_timedwait(pthread_cond_t
* __restrict
,
300 pthread_mutex_t
* __restrict
,
301 const struct timespec
* __restrict
) __DARWIN_ALIAS_C(pthread_cond_timedwait
);
303 #else /* LIBC_ALIAS_PTHREAD_COND_TIMEDWAIT */
304 int pthread_cond_timedwait(pthread_cond_t
* __restrict
,
305 pthread_mutex_t
* __restrict
,
306 const struct timespec
* __restrict
) LIBC_ALIAS_C(pthread_cond_timedwait
);
307 #endif /* !LIBC_ALIAS_PTHREAD_COND_TIMEDWAIT */
310 #ifndef LIBC_ALIAS_PTHREAD_COND_WAIT
312 int pthread_cond_wait(pthread_cond_t
* __restrict
,
313 pthread_mutex_t
* __restrict
) __DARWIN_ALIAS_C(pthread_cond_wait
);
315 #else /* LIBC_ALIAS_PTHREAD_COND_WAIT */
316 int pthread_cond_wait(pthread_cond_t
* __restrict
,
317 pthread_mutex_t
* __restrict
) LIBC_ALIAS_C(pthread_cond_wait
);
318 #endif /* !LIBC_ALIAS_PTHREAD_COND_WAIT */
320 int pthread_condattr_destroy(pthread_condattr_t
*);
321 int pthread_condattr_init(pthread_condattr_t
*);
322 int pthread_condattr_getpshared(const pthread_condattr_t
* __restrict
,
324 int pthread_condattr_setpshared(pthread_condattr_t
*,
326 int pthread_create(pthread_t
* __restrict
,
327 const pthread_attr_t
* __restrict
,
330 int pthread_detach(pthread_t
);
331 int pthread_equal(pthread_t
,
333 void pthread_exit(void *) __dead2
;
334 int pthread_getconcurrency(void);
335 int pthread_getschedparam(pthread_t
, int * __restrict
, struct sched_param
* __restrict
);
336 void *pthread_getspecific(pthread_key_t
);
338 #ifndef LIBC_ALIAS_PTHREAD_JOIN
340 int pthread_join(pthread_t
, void **) __DARWIN_ALIAS_C(pthread_join
);
342 #else /* LIBC_ALIAS_PTHREAD_JOIN */
343 int pthread_join(pthread_t
, void **) LIBC_ALIAS_C(pthread_join
);
344 #endif /* !LIBC_ALIAS_PTHREAD_JOIN */
346 int pthread_key_create(pthread_key_t
*, void (*)(void *));
347 int pthread_key_delete(pthread_key_t
);
348 int pthread_mutex_destroy(pthread_mutex_t
*);
349 int pthread_mutex_getprioceiling(const pthread_mutex_t
* __restrict
, int * __restrict
);
350 int pthread_mutex_init(pthread_mutex_t
* __restrict
, const pthread_mutexattr_t
* __restrict
);
351 int pthread_mutex_lock(pthread_mutex_t
*);
352 int pthread_mutex_setprioceiling(pthread_mutex_t
* __restrict
, int, int * __restrict
);
353 int pthread_mutex_trylock(pthread_mutex_t
*);
354 int pthread_mutex_unlock(pthread_mutex_t
*);
356 #ifndef LIBC_ALIAS_PTHREAD_MUTEXATTR_DESTROY
358 int pthread_mutexattr_destroy(pthread_mutexattr_t
*) __DARWIN_ALIAS(pthread_mutexattr_destroy
);
360 #else /* LIBC_ALIAS_PTHREAD_MUTEXATTR_DESTROY */
361 int pthread_mutexattr_destroy(pthread_mutexattr_t
*) LIBC_ALIAS(pthread_mutexattr_destroy
);
362 #endif /* !LIBC_ALIAS_PTHREAD_MUTEXATTR_DESTROY */
364 int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t
* __restrict
, int * __restrict
);
365 int pthread_mutexattr_getprotocol(const pthread_mutexattr_t
* __restrict
, int * __restrict
);
366 int pthread_mutexattr_getpshared(const pthread_mutexattr_t
* __restrict
, int * __restrict
);
367 int pthread_mutexattr_gettype(const pthread_mutexattr_t
* __restrict
, int * __restrict
);
368 int pthread_mutexattr_init(pthread_mutexattr_t
*);
369 int pthread_mutexattr_setprioceiling(pthread_mutexattr_t
*, int);
370 int pthread_mutexattr_setprotocol(pthread_mutexattr_t
*, int);
371 int pthread_mutexattr_setpshared(pthread_mutexattr_t
*, int );
372 int pthread_mutexattr_settype(pthread_mutexattr_t
*, int);
373 int pthread_once(pthread_once_t
*, void (*)(void));
375 #ifndef LIBC_ALIAS_PTHREAD_RWLOCK_DESTROY
377 int pthread_rwlock_destroy(pthread_rwlock_t
* ) __DARWIN_ALIAS(pthread_rwlock_destroy
);
379 #else /* LIBC_ALIAS_PTHREAD_RWLOCK_DESTROY */
380 int pthread_rwlock_destroy(pthread_rwlock_t
* ) LIBC_ALIAS(pthread_rwlock_destroy
);
381 #endif /* !LIBC_ALIAS_PTHREAD_RWLOCK_DESTROY */
384 #ifndef LIBC_ALIAS_PTHREAD_RWLOCK_INIT
386 int pthread_rwlock_init(pthread_rwlock_t
* __restrict
, const pthread_rwlockattr_t
* __restrict
) __DARWIN_ALIAS(pthread_rwlock_init
);
388 #else /* LIBC_ALIAS_PTHREAD_RWLOCK_INIT */
389 int pthread_rwlock_init(pthread_rwlock_t
* __restrict
, const pthread_rwlockattr_t
* __restrict
) LIBC_ALIAS(pthread_rwlock_init
);
390 #endif /* !LIBC_ALIAS_PTHREAD_RWLOCK_INIT */
393 #ifndef LIBC_ALIAS_PTHREAD_RWLOCK_RDLOCK
395 int pthread_rwlock_rdlock(pthread_rwlock_t
*) __DARWIN_ALIAS(pthread_rwlock_rdlock
);
397 #else /* LIBC_ALIAS_PTHREAD_RWLOCK_RDLOCK */
398 int pthread_rwlock_rdlock(pthread_rwlock_t
*) LIBC_ALIAS(pthread_rwlock_rdlock
);
399 #endif /* !LIBC_ALIAS_PTHREAD_RWLOCK_RDLOCK */
402 #ifndef LIBC_ALIAS_PTHREAD_RWLOCK_TRYRDLOCK
404 int pthread_rwlock_tryrdlock(pthread_rwlock_t
*) __DARWIN_ALIAS(pthread_rwlock_tryrdlock
);
406 #else /* LIBC_ALIAS_PTHREAD_RWLOCK_TRYRDLOCK */
407 int pthread_rwlock_tryrdlock(pthread_rwlock_t
*) LIBC_ALIAS(pthread_rwlock_tryrdlock
);
408 #endif /* !LIBC_ALIAS_PTHREAD_RWLOCK_TRYRDLOCK */
411 #ifndef LIBC_ALIAS_PTHREAD_RWLOCK_TRYWRLOCK
413 int pthread_rwlock_trywrlock(pthread_rwlock_t
*) __DARWIN_ALIAS(pthread_rwlock_trywrlock
);
415 #else /* LIBC_ALIAS_PTHREAD_RWLOCK_TRYWRLOCK */
416 int pthread_rwlock_trywrlock(pthread_rwlock_t
*) LIBC_ALIAS(pthread_rwlock_trywrlock
);
417 #endif /* !LIBC_ALIAS_PTHREAD_RWLOCK_TRYWRLOCK */
420 #ifndef LIBC_ALIAS_PTHREAD_RWLOCK_WRLOCK
422 int pthread_rwlock_wrlock(pthread_rwlock_t
*) __DARWIN_ALIAS(pthread_rwlock_wrlock
);
424 #else /* LIBC_ALIAS_PTHREAD_RWLOCK_WRLOCK */
425 int pthread_rwlock_wrlock(pthread_rwlock_t
*) LIBC_ALIAS(pthread_rwlock_wrlock
);
426 #endif /* !LIBC_ALIAS_PTHREAD_RWLOCK_WRLOCK */
429 #ifndef LIBC_ALIAS_PTHREAD_RWLOCK_UNLOCK
431 int pthread_rwlock_unlock(pthread_rwlock_t
*) __DARWIN_ALIAS(pthread_rwlock_unlock
);
433 #else /* LIBC_ALIAS_PTHREAD_RWLOCK_UNLOCK */
434 int pthread_rwlock_unlock(pthread_rwlock_t
*) LIBC_ALIAS(pthread_rwlock_unlock
);
435 #endif /* !LIBC_ALIAS_PTHREAD_RWLOCK_UNLOCK */
437 int pthread_rwlockattr_destroy(pthread_rwlockattr_t
*);
438 int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t
* __restrict
,
440 int pthread_rwlockattr_init(pthread_rwlockattr_t
*);
441 int pthread_rwlockattr_setpshared(pthread_rwlockattr_t
*,
443 pthread_t
pthread_self(void);
446 #ifndef LIBC_ALIAS_PTHREAD_SETCANCELSTATE
448 int pthread_setcancelstate(int , int *) __DARWIN_ALIAS(pthread_setcancelstate
);
450 #else /* LIBC_ALIAS_PTHREAD_SETCANCELSTATE */
451 int pthread_setcancelstate(int , int *) LIBC_ALIAS(pthread_setcancelstate
);
452 #endif /* !LIBC_ALIAS_PTHREAD_SETCANCELSTATE */
455 #ifndef LIBC_ALIAS_PTHREAD_SETCANCELTYPE
457 int pthread_setcanceltype(int , int *) __DARWIN_ALIAS(pthread_setcanceltype
);
459 #else /* LIBC_ALIAS_PTHREAD_SETCANCELTYPE */
460 int pthread_setcanceltype(int , int *) LIBC_ALIAS(pthread_setcanceltype
);
461 #endif /* !LIBC_ALIAS_PTHREAD_SETCANCELTYPE */
463 int pthread_setconcurrency(int);
464 int pthread_setschedparam(pthread_t
,
466 const struct sched_param
*);
467 int pthread_setspecific(pthread_key_t
,
469 void pthread_testcancel(void) __DARWIN_ALIAS(pthread_testcancel
);
471 #if (!defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)) || defined(_DARWIN_C_SOURCE)
472 /* returns non-zero if pthread_create or cthread_fork have been called */
473 int pthread_is_threaded_np(void);
475 #if defined(__i386__) || defined(__x86_64__)
476 int pthread_threadid_np(pthread_t
,__uint64_t
*);
479 /*SPI to set and get pthread name*/
480 int pthread_getname_np(pthread_t
,char*,size_t);
481 int pthread_setname_np(const char*);
482 /* returns non-zero if the current thread is the main thread */
483 int pthread_main_np(void);
485 /* return the mach thread bound to the pthread */
486 mach_port_t
pthread_mach_thread_np(pthread_t
);
487 size_t pthread_get_stacksize_np(pthread_t
);
488 void * pthread_get_stackaddr_np(pthread_t
);
490 /* Like pthread_cond_signal(), but only wake up the specified pthread */
491 int pthread_cond_signal_thread_np(pthread_cond_t
*, pthread_t
);
493 /* Like pthread_cond_timedwait, but use a relative timeout */
494 int pthread_cond_timedwait_relative_np(pthread_cond_t
*,
496 const struct timespec
*);
498 /* Like pthread_create(), but leaves the thread suspended */
499 int pthread_create_suspended_np(pthread_t
*,
500 const pthread_attr_t
*,
503 int pthread_kill(pthread_t
, int);
505 pthread_t
pthread_from_mach_thread_np(mach_port_t
);
508 #ifndef LIBC_ALIAS_PTHREAD_SIGMASK
510 int pthread_sigmask(int, const sigset_t
*, sigset_t
*) __DARWIN_ALIAS(pthread_sigmask
);
512 #else /* LIBC_ALIAS_PTHREAD_SIGMASK */
513 int pthread_sigmask(int, const sigset_t
*, sigset_t
*) LIBC_ALIAS(pthread_sigmask
);
514 #endif /* !LIBC_ALIAS_PTHREAD_SIGMASK */
516 void pthread_yield_np(void);
517 #endif /* (!_POSIX_C_SOURCE && !_XOPEN_SOURCE) || _DARWIN_C_SOURCE */
519 #endif /* _PTHREAD_H */