gthread.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 modify
  5. * it under the terms of the GNU Lesser General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * licence, 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. G_END_DECLS
  306. #endif /* __G_THREAD_H__ */