tinycthread.h
Go to the documentation of this file.
1 /* -*- mode: c; tab-width: 2; indent-tabs-mode: nil; -*-
2 Copyright (c) 2012 Marcus Geelnard
3 
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7 
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11 
12  1. The origin of this software must not be misrepresented; you must not
13  claim that you wrote the original software. If you use this software
14  in a product, an acknowledgment in the product documentation would be
15  appreciated but is not required.
16 
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19 
20  3. This notice may not be removed or altered from any source
21  distribution.
22 */
23 
24 #ifndef _TINYCTHREAD_H_
25 #define _TINYCTHREAD_H_
26 
50 /* Which platform are we on? */
51 #if !defined(_TTHREAD_PLATFORM_DEFINED_)
52  #if defined(_WIN32) || defined(__WIN32__) || defined(__WINDOWS__)
53  #define _TTHREAD_WIN32_
54  #else
55  #define _TTHREAD_POSIX_
56  #endif
57  #define _TTHREAD_PLATFORM_DEFINED_
58 #endif
59 
60 /* Activate some POSIX functionality (e.g. clock_gettime and recursive mutexes) */
61 #if defined(_TTHREAD_POSIX_)
62  #undef _FEATURES_H
63  #if !defined(_GNU_SOURCE)
64  #define _GNU_SOURCE
65  #endif
66  #if !defined(_POSIX_C_SOURCE) || ((_POSIX_C_SOURCE - 0) < 199309L)
67  #undef _POSIX_C_SOURCE
68  #define _POSIX_C_SOURCE 199309L
69  #endif
70  #if !defined(_XOPEN_SOURCE) || ((_XOPEN_SOURCE - 0) < 500)
71  #undef _XOPEN_SOURCE
72  #define _XOPEN_SOURCE 500
73  #endif
74 #endif
75 
76 /* Generic includes */
77 #include <time.h>
78 
79 /* Platform specific includes */
80 #if defined(_TTHREAD_POSIX_)
81  #include <sys/time.h>
82  #include <pthread.h>
83 #elif defined(_TTHREAD_WIN32_)
84  #ifndef WIN32_LEAN_AND_MEAN
85  #define WIN32_LEAN_AND_MEAN
86  #define __UNDEF_LEAN_AND_MEAN
87  #endif
88  #include <windows.h>
89  #ifdef __UNDEF_LEAN_AND_MEAN
90  #undef WIN32_LEAN_AND_MEAN
91  #undef __UNDEF_LEAN_AND_MEAN
92  #endif
93 #endif
94 
95 /* Workaround for missing TIME_UTC: If time.h doesn't provide TIME_UTC,
96  it's quite likely that libc does not support it either. Hence, fall back to
97  the only other supported time specifier: CLOCK_REALTIME (and if that fails,
98  we're probably emulating clock_gettime anyway, so anything goes). */
99 #ifndef TIME_UTC
100  #ifdef CLOCK_REALTIME
101  #define TIME_UTC CLOCK_REALTIME
102  #else
103  #define TIME_UTC 0
104  #endif
105 #endif
106 
107 /* Workaround for missing clock_gettime (most Windows compilers, afaik) */
108 #if defined(_TTHREAD_WIN32_) || defined(__APPLE_CC__)
109 #define _TTHREAD_EMULATE_CLOCK_GETTIME_
110 /* Emulate struct timespec */
111 #if defined(_TTHREAD_WIN32_)
112 struct _ttherad_timespec {
113  time_t tv_sec;
114  long tv_nsec;
115 };
116 #define timespec _ttherad_timespec
117 #endif
118 
119 /* Emulate clockid_t */
120 typedef int _tthread_clockid_t;
121 #define clockid_t _tthread_clockid_t
122 
123 /* Emulate clock_gettime */
124 int _tthread_clock_gettime(clockid_t clk_id, struct timespec *ts);
125 #define clock_gettime _tthread_clock_gettime
126 #ifndef CLOCK_REALTIME
127  #define CLOCK_REALTIME 0
128 #endif
129 #endif
130 
131 
133 #define TINYCTHREAD_VERSION_MAJOR 1
134 
135 #define TINYCTHREAD_VERSION_MINOR 1
136 
137 #define TINYCTHREAD_VERSION (TINYCTHREAD_VERSION_MAJOR * 100 + TINYCTHREAD_VERSION_MINOR)
138 
158 /* FIXME: Check for a PROPER value of __STDC_VERSION__ to know if we have C11 */
159 #if !(defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201102L)) && !defined(_Thread_local)
160  #if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_CC) || defined(__IBMCPP__)
161  #define _Thread_local __thread
162  #else
163  #define _Thread_local __declspec(thread)
164  #endif
165 #endif
166 
167 /* Macros */
168 #define TSS_DTOR_ITERATIONS 0
169 
170 /* Function return values */
171 #define thrd_error 0
172 #define thrd_success 1
173 #define thrd_timeout 2
174 #define thrd_busy 3
175 #define thrd_nomem 4
177 /* Mutex types */
178 #define mtx_plain 1
179 #define mtx_timed 2
180 #define mtx_try 4
181 #define mtx_recursive 8
182 
183 /* Mutex */
184 #if defined(_TTHREAD_WIN32_)
185 typedef struct {
186  CRITICAL_SECTION mHandle; /* Critical section handle */
187  int mAlreadyLocked; /* TRUE if the mutex is already locked */
188  int mRecursive; /* TRUE if the mutex is recursive */
189 } mtx_t;
190 #else
191 typedef pthread_mutex_t mtx_t;
192 #endif
193 
206 int mtx_init(mtx_t *mtx, int type);
207 
211 void mtx_destroy(mtx_t *mtx);
212 
221 int mtx_lock(mtx_t *mtx);
222 
225 int mtx_timedlock(mtx_t *mtx, const struct timespec *ts);
226 
235 int mtx_trylock(mtx_t *mtx);
236 
242 int mtx_unlock(mtx_t *mtx);
243 
244 /* Condition variable */
245 #if defined(_TTHREAD_WIN32_)
246 typedef struct {
247  HANDLE mEvents[2]; /* Signal and broadcast event HANDLEs. */
248  unsigned int mWaitersCount; /* Count of the number of waiters. */
249  CRITICAL_SECTION mWaitersCountLock; /* Serialize access to mWaitersCount. */
250 } cnd_t;
251 #else
252 typedef pthread_cond_t cnd_t;
253 #endif
254 
260 int cnd_init(cnd_t *cond);
261 
265 void cnd_destroy(cnd_t *cond);
266 
275 int cnd_signal(cnd_t *cond);
276 
285 int cnd_broadcast(cnd_t *cond);
286 
297 int cnd_wait(cnd_t *cond, mtx_t *mtx);
298 
311 int cnd_timedwait(cnd_t *cond, mtx_t *mtx, const struct timespec *ts);
312 
313 /* Thread */
314 #if defined(_TTHREAD_WIN32_)
315 typedef HANDLE thrd_t;
316 #else
317 typedef pthread_t thrd_t;
318 #endif
319 
328 typedef int (*thrd_start_t)(void *arg);
329 
342 int thrd_create(thrd_t *thr, thrd_start_t func, void *arg);
343 
347 thrd_t thrd_current(void);
348 
351 int thrd_detach(thrd_t thr);
352 
358 int thrd_equal(thrd_t thr0, thrd_t thr1);
359 
363 void thrd_exit(int res);
364 
374 int thrd_join(thrd_t thr, int *res);
375 
386 int thrd_sleep(const struct timespec *time_point, struct timespec *remaining);
387 
392 void thrd_yield(void);
393 
394 /* Thread local storage */
395 #if defined(_TTHREAD_WIN32_)
396 typedef DWORD tss_t;
397 #else
398 typedef pthread_key_t tss_t;
399 #endif
400 
404 typedef void (*tss_dtor_t)(void *val);
405 
416 int tss_create(tss_t *key, tss_dtor_t dtor);
417 
423 void tss_delete(tss_t key);
424 
430 void *tss_get(tss_t key);
431 
439 int tss_set(tss_t key, void *val);
440 
441 
442 #endif /* _TINYTHREAD_H_ */
443 
int mtx_trylock(mtx_t *mtx)
Definition: tinycthread.c:109
thrd_t thrd_current(void)
Definition: tinycthread.c:393
typedef void(APIENTRY *GLDEBUGPROC)(GLenum source
pthread_cond_t cnd_t
Definition: tinycthread.h:252
int mtx_timedlock(mtx_t *mtx, const struct timespec *ts)
Definition: tinycthread.c:101
int mtx_unlock(mtx_t *mtx)
Definition: tinycthread.c:124
int tss_set(tss_t key, void *val)
Definition: tinycthread.c:561
int thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
Definition: tinycthread.c:361
void thrd_yield(void)
Definition: tinycthread.c:512
int mtx_init(mtx_t *mtx, int type)
Definition: tinycthread.c:56
int(* thrd_start_t)(void *arg)
Definition: tinycthread.h:328
Definition: arg_fwd.hpp:23
int cnd_init(cnd_t *cond)
Definition: tinycthread.c:140
GLuint GLfloat * val
GLuint64 key
Definition: glext.h:8966
int cnd_broadcast(cnd_t *cond)
Definition: tinycthread.c:211
void mtx_destroy(mtx_t *mtx)
Definition: tinycthread.c:77
void * tss_get(tss_t key)
Definition: tinycthread.c:552
void(* tss_dtor_t)(void *val)
Definition: tinycthread.h:404
int cnd_wait(cnd_t *cond, mtx_t *mtx)
Definition: tinycthread.c:285
void thrd_exit(int res)
Definition: tinycthread.c:418
int cnd_signal(cnd_t *cond)
Definition: tinycthread.c:186
GLenum func
int tss_create(tss_t *key, tss_dtor_t dtor)
Definition: tinycthread.c:521
int mtx_lock(mtx_t *mtx)
Definition: tinycthread.c:86
pthread_mutex_t mtx_t
Definition: tinycthread.h:191
pthread_key_t tss_t
Definition: tinycthread.h:398
int cnd_timedwait(cnd_t *cond, mtx_t *mtx, const struct timespec *ts)
Definition: tinycthread.c:294
int thrd_join(thrd_t thr, int *res)
Definition: tinycthread.c:432
int thrd_sleep(const struct timespec *time_point, struct timespec *remaining)
Definition: tinycthread.c:465
GLenum type
void tss_delete(tss_t key)
Definition: tinycthread.c:543
int thrd_detach(thrd_t thr)
Definition: tinycthread.c:402
pthread_t thrd_t
Definition: tinycthread.h:317
void cnd_destroy(cnd_t *cond)
Definition: tinycthread.c:169
GLuint res
Definition: glext.h:8856
int thrd_equal(thrd_t thr0, thrd_t thr1)
Definition: tinycthread.c:409


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:11