tinycthread.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /* -*- mode: c; tab-width: 2; indent-tabs-mode: nil; -*-
  2. Copyright (c) 2012 Marcus Geelnard
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not
  10. claim that you wrote the original software. If you use this software
  11. in a product, an acknowledgment in the product documentation would be
  12. appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source
  16. distribution.
  17. */
  18. #ifndef _TINYCTHREAD_H_
  19. #define _TINYCTHREAD_H_
  20. /**
  21. * @file
  22. * @mainpage TinyCThread API Reference
  23. *
  24. * @section intro_sec Introduction
  25. * TinyCThread is a minimal, portable implementation of basic threading
  26. * classes for C.
  27. *
  28. * They closely mimic the functionality and naming of the C11 standard, and
  29. * should be easily replaceable with the corresponding standard variants.
  30. *
  31. * @section port_sec Portability
  32. * The Win32 variant uses the native Win32 API for implementing the thread
  33. * classes, while for other systems, the POSIX threads API (pthread) is used.
  34. *
  35. * @section misc_sec Miscellaneous
  36. * The following special keywords are available: #_Thread_local.
  37. *
  38. * For more detailed information, browse the different sections of this
  39. * documentation. A good place to start is:
  40. * tinycthread.h.
  41. */
  42. /* Which platform are we on? */
  43. #if !defined(_TTHREAD_PLATFORM_DEFINED_)
  44. #if defined(_WIN32) || defined(__WIN32__) || defined(__WINDOWS__)
  45. #define _TTHREAD_WIN32_
  46. #else
  47. #define _TTHREAD_POSIX_
  48. #endif
  49. #define _TTHREAD_PLATFORM_DEFINED_
  50. #endif
  51. /* Activate some POSIX functionality (e.g. clock_gettime and recursive mutexes) */
  52. #if defined(_TTHREAD_POSIX_)
  53. #undef _FEATURES_H
  54. #if !defined(_GNU_SOURCE)
  55. #define _GNU_SOURCE
  56. #endif
  57. #if !defined(_POSIX_C_SOURCE) || ((_POSIX_C_SOURCE - 0) < 199309L)
  58. #undef _POSIX_C_SOURCE
  59. #define _POSIX_C_SOURCE 199309L
  60. #endif
  61. #if !defined(_XOPEN_SOURCE) || ((_XOPEN_SOURCE - 0) < 500)
  62. #undef _XOPEN_SOURCE
  63. #define _XOPEN_SOURCE 500
  64. #endif
  65. #endif
  66. /* Generic includes */
  67. #include <time.h>
  68. /* Platform specific includes */
  69. #if defined(_TTHREAD_POSIX_)
  70. #include <sys/time.h>
  71. #include <pthread.h>
  72. #elif defined(_TTHREAD_WIN32_)
  73. #ifndef WIN32_LEAN_AND_MEAN
  74. #define WIN32_LEAN_AND_MEAN
  75. #define __UNDEF_LEAN_AND_MEAN
  76. #endif
  77. #include <windows.h>
  78. #ifdef __UNDEF_LEAN_AND_MEAN
  79. #undef WIN32_LEAN_AND_MEAN
  80. #undef __UNDEF_LEAN_AND_MEAN
  81. #endif
  82. #endif
  83. /* Workaround for missing TIME_UTC: If time.h doesn't provide TIME_UTC,
  84. it's quite likely that libc does not support it either. Hence, fall back to
  85. the only other supported time specifier: CLOCK_REALTIME (and if that fails,
  86. we're probably emulating clock_gettime anyway, so anything goes). */
  87. #ifndef TIME_UTC
  88. #ifdef CLOCK_REALTIME
  89. #define TIME_UTC CLOCK_REALTIME
  90. #else
  91. #define TIME_UTC 0
  92. #endif
  93. #endif
  94. /* Workaround for missing clock_gettime (most Windows compilers, afaik) */
  95. #if defined(_TTHREAD_WIN32_) || defined(__APPLE_CC__)
  96. #define _TTHREAD_EMULATE_CLOCK_GETTIME_
  97. /* Emulate struct timespec */
  98. #if defined(_TTHREAD_WIN32_)
  99. struct _ttherad_timespec {
  100. time_t tv_sec;
  101. long tv_nsec;
  102. };
  103. #define timespec _ttherad_timespec
  104. #endif
  105. /* Emulate clockid_t */
  106. typedef int _tthread_clockid_t;
  107. #define clockid_t _tthread_clockid_t
  108. /* Emulate clock_gettime */
  109. int _tthread_clock_gettime(clockid_t clk_id, struct timespec *ts);
  110. #define clock_gettime _tthread_clock_gettime
  111. #ifndef CLOCK_REALTIME
  112. #define CLOCK_REALTIME 0
  113. #endif
  114. #endif
  115. /** TinyCThread version (major number). */
  116. #define TINYCTHREAD_VERSION_MAJOR 1
  117. /** TinyCThread version (minor number). */
  118. #define TINYCTHREAD_VERSION_MINOR 1
  119. /** TinyCThread version (full version). */
  120. #define TINYCTHREAD_VERSION (TINYCTHREAD_VERSION_MAJOR * 100 + TINYCTHREAD_VERSION_MINOR)
  121. /**
  122. * @def _Thread_local
  123. * Thread local storage keyword.
  124. * A variable that is declared with the @c _Thread_local keyword makes the
  125. * value of the variable local to each thread (known as thread-local storage,
  126. * or TLS). Example usage:
  127. * @code
  128. * // This variable is local to each thread.
  129. * _Thread_local int variable;
  130. * @endcode
  131. * @note The @c _Thread_local keyword is a macro that maps to the corresponding
  132. * compiler directive (e.g. @c __declspec(thread)).
  133. * @note This directive is currently not supported on Mac OS X (it will give
  134. * a compiler error), since compile-time TLS is not supported in the Mac OS X
  135. * executable format. Also, some older versions of MinGW (before GCC 4.x) do
  136. * not support this directive.
  137. * @hideinitializer
  138. */
  139. /* FIXME: Check for a PROPER value of __STDC_VERSION__ to know if we have C11 */
  140. #if !(defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201102L)) && !defined(_Thread_local)
  141. #if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_CC) || defined(__IBMCPP__)
  142. #define _Thread_local __thread
  143. #else
  144. #define _Thread_local __declspec(thread)
  145. #endif
  146. #endif
  147. /* Macros */
  148. #define TSS_DTOR_ITERATIONS 0
  149. /* Function return values */
  150. #define thrd_error 0 /**< The requested operation failed */
  151. #define thrd_success 1 /**< The requested operation succeeded */
  152. #define thrd_timeout 2 /**< The time specified in the call was reached without acquiring the requested resource */
  153. #define thrd_busy 3 /**< The requested operation failed because a tesource requested by a test and return function is already in use */
  154. #define thrd_nomem 4 /**< The requested operation failed because it was unable to allocate memory */
  155. /* Mutex types */
  156. #define mtx_plain 1
  157. #define mtx_timed 2
  158. #define mtx_try 4
  159. #define mtx_recursive 8
  160. /* Mutex */
  161. #if defined(_TTHREAD_WIN32_)
  162. typedef struct {
  163. CRITICAL_SECTION mHandle; /* Critical section handle */
  164. int mAlreadyLocked; /* TRUE if the mutex is already locked */
  165. int mRecursive; /* TRUE if the mutex is recursive */
  166. } mtx_t;
  167. #else
  168. typedef pthread_mutex_t mtx_t;
  169. #endif
  170. /** Create a mutex object.
  171. * @param mtx A mutex object.
  172. * @param type Bit-mask that must have one of the following six values:
  173. * @li @c mtx_plain for a simple non-recursive mutex
  174. * @li @c mtx_timed for a non-recursive mutex that supports timeout
  175. * @li @c mtx_try for a non-recursive mutex that supports test and return
  176. * @li @c mtx_plain | @c mtx_recursive (same as @c mtx_plain, but recursive)
  177. * @li @c mtx_timed | @c mtx_recursive (same as @c mtx_timed, but recursive)
  178. * @li @c mtx_try | @c mtx_recursive (same as @c mtx_try, but recursive)
  179. * @return @ref thrd_success on success, or @ref thrd_error if the request could
  180. * not be honored.
  181. */
  182. int mtx_init(mtx_t *mtx, int type);
  183. /** Release any resources used by the given mutex.
  184. * @param mtx A mutex object.
  185. */
  186. void mtx_destroy(mtx_t *mtx);
  187. /** Lock the given mutex.
  188. * Blocks until the given mutex can be locked. If the mutex is non-recursive, and
  189. * the calling thread already has a lock on the mutex, this call will block
  190. * forever.
  191. * @param mtx A mutex object.
  192. * @return @ref thrd_success on success, or @ref thrd_error if the request could
  193. * not be honored.
  194. */
  195. int mtx_lock(mtx_t *mtx);
  196. /** NOT YET IMPLEMENTED.
  197. */
  198. int mtx_timedlock(mtx_t *mtx, const struct timespec *ts);
  199. /** Try to lock the given mutex.
  200. * The specified mutex shall support either test and return or timeout. If the
  201. * mutex is already locked, the function returns without blocking.
  202. * @param mtx A mutex object.
  203. * @return @ref thrd_success on success, or @ref thrd_busy if the resource
  204. * requested is already in use, or @ref thrd_error if the request could not be
  205. * honored.
  206. */
  207. int mtx_trylock(mtx_t *mtx);
  208. /** Unlock the given mutex.
  209. * @param mtx A mutex object.
  210. * @return @ref thrd_success on success, or @ref thrd_error if the request could
  211. * not be honored.
  212. */
  213. int mtx_unlock(mtx_t *mtx);
  214. /* Condition variable */
  215. #if defined(_TTHREAD_WIN32_)
  216. typedef struct {
  217. HANDLE mEvents[2]; /* Signal and broadcast event HANDLEs. */
  218. unsigned int mWaitersCount; /* Count of the number of waiters. */
  219. CRITICAL_SECTION mWaitersCountLock; /* Serialize access to mWaitersCount. */
  220. } cnd_t;
  221. #else
  222. typedef pthread_cond_t cnd_t;
  223. #endif
  224. /** Create a condition variable object.
  225. * @param cond A condition variable object.
  226. * @return @ref thrd_success on success, or @ref thrd_error if the request could
  227. * not be honored.
  228. */
  229. int cnd_init(cnd_t *cond);
  230. /** Release any resources used by the given condition variable.
  231. * @param cond A condition variable object.
  232. */
  233. void cnd_destroy(cnd_t *cond);
  234. /** Signal a condition variable.
  235. * Unblocks one of the threads that are blocked on the given condition variable
  236. * at the time of the call. If no threads are blocked on the condition variable
  237. * at the time of the call, the function does nothing and return success.
  238. * @param cond A condition variable object.
  239. * @return @ref thrd_success on success, or @ref thrd_error if the request could
  240. * not be honored.
  241. */
  242. int cnd_signal(cnd_t *cond);
  243. /** Broadcast a condition variable.
  244. * Unblocks all of the threads that are blocked on the given condition variable
  245. * at the time of the call. If no threads are blocked on the condition variable
  246. * at the time of the call, the function does nothing and return success.
  247. * @param cond A condition variable object.
  248. * @return @ref thrd_success on success, or @ref thrd_error if the request could
  249. * not be honored.
  250. */
  251. int cnd_broadcast(cnd_t *cond);
  252. /** Wait for a condition variable to become signaled.
  253. * The function atomically unlocks the given mutex and endeavors to block until
  254. * the given condition variable is signaled by a call to cnd_signal or to
  255. * cnd_broadcast. When the calling thread becomes unblocked it locks the mutex
  256. * before it returns.
  257. * @param cond A condition variable object.
  258. * @param mtx A mutex object.
  259. * @return @ref thrd_success on success, or @ref thrd_error if the request could
  260. * not be honored.
  261. */
  262. int cnd_wait(cnd_t *cond, mtx_t *mtx);
  263. /** Wait for a condition variable to become signaled.
  264. * The function atomically unlocks the given mutex and endeavors to block until
  265. * the given condition variable is signaled by a call to cnd_signal or to
  266. * cnd_broadcast, or until after the specified time. When the calling thread
  267. * becomes unblocked it locks the mutex before it returns.
  268. * @param cond A condition variable object.
  269. * @param mtx A mutex object.
  270. * @param xt A point in time at which the request will time out (absolute time).
  271. * @return @ref thrd_success upon success, or @ref thrd_timeout if the time
  272. * specified in the call was reached without acquiring the requested resource, or
  273. * @ref thrd_error if the request could not be honored.
  274. */
  275. int cnd_timedwait(cnd_t *cond, mtx_t *mtx, const struct timespec *ts);
  276. /* Thread */
  277. #if defined(_TTHREAD_WIN32_)
  278. typedef HANDLE thrd_t;
  279. #else
  280. typedef pthread_t thrd_t;
  281. #endif
  282. /** Thread start function.
  283. * Any thread that is started with the @ref thrd_create() function must be
  284. * started through a function of this type.
  285. * @param arg The thread argument (the @c arg argument of the corresponding
  286. * @ref thrd_create() call).
  287. * @return The thread return value, which can be obtained by another thread
  288. * by using the @ref thrd_join() function.
  289. */
  290. typedef int (*thrd_start_t)(void *arg);
  291. /** Create a new thread.
  292. * @param thr Identifier of the newly created thread.
  293. * @param func A function pointer to the function that will be executed in
  294. * the new thread.
  295. * @param arg An argument to the thread function.
  296. * @return @ref thrd_success on success, or @ref thrd_nomem if no memory could
  297. * be allocated for the thread requested, or @ref thrd_error if the request
  298. * could not be honored.
  299. * @note A thread’s identifier may be reused for a different thread once the
  300. * original thread has exited and either been detached or joined to another
  301. * thread.
  302. */
  303. int thrd_create(thrd_t *thr, thrd_start_t func, void *arg);
  304. /** Identify the calling thread.
  305. * @return The identifier of the calling thread.
  306. */
  307. thrd_t thrd_current(void);
  308. /** NOT YET IMPLEMENTED.
  309. */
  310. int thrd_detach(thrd_t thr);
  311. /** Compare two thread identifiers.
  312. * The function determines if two thread identifiers refer to the same thread.
  313. * @return Zero if the two thread identifiers refer to different threads.
  314. * Otherwise a nonzero value is returned.
  315. */
  316. int thrd_equal(thrd_t thr0, thrd_t thr1);
  317. /** Terminate execution of the calling thread.
  318. * @param res Result code of the calling thread.
  319. */
  320. void thrd_exit(int res);
  321. /** Wait for a thread to terminate.
  322. * The function joins the given thread with the current thread by blocking
  323. * until the other thread has terminated.
  324. * @param thr The thread to join with.
  325. * @param res If this pointer is not NULL, the function will store the result
  326. * code of the given thread in the integer pointed to by @c res.
  327. * @return @ref thrd_success on success, or @ref thrd_error if the request could
  328. * not be honored.
  329. */
  330. int thrd_join(thrd_t thr, int *res);
  331. /** Put the calling thread to sleep.
  332. * Suspend execution of the calling thread.
  333. * @param time_point A point in time at which the thread will resume (absolute time).
  334. * @param remaining If non-NULL, this parameter will hold the remaining time until
  335. * time_point upon return. This will typically be zero, but if
  336. * the thread was woken up by a signal that is not ignored before
  337. * time_point was reached @c remaining will hold a positive
  338. * time.
  339. * @return 0 (zero) on successful sleep, or -1 if an interrupt occurred.
  340. */
  341. int thrd_sleep(const struct timespec *time_point, struct timespec *remaining);
  342. /** Yield execution to another thread.
  343. * Permit other threads to run, even if the current thread would ordinarily
  344. * continue to run.
  345. */
  346. void thrd_yield(void);
  347. /* Thread local storage */
  348. #if defined(_TTHREAD_WIN32_)
  349. typedef DWORD tss_t;
  350. #else
  351. typedef pthread_key_t tss_t;
  352. #endif
  353. /** Destructor function for a thread-specific storage.
  354. * @param val The value of the destructed thread-specific storage.
  355. */
  356. typedef void (*tss_dtor_t)(void *val);
  357. /** Create a thread-specific storage.
  358. * @param key The unique key identifier that will be set if the function is
  359. * successful.
  360. * @param dtor Destructor function. This can be NULL.
  361. * @return @ref thrd_success on success, or @ref thrd_error if the request could
  362. * not be honored.
  363. * @note The destructor function is not supported under Windows. If @c dtor is
  364. * not NULL when calling this function under Windows, the function will fail
  365. * and return @ref thrd_error.
  366. */
  367. int tss_create(tss_t *key, tss_dtor_t dtor);
  368. /** Delete a thread-specific storage.
  369. * The function releases any resources used by the given thread-specific
  370. * storage.
  371. * @param key The key that shall be deleted.
  372. */
  373. void tss_delete(tss_t key);
  374. /** Get the value for a thread-specific storage.
  375. * @param key The thread-specific storage identifier.
  376. * @return The value for the current thread held in the given thread-specific
  377. * storage.
  378. */
  379. void *tss_get(tss_t key);
  380. /** Set the value for a thread-specific storage.
  381. * @param key The thread-specific storage identifier.
  382. * @param val The value of the thread-specific storage to set for the current
  383. * thread.
  384. * @return @ref thrd_success on success, or @ref thrd_error if the request could
  385. * not be honored.
  386. */
  387. int tss_set(tss_t key, void *val);
  388. #endif /* _TINYTHREAD_H_ */