gthread.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. G_BEGIN_DECLS
  31. #define G_THREAD_ERROR g_thread_error_quark ()
  32. GLIB_AVAILABLE_IN_ALL
  33. GQuark g_thread_error_quark (void);
  34. typedef enum
  35. {
  36. G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
  37. } GThreadError;
  38. typedef gpointer (*GThreadFunc) (gpointer data);
  39. typedef struct _GThread GThread;
  40. typedef union _GMutex GMutex;
  41. typedef struct _GRecMutex GRecMutex;
  42. typedef struct _GRWLock GRWLock;
  43. typedef struct _GCond GCond;
  44. typedef struct _GPrivate GPrivate;
  45. typedef struct _GOnce GOnce;
  46. union _GMutex
  47. {
  48. /*< private >*/
  49. gpointer p;
  50. guint i[2];
  51. };
  52. struct _GRWLock
  53. {
  54. /*< private >*/
  55. gpointer p;
  56. guint i[2];
  57. };
  58. struct _GCond
  59. {
  60. /*< private >*/
  61. gpointer p;
  62. guint i[2];
  63. };
  64. struct _GRecMutex
  65. {
  66. /*< private >*/
  67. gpointer p;
  68. guint i[2];
  69. };
  70. #define G_PRIVATE_INIT(notify) { NULL, (notify), { NULL, NULL } }
  71. struct _GPrivate
  72. {
  73. /*< private >*/
  74. gpointer p;
  75. GDestroyNotify notify;
  76. gpointer future[2];
  77. };
  78. typedef enum
  79. {
  80. G_ONCE_STATUS_NOTCALLED,
  81. G_ONCE_STATUS_PROGRESS,
  82. G_ONCE_STATUS_READY
  83. } GOnceStatus;
  84. #define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
  85. struct _GOnce
  86. {
  87. volatile GOnceStatus status;
  88. volatile gpointer retval;
  89. };
  90. #define G_LOCK_NAME(name) g__ ## name ## _lock
  91. #define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
  92. #define G_LOCK_DEFINE(name) GMutex G_LOCK_NAME (name)
  93. #define G_LOCK_EXTERN(name) extern GMutex G_LOCK_NAME (name)
  94. #ifdef G_DEBUG_LOCKS
  95. # define G_LOCK(name) G_STMT_START{ \
  96. g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
  97. "file %s: line %d (%s): locking: %s ", \
  98. __FILE__, __LINE__, G_STRFUNC, \
  99. #name); \
  100. g_mutex_lock (&G_LOCK_NAME (name)); \
  101. }G_STMT_END
  102. # define G_UNLOCK(name) G_STMT_START{ \
  103. g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
  104. "file %s: line %d (%s): unlocking: %s ", \
  105. __FILE__, __LINE__, G_STRFUNC, \
  106. #name); \
  107. g_mutex_unlock (&G_LOCK_NAME (name)); \
  108. }G_STMT_END
  109. # define G_TRYLOCK(name) \
  110. (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
  111. "file %s: line %d (%s): try locking: %s ", \
  112. __FILE__, __LINE__, G_STRFUNC, \
  113. #name), g_mutex_trylock (&G_LOCK_NAME (name)))
  114. #else /* !G_DEBUG_LOCKS */
  115. # define G_LOCK(name) g_mutex_lock (&G_LOCK_NAME (name))
  116. # define G_UNLOCK(name) g_mutex_unlock (&G_LOCK_NAME (name))
  117. # define G_TRYLOCK(name) g_mutex_trylock (&G_LOCK_NAME (name))
  118. #endif /* !G_DEBUG_LOCKS */
  119. GLIB_AVAILABLE_IN_2_32
  120. GThread * g_thread_ref (GThread *thread);
  121. GLIB_AVAILABLE_IN_2_32
  122. void g_thread_unref (GThread *thread);
  123. GLIB_AVAILABLE_IN_2_32
  124. GThread * g_thread_new (const gchar *name,
  125. GThreadFunc func,
  126. gpointer data);
  127. GLIB_AVAILABLE_IN_2_32
  128. GThread * g_thread_try_new (const gchar *name,
  129. GThreadFunc func,
  130. gpointer data,
  131. GError **error);
  132. GLIB_AVAILABLE_IN_ALL
  133. GThread * g_thread_self (void);
  134. GLIB_AVAILABLE_IN_ALL
  135. void g_thread_exit (gpointer retval);
  136. GLIB_AVAILABLE_IN_ALL
  137. gpointer g_thread_join (GThread *thread);
  138. GLIB_AVAILABLE_IN_ALL
  139. void g_thread_yield (void);
  140. GLIB_AVAILABLE_IN_2_32
  141. void g_mutex_init (GMutex *mutex);
  142. GLIB_AVAILABLE_IN_2_32
  143. void g_mutex_clear (GMutex *mutex);
  144. GLIB_AVAILABLE_IN_ALL
  145. void g_mutex_lock (GMutex *mutex);
  146. GLIB_AVAILABLE_IN_ALL
  147. gboolean g_mutex_trylock (GMutex *mutex);
  148. GLIB_AVAILABLE_IN_ALL
  149. void g_mutex_unlock (GMutex *mutex);
  150. GLIB_AVAILABLE_IN_2_32
  151. void g_rw_lock_init (GRWLock *rw_lock);
  152. GLIB_AVAILABLE_IN_2_32
  153. void g_rw_lock_clear (GRWLock *rw_lock);
  154. GLIB_AVAILABLE_IN_2_32
  155. void g_rw_lock_writer_lock (GRWLock *rw_lock);
  156. GLIB_AVAILABLE_IN_2_32
  157. gboolean g_rw_lock_writer_trylock (GRWLock *rw_lock);
  158. GLIB_AVAILABLE_IN_2_32
  159. void g_rw_lock_writer_unlock (GRWLock *rw_lock);
  160. GLIB_AVAILABLE_IN_2_32
  161. void g_rw_lock_reader_lock (GRWLock *rw_lock);
  162. GLIB_AVAILABLE_IN_2_32
  163. gboolean g_rw_lock_reader_trylock (GRWLock *rw_lock);
  164. GLIB_AVAILABLE_IN_2_32
  165. void g_rw_lock_reader_unlock (GRWLock *rw_lock);
  166. GLIB_AVAILABLE_IN_2_32
  167. void g_rec_mutex_init (GRecMutex *rec_mutex);
  168. GLIB_AVAILABLE_IN_2_32
  169. void g_rec_mutex_clear (GRecMutex *rec_mutex);
  170. GLIB_AVAILABLE_IN_2_32
  171. void g_rec_mutex_lock (GRecMutex *rec_mutex);
  172. GLIB_AVAILABLE_IN_2_32
  173. gboolean g_rec_mutex_trylock (GRecMutex *rec_mutex);
  174. GLIB_AVAILABLE_IN_2_32
  175. void g_rec_mutex_unlock (GRecMutex *rec_mutex);
  176. GLIB_AVAILABLE_IN_2_32
  177. void g_cond_init (GCond *cond);
  178. GLIB_AVAILABLE_IN_2_32
  179. void g_cond_clear (GCond *cond);
  180. GLIB_AVAILABLE_IN_ALL
  181. void g_cond_wait (GCond *cond,
  182. GMutex *mutex);
  183. GLIB_AVAILABLE_IN_ALL
  184. void g_cond_signal (GCond *cond);
  185. GLIB_AVAILABLE_IN_ALL
  186. void g_cond_broadcast (GCond *cond);
  187. GLIB_AVAILABLE_IN_2_32
  188. gboolean g_cond_wait_until (GCond *cond,
  189. GMutex *mutex,
  190. gint64 end_time);
  191. GLIB_AVAILABLE_IN_ALL
  192. gpointer g_private_get (GPrivate *key);
  193. GLIB_AVAILABLE_IN_ALL
  194. void g_private_set (GPrivate *key,
  195. gpointer value);
  196. GLIB_AVAILABLE_IN_2_32
  197. void g_private_replace (GPrivate *key,
  198. gpointer value);
  199. GLIB_AVAILABLE_IN_ALL
  200. gpointer g_once_impl (GOnce *once,
  201. GThreadFunc func,
  202. gpointer arg);
  203. GLIB_AVAILABLE_IN_ALL
  204. gboolean g_once_init_enter (volatile void *location);
  205. GLIB_AVAILABLE_IN_ALL
  206. void g_once_init_leave (volatile void *location,
  207. gsize result);
  208. #ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
  209. # define g_once(once, func, arg) g_once_impl ((once), (func), (arg))
  210. #else /* !G_ATOMIC_OP_MEMORY_BARRIER_NEEDED*/
  211. # define g_once(once, func, arg) \
  212. (((once)->status == G_ONCE_STATUS_READY) ? \
  213. (once)->retval : \
  214. g_once_impl ((once), (func), (arg)))
  215. #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
  216. #ifdef __GNUC__
  217. # define g_once_init_enter(location) \
  218. (G_GNUC_EXTENSION ({ \
  219. G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer)); \
  220. (void) (0 ? (gpointer) *(location) : 0); \
  221. (!g_atomic_pointer_get (location) && \
  222. g_once_init_enter (location)); \
  223. }))
  224. # define g_once_init_leave(location, result) \
  225. (G_GNUC_EXTENSION ({ \
  226. G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer)); \
  227. (void) (0 ? *(location) = (result) : 0); \
  228. g_once_init_leave ((location), (gsize) (result)); \
  229. }))
  230. #else
  231. # define g_once_init_enter(location) \
  232. (g_once_init_enter((location)))
  233. # define g_once_init_leave(location, result) \
  234. (g_once_init_leave((location), (gsize) (result)))
  235. #endif
  236. GLIB_AVAILABLE_IN_2_36
  237. guint g_get_num_processors (void);
  238. G_END_DECLS
  239. #endif /* __G_THREAD_H__ */