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. */
25 #define __need_sigset_t
27 #define __need_timespec
30 /* Linux has no ENOTSUP error code. */
32 #define ENOTSUP EOPNOTSUPP
40 /* Thread identifiers */
41 typedef unsigned long int pthread_t;
43 /* Thread descriptors */
44 typedef struct _pthread_descr_struct *_pthread_descr;
46 /* Waiting queues (not abstract because mutexes and conditions aren't). */
49 _pthread_descr head; /* First element, or NULL if queue empty. */
50 _pthread_descr tail; /* Last element, or NULL if queue empty. */
53 /* Mutexes (not abstract because of PTHREAD_MUTEX_INITIALIZER). */
56 int m_spinlock; /* Spin lock to guarantee mutual exclusion. */
57 int m_count; /* 0 if free, > 0 if taken. */
58 _pthread_descr m_owner; /* Owner of mutex (for recursive mutexes) */
59 int m_kind; /* Kind of mutex */
60 struct _pthread_queue m_waiting; /* Threads waiting on this mutex. */
63 #define PTHREAD_MUTEX_INITIALIZER \
64 {0, 0, 0, PTHREAD_MUTEX_FAST_NP, {0, 0}}
65 #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
66 {0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, {0, 0}}
68 /* Conditions (not abstract because of PTHREAD_COND_INITIALIZER */
71 int c_spinlock; /* Spin lock to protect the queue. */
72 struct _pthread_queue c_waiting; /* Threads waiting on this condition. */
75 #define PTHREAD_COND_INITIALIZER {0, {0, 0}}
78 /* Read-write locks. */
81 int rw_spinlock; /* Spin lock to guarantee mutual exclusion */
82 int rw_readers; /* Number of readers */
83 _pthread_descr rw_writer; /* Identity of writer, or NULL if none */
84 struct _pthread_queue rw_read_waiting; /* Threads waiting for reading */
85 struct _pthread_queue rw_write_waiting; /* Threads waiting for writing */
86 int rw_kind; /* Reader/Writer preference selection */
87 int rw_pshared; /* Shared between processes or not */
90 # define PTHREAD_RWLOCK_INITIALIZER \
91 { 0, 0, 0, {0, 0}, {0, 0}, \
92 PTHREAD_RWLOCK_DEFAULT_NP, PTHREAD_PROCESS_PRIVATE }
99 PTHREAD_CREATE_JOINABLE,
100 PTHREAD_CREATE_DETACHED
105 PTHREAD_INHERIT_SCHED,
106 PTHREAD_EXPLICIT_SCHED
111 PTHREAD_SCOPE_SYSTEM,
112 PTHREAD_SCOPE_PROCESS
119 struct sched_param schedparam;
126 PTHREAD_MUTEX_FAST_NP,
127 PTHREAD_MUTEX_RECURSIVE_NP,
128 PTHREAD_MUTEX_ERRORCHECK_NP
131 PTHREAD_MUTEX_NORMAL = PTHREAD_MUTEX_FAST_NP,
132 PTHREAD_MUTEX_RECURSIVE = PTHREAD_MUTEX_RECURSIVE_NP,
133 PTHREAD_MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK_NP,
134 PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
141 } pthread_mutexattr_t;
146 } pthread_condattr_t;
151 PTHREAD_PROCESS_PRIVATE,
152 PTHREAD_PROCESS_SHARED
157 PTHREAD_RWLOCK_PREFER_READER_NP,
158 PTHREAD_RWLOCK_PREFER_WRITER_NP,
159 PTHREAD_RWLOCK_DEFAULT_NP = PTHREAD_RWLOCK_PREFER_WRITER_NP
166 } pthread_rwlockattr_t;
169 /* Keys for thread-specific data */
171 typedef unsigned int pthread_key_t;
173 /* Once-only execution */
175 typedef int pthread_once_t;
177 #define PTHREAD_ONCE_INIT 0
179 /* Cleanup buffers */
181 struct _pthread_cleanup_buffer
183 void (*routine) __P ((void *)); /* Function to call. */
184 void *arg; /* Its argument. */
185 int canceltype; /* Saved cancellation type. */
186 struct _pthread_cleanup_buffer *prev; /* Chaining of cleanup functions. */
191 enum { PTHREAD_CANCEL_ENABLE, PTHREAD_CANCEL_DISABLE };
192 enum { PTHREAD_CANCEL_DEFERRED, PTHREAD_CANCEL_ASYNCHRONOUS };
193 #define PTHREAD_CANCELED ((void *) -1)
196 /* Function for handling threads. */
198 /* Create a thread with given attributes ATTR (or default attributes
199 if ATTR is NULL), and call function START_ROUTINE with given
201 extern int pthread_create __P ((pthread_t *__thread,
202 __const pthread_attr_t *__attr,
203 void *(*__start_routine) (void *),
206 /* Obtain the identifier of the current thread. */
207 extern pthread_t pthread_self __P ((void));
209 /* Compare two thread identifiers. */
210 extern int pthread_equal __P ((pthread_t __thread1, pthread_t __thread2));
212 /* Terminate calling thread. */
213 extern void pthread_exit __P ((void *__retval)) __attribute__ ((__noreturn__));
215 /* Make calling thread wait for termination of the thread TH. The
216 exit status of the thread is stored in *THREAD_RETURN, if THREAD_RETURN
218 extern int pthread_join __P ((pthread_t __th, void **__thread_return));
220 /* Indicate that the thread TH is never to be joined with PTHREAD_JOIN.
221 The resources of TH will therefore be freed immediately when it
222 terminates, instead of waiting for another thread to perform PTHREAD_JOIN
224 extern int pthread_detach __P ((pthread_t __th));
227 /* Functions for handling attributes. */
229 /* Initialize thread attribute *ATTR with default attributes
230 (detachstate is PTHREAD_JOINABLE, scheduling policy is SCHED_OTHER). */
231 extern int pthread_attr_init __P ((pthread_attr_t *__attr));
233 /* Destroy thread attribute *ATTR. */
234 extern int pthread_attr_destroy __P ((pthread_attr_t *__attr));
236 /* Set the `detachstate' attribute in *ATTR according to DETACHSTATE. */
237 extern int pthread_attr_setdetachstate __P ((pthread_attr_t *__attr,
240 /* Return in *DETACHSTATE the `detachstate' attribute in *ATTR. */
241 extern int pthread_attr_getdetachstate __P ((__const pthread_attr_t *__attr,
242 int *__detachstate));
244 /* Set scheduling parameters (priority, etc) in *ATTR according to PARAM. */
245 extern int pthread_attr_setschedparam __P ((pthread_attr_t *__attr,
246 __const struct sched_param *__param));
248 /* Return in *PARAM the scheduling parameters of *ATTR. */
249 extern int pthread_attr_getschedparam __P ((__const pthread_attr_t *__attr,
250 struct sched_param *__param));
252 /* Set scheduling policy in *ATTR according to POLICY. */
253 extern int pthread_attr_setschedpolicy __P ((pthread_attr_t *__attr,
256 /* Return in *POLICY the scheduling policy of *ATTR. */
257 extern int pthread_attr_getschedpolicy __P ((__const pthread_attr_t *__attr,
260 /* Set scheduling inheritance mode in *ATTR according to INHERIT. */
261 extern int pthread_attr_setinheritsched __P ((pthread_attr_t *__attr,
264 /* Return in *INHERIT the scheduling inheritance mode of *ATTR. */
265 extern int pthread_attr_getinheritsched __P ((__const pthread_attr_t *__attr,
268 /* Set scheduling contention scope in *ATTR according to SCOPE. */
269 extern int pthread_attr_setscope __P ((pthread_attr_t *__attr, int __scope));
271 /* Return in *SCOPE the scheduling contention scope of *ATTR. */
272 extern int pthread_attr_getscope __P ((__const pthread_attr_t *__attr,
275 /* Functions for scheduling control. */
277 /* Set the scheduling parameters for TARGET_THREAD according to POLICY
279 extern int pthread_setschedparam __P ((pthread_t __target_thread, int __policy,
280 __const struct sched_param *__param));
282 /* Return in *POLICY and *PARAM the scheduling parameters for TARGET_THREAD. */
283 extern int pthread_getschedparam __P ((pthread_t __target_thread,
285 struct sched_param *__param));
288 /* Functions for mutex handling. */
290 /* Initialize MUTEX using attributes in *MUTEX_ATTR, or use the
291 default values if later is NULL. */
292 extern int __pthread_mutex_init __P ((pthread_mutex_t *__mutex,
293 __const pthread_mutexattr_t *__mutex_attr));
294 extern int pthread_mutex_init __P ((pthread_mutex_t *__mutex,
295 __const pthread_mutexattr_t *__mutex_attr));
298 extern int __pthread_mutex_destroy __P ((pthread_mutex_t *__mutex));
299 extern int pthread_mutex_destroy __P ((pthread_mutex_t *__mutex));
301 /* Try to lock MUTEX. */
302 extern int __pthread_mutex_trylock __P ((pthread_mutex_t *__mutex));
303 extern int pthread_mutex_trylock __P ((pthread_mutex_t *__mutex));
305 /* Wait until lock for MUTEX becomes available and lock it. */
306 extern int __pthread_mutex_lock __P ((pthread_mutex_t *__mutex));
307 extern int pthread_mutex_lock __P ((pthread_mutex_t *__mutex));
310 extern int __pthread_mutex_unlock __P ((pthread_mutex_t *__mutex));
311 extern int pthread_mutex_unlock __P ((pthread_mutex_t *__mutex));
314 /* Functions for handling mutex attributes. */
316 /* Initialize mutex attribute object ATTR with default attributes
317 (kind is PTHREAD_MUTEX_FAST_NP). */
318 extern int __pthread_mutexattr_init __P ((pthread_mutexattr_t *__attr));
319 extern int pthread_mutexattr_init __P ((pthread_mutexattr_t *__attr));
321 /* Destroy mutex attribute object ATTR. */
322 extern int __pthread_mutexattr_destroy __P ((pthread_mutexattr_t *__attr));
323 extern int pthread_mutexattr_destroy __P ((pthread_mutexattr_t *__attr));
325 /* Set the mutex kind attribute in *ATTR to KIND (either PTHREAD_MUTEX_FAST_NP
326 or PTHREAD_MUTEX_RECURSIVE_NP). */
327 extern int __pthread_mutexattr_setkind_np __P ((pthread_mutexattr_t *__attr,
329 extern int pthread_mutexattr_setkind_np __P ((pthread_mutexattr_t *__attr,
331 /* Return in *KIND the mutex kind attribute in *ATTR. */
332 extern int pthread_mutexattr_getkind_np __P ((__const pthread_mutexattr_t *__attr,
336 /* Functions for handling conditional variables. */
338 /* Initialize condition variable COND using attributes ATTR, or use
339 the default values if later is NULL. */
340 extern int pthread_cond_init __P ((pthread_cond_t *__cond,
341 __const pthread_condattr_t *__cond_attr));
343 /* Destroy condition variable COND. */
344 extern int pthread_cond_destroy __P ((pthread_cond_t *__cond));
346 /* Wake up one thread waiting for condition variable COND. */
347 extern int pthread_cond_signal __P ((pthread_cond_t *__cond));
349 /* Wake up all threads waiting for condition variables COND. */
350 extern int pthread_cond_broadcast __P ((pthread_cond_t *__cond));
352 /* Wait for condition variable COND to be signaled or broadcast.
353 MUTEX is assumed to be locked before. */
354 extern int pthread_cond_wait __P ((pthread_cond_t *__cond,
355 pthread_mutex_t *__mutex));
357 /* Wait for condition variable COND to be signaled or broadcast until
358 ABSTIME. MUTEX is assumed to be locked before. ABSTIME is an
359 absolute time specification; zero is the beginning of the epoch
360 (00:00:00 GMT, January 1, 1970). */
361 extern int pthread_cond_timedwait __P ((pthread_cond_t *__cond,
362 pthread_mutex_t *__mutex,
363 __const struct timespec *__abstime));
365 /* Functions for handling condition variable attributes. */
367 /* Initialize condition variable attribute ATTR. */
368 extern int pthread_condattr_init __P ((pthread_condattr_t *__attr));
370 /* Destroy condition variable attribute ATTR. */
371 extern int pthread_condattr_destroy __P ((pthread_condattr_t *__attr));
375 /* Functions for handling read-write locks. */
377 /* Initialize read-write lock RWLOCK using attributes ATTR, or use
378 the default values if later is NULL. */
379 extern int pthread_rwlock_init __P ((pthread_rwlock_t *__rwlock,
380 __const pthread_rwlockattr_t *__attr));
382 /* Destroy read-write lock RWLOCK. */
383 extern int pthread_rwlock_destroy __P ((pthread_rwlock_t *__rwlock));
385 /* Acquire read lock for RWLOCK. */
386 extern int pthread_rwlock_rdlock __P ((pthread_rwlock_t *__rwlock));
388 /* Try to acquire read lock for RWLOCK. */
389 extern int pthread_rwlock_tryrdlock __P ((pthread_rwlock_t *__rwlock));
391 /* Acquire write lock for RWLOCK. */
392 extern int pthread_rwlock_wrlock __P ((pthread_rwlock_t *__rwlock));
394 /* Try to acquire writelock for RWLOCK. */
395 extern int pthread_rwlock_trywrlock __P ((pthread_rwlock_t *__rwlock));
398 extern int pthread_rwlock_unlock __P ((pthread_rwlock_t *__rwlock));
401 /* Functions for handling read-write lock attributes. */
403 /* Initialize attribute object ATTR with default values. */
404 extern int pthread_rwlockattr_init __P ((pthread_rwlockattr_t *__attr));
406 /* Destroy attribute object ATTR. */
407 extern int pthread_rwlockattr_destroy __P ((pthread_rwlockattr_t *__attr));
409 /* Return current setting of process-shared attribute of ATTR in PSHARED. */
410 extern int pthread_rwlockattr_getpshared __P ((__const
411 pthread_rwlockattr_t *__attr,
414 /* Set process-shared attribute of ATTR to PSHARED. */
415 extern int pthread_rwlockattr_setpshared __P ((pthread_rwlockattr_t *__attr,
418 /* Return current setting of reader/writer preference. */
419 extern int pthread_rwlockattr_getkind_np __P ((__const
420 pthread_rwlockattr_t *__attr,
423 /* Set reader/write preference. */
424 extern int pthread_rwlockattr_setkind_np __P ((pthread_rwlockattr_t *__attr,
429 /* Functions for handling thread-specific data */
431 /* Create a key value identifying a location in the thread-specific data
432 area. Each thread maintains a distinct thread-specific data area.
433 DESTR_FUNCTION, if non-NULL, is called with
434 the value associated to that key when the key is destroyed.
435 DESTR_FUNCTION is not called if the value associated is NULL
436 when the key is destroyed. */
437 extern int __pthread_key_create __P ((pthread_key_t *__key,
438 void (*__destr_function) (void *)));
439 extern int pthread_key_create __P ((pthread_key_t *__key,
440 void (*__destr_function) (void *)));
443 extern int pthread_key_delete __P ((pthread_key_t __key));
445 /* Store POINTER in the thread-specific data slot identified by KEY. */
446 extern int __pthread_setspecific __P ((pthread_key_t __key,
447 __const void *__pointer));
448 extern int pthread_setspecific __P ((pthread_key_t __key,
449 __const void *__pointer));
451 /* Return current value of the thread-specific data slot identified by KEY. */
452 extern void *__pthread_getspecific __P ((pthread_key_t __key));
453 extern void *pthread_getspecific __P ((pthread_key_t __key));
456 /* Functions for handling initialization */
458 /* Guarantee that the initialization function INIT_ROUTINE will be called
459 only once, even if pthread_once is executed several times with the
460 same ONCE_CONTROL argument. ONCE_CONTROL must point to a static or
461 extern variable initialized to PTHREAD_ONCE_INIT. */
462 extern int __pthread_once __P ((pthread_once_t *__once_control,
463 void (*__init_routine) (void)));
464 extern int pthread_once __P ((pthread_once_t *__once_control,
465 void (*__init_routine) (void)));
468 /* Functions for handling cancellation. */
470 /* Set cancelability state of current thread to STATE, returning old
471 state in *OLDSTATE if OLDSTATE is not NULL. */
472 extern int pthread_setcancelstate __P ((int __state, int *__oldstate));
474 /* Set cancellation state of current thread to TYPE, returning the old
475 type in *OLDTYPE if OLDTYPE is not NULL. */
476 extern int __pthread_setcanceltype __P ((int __type, int *__oldtype));
477 extern int pthread_setcanceltype __P ((int __type, int *__oldtype));
479 /* Cancel THREAD immediately or at the next possibility. */
480 extern int pthread_cancel __P ((pthread_t __thread));
482 /* Test for pending cancellation for the current thread and terminate
483 the thread as per pthread_exit(PTHREAD_CANCELED) if it has been
485 extern void pthread_testcancel __P ((void));
488 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
489 when the thread is cancelled or calls pthread_exit. ROUTINE will also
490 be called with arguments ARG when the matching pthread_cleanup_pop
491 is executed with non-zero EXECUTE argument.
492 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
493 be used in matching pairs at the same nesting level of braces. */
495 #define pthread_cleanup_push(routine,arg) \
496 { struct _pthread_cleanup_buffer _buffer; \
497 _pthread_cleanup_push (&_buffer, (routine), (arg));
499 extern void _pthread_cleanup_push __P ((struct _pthread_cleanup_buffer *__buffer,
500 void (*__routine) (void *),
503 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
504 If EXECUTE is non-zero, the handler function is called. */
506 #define pthread_cleanup_pop(execute) \
507 _pthread_cleanup_pop (&_buffer, (execute)); }
509 extern void _pthread_cleanup_pop __P ((struct _pthread_cleanup_buffer *__buffer,
512 /* Install a cleanup handler as pthread_cleanup_push does, but also
513 saves the current cancellation type and set it to deferred cancellation. */
515 #define pthread_cleanup_push_defer_np(routine,arg) \
516 { struct _pthread_cleanup_buffer _buffer; \
517 _pthread_cleanup_push_defer (&_buffer, (routine), (arg));
519 extern void _pthread_cleanup_push_defer __P ((struct _pthread_cleanup_buffer *__buffer,
520 void (*__routine) (void *),
523 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
524 restores the cancellation type that was in effect when the matching
525 pthread_cleanup_push_defer was called. */
527 #define pthread_cleanup_pop_restore_np(execute) \
528 _pthread_cleanup_pop_restore (&_buffer, (execute)); }
530 extern void _pthread_cleanup_pop_restore __P ((struct _pthread_cleanup_buffer *__buffer,
533 /* Functions for handling signals. */
535 /* Modify the signal mask for the calling thread. The arguments have
536 the same meaning as for sigprocmask(2). */
538 extern int pthread_sigmask __P ((int __how, __const sigset_t *__newmask,
539 sigset_t *__oldmask));
541 /* Send signal SIGNO to the given thread. */
543 extern int pthread_kill __P ((pthread_t __thread, int __signo));
546 /* Functions for handling process creation and process execution. */
548 /* Install handlers to be called when a new process is created with FORK.
549 The PREPARE handler is called in the parent process just before performing
550 FORK. The PARENT handler is called in the parent process just after FORK.
551 The CHILD handler is called in the child process. Each of the three
552 handlers can be NULL, meaning that no handler needs to be called at that
554 PTHREAD_ATFORK can be called several times, in which case the PREPARE
555 handlers are called in LIFO order (last added with PTHREAD_ATFORK,
556 first called before FORK), and the PARENT and CHILD handlers are called
557 in FIFO (first added, first called). */
559 extern int __pthread_atfork __P ((void (*__prepare) (void),
560 void (*__parent) (void),
561 void (*__child) (void)));
562 extern int pthread_atfork __P ((void (*__prepare) (void),
563 void (*__parent) (void),
564 void (*__child) (void)));
566 /* Terminate all threads in the program except the calling process.
567 Should be called just before invoking one of the exec*() functions. */
569 extern void __pthread_kill_other_threads_np __P ((void));
570 extern void pthread_kill_other_threads_np __P ((void));
573 /* This function is called to initialize the pthread library. */
574 extern void __pthread_initialize __P ((void));
578 #endif /* pthread.h */