26 #if !LELY_NO_THREADS && LELY_HAVE_PTHREAD_H 32 #include <processthreadsapi.h> 35 #undef LELY_HAVE_SCHED 36 #if _POSIX_C_SOURCE >= 200112L && defined(_POSIX_PRIORITY_SCHEDULING) 37 #define LELY_HAVE_SCHED 1 44 pthread_once(flag, func);
50 int errsv = pthread_cond_broadcast(cond);
61 pthread_cond_destroy(cond);
67 int errsv = pthread_cond_init(cond, NULL);
78 int errsv = pthread_cond_signal(cond);
90 while ((errsv = pthread_cond_timedwait(cond, mtx, ts)) == EINTR)
103 while ((errsv = pthread_cond_wait(cond, mtx)) == EINTR)
115 pthread_mutex_destroy(mtx);
122 pthread_mutexattr_t attr;
124 errsv = pthread_mutexattr_init(&attr);
126 goto error_mutexattr_init;
128 errsv = pthread_mutexattr_settype(&attr, (type &
mtx_recursive)
129 ? PTHREAD_MUTEX_RECURSIVE
130 : PTHREAD_MUTEX_NORMAL);
133 goto error_mutexattr_settype;
134 errsv = pthread_mutex_init(mtx, &attr);
136 goto error_mutex_init;
137 pthread_mutexattr_destroy(&attr);
142 error_mutexattr_settype:
143 pthread_mutexattr_destroy(&attr);
144 error_mutexattr_init:
153 while ((errsv = pthread_mutex_lock(mtx)) == EINTR)
166 while ((errsv = pthread_mutex_timedlock(mtx, ts)) == EINTR)
179 while ((errsv = pthread_mutex_trylock(mtx)) == EINTR)
191 int errsv = pthread_mutex_unlock(mtx);
203 #pragma GCC diagnostic push 204 #pragma GCC diagnostic ignored "-Wcast-function-type" 206 int errsv = pthread_create(thr, NULL, (
void *(*)(
void *))func, arg);
208 #pragma GCC diagnostic pop 220 return pthread_self();
226 int errsv = pthread_detach(thr);
237 return pthread_equal(thr0, thr1);
243 pthread_exit((
void *)(intptr_t)res);
252 void *value_ptr = NULL;
253 int errsv = pthread_join((pthread_t)thr, &value_ptr);
259 *res = (intptr_t)value_ptr;
264 thrd_sleep(
const struct timespec *duration,
struct timespec *remaining)
267 int res = nanosleep(duration, remaining);
269 res = errno == EINTR ? -1 : -2;
281 #elif LELY_HAVE_SCHED 292 int errsv = pthread_key_create(key, dtor);
303 pthread_key_delete(key);
309 return pthread_getspecific(key);
315 int errsv = pthread_setspecific(key, val);
323 #endif // !LELY_NO_THREADS && LELY_HAVE_PTHREAD_H int cnd_timedwait(cnd_t *cond, mtx_t *mtx, const struct timespec *ts)
Atomically unlocks the mutex at mtx and endeavors to block until the condition variable at cond is si...
int cnd_wait(cnd_t *cond, mtx_t *mtx)
Atomically unlocks the mutex at mtx and endeavors to block until the condition variable at cond is si...
void call_once(once_flag *flag, void(*func)(void))
Uses the once_flag at flag to ensure that func is called exactly once, the first time the call_once()...
int cnd_broadcast(cnd_t *cond)
Unblocks all of the threads that are blocked on the condition variable at cond at the time of the cal...
Indicates that the requested operation failed because a resource requested by a test and return funct...
A mutex type that supports recursive locking.
int thrd_equal(thrd_t thr0, thrd_t thr1)
Determines whether the thread identified by thr0 refers to the thread identified by thr1...
int tss_set(tss_t key, void *val)
Sets the value for the current thread held in the thread-specific storage identified by key to val...
int mtx_unlock(mtx_t *mtx)
Unlocks the mutex at mtx.
void cnd_destroy(cnd_t *cond)
Releases all resources used by the condition variable at cond.
pthread_t thrd_t
A complete object type that holds an identifier for a thread.
int mtx_timedlock(mtx_t *mtx, const struct timespec *ts)
Endeavors to block until it locks the mutex at mtx or until after the TIME_UTC-based calendar time at...
Indicates that the time specified in the call was reached without acquiring the requested resource...
Indicates that the requested operation succeeded.
int mtx_init(mtx_t *mtx, int type)
Creates a mutex object with properties indicated by type, which must have one of the four values: ...
pthread_key_t tss_t
A complete object type that holds an identifier for a thread-specific storage pointer.
thrd_t thrd_current(void)
Identifies the thread that called it.
This header file is part of the C11 and POSIX compatibility library; it includes <threads.h>, if it exists, and defines any missing functionality.
pthread_cond_t cnd_t
A complete object type that holds an identifier for a condition variable.
pthread_mutex_t mtx_t
A complete object type that holds an identifier for a mutex.
int mtx_trylock(mtx_t *mtx)
Endeavors to lock the mutex at mtx.
void *(* thrd_start_t)(void *)
The function pointer type that is passed to thrd_create() to create a new thread. ...
int thrd_sleep(const struct timespec *duration, struct timespec *remaining)
Suspends execution of the calling thread until either the interval specified by duration has elapsed ...
int cnd_signal(cnd_t *cond)
Unblocks one of the threads that are blocked on the condition variable at cond at the time of the cal...
pthread_once_t once_flag
A complete object type that holds a flag for use by call_once().
Indicates that the requested operation failed because it was unable to allocate memory.
int thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
Creates a new thread executing func(arg).
int thrd_detach(thrd_t thr)
Tells the operating system to dispose of any resources allocated to the thread identified by thr when...
int tss_create(tss_t *key, tss_dtor_t dtor)
Creates a thread-specific storage pointer with destructor dtor, which may be NULL.
int thrd_join(thrd_t thr, int *res)
Joins the thread identified by thr with the current thread by blocking until the other thread has ter...
This header file is part of the C11 and POSIX compatibility library; it includes <stdint.h> and defines any missing functionality.
Indicates that the requested operation failed.
void mtx_destroy(mtx_t *mtx)
Releases any resources used by the mutex at mtx.
int mtx_lock(mtx_t *mtx)
Blocks until it locks the mutex at mtx.
void tss_delete(tss_t key)
Releases any resources used by the thread-specific storage identified by key.
#define _Noreturn
A function declared with a _Noreturn function specifier SHALL not return to its caller.
void * tss_get(tss_t key)
Returns the value for the current thread held in the thread-specific storage identified by key...
_Noreturn void thrd_exit(int res)
Terminates execution of the calling thread and sets its result code to res.
This is the internal header file of the C11 and POSIX compatibility library.
int cnd_init(cnd_t *cond)
Creates a condition variable.
void thrd_yield(void)
Endeavors to permit other threads to run, even if the current thread would ordinarily continue to run...
void(* tss_dtor_t)(void *)
The function pointer type used for a destructor for a thread-specific storage pointer.