Blame manual/threads.texi

Packit 6c4009
@node Threads
Packit 6c4009
@c @node Threads, Internal Probes, Debugging Support, Top
Packit 6c4009
@c %MENU% Functions, constants, and data types for working with threads
Packit 6c4009
@chapter Threads
Packit 6c4009
@cindex threads
Packit 6c4009
Packit 6c4009
This chapter describes functions used for managing threads.
Packit 6c4009
@Theglibc{} provides two threading implementations: ISO C threads and
Packit 6c4009
POSIX threads.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* ISO C Threads::	Threads based on the ISO C specification.
Packit 6c4009
* POSIX Threads::	Threads based on the POSIX specification.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node ISO C Threads
Packit 6c4009
@section ISO C Threads
Packit 6c4009
@cindex ISO C threads
Packit 6c4009
@cindex C threads
Packit 6c4009
@pindex threads.h
Packit 6c4009
Packit 6c4009
This section describes the @glibcadj{} ISO C threads implementation.
Packit 6c4009
To have a deeper understanding of this API, it is strongly recommended
Packit 6c4009
to read ISO/IEC 9899:2011, section 7.26, in which ISO C threads were
Packit 6c4009
originally specified.  All types and function prototypes are declared
Packit 6c4009
in the header file @file{threads.h}.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* ISO C Threads Return Values:: Symbolic constants that represent a
Packit 6c4009
				function's return value.
