gthread.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /* GLIB - Library of useful routines for C programming
  2. * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  19. * file for a list of people on the GLib Team. See the ChangeLog
  20. * files for a list of changes. These files are distributed with
  21. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  22. */
  23. #ifndef __G_THREAD_H__
  24. #define __G_THREAD_H__
  25. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  26. #error "Only <glib.h> can be included directly."
  27. #endif
  28. #include <glib/gatomic.h>
  29. #include <glib/gerror.h>
  30. #include <glib/gutils.h>
  31. G_BEGIN_DECLS
  32. #define G_THREAD_ERROR g_thread_error_quark ()
  33. GLIB_AVAILABLE_IN_ALL
  34. GQuark g_thread_error_quark (void);
  35. typedef enum
  36. {
  37. G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
  38. } GThreadError;
  39. typedef gpointer (*GThreadFunc) (gpointer data);
  40. typedef struct _GThread GThread;
  41. typedef union _GMutex GMutex;
  42. typedef struct _GRecMutex GRecMutex;
  43. typedef struct _GRWLock GRWLock;
  44. typedef struct _GCond GCond;
  45. typedef struct _GPrivate GPrivate;
  46. typedef struct _GOnce GOnce;
  47. union _GMutex
  48. {
  49. /*< private >*/
  50. gpointer p;
  51. guint i[2];
  52. };
  53. struct _GRWLock
  54. {
  55. /*< private >*/
  56. gpointer p;
  57. guint i[2];
  58. };
  59. struct _GCond
  60. {
  61. /*< private >*/
  62. gpointer p;
  63. guint i[2];
  64. };
  65. struct _GRecMutex
  66. {
  67. /*< private >*/
  68. gpointer p;
  69. guint i[2];
  70. };
  71. #define G_PRIVATE_INIT(notify) { NULL, (notify), { NULL, NULL } }
  72. struct _GPrivate
  73. {
  74. /*< private >*/
  75. gpointer p;
  76. GDestroyNotify notify;
  77. gpointer future[2];
  78. };
  79. typedef enum
  80. {
  81. G_ONCE_STATUS_NOTCALLED,
  82. G_ONCE_STATUS_PROGRESS,
  83. G_ONCE_STATUS_READY
  84. } GOnceStatus;
  85. #define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
  86. struct _GOnce
  87. {
  88. volatile GOnceStatus status;
  89. volatile gpointer retval;
  90. };
  91. #define G_LOCK_NAME(name) g__ ## name ## _lock
  92. #define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
  93. #define G_LOCK_DEFINE(name) GMutex G_LOCK_NAME (name)
  94. #define G_LOCK_EXTERN(name) extern GMutex G_LOCK_NAME (name)
  95. #ifdef G_DEBUG_LOCKS
  96. # define G_LOCK(name) G_STMT_START{ \
  97. g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
  98. "file %s: line %d (%s): locking: %s ", \
  99. __FILE__, __LINE__, G_STRFUNC, \
  100. #name); \
  101. g_mutex_lock (&G_LOCK_NAME (name)); \
  102. }G_STMT_END
  103. # define G_UNLOCK(name) G_STMT_START{ \
  104. g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
  105. "file %s: line %d (%s): unlocking: %s ", \
  106. __FILE__, __LINE__, G_STRFUNC, \
  107. #name); \
  108. g_mutex_unlock (&G_LOCK_NAME (name)); \
  109. }G_STMT_END
  110. # define G_TRYLOCK(name) \
  111. (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
  112. "file %s: line %d (%s): try locking: %s ", \
  113. __FILE__, __LINE__, G_STRFUNC, \
  114. #name), g_mutex_trylock (&G_LOCK_NAME (name)))
  115. #else /* !G_DEBUG_LOCKS */
  116. # define G_LOCK(name) g_mutex_lock (&G_LOCK_NAME (name))
  117. # define G_UNLOCK(name) g_mutex_unlock (&G_LOCK_NAME (name))
  118. # define G_TRYLOCK(name) g_mutex_trylock (&G_LOCK_NAME (name))
  119. #endif /* !G_DEBUG_LOCKS */
  120. GLIB_AVAILABLE_IN_2_32
  121. GThread * g_thread_ref (GThread *thread);
  122. GLIB_AVAILABLE_IN_2_32
  123. void g_thread_unref (GThread *thread);
  124. GLIB_AVAILABLE_IN_2_32
  125. GThread * g_thread_new (const gchar *name,
  126. GThreadFunc func,
  127. gpointer data);
  128. GLIB_AVAILABLE_IN_2_32
  129. GThread * g_thread_try_new (const gchar *name,
  130. GThreadFunc func,
  131. gpointer data,
  132. GError **error);
  133. GLIB_AVAILABLE_IN_ALL
  134. GThread * g_thread_self (void);
  135. GLIB_AVAILABLE_IN_ALL
  136. void g_thread_exit (gpointer retval);
  137. GLIB_AVAILABLE_IN_ALL
  138. gpointer g_thread_join (GThread *thread);
  139. GLIB_AVAILABLE_IN_ALL
  140. void g_thread_yield (void);
  141. GLIB_AVAILABLE_IN_2_32
  142. void g_mutex_init (GMutex *mutex);
  143. GLIB_AVAILABLE_IN_2_32
  144. void g_mutex_clear (GMutex *mutex);
  145. GLIB_AVAILABLE_IN_ALL
  146. void g_mutex_lock (GMutex *mutex);
  147. GLIB_AVAILABLE_IN_ALL
  148. gboolean g_mutex_trylock (GMutex *mutex);
  149. GLIB_AVAILABLE_IN_ALL
  150. void g_mutex_unlock (GMutex *mutex);
  151. GLIB_AVAILABLE_IN_2_32
  152. void g_rw_lock_init (GRWLock *rw_lock);
  153. GLIB_AVAILABLE_IN_2_32
  154. void g_rw_lock_clear (GRWLock *rw_lock);
  155. GLIB_AVAILABLE_IN_2_32
  156. void g_rw_lock_writer_lock (GRWLock *rw_lock);
  157. GLIB_AVAILABLE_IN_2_32
  158. gboolean g_rw_lock_writer_trylock (GRWLock *rw_lock);
  159. GLIB_AVAILABLE_IN_2_32
  160. void g_rw_lock_writer_unlock (GRWLock *rw_lock);
  161. GLIB_AVAILABLE_IN_2_32
  162. void g_rw_lock_reader_lock (GRWLock *rw_lock);
  163. GLIB_AVAILABLE_IN_2_32
  164. gboolean g_rw_lock_reader_trylock (GRWLock *rw_lock);
  165. GLIB_AVAILABLE_IN_2_32
  166. void g_rw_lock_reader_unlock (GRWLock *rw_lock);
  167. GLIB_AVAILABLE_IN_2_32
  168. void g_rec_mutex_init (GRecMutex *rec_mutex);
  169. GLIB_AVAILABLE_IN_2_32
  170. void g_rec_mutex_clear (GRecMutex *rec_mutex);
  171. GLIB_AVAILABLE_IN_2_32
  172. void g_rec_mutex_lock (GRecMutex *rec_mutex);
  173. GLIB_AVAILABLE_IN_2_32
  174. gboolean g_rec_mutex_trylock (GRecMutex *rec_mutex);
  175. GLIB_AVAILABLE_IN_2_32
  176. void g_rec_mutex_unlock (GRecMutex *rec_mutex);
  177. GLIB_AVAILABLE_IN_2_32
  178. void g_cond_init (GCond *cond);
  179. GLIB_AVAILABLE_IN_2_32
  180. void g_cond_clear (GCond *cond);
  181. GLIB_AVAILABLE_IN_ALL
  182. void g_cond_wait (GCond *cond,
  183. GMutex *mutex);
  184. GLIB_AVAILABLE_IN_ALL
  185. void g_cond_signal (GCond *cond);
  186. GLIB_AVAILABLE_IN_ALL
  187. void g_cond_broadcast (GCond *cond);
  188. GLIB_AVAILABLE_IN_2_32
  189. gboolean g_cond_wait_until (GCond *cond,
  190. GMutex *mutex,
  191. gint64 end_time);
  192. GLIB_AVAILABLE_IN_ALL
  193. gpointer g_private_get (GPrivate *key);
  194. GLIB_AVAILABLE_IN_ALL
  195. void g_private_set (GPrivate *key,
  196. gpointer value);
  197. GLIB_AVAILABLE_IN_2_32
  198. void g_private_replace (GPrivate *key,
  199. gpointer value);
  200. GLIB_AVAILABLE_IN_ALL
  201. gpointer g_once_impl (GOnce *once,
  202. GThreadFunc func,
  203. gpointer arg);
  204. GLIB_AVAILABLE_IN_ALL
  205. gboolean g_once_init_enter (volatile void *location);
  206. GLIB_AVAILABLE_IN_ALL
  207. void g_once_init_leave (volatile void *location,
  208. gsize result);
  209. #ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
  210. # define g_once(once, func, arg) g_once_impl ((once), (func), (arg))
  211. #else /* !G_ATOMIC_OP_MEMORY_BARRIER_NEEDED*/
  212. # define g_once(once, func, arg) \
  213. (((once)->status == G_ONCE_STATUS_READY) ? \
  214. (once)->retval : \
  215. g_once_impl ((once), (func), (arg)))
  216. #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
  217. #ifdef __GNUC__
  218. # define g_once_init_enter(location) \
  219. (G_GNUC_EXTENSION ({ \
  220. G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer)); \
  221. (void) (0 ? (gpointer) *(location) : 0); \
  222. (!g_atomic_pointer_get (location) && \
  223. g_once_init_enter (location)); \
  224. }))
  225. # define g_once_init_leave(location, result) \
  226. (G_GNUC_EXTENSION ({ \
  227. G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer)); \
  228. (void) (0 ? *(location) = (result) : 0); \
  229. g_once_init_leave ((location), (gsize) (result)); \
  230. }))
  231. #else
  232. # define g_once_init_enter(location) \
  233. (g_once_init_enter((location)))
  234. # define g_once_init_leave(location, result) \
  235. (g_once_init_leave((location), (gsize) (result)))
  236. #endif
  237. GLIB_AVAILABLE_IN_2_36
  238. guint g_get_num_processors (void);
  239. /**
  240. * GMutexLocker:
  241. *
  242. * Opaque type. See g_mutex_locker_new() for details.
  243. * Since: 2.44
  244. */
  245. typedef void GMutexLocker;
  246. /**
  247. * g_mutex_locker_new:
  248. * @mutex: a mutex to lock
  249. *
  250. * Lock @mutex and return a new #GMutexLocker. Unlock with
  251. * g_mutex_locker_free(). Using g_mutex_unlock() on @mutex
  252. * while a #GMutexLocker exists can lead to undefined behaviour.
  253. *
  254. * This is intended to be used with g_autoptr(). Note that g_autoptr()
  255. * is only available when using GCC or clang, so the following example
  256. * will only work with those compilers:
  257. * |[
  258. * typedef struct
  259. * {
  260. * ...
  261. * GMutex mutex;
  262. * ...
  263. * } MyObject;
  264. *
  265. * static void
  266. * my_object_do_stuff (MyObject *self)
  267. * {
  268. * g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&self->mutex);
  269. *
  270. * // Code with mutex locked here
  271. *
  272. * if (cond)
  273. * // No need to unlock
  274. * return;
  275. *
  276. * // Optionally early unlock
  277. * g_clear_pointer (&locker, g_mutex_locker_free);
  278. *
  279. * // Code with mutex unlocked here
  280. * }
  281. * ]|
  282. *
  283. * Returns: a #GMutexLocker
  284. * Since: 2.44
  285. */
  286. static inline GMutexLocker *
  287. g_mutex_locker_new (GMutex *mutex)
  288. {
  289. g_mutex_lock (mutex);
  290. return (GMutexLocker *) mutex;
  291. }
  292. /**
  293. * g_mutex_locker_free:
  294. * @locker: a GMutexLocker
  295. *
  296. * Unlock @locker's mutex. See g_mutex_locker_new() for details.
  297. *
  298. * Since: 2.44
  299. */
  300. static inline void
  301. g_mutex_locker_free (GMutexLocker *locker)
  302. {
  303. g_mutex_unlock ((GMutex *) locker);
  304. }
  305. /**
  306. * GRecMutexLocker:
  307. *
  308. * Opaque type. See g_rec_mutex_locker_new() for details.
  309. * Since: 2.60
  310. */
  311. typedef void GRecMutexLocker;
  312. /**
  313. * g_rec_mutex_locker_new:
  314. * @rec_mutex: a recursive mutex to lock
  315. *
  316. * Lock @rec_mutex and return a new #GRecMutexLocker. Unlock with
  317. * g_rec_mutex_locker_free(). Using g_rec_mutex_unlock() on @rec_mutex
  318. * while a #GRecMutexLocker exists can lead to undefined behaviour.
  319. *
  320. * This is intended to be used with g_autoptr(). Note that g_autoptr()
  321. * is only available when using GCC or clang, so the following example
  322. * will only work with those compilers:
  323. * |[
  324. * typedef struct
  325. * {
  326. * ...
  327. * GRecMutex rec_mutex;
  328. * ...
  329. * } MyObject;
  330. *
  331. * static void
  332. * my_object_do_stuff (MyObject *self)
  333. * {
  334. * g_autoptr(GRecMutexLocker) locker = g_rec_mutex_locker_new (&self->rec_mutex);
  335. *
  336. * // Code with rec_mutex locked here
  337. *
  338. * if (cond)
  339. * // No need to unlock
  340. * return;
  341. *
  342. * // Optionally early unlock
  343. * g_clear_pointer (&locker, g_rec_mutex_locker_free);
  344. *
  345. * // Code with rec_mutex unlocked here
  346. * }
  347. * ]|
  348. *
  349. * Returns: a #GRecMutexLocker
  350. * Since: 2.60
  351. */
  352. static inline GRecMutexLocker *
  353. g_rec_mutex_locker_new (GRecMutex *rec_mutex)
  354. {
  355. g_rec_mutex_lock (rec_mutex);
  356. return (GRecMutexLocker *) rec_mutex;
  357. }
  358. /**
  359. * g_rec_mutex_locker_free:
  360. * @locker: a GRecMutexLocker
  361. *
  362. * Unlock @locker's recursive mutex. See g_rec_mutex_locker_new() for details.
  363. *
  364. * Since: 2.60
  365. */
  366. static inline void
  367. g_rec_mutex_locker_free (GRecMutexLocker *locker)
  368. {
  369. g_rec_mutex_unlock ((GRecMutex *) locker);
  370. }
  371. /**
  372. * GRWLockWriterLocker:
  373. *
  374. * Opaque type. See g_rw_lock_writer_locker_new() for details.
  375. * Since: 2.62
  376. */
  377. typedef void GRWLockWriterLocker;
  378. /**
  379. * g_rw_lock_writer_locker_new:
  380. * @rw_lock: a #GRWLock
  381. *
  382. * Obtain a write lock on @rw_lock and return a new #GRWLockWriterLocker.
  383. * Unlock with g_rw_lock_writer_locker_free(). Using g_rw_lock_writer_unlock()
  384. * on @rw_lock while a #GRWLockWriterLocker exists can lead to undefined
  385. * behaviour.
  386. *
  387. * This is intended to be used with g_autoptr(). Note that g_autoptr()
  388. * is only available when using GCC or clang, so the following example
  389. * will only work with those compilers:
  390. * |[
  391. * typedef struct
  392. * {
  393. * ...
  394. * GRWLock rw_lock;
  395. * GPtrArray *array;
  396. * ...
  397. * } MyObject;
  398. *
  399. * static gchar *
  400. * my_object_get_data (MyObject *self, guint index)
  401. * {
  402. * g_autoptr(GRWLockReaderLocker) locker = g_rw_lock_reader_locker_new (&self->rw_lock);
  403. *
  404. * // Code with a read lock obtained on rw_lock here
  405. *
  406. * if (self->array == NULL)
  407. * // No need to unlock
  408. * return NULL;
  409. *
  410. * if (index < self->array->len)
  411. * // No need to unlock
  412. * return g_ptr_array_index (self->array, index);
  413. *
  414. * // Optionally early unlock
  415. * g_clear_pointer (&locker, g_rw_lock_reader_locker_free);
  416. *
  417. * // Code with rw_lock unlocked here
  418. * return NULL;
  419. * }
  420. *
  421. * static void
  422. * my_object_set_data (MyObject *self, guint index, gpointer data)
  423. * {
  424. * g_autoptr(GRWLockWriterLocker) locker = g_rw_lock_writer_locker_new (&self->rw_lock);
  425. *
  426. * // Code with a write lock obtained on rw_lock here
  427. *
  428. * if (self->array == NULL)
  429. * self->array = g_ptr_array_new ();
  430. *
  431. * if (cond)
  432. * // No need to unlock
  433. * return;
  434. *
  435. * if (index >= self->array->len)
  436. * g_ptr_array_set_size (self->array, index+1);
  437. * g_ptr_array_index (self->array, index) = data;
  438. *
  439. * // Optionally early unlock
  440. * g_clear_pointer (&locker, g_rw_lock_writer_locker_free);
  441. *
  442. * // Code with rw_lock unlocked here
  443. * }
  444. * ]|
  445. *
  446. * Returns: a #GRWLockWriterLocker
  447. * Since: 2.62
  448. */
  449. static inline GRWLockWriterLocker *
  450. g_rw_lock_writer_locker_new (GRWLock *rw_lock)
  451. {
  452. g_rw_lock_writer_lock (rw_lock);
  453. return (GRWLockWriterLocker *) rw_lock;
  454. }
  455. /**
  456. * g_rw_lock_writer_locker_free:
  457. * @locker: a GRWLockWriterLocker
  458. *
  459. * Release a write lock on @locker's read-write lock. See
  460. * g_rw_lock_writer_locker_new() for details.
  461. *
  462. * Since: 2.62
  463. */
  464. static inline void
  465. g_rw_lock_writer_locker_free (GRWLockWriterLocker *locker)
  466. {
  467. g_rw_lock_writer_unlock ((GRWLock *) locker);
  468. }
  469. /**
  470. * GRWLockReaderLocker:
  471. *
  472. * Opaque type. See g_rw_lock_reader_locker_new() for details.
  473. * Since: 2.62
  474. */
  475. typedef void GRWLockReaderLocker;
  476. /**
  477. * g_rw_lock_reader_locker_new:
  478. * @rw_lock: a #GRWLock
  479. *
  480. * Obtain a read lock on @rw_lock and return a new #GRWLockReaderLocker.
  481. * Unlock with g_rw_lock_reader_locker_free(). Using g_rw_lock_reader_unlock()
  482. * on @rw_lock while a #GRWLockReaderLocker exists can lead to undefined
  483. * behaviour.
  484. *
  485. * This is intended to be used with g_autoptr(). For a code sample, see
  486. * g_rw_lock_writer_locker_new().
  487. *
  488. * Returns: a #GRWLockReaderLocker
  489. * Since: 2.62
  490. */
  491. static inline GRWLockReaderLocker *
  492. g_rw_lock_reader_locker_new (GRWLock *rw_lock)
  493. {
  494. g_rw_lock_reader_lock (rw_lock);
  495. return (GRWLockReaderLocker *) rw_lock;
  496. }
  497. /**
  498. * g_rw_lock_reader_locker_free:
  499. * @locker: a GRWLockReaderLocker
  500. *
  501. * Release a read lock on @locker's read-write lock. See
  502. * g_rw_lock_reader_locker_new() for details.
  503. *
  504. * Since: 2.62
  505. */
  506. static inline void
  507. g_rw_lock_reader_locker_free (GRWLockReaderLocker *locker)
  508. {
  509. g_rw_lock_reader_unlock ((GRWLock *) locker);
  510. }
  511. G_END_DECLS
  512. #endif /* __G_THREAD_H__ */