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;
130 PTHREAD_MUTEX_FAST_NP,
131 PTHREAD_MUTEX_RECURSIVE_NP,
132 PTHREAD_MUTEX_ERRORCHECK_NP
135 PTHREAD_MUTEX_NORMAL = PTHREAD_MUTEX_FAST_NP,
136 PTHREAD_MUTEX_RECURSIVE = PTHREAD_MUTEX_RECURSIVE_NP,
137 PTHREAD_MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK_NP,
138 PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
145 } pthread_mutexattr_t;
150 } pthread_condattr_t;
155 PTHREAD_PROCESS_PRIVATE,
156 PTHREAD_PROCESS_SHARED
161 PTHREAD_RWLOCK_PREFER_READER_NP,
162 PTHREAD_RWLOCK_PREFER_WRITER_NP,
163 PTHREAD_RWLOCK_DEFAULT_NP = PTHREAD_RWLOCK_PREFER_WRITER_NP
170 } pthread_rwlockattr_t;
173 /* Keys for thread-specific data */
175 typedef unsigned int pthread_key_t;
177 /* Once-only execution */
179 typedef int pthread_once_t;
181 #define PTHREAD_ONCE_INIT 0
183 /* Cleanup buffers */
185 struct _pthread_cleanup_buffer
187 void (*routine) __P ((void *)); /* Function to call. */
188 void *arg; /* Its argument. */
189 int canceltype; /* Saved cancellation type. */
190 struct _pthread_cleanup_buffer *prev; /* Chaining of cleanup functions. */
195 enum { PTHREAD_CANCEL_ENABLE, PTHREAD_CANCEL_DISABLE };
196 enum { PTHREAD_CANCEL_DEFERRED, PTHREAD_CANCEL_ASYNCHRONOUS };
197 #define PTHREAD_CANCELED ((void *) -1)
200 /* Function for handling threads. */
202 /* Create a thread with given attributes ATTR (or default attributes
203 if ATTR is NULL), and call function START_ROUTINE with given
205 extern int pthread_create __P ((pthread_t *__thread,
206 __const pthread_attr_t *__attr,
207 void *(*__start_routine) (void *),
210 /* Obtain the identifier of the current thread. */
211 extern pthread_t pthread_self __P ((void));
213 /* Compare two thread identifiers. */
214 extern int pthread_equal __P ((pthread_t __thread1, pthread_t __thread2));
216 /* Terminate calling thread. */
217 extern void pthread_exit __P ((void *__retval)) __attribute__ ((__noreturn__));
219 /* Make calling thread wait for termination of the thread TH. The
220 exit status of the thread is stored in *THREAD_RETURN, if THREAD_RETURN
222 extern int pthread_join __P ((pthread_t __th, void **__thread_return));
224 /* Indicate that the thread TH is never to be joined with PTHREAD_JOIN.
225 The resources of TH will therefore be freed immediately when it
226 terminates, instead of waiting for another thread to perform PTHREAD_JOIN
228 extern int pthread_detach __P ((pthread_t __th));
231 /* Functions for handling attributes. */
233 /* Initialize thread attribute *ATTR with default attributes
234 (detachstate is PTHREAD_JOINABLE, scheduling policy is SCHED_OTHER). */
235 extern int pthread_attr_init __P ((pthread_attr_t *__attr));
237 /* Destroy thread attribute *ATTR. */
238 extern int pthread_attr_destroy __P ((pthread_attr_t *__attr));
240 /* Set the `detachstate' attribute in *ATTR according to DETACHSTATE. */
241 extern int pthread_attr_setdetachstate __P ((pthread_attr_t *__attr,
244 /* Return in *DETACHSTATE the `detachstate' attribute in *ATTR. */
245 extern int pthread_attr_getdetachstate __P ((__const pthread_attr_t *__attr,
246 int *__detachstate));
248 /* Set scheduling parameters (priority, etc) in *ATTR according to PARAM. */
249 extern int pthread_attr_setschedparam __P ((pthread_attr_t *__attr,
250 __const struct sched_param *__param));
252 /* Return in *PARAM the scheduling parameters of *ATTR. */
253 extern int pthread_attr_getschedparam __P ((__const pthread_attr_t *__attr,
254 struct sched_param *__param));
256 /* Set scheduling policy in *ATTR according to POLICY. */
257 extern int pthread_attr_setschedpolicy __P ((pthread_attr_t *__attr,
260 /* Return in *POLICY the scheduling policy of *ATTR. */
261 extern int pthread_attr_getschedpolicy __P ((__const pthread_attr_t *__attr,
264 /* Set scheduling inheritance mode in *ATTR according to INHERIT. */
265 extern int pthread_attr_setinheritsched __P ((pthread_attr_t *__attr,
268 /* Return in *INHERIT the scheduling inheritance mode of *ATTR. */
269 extern int pthread_attr_getinheritsched __P ((__const pthread_attr_t *__attr,
272 /* Set scheduling contention scope in *ATTR according to SCOPE. */
273 extern int pthread_attr_setscope __P ((pthread_attr_t *__attr, int __scope));
275 /* Return in *SCOPE the scheduling contention scope of *ATTR. */
276 extern int pthread_attr_getscope __P ((__const pthread_attr_t *__attr,
280 /* Set the size of the guard area at the bottom of the thread. */
281 extern int __pthread_attr_setguardsize __P ((pthread_attr_t *__attr,
282 size_t __guardsize));
283 extern int pthread_attr_setguardsize __P ((pthread_attr_t *__attr,
284 size_t __guardsize));
286 /* Get the size of the guard area at the bottom of the thread. */
287 extern int __pthread_attr_getguardsize __P ((__const pthread_attr_t *__attr,
288 size_t *__guardsize));
289 extern int pthread_attr_getguardsize __P ((__const pthread_attr_t *__attr,
290 size_t *__guardsize));
292 /* Set the starting address of the stack of the thread to be created.
293 Depending on whether the stack grows up or doen the value must either
294 be higher or lower than all the address in the memory block. The
295 minimal size of the block must be PTHREAD_STACK_SIZE. */
296 extern int __pthread_attr_setstackaddr __P ((pthread_attr_t *__attr,
298 extern int pthread_attr_setstackaddr __P ((pthread_attr_t *__attr,
301 /* Return the previously set address for the stack. */
302 extern int __pthread_attr_getstackaddr __P ((__const pthread_attr_t *__attr,
303 void **__stackaddr));
304 extern int pthread_attr_getstackaddr __P ((__const pthread_attr_t *__attr,
305 void **__stackaddr));
307 /* Add information about the minimum stack size needed for the thread
308 to be started. This size must never be less than PTHREAD_STACK_SIZE
309 and must also not exceed the system limits. */
310 extern int __pthread_attr_setstacksize __P ((pthread_attr_t *__attr,
311 size_t __stacksize));
312 extern int pthread_attr_setstacksize __P ((pthread_attr_t *__attr,
313 size_t __stacksize));
315 /* Return the currently used minimal stack size. */
316 extern int __pthread_attr_getstacksize __P ((__const pthread_attr_t *__attr,
317 size_t *__stacksize));
318 extern int pthread_attr_getstacksize __P ((__const pthread_attr_t *__attr,
319 size_t *__stacksize));
322 /* Functions for scheduling control. */
324 /* Set the scheduling parameters for TARGET_THREAD according to POLICY
326 extern int pthread_setschedparam __P ((pthread_t __target_thread, int __policy,
327 __const struct sched_param *__param));
329 /* Return in *POLICY and *PARAM the scheduling parameters for TARGET_THREAD. */
330 extern int pthread_getschedparam __P ((pthread_t __target_thread,
332 struct sched_param *__param));
335 /* Determine level of concurrency. */
336 extern int __pthread_getconcurrency __P ((void));
337 extern int pthread_getconcurrency __P ((void));
339 /* Set new concurrency level to LEVEL. */
340 extern int __pthread_setconcurrency __P ((int __level));
341 extern int pthread_setconcurrency __P ((int __level));
344 /* Functions for mutex handling. */
346 /* Initialize MUTEX using attributes in *MUTEX_ATTR, or use the
347 default values if later is NULL. */
348 extern int __pthread_mutex_init __P ((pthread_mutex_t *__mutex,
349 __const pthread_mutexattr_t *__mutex_attr));
350 extern int pthread_mutex_init __P ((pthread_mutex_t *__mutex,
351 __const pthread_mutexattr_t *__mutex_attr));
354 extern int __pthread_mutex_destroy __P ((pthread_mutex_t *__mutex));
355 extern int pthread_mutex_destroy __P ((pthread_mutex_t *__mutex));
357 /* Try to lock MUTEX. */
358 extern int __pthread_mutex_trylock __P ((pthread_mutex_t *__mutex));
359 extern int pthread_mutex_trylock __P ((pthread_mutex_t *__mutex));
361 /* Wait until lock for MUTEX becomes available and lock it. */
362 extern int __pthread_mutex_lock __P ((pthread_mutex_t *__mutex));
363 extern int pthread_mutex_lock __P ((pthread_mutex_t *__mutex));
366 extern int __pthread_mutex_unlock __P ((pthread_mutex_t *__mutex));
367 extern int pthread_mutex_unlock __P ((pthread_mutex_t *__mutex));
370 /* Functions for handling mutex attributes. */
372 /* Initialize mutex attribute object ATTR with default attributes
373 (kind is PTHREAD_MUTEX_FAST_NP). */
374 extern int __pthread_mutexattr_init __P ((pthread_mutexattr_t *__attr));
375 extern int pthread_mutexattr_init __P ((pthread_mutexattr_t *__attr));
377 /* Destroy mutex attribute object ATTR. */
378 extern int __pthread_mutexattr_destroy __P ((pthread_mutexattr_t *__attr));
379 extern int pthread_mutexattr_destroy __P ((pthread_mutexattr_t *__attr));
381 /* Set the mutex kind attribute in *ATTR to KIND (either PTHREAD_MUTEX_FAST_NP
382 or PTHREAD_MUTEX_RECURSIVE_NP). */
383 extern int __pthread_mutexattr_setkind_np __P ((pthread_mutexattr_t *__attr,
385 extern int pthread_mutexattr_setkind_np __P ((pthread_mutexattr_t *__attr,
387 /* Return in *KIND the mutex kind attribute in *ATTR. */
388 extern int pthread_mutexattr_getkind_np __P ((__const pthread_mutexattr_t *__attr,
392 /* Functions for handling conditional variables. */
394 /* Initialize condition variable COND using attributes ATTR, or use
395 the default values if later is NULL. */
396 extern int pthread_cond_init __P ((pthread_cond_t *__cond,
397 __const pthread_condattr_t *__cond_attr));
399 /* Destroy condition variable COND. */
400 extern int pthread_cond_destroy __P ((pthread_cond_t *__cond));
402 /* Wake up one thread waiting for condition variable COND. */
403 extern int pthread_cond_signal __P ((pthread_cond_t *__cond));
405 /* Wake up all threads waiting for condition variables COND. */
406 extern int pthread_cond_broadcast __P ((pthread_cond_t *__cond));
408 /* Wait for condition variable COND to be signaled or broadcast.
409 MUTEX is assumed to be locked before. */
410 extern int pthread_cond_wait __P ((pthread_cond_t *__cond,
411 pthread_mutex_t *__mutex));
413 /* Wait for condition variable COND to be signaled or broadcast until
414 ABSTIME. MUTEX is assumed to be locked before. ABSTIME is an
415 absolute time specification; zero is the beginning of the epoch
416 (00:00:00 GMT, January 1, 1970). */
417 extern int pthread_cond_timedwait __P ((pthread_cond_t *__cond,
418 pthread_mutex_t *__mutex,
419 __const struct timespec *__abstime));
421 /* Functions for handling condition variable attributes. */
423 /* Initialize condition variable attribute ATTR. */
424 extern int pthread_condattr_init __P ((pthread_condattr_t *__attr));
426 /* Destroy condition variable attribute ATTR. */
427 extern int pthread_condattr_destroy __P ((pthread_condattr_t *__attr));
431 /* Functions for handling read-write locks. */
433 /* Initialize read-write lock RWLOCK using attributes ATTR, or use
434 the default values if later is NULL. */
435 extern int pthread_rwlock_init __P ((pthread_rwlock_t *__rwlock,
436 __const pthread_rwlockattr_t *__attr));
438 /* Destroy read-write lock RWLOCK. */
439 extern int pthread_rwlock_destroy __P ((pthread_rwlock_t *__rwlock));
441 /* Acquire read lock for RWLOCK. */
442 extern int pthread_rwlock_rdlock __P ((pthread_rwlock_t *__rwlock));
444 /* Try to acquire read lock for RWLOCK. */
445 extern int pthread_rwlock_tryrdlock __P ((pthread_rwlock_t *__rwlock));
447 /* Acquire write lock for RWLOCK. */
448 extern int pthread_rwlock_wrlock __P ((pthread_rwlock_t *__rwlock));
450 /* Try to acquire writelock for RWLOCK. */
451 extern int pthread_rwlock_trywrlock __P ((pthread_rwlock_t *__rwlock));
454 extern int pthread_rwlock_unlock __P ((pthread_rwlock_t *__rwlock));
457 /* Functions for handling read-write lock attributes. */
459 /* Initialize attribute object ATTR with default values. */
460 extern int pthread_rwlockattr_init __P ((pthread_rwlockattr_t *__attr));
462 /* Destroy attribute object ATTR. */
463 extern int pthread_rwlockattr_destroy __P ((pthread_rwlockattr_t *__attr));
465 /* Return current setting of process-shared attribute of ATTR in PSHARED. */
466 extern int pthread_rwlockattr_getpshared __P ((__const
467 pthread_rwlockattr_t *__attr,
470 /* Set process-shared attribute of ATTR to PSHARED. */
471 extern int pthread_rwlockattr_setpshared __P ((pthread_rwlockattr_t *__attr,
474 /* Return current setting of reader/writer preference. */
475 extern int pthread_rwlockattr_getkind_np __P ((__const
476 pthread_rwlockattr_t *__attr,
479 /* Set reader/write preference. */
480 extern int pthread_rwlockattr_setkind_np __P ((pthread_rwlockattr_t *__attr,
485 /* Functions for handling thread-specific data */
487 /* Create a key value identifying a location in the thread-specific data
488 area. Each thread maintains a distinct thread-specific data area.
489 DESTR_FUNCTION, if non-NULL, is called with
490 the value associated to that key when the key is destroyed.
491 DESTR_FUNCTION is not called if the value associated is NULL
492 when the key is destroyed. */
493 extern int __pthread_key_create __P ((pthread_key_t *__key,
494 void (*__destr_function) (void *)));
495 extern int pthread_key_create __P ((pthread_key_t *__key,
496 void (*__destr_function) (void *)));
499 extern int pthread_key_delete __P ((pthread_key_t __key));
501 /* Store POINTER in the thread-specific data slot identified by KEY. */
502 extern int __pthread_setspecific __P ((pthread_key_t __key,
503 __const void *__pointer));
504 extern int pthread_setspecific __P ((pthread_key_t __key,
505 __const void *__pointer));
507 /* Return current value of the thread-specific data slot identified by KEY. */
508 extern void *__pthread_getspecific __P ((pthread_key_t __key));
509 extern void *pthread_getspecific __P ((pthread_key_t __key));
512 /* Functions for handling initialization */
514 /* Guarantee that the initialization function INIT_ROUTINE will be called
515 only once, even if pthread_once is executed several times with the
516 same ONCE_CONTROL argument. ONCE_CONTROL must point to a static or
517 extern variable initialized to PTHREAD_ONCE_INIT. */
518 extern int __pthread_once __P ((pthread_once_t *__once_control,
519 void (*__init_routine) (void)));
520 extern int pthread_once __P ((pthread_once_t *__once_control,
521 void (*__init_routine) (void)));
524 /* Functions for handling cancellation. */
526 /* Set cancelability state of current thread to STATE, returning old
527 state in *OLDSTATE if OLDSTATE is not NULL. */
528 extern int pthread_setcancelstate __P ((int __state, int *__oldstate));
530 /* Set cancellation state of current thread to TYPE, returning the old
531 type in *OLDTYPE if OLDTYPE is not NULL. */
532 extern int __pthread_setcanceltype __P ((int __type, int *__oldtype));
533 extern int pthread_setcanceltype __P ((int __type, int *__oldtype));
535 /* Cancel THREAD immediately or at the next possibility. */
536 extern int pthread_cancel __P ((pthread_t __thread));
538 /* Test for pending cancellation for the current thread and terminate
539 the thread as per pthread_exit(PTHREAD_CANCELED) if it has been
541 extern void pthread_testcancel __P ((void));
544 /* Install a cleanup handler: ROUTINE will be called with arguments ARG
545 when the thread is cancelled or calls pthread_exit. ROUTINE will also
546 be called with arguments ARG when the matching pthread_cleanup_pop
547 is executed with non-zero EXECUTE argument.
548 pthread_cleanup_push and pthread_cleanup_pop are macros and must always
549 be used in matching pairs at the same nesting level of braces. */
551 #define pthread_cleanup_push(routine,arg) \
552 { struct _pthread_cleanup_buffer _buffer; \
553 _pthread_cleanup_push (&_buffer, (routine), (arg));
555 extern void _pthread_cleanup_push __P ((struct _pthread_cleanup_buffer *__buffer,
556 void (*__routine) (void *),
559 /* Remove a cleanup handler installed by the matching pthread_cleanup_push.
560 If EXECUTE is non-zero, the handler function is called. */
562 #define pthread_cleanup_pop(execute) \
563 _pthread_cleanup_pop (&_buffer, (execute)); }
565 extern void _pthread_cleanup_pop __P ((struct _pthread_cleanup_buffer *__buffer,
568 /* Install a cleanup handler as pthread_cleanup_push does, but also
569 saves the current cancellation type and set it to deferred cancellation. */
571 #define pthread_cleanup_push_defer_np(routine,arg) \
572 { struct _pthread_cleanup_buffer _buffer; \
573 _pthread_cleanup_push_defer (&_buffer, (routine), (arg));
575 extern void _pthread_cleanup_push_defer __P ((struct _pthread_cleanup_buffer *__buffer,
576 void (*__routine) (void *),
579 /* Remove a cleanup handler as pthread_cleanup_pop does, but also
580 restores the cancellation type that was in effect when the matching
581 pthread_cleanup_push_defer was called. */
583 #define pthread_cleanup_pop_restore_np(execute) \
584 _pthread_cleanup_pop_restore (&_buffer, (execute)); }
586 extern void _pthread_cleanup_pop_restore __P ((struct _pthread_cleanup_buffer *__buffer,
589 /* Functions for handling signals. */
591 /* Modify the signal mask for the calling thread. The arguments have
592 the same meaning as for sigprocmask(2). */
594 extern int pthread_sigmask __P ((int __how, __const sigset_t *__newmask,
595 sigset_t *__oldmask));
597 /* Send signal SIGNO to the given thread. */
599 extern int pthread_kill __P ((pthread_t __thread, int __signo));
602 /* Functions for handling process creation and process execution. */
604 /* Install handlers to be called when a new process is created with FORK.
605 The PREPARE handler is called in the parent process just before performing
606 FORK. The PARENT handler is called in the parent process just after FORK.
607 The CHILD handler is called in the child process. Each of the three
608 handlers can be NULL, meaning that no handler needs to be called at that
610 PTHREAD_ATFORK can be called several times, in which case the PREPARE
611 handlers are called in LIFO order (last added with PTHREAD_ATFORK,
612 first called before FORK), and the PARENT and CHILD handlers are called
613 in FIFO (first added, first called). */
615 extern int __pthread_atfork __P ((void (*__prepare) (void),
616 void (*__parent) (void),
617 void (*__child) (void)));
618 extern int pthread_atfork __P ((void (*__prepare) (void),
619 void (*__parent) (void),
620 void (*__child) (void)));
622 /* Terminate all threads in the program except the calling process.
623 Should be called just before invoking one of the exec*() functions. */
625 extern void __pthread_kill_other_threads_np __P ((void));
626 extern void pthread_kill_other_threads_np __P ((void));
629 /* This function is called to initialize the pthread library. */
630 extern void __pthread_initialize __P ((void));
634 #endif /* pthread.h */