1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
5 /* This program is free software; you can redistribute it and/or */
6 /* modify it under the terms of the GNU Library General Public License */
7 /* as published by the Free Software Foundation; either version 2 */
8 /* of the License, or (at your option) any later version. */
10 /* This program is distributed in the hope that it will be useful, */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
13 /* GNU Library General Public License for more details. */
23 #define __need_sigset_t
25 #include <bits/pthreadtypes.h>
32 #define PTHREAD_MUTEX_INITIALIZER \
33 {0, 0, 0, PTHREAD_MUTEX_FAST_NP, {0, 0}}
35 # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
36 {0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, {0, 0}}
39 #define PTHREAD_COND_INITIALIZER {{0, 0}, 0}
42 # define PTHREAD_RWLOCK_INITIALIZER \
43 { {0, 0}, 0, NULL, NULL, NULL, \
44 PTHREAD_RWLOCK_DEFAULT_NP, PTHREAD_PROCESS_PRIVATE }
47 /* Values for attributes. */
51 PTHREAD_CREATE_JOINABLE
,
52 #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
53 PTHREAD_CREATE_DETACHED
54 #define PTHREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
59 PTHREAD_INHERIT_SCHED
,
60 #define PTHREAD_INHERIT_SCHED PTHREAD_INHERIT_SCHED
61 PTHREAD_EXPLICIT_SCHED
62 #define PTHREAD_EXPLICIT_SCHED PTHREAD_EXPLICIT_SCHED
68 #define PTHREAD_SCOPE_SYSTEM PTHREAD_SCOPE_SYSTEM
70 #define PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_PROCESS
75 PTHREAD_MUTEX_FAST_NP
,
76 PTHREAD_MUTEX_RECURSIVE_NP
,
77 PTHREAD_MUTEX_ERRORCHECK_NP
80 PTHREAD_MUTEX_NORMAL
= PTHREAD_MUTEX_FAST_NP
,
81 PTHREAD_MUTEX_RECURSIVE
= PTHREAD_MUTEX_RECURSIVE_NP
,
82 PTHREAD_MUTEX_ERRORCHECK
= PTHREAD_MUTEX_ERRORCHECK_NP
,
83 PTHREAD_MUTEX_DEFAULT
= PTHREAD_MUTEX_NORMAL
89 PTHREAD_PROCESS_PRIVATE
,
90 #define PTHREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE
91 PTHREAD_PROCESS_SHARED
92 #define PTHREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED
98 PTHREAD_RWLOCK_PREFER_READER_NP
,
99 PTHREAD_RWLOCK_PREFER_WRITER_NP
,
100 PTHREAD_RWLOCK_DEFAULT_NP
= PTHREAD_RWLOCK_PREFER_WRITER_NP
104 #define PTHREAD_ONCE_INIT 0
106 /* Cleanup buffers */
108 struct _pthread_cleanup_buffer
110 void (*routine
) __PMT ((void *)); /* Function to call. */
111 void *arg
; /* Its argument. */
112 int canceltype
; /* Saved cancellation type. */
113 struct _pthread_cleanup_buffer
*prev
; /* Chaining of cleanup functions. */
120 PTHREAD_CANCEL_ENABLE
,
121 #define PTHREAD_CANCEL_ENABLE PTHREAD_CANCEL_ENABLE
122 PTHREAD_CANCEL_DISABLE
123 #define PTHREAD_CANCEL_DISABLE PTHREAD_CANCEL_DISABLE
127 PTHREAD_CANCEL_DEFERRED
,
128 #define PTHREAD_CANCEL_DEFERRED PTHREAD_CANCEL_DEFERRED
129 PTHREAD_CANCEL_ASYNCHRONOUS
130 #define PTHREAD_CANCEL_ASYNCHRONOUS PTHREAD_CANCEL_ASYNCHRONOUS
132 #define PTHREAD_CANCELED ((void *) -1)
135 /* Function for handling threads. */
137 /* Create a thread with given attributes ATTR (or default attributes
138 if ATTR is NULL), and call function START_ROUTINE with given
140 extern int pthread_create
__P ((pthread_t
*__thread
,
141 __const pthread_attr_t
*__attr
,
142 void *(*__start_routine
) (void *),
145 /* Obtain the identifier of the current thread. */
146 extern pthread_t pthread_self
__P ((void));
148 /* Compare two thread identifiers. */
149 extern int pthread_equal
__P ((pthread_t __thread1
, pthread_t __thread2
));
151 /* Terminate calling thread. */
152 extern void pthread_exit
__P ((void *__retval
)) __attribute__ ((__noreturn__
));
154 /* Make calling thread wait for termination of the thread TH. The
155 exit status of the thread is stored in *THREAD_RETURN, if THREAD_RETURN
157 extern int pthread_join
__P ((pthread_t __th
, void **__thread_return
));
159 /* Indicate that the thread TH is never to be joined with PTHREAD_JOIN.
160 The resources of TH will therefore be freed immediately when it
161 terminates, instead of waiting for another thread to perform PTHREAD_JOIN
163 extern int pthread_detach
__P ((pthread_t __th
));
166 /* Functions for handling attributes. */
168 /* Initialize thread attribute *ATTR with default attributes
169 (detachstate is PTHREAD_JOINABLE, scheduling policy is SCHED_OTHER,
170 no user-provided stack). */
171 extern int pthread_attr_init
__P ((pthread_attr_t
*__attr
));
173 /* Destroy thread attribute *ATTR. */
174 extern int pthread_attr_destroy
__P ((pthread_attr_t
*__attr
));
176 /* Set the `detachstate' attribute in *ATTR according to DETACHSTATE. */
177 extern int pthread_attr_setdetachstate
__P ((pthread_attr_t
*__attr
,
180 /* Return in *DETACHSTATE the `detachstate' attribute in *ATTR. */
181 extern int pthread_attr_getdetachstate
__P ((__const pthread_attr_t
*__attr
,
182 int *__detachstate
));
184 /* Set scheduling parameters (priority, etc) in *ATTR according to PARAM. */
185 extern int pthread_attr_setschedparam
__P ((pthread_attr_t
*__attr
,
186 __const
struct sched_param
*__param
));
188 /* Return in *PARAM the scheduling parameters of *ATTR. */
189 extern int pthread_attr_getschedparam
__P ((__const pthread_attr_t
*__attr
,
190 struct sched_param
*__param
));
192 /* Set scheduling policy in *ATTR according to POLICY. */
193 extern int pthread_attr_setschedpolicy
__P ((pthread_attr_t
*__attr
,
196 /* Return in *POLICY the scheduling policy of *ATTR. */
197 extern int pthread_attr_getschedpolicy
__P ((__const pthread_attr_t
*__attr
,
200 /* Set scheduling inheritance mode in *ATTR according to INHERIT. */
201 extern int pthread_attr_setinheritsched
__P ((pthread_attr_t
*__attr
,
204 /* Return in *INHERIT the scheduling inheritance mode of *ATTR. */
205 extern int pthread_attr_getinheritsched
__P ((__const pthread_attr_t
*__attr
,
208 /* Set scheduling contention scope in *ATTR according to SCOPE. */
209 extern int pthread_attr_setscope
__P ((pthread_attr_t
*__attr
, int __scope
));
211 /* Return in *SCOPE the scheduling contention scope of *ATTR. */
212 extern int pthread_attr_getscope
__P ((__const pthread_attr_t
*__attr
,
216 /* Set the size of the guard area at the bottom of the thread. */
217 extern int __pthread_attr_setguardsize
__P ((pthread_attr_t
*__attr
,
218 size_t __guardsize
));
219 extern int pthread_attr_setguardsize
__P ((pthread_attr_t
*__attr
,
220 size_t __guardsize
));
222 /* Get the size of the guard area at the bottom of the thread. */
223 extern int __pthread_attr_getguardsize
__P ((__const pthread_attr_t
*__attr
,
224 size_t *__guardsize
));
225 extern int pthread_attr_getguardsize
__P ((__const pthread_attr_t
*__attr
,
226 size_t *__guardsize
));
229 /* Set the starting address of the stack of the thread to be created.
230 Depending on whether the stack grows up or doen the value must either
231 be higher or lower than all the address in the memory block. The
232 minimal size of the block must be PTHREAD_STACK_SIZE. */
233 extern int __pthread_attr_setstackaddr
__P ((pthread_attr_t
*__attr
,
235 extern int pthread_attr_setstackaddr
__P ((pthread_attr_t
*__attr
,
238 /* Return the previously set address for the stack. */
239 extern int __pthread_attr_getstackaddr
__P ((__const pthread_attr_t
*__attr
,
240 void **__stackaddr
));
241 extern int pthread_attr_getstackaddr
__P ((__const pthread_attr_t
*__attr
,
242 void **__stackaddr
));
244 /* Add information about the minimum stack size needed for the thread
245 to be started. This size must never be less than PTHREAD_STACK_SIZE
246 and must also not exceed the system limits. */
247 extern int __pthread_attr_setstacksize
__P ((pthread_attr_t
*__attr
,
248 size_t __stacksize
));
249 extern int pthread_attr_setstacksize
__P ((pthread_attr_t
*__attr
,
250 size_t __stacksize
));
252 /* Return the currently used minimal stack size. */
253 extern int __pthread_attr_getstacksize
__P ((__const pthread_attr_t
*__attr
,
254 size_t *__stacksize
));
255 extern int pthread_attr_getstacksize
__P ((__const pthread_attr_t
*__attr
,
256 size_t *__stacksize
));
258 /* Functions for scheduling control. */
260 /* Set the scheduling parameters for TARGET_THREAD according to POLICY
262 extern int pthread_setschedparam
__P ((pthread_t __target_thread
, int __policy
,
263 __const
struct sched_param
*__param
));
265 /* Return in *POLICY and *PARAM the scheduling parameters for TARGET_THREAD. */
266 extern int pthread_getschedparam
__P ((pthread_t __target_thread
,
268 struct sched_param
*__param
));
271 /* Determine level of concurrency. */
272 extern int __pthread_getconcurrency
__P ((void));
273 extern int pthread_getconcurrency
__P ((void));
275 /* Set new concurrency level to LEVEL. */
276 extern int __pthread_setconcurrency
__P ((int __level
));
277 extern int pthread_setconcurrency
__P ((int __level
));
280 /* Functions for mutex handling. */
282 /* Initialize MUTEX using attributes in *MUTEX_ATTR, or use the
283 default values if later is NULL. */
284 extern int __pthread_mutex_init
__P ((pthread_mutex_t
*__mutex
,
285 __const pthread_mutexattr_t
*__mutex_attr
));
286 extern int pthread_mutex_init
__P ((pthread_mutex_t
*__mutex
,
287 __const pthread_mutexattr_t
*__mutex_attr
));
290 extern int __pthread_mutex_destroy
__P ((pthread_mutex_t
*__mutex
));
291 extern int pthread_mutex_destroy
__P ((pthread_mutex_t
*__mutex
));
293 /* Try to lock MUTEX. */
294 extern int __pthread_mutex_trylock
__P ((pthread_mutex_t
*__mutex
));
295 extern int pthread_mutex_trylock
__P ((pthread_mutex_t
*__mutex
));
297 /* Wait until lock for MUTEX becomes available and lock it. */
298 extern int __pthread_mutex_lock
__P ((pthread_mutex_t
*__mutex
));
299 extern int pthread_mutex_lock
__P ((pthread_mutex_t
*__mutex
));
302 extern int __pthread_mutex_unlock
__P ((pthread_mutex_t
*__mutex
));
303 extern int pthread_mutex_unlock
__P ((pthread_mutex_t
*__mutex
));
306 /* Functions for handling mutex attributes. */
308 /* Initialize mutex attribute object ATTR with default attributes
309 (kind is PTHREAD_MUTEX_FAST_NP). */
310 extern int __pthread_mutexattr_init
__P ((pthread_mutexattr_t
*__attr
));
311 extern int pthread_mutexattr_init
__P ((pthread_mutexattr_t
*__attr
));
313 /* Destroy mutex attribute object ATTR. */
314 extern int __pthread_mutexattr_destroy
__P ((pthread_mutexattr_t
*__attr
));
315 extern int pthread_mutexattr_destroy
__P ((pthread_mutexattr_t
*__attr
));
318 /* Set the mutex kind attribute in *ATTR to KIND (either PTHREAD_MUTEX_NORMAL,
319 PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_ERRORCHECK, or
320 PTHREAD_MUTEX_DEFAULT). */
321 extern int pthread_mutexattr_settype
__P ((pthread_mutexattr_t
*__attr
,
324 /* Return in *KIND the mutex kind attribute in *ATTR. */
325 extern int pthread_mutexattr_gettype
__P ((__const pthread_mutexattr_t
*__attr
,
330 /* Functions for handling conditional variables. */
332 /* Initialize condition variable COND using attributes ATTR, or use
333 the default values if later is NULL. */
334 extern int pthread_cond_init
__P ((pthread_cond_t
*__cond
,
335 __const pthread_condattr_t
*__cond_attr
));
337 /* Destroy condition variable COND. */
338 extern int pthread_cond_destroy
__P ((pthread_cond_t
*__cond
));
340 /* Wake up one thread waiting for condition variable COND. */
341 extern int pthread_cond_signal
__P ((pthread_cond_t
*__cond
));
343 /* Wake up all threads waiting for condition variables COND. */
344 extern int pthread_cond_broadcast
__P ((pthread_cond_t
*__cond
));
346 /* Wait for condition variable COND to be signaled or broadcast.
347 MUTEX is assumed to be locked before. */
348 extern int pthread_cond_wait
__P ((pthread_cond_t
*__cond
,
349 pthread_mutex_t
*__mutex
));
351 /* Wait for condition variable COND to be signaled or broadcast until
352 ABSTIME. MUTEX is assumed to be locked before. ABSTIME is an
353 absolute time specification; zero is the beginning of the epoch
354 (00:00:00 GMT, January 1, 1970). */
355 extern int pthread_cond_timedwait
__P ((pthread_cond_t
*__cond
,
356 pthread_mutex_t
*__mutex
,
357 __const
struct timespec
*__abstime
));
359 /* Functions for handling condition variable attributes. */
361 /* Initialize condition variable attribute ATTR. */
362 extern int pthread_condattr_init
__P ((pthread_condattr_t
*__attr
));
364 /* Destroy condition variable attribute ATTR. */
365 extern int pthread_condattr_destroy
__P ((pthread_condattr_t
*__attr
));
369 /* Functions for handling read-write locks. */
371 /* Initialize read-write lock RWLOCK using attributes ATTR, or use
372 the default values if later is NULL. */
373 extern int pthread_rwlock_init
__P ((pthread_rwlock_t
*__rwlock
,
374 __const pthread_rwlockattr_t
*__attr
));
376 /* Destroy read-write lock RWLOCK. */
377 extern int pthread_rwlock_destroy
__P ((pthread_rwlock_t
*__rwlock
));
379 /* Acquire read lock for RWLOCK. */
380 extern int pthread_rwlock_rdlock
__P ((pthread_rwlock_t
*__rwlock
));
382 /* Try to acquire read lock for RWLOCK. */
383 extern int pthread_rwlock_tryrdlock
__P ((pthread_rwlock_t
*__rwlock
));
385 /* Acquire write lock for RWLOCK. */
386 extern int pthread_rwlock_wrlock
__P ((pthread_rwlock_t
*__rwlock
));
388 /* Try to acquire writelock for RWLOCK. */
389 extern int pthread_rwlock_trywrlock
__P ((pthread_rwlock_t
*__rwlock
));
392 extern int pthread_rwlock_unlock
__P ((pthread_rwlock_t
*__rwlock
));
395 /* Functions for handling read-write lock attributes. */
397 /* Initialize attribute object ATTR with default values. */
398 extern int pthread_rwlockattr_init
__P ((pthread_rwlockattr_t
*__attr
));
400 /* Destroy attribute object ATTR. */
401 extern int pthread_rwlockattr_destroy
__P ((pthread_rwlockattr_t
*__attr
));
403 /* Return current setting of process-shared attribute of ATTR in PSHARED. */
404 extern int pthread_rwlockattr_getpshared
__P ((__const
405 pthread_rwlockattr_t
*__attr
,
408 /* Set process-shared attribute of ATTR to PSHARED. */
409 extern int pthread_rwlockattr_setpshared
__P ((pthread_rwlockattr_t
*__attr
,
412 /* Return current setting of reader/writer preference. */
413 extern int pthread_rwlockattr_getkind_np
__P ((__const
414 pthread_rwlockattr_t
*__attr
,
417 /* Set reader/write preference. */
418 extern int pthread_rwlockattr_setkind_np
__P ((pthread_rwlockattr_t
*__attr
,
423 /* Functions for handling thread-specific data */
425 /* Create a key value identifying a location in the thread-specific data
426 area. Each thread maintains a distinct thread-specific data area.
427 DESTR_FUNCTION, if non-NULL, is called with
428 the value associated to that key when the key is destroyed.
429 DESTR_FUNCTION is not called if the value associated is NULL
430 when the key is destroyed. */
431 extern int __pthread_key_create
__P ((pthread_key_t
*__key
,
432 void (*__destr_function
) (void *)));
433 extern int pthread_key_create
__P ((pthread_key_t
*__key
,
434 void (*__destr_function
) (void *)));
437 extern int pthread_key_delete
__P ((pthread_key_t __key
));
439 /* Store POINTER in the thread-specific data slot identified by KEY. */
440 extern int __pthread_setspecific
__P ((pthread_key_t __key
,
441 __const
void *__pointer
));
442 extern int pthread_setspecific
__P ((pthread_key_t __key
,
443 __const
void *__pointer
));
445 /* Return current value of the thread-specific data slot identified by KEY. */
446 extern void *__pthread_getspecific
__P ((pthread_key_t __key
));
447 extern void *pthread_getspecific
__P ((pthread_key_t __key
));
450 /* Functions for handling initialization */
452 /* Guarantee that the initialization function INIT_ROUTINE will be called
453 only once, even if pthread_once is executed several times with the
454 same ONCE_CONTROL argument. ONCE_CONTROL must point to a static or
455 extern variable initialized to PTHREAD_ONCE_INIT. */
456 extern int __pthread_once
__P ((pthread_once_t
*__once_control
,
457 void (*__init_routine
) (void)));
458 extern int pthread_once
__P ((pthread_once_t
*__once_control
,
459 void (*__init_routine
) (void)));
462 /* Functions for handling cancellation. */
464 /* Set cancelability state of current thread to STATE, returning old
465 state in *OLDSTATE if OLDSTATE is not NULL. */
466 extern int pthread_setcancelstate
__P ((int __state
, int *__oldstate
));
468 /* Set cancellation state of current thread to TYPE, returning the old
469 type in *OLDTYPE if OLDTYPE is not NULL. */
470 extern int __pthread_setcanceltype
__P ((int __type
, int *__oldtype
));
471 extern int pthread_setcanceltype
__P ((int __type
, int *__oldtype
));
473 /* Cancel THREAD immediately or at the next possibility. */
474 extern int pthread_cancel
__P ((pthread_t __thread
));
476 /* Test for pending cancellation for the current thread and terminate
477 the thread as per pthread_exit(PTHREAD_CANCELED) if it has been
479 extern void pthread_testcancel
__P ((void));
482 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
483 when the thread is cancelled or calls pthread_exit. ROUTINE will also
484 be called with arguments ARG when the matching pthread_cleanup_pop
485 is executed with non-zero EXECUTE argument.
486 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
487 be used in matching pairs at the same nesting level of braces. */
489 #define pthread_cleanup_push(routine,arg) \
490 { struct _pthread_cleanup_buffer _buffer; \
491 _pthread_cleanup_push (&_buffer, (routine), (arg));
493 extern void _pthread_cleanup_push
__P ((struct _pthread_cleanup_buffer
*__buffer
,
494 void (*__routine
) (void *),
497 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
498 If EXECUTE is non-zero, the handler function is called. */
500 #define pthread_cleanup_pop(execute) \
501 _pthread_cleanup_pop (&_buffer, (execute)); }
503 extern void _pthread_cleanup_pop
__P ((struct _pthread_cleanup_buffer
*__buffer
,
506 /* Install a cleanup handler as pthread_cleanup_push does, but also
507 saves the current cancellation type and set it to deferred cancellation. */
510 # define pthread_cleanup_push_defer_np(routine,arg) \
511 { struct _pthread_cleanup_buffer _buffer; \
512 _pthread_cleanup_push_defer (&_buffer, (routine), (arg));
514 extern void _pthread_cleanup_push_defer
__P ((struct _pthread_cleanup_buffer
*__buffer
,
515 void (*__routine
) (void *),
518 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
519 restores the cancellation type that was in effect when the matching
520 pthread_cleanup_push_defer was called. */
522 # define pthread_cleanup_pop_restore_np(execute) \
523 _pthread_cleanup_pop_restore (&_buffer, (execute)); }
525 extern void _pthread_cleanup_pop_restore
__P ((struct _pthread_cleanup_buffer
*__buffer
,
529 /* Functions for handling signals. */
530 #include <bits/sigthread.h>
533 /* Functions for handling process creation and process execution. */
535 /* Install handlers to be called when a new process is created with FORK.
536 The PREPARE handler is called in the parent process just before performing
537 FORK. The PARENT handler is called in the parent process just after FORK.
538 The CHILD handler is called in the child process. Each of the three
539 handlers can be NULL, meaning that no handler needs to be called at that
541 PTHREAD_ATFORK can be called several times, in which case the PREPARE
542 handlers are called in LIFO order (last added with PTHREAD_ATFORK,
543 first called before FORK), and the PARENT and CHILD handlers are called
544 in FIFO (first added, first called). */
546 extern int __pthread_atfork
__P ((void (*__prepare
) (void),
547 void (*__parent
) (void),
548 void (*__child
) (void)));
549 extern int pthread_atfork
__P ((void (*__prepare
) (void),
550 void (*__parent
) (void),
551 void (*__child
) (void)));
553 /* Terminate all threads in the program except the calling process.
554 Should be called just before invoking one of the exec*() functions. */
556 extern void __pthread_kill_other_threads_np
__P ((void));
557 extern void pthread_kill_other_threads_np
__P ((void));
560 /* This function is called to initialize the pthread library. */
561 extern void __pthread_initialize
__P ((void));
565 #endif /* pthread.h */