Lely core libraries  2.3.4
threads.h
Go to the documentation of this file.
1 
22 #ifndef LELY_LIBC_THREADS_H_
23 #define LELY_LIBC_THREADS_H_
24 
25 #include <lely/features.h>
26 
27 // clang-format off
28 #ifndef LELY_HAVE_THREADS_H
29 #if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
30 #define LELY_HAVE_THREADS_H 1
31 #endif
32 #endif
33 // clang-format on
34 
35 #if LELY_HAVE_THREADS_H
36 #include <threads.h>
37 #else // !LELY_HAVE_UNISTD_H
38 
39 #include <lely/libc/time.h>
40 
41 #ifndef LELY_HAVE_PTHREAD_H
42 #if _POSIX_THREADS >= 200112L || defined(__MINGW32__)
43 #define LELY_HAVE_PTHREAD_H 1
44 #endif
45 #endif
46 
47 #if LELY_HAVE_PTHREAD_H
48 #include <pthread.h>
49 #elif _WIN32
50 #include <windows.h>
51 #else
52 #error This file requires POSIX Threads or Windows.
53 #endif
54 
55 #ifndef thread_local
56 #if __cplusplus >= 201103L
57 // thread_local is a keyword in C++11 and later.
58 #else
59 #define thread_local _Thread_local
60 #endif
61 #endif
62 
64 #if LELY_HAVE_PTHREAD_H
65 #define ONCE_FLAG_INIT PTHREAD_ONCE_INIT
66 #elif _WIN32
67 #define ONCE_FLAG_INIT 0
68 #endif
69 
74 #define TSS_DTOR_ITERATIONS 1
75 
77 #if LELY_HAVE_PTHREAD_H
78 typedef pthread_cond_t cnd_t;
79 #elif _WIN32
80 typedef CONDITION_VARIABLE cnd_t;
81 #endif
82 
84 #if LELY_HAVE_PTHREAD_H
85 typedef pthread_t thrd_t;
86 #elif _WIN32
87 typedef void *thrd_t;
88 #endif
89 
94 #if LELY_HAVE_PTHREAD_H
95 typedef pthread_key_t tss_t;
96 #elif _WIN32
97 typedef DWORD tss_t;
98 #endif
99 
101 #if LELY_HAVE_PTHREAD_H
102 typedef pthread_mutex_t mtx_t;
103 #elif _WIN32
104 typedef CRITICAL_SECTION mtx_t;
105 #endif
106 
107 enum {
117 };
118 
119 enum {
139 };
140 
142 #if LELY_HAVE_PTHREAD_H
143 typedef pthread_once_t once_flag;
144 #elif _WIN32
145 typedef long once_flag;
146 #endif
147 
148 #ifdef __cplusplus
149 extern "C" {
150 #endif
151 
156 #if LELY_HAVE_PTHREAD_H
157 typedef void (*tss_dtor_t)(void *);
158 #elif _WIN32
159 // clang-format off
160 typedef void (WINAPI *tss_dtor_t)(void *);
161 // clang-format on
162 #endif
163 
168 typedef int (*thrd_start_t)(void *);
169 
177 void call_once(once_flag *flag, void (*func)(void));
178 
188 int cnd_broadcast(cnd_t *cond);
189 
195 void cnd_destroy(cnd_t *cond);
196 
207 int cnd_init(cnd_t *cond);
208 
218 int cnd_signal(cnd_t *cond);
219 
232 int cnd_timedwait(cnd_t *cond, mtx_t *mtx, const struct timespec *ts);
233 
244 int cnd_wait(cnd_t *cond, mtx_t *mtx);
245 
250 void mtx_destroy(mtx_t *mtx);
251 
266 int mtx_init(mtx_t *mtx, int type);
267 
276 int mtx_lock(mtx_t *mtx);
277 
288 int mtx_timedlock(mtx_t *mtx, const struct timespec *ts);
289 
298 int mtx_trylock(mtx_t *mtx);
299 
307 int mtx_unlock(mtx_t *mtx);
308 
321 int thrd_create(thrd_t *thr, thrd_start_t func, void *arg);
322 
328 thrd_t thrd_current(void);
329 
339 int thrd_detach(thrd_t thr);
340 
348 int thrd_equal(thrd_t thr0, thrd_t thr1);
349 
358 _Noreturn void thrd_exit(int res);
359 
371 int thrd_join(thrd_t thr, int *res);
372 
390 int thrd_sleep(const struct timespec *duration, struct timespec *remaining);
391 
396 void thrd_yield(void);
397 
410 int tss_create(tss_t *key, tss_dtor_t dtor);
411 
416 void tss_delete(tss_t key);
417 
425 void *tss_get(tss_t key);
426 
434 int tss_set(tss_t key, void *val);
435 
436 #ifdef __cplusplus
437 }
438 #endif
439 
440 #endif // !LELY_HAVE_THREADS_H
441 
442 #endif // !LELY_LIBC_THREADS_H_
cnd_signal
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...
Definition: threads-pthread.c:80
tss_delete
void tss_delete(tss_t key)
Releases any resources used by the thread-specific storage identified by key.
Definition: threads-pthread.c:295
features.h
mtx_destroy
void mtx_destroy(mtx_t *mtx)
Releases any resources used by the mutex at mtx.
Definition: threads-pthread.c:113
thrd_detach
int thrd_detach(thrd_t thr)
Tells the operating system to dispose of any resources allocated to the thread identified by thr when...
Definition: threads-pthread.c:218
mtx_trylock
int mtx_trylock(mtx_t *mtx)
Endeavors to lock the mutex at mtx.
Definition: threads-pthread.c:172
tss_dtor_t
void(* tss_dtor_t)(void *)
The function pointer type used for a destructor for a thread-specific storage pointer.
Definition: threads.h:157
thrd_t
pthread_t thrd_t
A complete object type that holds an identifier for a thread.
Definition: threads.h:85
time.h
threads.h
thrd_nomem
@ thrd_nomem
Indicates that the requested operation failed because it was unable to allocate memory.
Definition: threads.h:138
mtx_lock
int mtx_lock(mtx_t *mtx)
Blocks until it locks the mutex at mtx.
Definition: threads-pthread.c:150
thrd_exit
_Noreturn void thrd_exit(int res)
Terminates execution of the calling thread and sets its result code to res.
Definition: threads-pthread.c:235
tss_get
void * tss_get(tss_t key)
Returns the value for the current thread held in the thread-specific storage identified by key.
Definition: threads-pthread.c:301
thrd_error
@ thrd_error
Indicates that the requested operation failed.
Definition: threads.h:123
tss_t
pthread_key_t tss_t
A complete object type that holds an identifier for a thread-specific storage pointer.
Definition: threads.h:95
mtx_t
pthread_mutex_t mtx_t
A complete object type that holds an identifier for a mutex.
Definition: threads.h:102
cnd_t
pthread_cond_t cnd_t
A complete object type that holds an identifier for a condition variable.
Definition: threads.h:78
thrd_success
@ thrd_success
Indicates that the requested operation succeeded.
Definition: threads.h:121
cnd_timedwait
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...
Definition: threads-pthread.c:91
mtx_unlock
int mtx_unlock(mtx_t *mtx)
Unlocks the mutex at mtx.
Definition: threads-pthread.c:183
_Noreturn
#define _Noreturn
A function declared with a _Noreturn function specifier SHALL not return to its caller.
Definition: features.h:224
thrd_current
thrd_t thrd_current(void)
Identifies the thread that called it.
Definition: threads-pthread.c:212
once_flag
pthread_once_t once_flag
A complete object type that holds a flag for use by call_once().
Definition: threads.h:143
mtx_timedlock
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...
Definition: threads-pthread.c:161
cnd_wait
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...
Definition: threads-pthread.c:102
thrd_equal
int thrd_equal(thrd_t thr0, thrd_t thr1)
Determines whether the thread identified by thr0 refers to the thread identified by thr1.
Definition: threads-pthread.c:229
thrd_sleep
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 ...
Definition: threads-pthread.c:258
mtx_recursive
@ mtx_recursive
A mutex type that supports recursive locking.
Definition: threads.h:111
thrd_join
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...
Definition: threads-pthread.c:244
tss_set
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.
Definition: threads-pthread.c:307
thrd_busy
@ thrd_busy
Indicates that the requested operation failed because a resource requested by a test and return funct...
Definition: threads.h:133
thrd_create
int thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
Creates a new thread executing func(arg).
Definition: threads-pthread.c:194
cnd_destroy
void cnd_destroy(cnd_t *cond)
Releases all resources used by the condition variable at cond.
Definition: threads-pthread.c:63
thrd_timedout
@ thrd_timedout
Indicates that the time specified in the call was reached without acquiring the requested resource.
Definition: threads.h:128
cnd_init
int cnd_init(cnd_t *cond)
Creates a condition variable.
Definition: threads-pthread.c:69
thrd_yield
void thrd_yield(void)
Endeavors to permit other threads to run, even if the current thread would ordinarily continue to run...
Definition: threads-pthread.c:277
call_once
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()...
Definition: threads-pthread.c:46
cnd_broadcast
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...
Definition: threads-pthread.c:52
thrd_start_t
int(* thrd_start_t)(void *)
The function pointer type that is passed to thrd_create() to create a new thread.
Definition: threads.h:168
tss_create
int tss_create(tss_t *key, tss_dtor_t dtor)
Creates a thread-specific storage pointer with destructor dtor, which may be NULL.
Definition: threads-pthread.c:284
mtx_timed
@ mtx_timed
A mutex type that supports timeout (not available with the native Windows API).
Definition: threads.h:116
mtx_init
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:
Definition: threads-pthread.c:119
mtx_plain
@ mtx_plain
A mutex type that supports neither timeout nor test and return.
Definition: threads.h:109