Packit 6c4009
* ISO C Thread Management::	Support for basic threading.
Packit 6c4009
* Call Once::			Single-call functions and macros.
Packit 6c4009
* ISO C Mutexes::		A low-level mechanism for mutual exclusion.
Packit 6c4009
* ISO C Condition Variables::	High-level objects for thread synchronization.
Packit 6c4009
* ISO C Thread-local Storage::	Functions to support thread-local storage.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node ISO C Threads Return Values
Packit 6c4009
@subsection Return Values
Packit 6c4009
Packit 6c4009
The ISO C thread specification provides the following enumeration
Packit 6c4009
constants for return values from functions in the API:
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item thrd_timedout
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
A specified time was reached without acquiring the requested resource,
Packit 6c4009
usually a mutex or condition variable.
Packit 6c4009
Packit 6c4009
@item thrd_success
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
The requested operation succeeded.
Packit 6c4009
Packit 6c4009
@item thrd_busy
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
The requested operation failed because a requested resource is already
Packit 6c4009
in use.
Packit 6c4009
Packit 6c4009
@item thrd_error
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
The requested operation failed.
Packit 6c4009
Packit 6c4009
@item thrd_nomem
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
The requested operation failed because it was unable to allocate
Packit 6c4009
enough memory.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node ISO C Thread Management
Packit 6c4009
@subsection Creation and Control
Packit 6c4009
@cindex thread creation
Packit 6c4009
@cindex thread control
Packit 6c4009
@cindex thread management
Packit 6c4009
Packit 6c4009
@Theglibc{} implements a set of functions that allow the user to easily
Packit 6c4009
create and use threads.  Additional functionality is provided to control
Packit 6c4009
the behavior of threads.
Packit 6c4009
Packit 6c4009
The following data types are defined for managing threads:
Packit 6c4009
Packit 6c4009
@deftp {Data Type} thrd_t
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
A unique object that identifies a thread.
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
@deftp {Data Type} thrd_start_t
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
This data type is an @code{int (*) (void *)} typedef that is passed to
Packit 6c4009
@code{thrd_create} when creating a new thread.  It should point to the
Packit 6c4009
first function that thread will run.
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
The following functions are used for working with threads:
Packit 6c4009
Packit 6c4009
@deftypefun int thrd_create (thrd_t *@var{thr}, thrd_start_t @var{func}, void *@var{arg})
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{thrd_create} creates a new thread that will execute the function
Packit 6c4009
@var{func}.  The object pointed to by @var{arg} will be used as the
Packit 6c4009
argument to @var{func}.  If successful, @var{thr} is set to the new
Packit 6c4009
thread identifier.
Packit 6c4009
Packit 6c4009
This function may return @code{thrd_success}, @code{thrd_nomem}, or
Packit 6c4009
@code{thrd_error}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun thrd_t thrd_current (void)
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This function returns the identifier of the calling thread.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int thrd_equal (thrd_t @var{lhs}, thrd_t @var{rhs})
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{thrd_equal} checks whether @var{lhs} and @var{rhs} refer to the
Packit 6c4009
same thread.  If @var{lhs} and @var{rhs} are different threads, this
Packit 6c4009
function returns @math{0}; otherwise, the return value is non-zero.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int thrd_sleep (const struct timespec *@var{time_point}, struct timespec *@var{remaining})
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{thrd_sleep} blocks the execution of the current thread for at
Packit 6c4009
least until the elapsed time pointed to by @var{time_point} has been
Packit 6c4009
reached.  This function does not take an absolute time, but a duration
Packit 6c4009
that the thread is required to be blocked.  @xref{Time Basics}, and
Packit 6c4009
@ref{Elapsed Time}.
Packit 6c4009
Packit 6c4009
The thread may wake early if a signal that is not ignored is received.
Packit 6c4009
In such a case, if @code{remaining} is not NULL, the remaining time
Packit 6c4009
duration is stored in the object pointed to by
Packit 6c4009
@var{remaining}.
Packit 6c4009
Packit 6c4009
@code{thrd_sleep} returns @math{0} if it blocked for at least the
Packit 6c4009
amount of time in @code{time_point}, @math{-1} if it was interrupted
Packit 6c4009
by a signal, or a negative number on failure.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun void thrd_yield (void)
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{thrd_yield} provides a hint to the implementation to reschedule
Packit 6c4009
the execution of the current thread, allowing other threads to run.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {_Noreturn void} thrd_exit (int @var{res})
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{thrd_exit} terminates execution of the calling thread and sets
Packit 6c4009
its result code to @var{res}.
Packit 6c4009
Packit 6c4009
If this function is called from a single-threaded process, the call is
Packit 6c4009
equivalent to calling @code{exit} with @code{EXIT_SUCCESS}
Packit 6c4009
(@pxref{Normal Termination}).  Also note that returning from a
Packit 6c4009
function that started a thread is equivalent to calling
Packit 6c4009
@code{thrd_exit}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int thrd_detach (thrd_t @var{thr})
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{thrd_detach} detaches the thread identified by @code{thr} from
Packit 6c4009
the current control thread.  The resources held by the detached thread
Packit 6c4009
will be freed automatically once the thread exits.  The parent thread
Packit 6c4009
will never be notified by any @var{thr} signal.
Packit 6c4009
Packit 6c4009
Calling @code{thrd_detach} on a thread that was previously detached or
Packit 6c4009
joined by another thread results in undefined behavior.
Packit 6c4009
Packit 6c4009
This function returns either @code{thrd_success} or @code{thrd_error}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int thrd_join (thrd_t @var{thr}, int *@var{res})
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{thrd_join} blocks the current thread until the thread identified
Packit 6c4009
by @code{thr} finishes execution.  If @code{res} is not NULL, the
Packit 6c4009
result code of the thread is put into the location pointed to by
Packit 6c4009
@var{res}.  The termination of the thread @dfn{synchronizes-with} the
Packit 6c4009
completion of this function, meaning both threads have arrived at a
Packit 6c4009
common point in their execution.
Packit 6c4009
Packit 6c4009
Calling @code{thrd_join} on a thread that was previously detached or
Packit 6c4009
joined by another thread results in undefined behavior.
Packit 6c4009
Packit 6c4009
This function returns either @code{thrd_success} or @code{thrd_error}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node Call Once
Packit 6c4009
@subsection Call Once
Packit 6c4009
@cindex call once
Packit 6c4009
@cindex single-call functions
Packit 6c4009
Packit 6c4009
In order to guarantee single access to a function, @theglibc{}
Packit 6c4009
implements a @dfn{call once function} to ensure a function is only
Packit 6c4009
called once in the presence of multiple, potentially calling threads.
Packit 6c4009
Packit 6c4009
@deftp {Data Type} once_flag
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
A complete object type capable of holding a flag used by @code{call_once}.
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
@defvr Macro ONCE_FLAG_INIT
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
This value is used to initialize an object of type @code{once_flag}.
Packit 6c4009
@end defvr
Packit 6c4009
Packit 6c4009
@deftypefun void call_once (once_flag *@var{flag}, void (*@var{func}) (void))
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{call_once} calls function @var{func} exactly once, even if
Packit 6c4009
invoked from several threads.  The completion of the function
Packit 6c4009
@var{func} synchronizes-with all previous or subsequent calls to
Packit 6c4009
@code{call_once} with the same @code{flag} variable.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node ISO C Mutexes
Packit 6c4009
@subsection Mutexes
Packit 6c4009
@cindex mutex
Packit 6c4009
@cindex mutual exclusion
Packit 6c4009
Packit 6c4009
To have better control of resources and how threads access them,
Packit 6c4009
@theglibc{} implements a @dfn{mutex} object, which can help avoid race
Packit 6c4009
conditions and other concurrency issues.  The term ``mutex'' refers to
Packit 6c4009
mutual exclusion.
Packit 6c4009
Packit 6c4009
The fundamental data type for a mutex is the @code{mtx_t}:
Packit 6c4009
Packit 6c4009
@deftp {Data Type} mtx_t
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
The @code{mtx_t} data type uniquely identifies a mutex object.
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
The ISO C standard defines several types of mutexes.  They are
Packit 6c4009
represented by the following symbolic constants:
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item mtx_plain
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
A mutex that does not support timeout, or test and return.
Packit 6c4009
Packit 6c4009
@item mtx_recursive
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
A mutex that supports recursive locking, which means that the owning
Packit 6c4009
thread can lock it more than once without causing deadlock.
Packit 6c4009
Packit 6c4009
@item mtx_timed
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
A mutex that supports timeout.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
The following functions are used for working with mutexes:
Packit 6c4009
Packit 6c4009
@deftypefun int mtx_init (mtx_t *@var{mutex}, int @var{type})
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{mtx_init} creates a new mutex object with type @var{type}.  The
Packit 6c4009
object pointed to by @var{mutex} is set to the identifier of the newly
Packit 6c4009
created mutex.
Packit 6c4009
Packit 6c4009
Not all combinations of mutex types are valid for the @code{type}
Packit 6c4009
argument.  Valid uses of mutex types for the @code{type} argument are:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item mtx_plain
Packit 6c4009
A non-recursive mutex that does not support timeout.
Packit 6c4009
Packit 6c4009
@item mtx_timed
Packit 6c4009
A non-recursive mutex that does support timeout.
Packit 6c4009
Packit 6c4009
@item mtx_plain | mtx_recursive
Packit 6c4009
A recursive mutex that does not support timeout.
Packit 6c4009
Packit 6c4009
@item mtx_timed | mtx_recursive
Packit 6c4009
A recursive mutex that does support timeout.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
This function returns either @code{thrd_success} or @code{thrd_error}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int mtx_lock (mtx_t *@var{mutex})
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
Packit 6c4009
@code{mtx_lock} blocks the current thread until the mutex pointed to
Packit 6c4009
by @var{mutex} is locked.  The behavior is undefined if the current
Packit 6c4009
thread has already locked the mutex and the mutex is not recursive.
Packit 6c4009
Packit 6c4009
Prior calls to @code{mtx_unlock} on the same mutex synchronize-with
Packit 6c4009
this operation (if this operation succeeds), and all lock/unlock
Packit 6c4009
operations on any given mutex form a single total order (similar to
Packit 6c4009
the modification order of an atomic).
Packit 6c4009
Packit 6c4009
This function returns either @code{thrd_success} or @code{thrd_error}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int mtx_timedlock (mtx_t *restrict @var{mutex}, const struct timespec *restrict @var{time_point})
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
Packit 6c4009
@code{mtx_timedlock} blocks the current thread until the mutex pointed
Packit 6c4009
to by @var{mutex} is locked or until the calendar time pointed to by
Packit 6c4009
@var{time_point} has been reached.  Since this function takes an
Packit 6c4009
absolute time, if a duration is required, the calendar time must be
Packit 6c4009
calculated manually.  @xref{Time Basics}, and @ref{Calendar Time}.
Packit 6c4009
Packit 6c4009
If the current thread has already locked the mutex and the mutex is
Packit 6c4009
not recursive, or if the mutex does not support timeout, the behavior
Packit 6c4009
of this function is undefined.
Packit 6c4009
Packit 6c4009
Prior calls to @code{mtx_unlock} on the same mutex synchronize-with
Packit 6c4009
this operation (if this operation succeeds), and all lock/unlock
Packit 6c4009
operations on any given mutex form a single total order (similar to
Packit 6c4009
the modification order of an atomic).
Packit 6c4009
Packit 6c4009
This function returns either @code{thrd_success} or @code{thrd_error}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int mtx_trylock (mtx_t *@var{mutex})
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
Packit 6c4009
@code{mtx_trylock} tries to lock the mutex pointed to by @var{mutex}
Packit 6c4009
without blocking.  It returns immediately if the mutex is already
Packit 6c4009
locked.
Packit 6c4009
Packit 6c4009
Prior calls to @code{mtx_unlock} on the same mutex synchronize-with
Packit 6c4009
this operation (if this operation succeeds), and all lock/unlock
Packit 6c4009
operations on any given mutex form a single total order (similar to
Packit 6c4009
the modification order of an atomic).
Packit 6c4009
Packit 6c4009
This function returns @code{thrd_success} if the lock was obtained,
Packit 6c4009
@code{thrd_busy} if the mutex is already locked, and @code{thrd_error}
Packit 6c4009
on failure.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int mtx_unlock (mtx_t *@var{mutex})
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{mtx_unlock} unlocks the mutex pointed to by @var{mutex}.  The
Packit 6c4009
behavior is undefined if the mutex is not locked by the calling
Packit 6c4009
thread.
Packit 6c4009
Packit 6c4009
This function synchronizes-with subsequent @code{mtx_lock},
Packit 6c4009
@code{mtx_trylock}, and @code{mtx_timedlock} calls on the same mutex.
Packit 6c4009
All lock/unlock operations on any given mutex form a single total
Packit 6c4009
order (similar to the modification order of an atomic).
Packit 6c4009
Packit 6c4009
This function returns either @code{thrd_success} or @code{thrd_error}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun void mtx_destroy (mtx_t *@var{mutex})
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{mtx_destroy} destroys the mutex pointed to by @var{mutex}.  If
Packit 6c4009
there are any threads waiting on the mutex, the behavior is
Packit 6c4009
undefined.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node ISO C Condition Variables
Packit 6c4009
@subsection Condition Variables
Packit 6c4009
@cindex condvar
Packit 6c4009
@cindex condition variables
Packit 6c4009
Packit 6c4009
Mutexes are not the only synchronization mechanisms available.  For
Packit 6c4009
some more complex tasks, @theglibc{} also implements @dfn{condition
Packit 6c4009
variables}, which allow the programmer to think at a higher level when
Packit 6c4009
solving complex synchronization problems.  They are used to
Packit 6c4009
synchronize threads waiting on a certain condition to happen.
Packit 6c4009
Packit 6c4009
The fundamental data type for condition variables is the @code{cnd_t}:
Packit 6c4009
Packit 6c4009
@deftp {Data Type} cnd_t
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
The @code{cnd_t} uniquely identifies a condition variable object.
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
The following functions are used for working with condition variables:
Packit 6c4009
Packit 6c4009
@deftypefun int cnd_init (cnd_t *@var{cond})
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{cnd_init} initializes a new condition variable, identified by
Packit 6c4009
@var{cond}.
Packit 6c4009
Packit 6c4009
This function may return @code{thrd_success}, @code{thrd_nomem}, or
Packit 6c4009
@code{thrd_error}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int cnd_signal (cnd_t *@var{cond})
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{cnd_signal} unblocks one thread that is currently waiting on the
Packit 6c4009
condition variable pointed to by @var{cond}.  If a thread is
Packit 6c4009
successfully unblocked, this function returns @code{thrd_success}.  If
Packit 6c4009
no threads are blocked, this function does nothing and returns
Packit 6c4009
@code{thrd_success}.  Otherwise, this function returns
Packit 6c4009
@code{thrd_error}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int cnd_broadcast (cnd_t *@var{cond})
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{cnd_broadcast} unblocks all the threads that are currently
Packit 6c4009
waiting on the condition variable pointed to by @var{cond}.  This
Packit 6c4009
function returns @code{thrd_success} on success.  If no threads are
Packit 6c4009
blocked, this function does nothing and returns
Packit 6c4009
@code{thrd_success}. Otherwise, this function returns
Packit 6c4009
@code{thrd_error}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int cnd_wait (cnd_t *@var{cond}, mtx_t *@var{mutex})
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
Packit 6c4009
@code{cnd_wait} atomically unlocks the mutex pointed to by @var{mutex}
Packit 6c4009
and blocks on the condition variable pointed to by @var{cond} until
Packit 6c4009
the thread is signaled by @code{cnd_signal} or @code{cnd_broadcast}.
Packit 6c4009
The mutex is locked again before the function returns.
Packit 6c4009
Packit 6c4009
This function returns either @code{thrd_success} or @code{thrd_error}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int cnd_timedwait (cnd_t *restrict @var{cond}, mtx_t *restrict @var{mutex}, const struct timespec *restrict @var{time_point})
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
Packit 6c4009
@code{cnd_timedwait} atomically unlocks the mutex pointed to by
Packit 6c4009
@var{mutex} and blocks on the condition variable pointed to by
Packit 6c4009
@var{cond} until the thread is signaled by @code{cnd_signal} or
Packit 6c4009
@code{cnd_broadcast}, or until the calendar time pointed to by
Packit 6c4009
@var{time_point} has been reached.  The mutex is locked again before
Packit 6c4009
the function returns.
Packit 6c4009
Packit 6c4009
As for @code{mtx_timedlock}, since this function takes an absolute
Packit 6c4009
time, if a duration is required, the calendar time must be calculated
Packit 6c4009
manually.  @xref{Time Basics}, and @ref{Calendar Time}.
Packit 6c4009
Packit 6c4009
This function may return @code{thrd_success}, @code{thrd_nomem}, or
Packit 6c4009
@code{thrd_error}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun void cnd_destroy (cnd_t *@var{cond})
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{cnd_destroy} destroys the condition variable pointed to by
Packit 6c4009
@var{cond}.  If there are threads waiting on @var{cond}, the behavior
Packit 6c4009
is undefined.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node ISO C Thread-local Storage
Packit 6c4009
@subsection Thread-local Storage
Packit 6c4009
@cindex thread-local storage
Packit 6c4009
Packit 6c4009
@Theglibc{} implements functions to provide @dfn{thread-local
Packit 6c4009
storage}, a mechanism by which variables can be defined to have unique
Packit 6c4009
per-thread storage, lifetimes that match the thread lifetime, and
Packit 6c4009
destructors that cleanup the unique per-thread storage.
Packit 6c4009
Packit 6c4009
Several data types and macros exist for working with thread-local
Packit 6c4009
storage:
Packit 6c4009
Packit 6c4009
@deftp {Data Type} tss_t
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
The @code{tss_t} data type identifies a thread-specific storage
Packit 6c4009
object.  Even if shared, every thread will have its own instance of
Packit 6c4009
the variable, with different values.
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
@deftp {Data Type} tss_dtor_t
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
The @code{tss_dtor_t} is a function pointer of type @code{void (*)
Packit 6c4009
(void *)}, to be used as a thread-specific storage destructor.  The
Packit 6c4009
function will be called when the current thread calls @code{thrd_exit}
Packit 6c4009
(but never when calling @code{tss_delete} or @code{exit}).
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
@defvr Macro thread_local
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@code{thread_local} is used to mark a variable with thread storage
Packit 6c4009
duration, which means it is created when the thread starts and cleaned
Packit 6c4009
up when the thread ends.
Packit 6c4009
Packit 6c4009
@emph{Note:} For C++, C++11 or later is required to use the
Packit 6c4009
@code{thread_local} keyword.
Packit 6c4009
@end defvr
Packit 6c4009
Packit 6c4009
@defvr Macro TSS_DTOR_ITERATIONS
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@code{TSS_DTOR_ITERATIONS} is an integer constant expression
Packit 6c4009
representing the maximum number of iterations over all thread-local
Packit 6c4009
destructors at the time of thread termination.  This value provides a
Packit 6c4009
bounded limit to the destruction of thread-local storage; e.g.,
Packit 6c4009
consider a destructor that creates more thread-local storage.
Packit 6c4009
@end defvr
Packit 6c4009
Packit 6c4009
The following functions are used to manage thread-local storage:
Packit 6c4009
Packit 6c4009
@deftypefun int tss_create (tss_t *@var{tss_key}, tss_dtor_t @var{destructor})
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{tss_create} creates a new thread-specific storage key and stores
Packit 6c4009
it in the object pointed to by @var{tss_key}.  Although the same key
Packit 6c4009
value may be used by different threads, the values bound to the key by
Packit 6c4009
@code{tss_set} are maintained on a per-thread basis and persist for
Packit 6c4009
the life of the calling thread.
Packit 6c4009
Packit 6c4009
If @code{destructor} is not NULL, a destructor function will be set,
Packit 6c4009
and called when the thread finishes its execution by calling
Packit 6c4009
@code{thrd_exit}.
Packit 6c4009
Packit 6c4009
This function returns @code{thrd_success} if @code{tss_key} is
Packit 6c4009
successfully set to a unique value for the thread; otherwise,
Packit 6c4009
@code{thrd_error} is returned and the value of @code{tss_key} is
Packit 6c4009
undefined.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int tss_set (tss_t @var{tss_key}, void *@var{val})
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{tss_set} sets the value of the thread-specific storage
Packit 6c4009
identified by @var{tss_key} for the current thread to @var{val}.
Packit 6c4009
Different threads may set different values to the same key.
Packit 6c4009
Packit 6c4009
This function returns either @code{thrd_success} or @code{thrd_error}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {void *} tss_get (tss_t @var{tss_key})
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{tss_get} returns the value identified by @var{tss_key} held in
Packit 6c4009
thread-specific storage for the current thread.  Different threads may
Packit 6c4009
get different values identified by the same key.  On failure,
Packit 6c4009
@code{tss_get} returns zero.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun void tss_delete (tss_t @var{tss_key})
Packit 6c4009
@standards{C11, threads.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{tss_delete} destroys the thread-specific storage identified by
Packit 6c4009
@var{tss_key}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node POSIX Threads
Packit 6c4009
@section POSIX Threads
Packit 6c4009
@cindex pthreads
Packit 6c4009
Packit 6c4009
This section describes the @glibcadj{} POSIX Threads implementation.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Thread-specific Data::          Support for creating and
Packit 6c4009
				  managing thread-specific data
Packit 6c4009
* Non-POSIX Extensions::          Additional functions to extend
Packit 6c4009
				  POSIX Thread functionality
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Thread-specific Data
Packit 6c4009
@subsection Thread-specific Data
Packit 6c4009
Packit 6c4009
The @glibcadj{} implements functions to allow users to create and manage
Packit 6c4009
data specific to a thread.  Such data may be destroyed at thread exit,
Packit 6c4009
if a destructor is provided.  The following functions are defined:
Packit 6c4009
Packit 6c4009
@deftypefun int pthread_key_create (pthread_key_t *@var{key}, void (*@var{destructor})(void*))
Packit 6c4009
@standards{POSIX, pthread.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c pthread_key_create ok
Packit 6c4009
@c  KEY_UNUSED ok
Packit 6c4009
@c  KEY_USABLE ok
Packit 6c4009
Create a thread-specific data key for the calling thread, referenced by
Packit 6c4009
@var{key}.
Packit 6c4009
Packit 6c4009
Objects declared with the C++11 @code{thread_local} keyword are destroyed
Packit 6c4009
before thread-specific data, so they should not be used in thread-specific
Packit 6c4009
data destructors or even as members of the thread-specific data, since the
Packit 6c4009
latter is passed as an argument to the destructor function.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int pthread_key_delete (pthread_key_t @var{key})
Packit 6c4009
@standards{POSIX, pthread.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c pthread_key_delete ok
Packit 6c4009
@c   This uses atomic compare and exchange to increment the seq number
Packit 6c4009
@c   after testing it's not a KEY_UNUSED seq number.
Packit 6c4009
@c  KEY_UNUSED dup ok
Packit 6c4009
Destroy the thread-specific data @var{key} in the calling thread.  The
Packit 6c4009
destructor for the thread-specific data is not called during destruction, nor
Packit 6c4009
is it called during thread exit.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun void *pthread_getspecific (pthread_key_t @var{key})
Packit 6c4009
@standards{POSIX, pthread.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c pthread_getspecific ok
Packit 6c4009
Return the thread-specific data associated with @var{key} in the calling
Packit 6c4009
thread.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int pthread_setspecific (pthread_key_t @var{key}, const void *@var{value})
Packit 6c4009
@standards{POSIX, pthread.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
Packit 6c4009
@c pthread_setspecific @asucorrupt @ascuheap @acucorrupt @acsmem
Packit 6c4009
@c   a level2 block may be allocated by a signal handler after
Packit 6c4009
@c   another call already made a decision to allocate it, thus losing
Packit 6c4009
@c   the allocated value.  the seq number is updated before the
Packit 6c4009
@c   value, which might cause an earlier-generation value to seem
Packit 6c4009
@c   current if setspecific is cancelled or interrupted by a signal
Packit 6c4009
@c  KEY_UNUSED ok
Packit 6c4009
@c  calloc dup @ascuheap @acsmem
Packit 6c4009
Associate the thread-specific @var{value} with @var{key} in the calling thread.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node Non-POSIX Extensions
Packit 6c4009
@subsection Non-POSIX Extensions
Packit 6c4009
Packit 6c4009
In addition to implementing the POSIX API for threads, @theglibc{} provides
Packit 6c4009
additional functions and interfaces to provide functionality not specified in
Packit 6c4009
the standard.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Default Thread Attributes::             Setting default attributes for
Packit 6c4009
					  threads in a process.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Default Thread Attributes
Packit 6c4009
@subsubsection Setting Process-wide defaults for thread attributes
Packit 6c4009
Packit 6c4009
@Theglibc{} provides non-standard API functions to set and get the default
Packit 6c4009
attributes used in the creation of threads in a process.
Packit 6c4009
Packit 6c4009
@deftypefun int pthread_getattr_default_np (pthread_attr_t *@var{attr})
Packit 6c4009
@standards{GNU, pthread.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
Packit 6c4009
@c Takes lock around read from default_pthread_attr.
Packit 6c4009
Get the default attribute values and set @var{attr} to match.  This
Packit 6c4009
function returns @math{0} on success and a non-zero error code on
Packit 6c4009
failure.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int pthread_setattr_default_np (pthread_attr_t *@var{attr})
Packit 6c4009
@standards{GNU, pthread.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{}}}
Packit 6c4009
@c pthread_setattr_default_np @ascuheap @asulock @aculock @acsmem
Packit 6c4009
@c  check_sched_policy_attr ok
Packit 6c4009
@c  check_sched_priority_attr ok
Packit 6c4009
@c   sched_get_priority_min dup ok
Packit 6c4009
@c   sched_get_priority_max dup ok
Packit 6c4009
@c  check_stacksize_attr ok
Packit 6c4009
@c  lll_lock @asulock @aculock
Packit 6c4009
@c  free dup @ascuheap @acsmem
Packit 6c4009
@c  realloc dup @ascuheap @acsmem
Packit 6c4009
@c  memcpy dup ok
Packit 6c4009
@c  lll_unlock @asulock @aculock
Packit 6c4009
Set the default attribute values to match the values in @var{attr}.  The
Packit 6c4009
function returns @math{0} on success and a non-zero error code on failure.
Packit 6c4009
The following error codes are defined for this function:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item EINVAL
Packit 6c4009
At least one of the values in @var{attr} does not qualify as valid for the
Packit 6c4009
attributes or the stack address is set in the attribute.
Packit 6c4009
@item ENOMEM
Packit 6c4009
The system does not have sufficient memory.
Packit 6c4009
@end table
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@c FIXME these are undocumented:
Packit 6c4009
@c pthread_atfork
Packit 6c4009
@c pthread_attr_destroy
Packit 6c4009
@c pthread_attr_getaffinity_np
Packit 6c4009
@c pthread_attr_getdetachstate
Packit 6c4009
@c pthread_attr_getguardsize
Packit 6c4009
@c pthread_attr_getinheritsched
Packit 6c4009
@c pthread_attr_getschedparam
Packit 6c4009
@c pthread_attr_getschedpolicy
Packit 6c4009
@c pthread_attr_getscope
Packit 6c4009
@c pthread_attr_getstack
Packit 6c4009
@c pthread_attr_getstackaddr
Packit 6c4009
@c pthread_attr_getstacksize
Packit 6c4009
@c pthread_attr_init
Packit 6c4009
@c pthread_attr_setaffinity_np
Packit 6c4009
@c pthread_attr_setdetachstate
Packit 6c4009
@c pthread_attr_setguardsize
Packit 6c4009
@c pthread_attr_setinheritsched
Packit 6c4009
@c pthread_attr_setschedparam
Packit 6c4009
@c pthread_attr_setschedpolicy
Packit 6c4009
@c pthread_attr_setscope
Packit 6c4009
@c pthread_attr_setstack
Packit 6c4009
@c pthread_attr_setstackaddr
Packit 6c4009
@c pthread_attr_setstacksize
Packit 6c4009
@c pthread_barrierattr_destroy
Packit 6c4009
@c pthread_barrierattr_getpshared
Packit 6c4009
@c pthread_barrierattr_init
Packit 6c4009
@c pthread_barrierattr_setpshared
Packit 6c4009
@c pthread_barrier_destroy
Packit 6c4009
@c pthread_barrier_init
Packit 6c4009
@c pthread_barrier_wait
Packit 6c4009
@c pthread_cancel
Packit 6c4009
@c pthread_cleanup_push
Packit 6c4009
@c pthread_cleanup_pop
Packit 6c4009
@c pthread_condattr_destroy
Packit 6c4009
@c pthread_condattr_getclock
Packit 6c4009
@c pthread_condattr_getpshared
Packit 6c4009
@c pthread_condattr_init
Packit 6c4009
@c pthread_condattr_setclock
Packit 6c4009
@c pthread_condattr_setpshared
Packit 6c4009
@c pthread_cond_broadcast
Packit 6c4009
@c pthread_cond_destroy
Packit 6c4009
@c pthread_cond_init
Packit 6c4009
@c pthread_cond_signal
Packit 6c4009
@c pthread_cond_timedwait
Packit 6c4009
@c pthread_cond_wait
Packit 6c4009
@c pthread_create
Packit 6c4009
@c pthread_detach
Packit 6c4009
@c pthread_equal
Packit 6c4009
@c pthread_exit
Packit 6c4009
@c pthread_getaffinity_np
Packit 6c4009
@c pthread_getattr_np
Packit 6c4009
@c pthread_getconcurrency
Packit 6c4009
@c pthread_getcpuclockid
Packit 6c4009
@c pthread_getname_np
Packit 6c4009
@c pthread_getschedparam
Packit 6c4009
@c pthread_join
Packit 6c4009
@c pthread_kill
Packit 6c4009
@c pthread_kill_other_threads_np
Packit 6c4009
@c pthread_mutexattr_destroy
Packit 6c4009
@c pthread_mutexattr_getkind_np
Packit 6c4009
@c pthread_mutexattr_getprioceiling
Packit 6c4009
@c pthread_mutexattr_getprotocol
Packit 6c4009
@c pthread_mutexattr_getpshared
Packit 6c4009
@c pthread_mutexattr_getrobust
Packit 6c4009
@c pthread_mutexattr_getrobust_np
Packit 6c4009
@c pthread_mutexattr_gettype
Packit 6c4009
@c pthread_mutexattr_init
Packit 6c4009
@c pthread_mutexattr_setkind_np
Packit 6c4009
@c pthread_mutexattr_setprioceiling
Packit 6c4009
@c pthread_mutexattr_setprotocol
Packit 6c4009
@c pthread_mutexattr_setpshared
Packit 6c4009
@c pthread_mutexattr_setrobust
Packit 6c4009
@c pthread_mutexattr_setrobust_np
Packit 6c4009
@c pthread_mutexattr_settype
Packit 6c4009
@c pthread_mutex_consistent
Packit 6c4009
@c pthread_mutex_consistent_np
Packit 6c4009
@c pthread_mutex_destroy
Packit 6c4009
@c pthread_mutex_getprioceiling
Packit 6c4009
@c pthread_mutex_init
Packit 6c4009
@c pthread_mutex_lock
Packit 6c4009
@c pthread_mutex_setprioceiling
Packit 6c4009
@c pthread_mutex_timedlock
Packit 6c4009
@c pthread_mutex_trylock
Packit 6c4009
@c pthread_mutex_unlock
Packit 6c4009
@c pthread_once
Packit 6c4009
@c pthread_rwlockattr_destroy
Packit 6c4009
@c pthread_rwlockattr_getkind_np
Packit 6c4009
@c pthread_rwlockattr_getpshared
Packit 6c4009
@c pthread_rwlockattr_init
Packit 6c4009
@c pthread_rwlockattr_setkind_np
Packit 6c4009
@c pthread_rwlockattr_setpshared
Packit 6c4009
@c pthread_rwlock_destroy
Packit 6c4009
@c pthread_rwlock_init
Packit 6c4009
@c pthread_rwlock_rdlock
Packit 6c4009
@c pthread_rwlock_timedrdlock
Packit 6c4009
@c pthread_rwlock_timedwrlock
Packit 6c4009
@c pthread_rwlock_tryrdlock
Packit 6c4009
@c pthread_rwlock_trywrlock
Packit 6c4009
@c pthread_rwlock_unlock
Packit 6c4009
@c pthread_rwlock_wrlock
Packit 6c4009
@c pthread_self
Packit 6c4009
@c pthread_setaffinity_np
Packit 6c4009
@c pthread_setcancelstate
Packit 6c4009
@c pthread_setcanceltype
Packit 6c4009
@c pthread_setconcurrency
Packit 6c4009
@c pthread_setname_np
Packit 6c4009
@c pthread_setschedparam
Packit 6c4009
@c pthread_setschedprio
Packit 6c4009
@c pthread_sigmask
Packit 6c4009
@c pthread_sigqueue
Packit 6c4009
@c pthread_spin_destroy
Packit 6c4009
@c pthread_spin_init
Packit 6c4009
@c pthread_spin_lock
Packit 6c4009
@c pthread_spin_trylock
Packit 6c4009
@c pthread_spin_unlock
Packit 6c4009
@c pthread_testcancel
Packit 6c4009
@c pthread_timedjoin_np
Packit 6c4009
@c pthread_tryjoin_np
Packit 6c4009
@c pthread_yield