Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

408 wiersze
17 KiB

  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 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but 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, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. */
  19. /*
  20. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  21. * file for a list of people on the GLib Team. See the ChangeLog
  22. * files for a list of changes. These files are distributed with
  23. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  24. */
  25. #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  26. #error "Only <glib.h> can be included directly."
  27. #endif
  28. #ifndef __G_THREAD_H__
  29. #define __G_THREAD_H__
  30. #include <glib/gerror.h>
  31. #include <glib/gutils.h> /* for G_INLINE_FUNC */
  32. #include <glib/gatomic.h> /* for g_atomic_pointer_get */
  33. G_BEGIN_DECLS
  34. /* GLib Thread support
  35. */
  36. extern GQuark g_thread_error_quark (void);
  37. #define G_THREAD_ERROR g_thread_error_quark ()
  38. typedef enum
  39. {
  40. G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
  41. } GThreadError;
  42. typedef gpointer (*GThreadFunc) (gpointer data);
  43. typedef enum
  44. {
  45. G_THREAD_PRIORITY_LOW,
  46. G_THREAD_PRIORITY_NORMAL,
  47. G_THREAD_PRIORITY_HIGH,
  48. G_THREAD_PRIORITY_URGENT
  49. } GThreadPriority;
  50. typedef struct _GThread GThread;
  51. struct _GThread
  52. {
  53. /*< private >*/
  54. GThreadFunc func;
  55. gpointer data;
  56. gboolean joinable;
  57. GThreadPriority priority;
  58. };
  59. typedef struct _GMutex GMutex;
  60. typedef struct _GCond GCond;
  61. typedef struct _GPrivate GPrivate;
  62. typedef struct _GStaticPrivate GStaticPrivate;
  63. typedef struct _GThreadFunctions GThreadFunctions;
  64. struct _GThreadFunctions
  65. {
  66. GMutex* (*mutex_new) (void);
  67. void (*mutex_lock) (GMutex *mutex);
  68. gboolean (*mutex_trylock) (GMutex *mutex);
  69. void (*mutex_unlock) (GMutex *mutex);
  70. void (*mutex_free) (GMutex *mutex);
  71. GCond* (*cond_new) (void);
  72. void (*cond_signal) (GCond *cond);
  73. void (*cond_broadcast) (GCond *cond);
  74. void (*cond_wait) (GCond *cond,
  75. GMutex *mutex);
  76. gboolean (*cond_timed_wait) (GCond *cond,
  77. GMutex *mutex,
  78. GTimeVal *end_time);
  79. void (*cond_free) (GCond *cond);
  80. GPrivate* (*private_new) (GDestroyNotify destructor);
  81. gpointer (*private_get) (GPrivate *private_key);
  82. void (*private_set) (GPrivate *private_key,
  83. gpointer data);
  84. void (*thread_create) (GThreadFunc func,
  85. gpointer data,
  86. gulong stack_size,
  87. gboolean joinable,
  88. gboolean bound,
  89. GThreadPriority priority,
  90. gpointer thread,
  91. GError **error);
  92. void (*thread_yield) (void);
  93. void (*thread_join) (gpointer thread);
  94. void (*thread_exit) (void);
  95. void (*thread_set_priority)(gpointer thread,
  96. GThreadPriority priority);
  97. void (*thread_self) (gpointer thread);
  98. gboolean (*thread_equal) (gpointer thread1,
  99. gpointer thread2);
  100. };
  101. GLIB_VAR GThreadFunctions g_thread_functions_for_glib_use;
  102. GLIB_VAR gboolean g_thread_use_default_impl;
  103. GLIB_VAR gboolean g_threads_got_initialized;
  104. GLIB_VAR guint64 (*g_thread_gettime) (void);
  105. /* initializes the mutex/cond/private implementation for glib, might
  106. * only be called once, and must not be called directly or indirectly
  107. * from another glib-function, e.g. as a callback.
  108. */
  109. void g_thread_init (GThreadFunctions *vtable);
  110. /* Errorcheck mutexes. If you define G_ERRORCHECK_MUTEXES, then all
  111. * mutexes will check for re-locking and re-unlocking */
  112. /* Initialize thread system with errorcheck mutexes. vtable must be
  113. * NULL. Do not call directly. Use #define G_ERRORCHECK_MUTEXES
  114. * instead.
  115. */
  116. void g_thread_init_with_errorcheck_mutexes (GThreadFunctions* vtable);
  117. /* Checks if thread support is initialized. Identical to the
  118. * g_thread_supported macro but provided for language bindings.
  119. */
  120. gboolean g_thread_get_initialized (void);
  121. /* A random number to recognize debug calls to g_mutex_... */
  122. #define G_MUTEX_DEBUG_MAGIC 0xf8e18ad7
  123. #ifdef G_ERRORCHECK_MUTEXES
  124. #define g_thread_init(vtable) g_thread_init_with_errorcheck_mutexes (vtable)
  125. #endif
  126. /* internal function for fallback static mutex implementation */
  127. GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex);
  128. #define g_static_mutex_get_mutex_impl_shortcut(mutex) \
  129. (g_atomic_pointer_get (mutex) ? *(mutex) : \
  130. g_static_mutex_get_mutex_impl (mutex))
  131. /* shorthands for conditional and unconditional function calls */
  132. #define G_THREAD_UF(op, arglist) \
  133. (*g_thread_functions_for_glib_use . op) arglist
  134. #define G_THREAD_CF(op, fail, arg) \
  135. (g_thread_supported () ? G_THREAD_UF (op, arg) : (fail))
  136. #define G_THREAD_ECF(op, fail, mutex, type) \
  137. (g_thread_supported () ? \
  138. ((type(*)(GMutex*, const gulong, gchar const*)) \
  139. (*g_thread_functions_for_glib_use . op)) \
  140. (mutex, G_MUTEX_DEBUG_MAGIC, G_STRLOC) : (fail))
  141. #ifndef G_ERRORCHECK_MUTEXES
  142. # define g_mutex_lock(mutex) \
  143. G_THREAD_CF (mutex_lock, (void)0, (mutex))
  144. # define g_mutex_trylock(mutex) \
  145. G_THREAD_CF (mutex_trylock, TRUE, (mutex))
  146. # define g_mutex_unlock(mutex) \
  147. G_THREAD_CF (mutex_unlock, (void)0, (mutex))
  148. # define g_mutex_free(mutex) \
  149. G_THREAD_CF (mutex_free, (void)0, (mutex))
  150. # define g_cond_wait(cond, mutex) \
  151. G_THREAD_CF (cond_wait, (void)0, (cond, mutex))
  152. # define g_cond_timed_wait(cond, mutex, abs_time) \
  153. G_THREAD_CF (cond_timed_wait, TRUE, (cond, mutex, abs_time))
  154. #else /* G_ERRORCHECK_MUTEXES */
  155. # define g_mutex_lock(mutex) \
  156. G_THREAD_ECF (mutex_lock, (void)0, (mutex), void)
  157. # define g_mutex_trylock(mutex) \
  158. G_THREAD_ECF (mutex_trylock, TRUE, (mutex), gboolean)
  159. # define g_mutex_unlock(mutex) \
  160. G_THREAD_ECF (mutex_unlock, (void)0, (mutex), void)
  161. # define g_mutex_free(mutex) \
  162. G_THREAD_ECF (mutex_free, (void)0, (mutex), void)
  163. # define g_cond_wait(cond, mutex) \
  164. (g_thread_supported () ? ((void(*)(GCond*, GMutex*, gulong, gchar*))\
  165. g_thread_functions_for_glib_use.cond_wait) \
  166. (cond, mutex, G_MUTEX_DEBUG_MAGIC, G_STRLOC) : (void) 0)
  167. # define g_cond_timed_wait(cond, mutex, abs_time) \
  168. (g_thread_supported () ? \
  169. ((gboolean(*)(GCond*, GMutex*, GTimeVal*, gulong, gchar*)) \
  170. g_thread_functions_for_glib_use.cond_timed_wait) \
  171. (cond, mutex, abs_time, G_MUTEX_DEBUG_MAGIC, G_STRLOC) : TRUE)
  172. #endif /* G_ERRORCHECK_MUTEXES */
  173. #if defined(G_THREADS_ENABLED) && defined(G_THREADS_MANDATORY)
  174. #define g_thread_supported() 1
  175. #else
  176. #define g_thread_supported() (g_threads_got_initialized)
  177. #endif
  178. #define g_mutex_new() G_THREAD_UF (mutex_new, ())
  179. #define g_cond_new() G_THREAD_UF (cond_new, ())
  180. #define g_cond_signal(cond) G_THREAD_CF (cond_signal, (void)0, (cond))
  181. #define g_cond_broadcast(cond) G_THREAD_CF (cond_broadcast, (void)0, (cond))
  182. #define g_cond_free(cond) G_THREAD_CF (cond_free, (void)0, (cond))
  183. #define g_private_new(destructor) G_THREAD_UF (private_new, (destructor))
  184. #define g_private_get(private_key) G_THREAD_CF (private_get, \
  185. ((gpointer)private_key), \
  186. (private_key))
  187. #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
  188. (void) (private_key = \
  189. (GPrivate*) (value)), \
  190. (private_key, value))
  191. #define g_thread_yield() G_THREAD_CF (thread_yield, (void)0, ())
  192. #define g_thread_create(func, data, joinable, error) \
  193. (g_thread_create_full (func, data, 0, joinable, FALSE, \
  194. G_THREAD_PRIORITY_NORMAL, error))
  195. GThread* g_thread_create_full (GThreadFunc func,
  196. gpointer data,
  197. gulong stack_size,
  198. gboolean joinable,
  199. gboolean bound,
  200. GThreadPriority priority,
  201. GError **error);
  202. GThread* g_thread_self (void);
  203. void g_thread_exit (gpointer retval);
  204. gpointer g_thread_join (GThread *thread);
  205. void g_thread_set_priority (GThread *thread,
  206. GThreadPriority priority);
  207. /* GStaticMutexes can be statically initialized with the value
  208. * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
  209. * much easier, than having to explicitly allocate the mutex before
  210. * use
  211. */
  212. #define g_static_mutex_lock(mutex) \
  213. g_mutex_lock (g_static_mutex_get_mutex (mutex))
  214. #define g_static_mutex_trylock(mutex) \
  215. g_mutex_trylock (g_static_mutex_get_mutex (mutex))
  216. #define g_static_mutex_unlock(mutex) \
  217. g_mutex_unlock (g_static_mutex_get_mutex (mutex))
  218. void g_static_mutex_init (GStaticMutex *mutex);
  219. void g_static_mutex_free (GStaticMutex *mutex);
  220. struct _GStaticPrivate
  221. {
  222. /*< private >*/
  223. guint index;
  224. };
  225. #define G_STATIC_PRIVATE_INIT { 0 }
  226. void g_static_private_init (GStaticPrivate *private_key);
  227. gpointer g_static_private_get (GStaticPrivate *private_key);
  228. void g_static_private_set (GStaticPrivate *private_key,
  229. gpointer data,
  230. GDestroyNotify notify);
  231. void g_static_private_free (GStaticPrivate *private_key);
  232. typedef struct _GStaticRecMutex GStaticRecMutex;
  233. struct _GStaticRecMutex
  234. {
  235. /*< private >*/
  236. GStaticMutex mutex;
  237. guint depth;
  238. GSystemThread owner;
  239. };
  240. #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT }
  241. void g_static_rec_mutex_init (GStaticRecMutex *mutex);
  242. void g_static_rec_mutex_lock (GStaticRecMutex *mutex);
  243. gboolean g_static_rec_mutex_trylock (GStaticRecMutex *mutex);
  244. void g_static_rec_mutex_unlock (GStaticRecMutex *mutex);
  245. void g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
  246. guint depth);
  247. guint g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex);
  248. void g_static_rec_mutex_free (GStaticRecMutex *mutex);
  249. typedef struct _GStaticRWLock GStaticRWLock;
  250. struct _GStaticRWLock
  251. {
  252. /*< private >*/
  253. GStaticMutex mutex;
  254. GCond *read_cond;
  255. GCond *write_cond;
  256. guint read_counter;
  257. gboolean have_writer;
  258. guint want_to_read;
  259. guint want_to_write;
  260. };
  261. #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, 0, 0 }
  262. void g_static_rw_lock_init (GStaticRWLock* lock);
  263. void g_static_rw_lock_reader_lock (GStaticRWLock* lock);
  264. gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
  265. void g_static_rw_lock_reader_unlock (GStaticRWLock* lock);
  266. void g_static_rw_lock_writer_lock (GStaticRWLock* lock);
  267. gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
  268. void g_static_rw_lock_writer_unlock (GStaticRWLock* lock);
  269. void g_static_rw_lock_free (GStaticRWLock* lock);
  270. void g_thread_foreach (GFunc thread_func,
  271. gpointer user_data);
  272. typedef enum
  273. {
  274. G_ONCE_STATUS_NOTCALLED,
  275. G_ONCE_STATUS_PROGRESS,
  276. G_ONCE_STATUS_READY
  277. } GOnceStatus;
  278. typedef struct _GOnce GOnce;
  279. struct _GOnce
  280. {
  281. volatile GOnceStatus status;
  282. volatile gpointer retval;
  283. };
  284. #define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
  285. gpointer g_once_impl (GOnce *once, GThreadFunc func, gpointer arg);
  286. #ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
  287. # define g_once(once, func, arg) g_once_impl ((once), (func), (arg))
  288. #else /* !G_ATOMIC_OP_MEMORY_BARRIER_NEEDED*/
  289. # define g_once(once, func, arg) \
  290. (((once)->status == G_ONCE_STATUS_READY) ? \
  291. (once)->retval : \
  292. g_once_impl ((once), (func), (arg)))
  293. #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
  294. /* initialize-once guards, keyed by value_location */
  295. G_INLINE_FUNC gboolean g_once_init_enter (volatile gsize *value_location);
  296. gboolean g_once_init_enter_impl (volatile gsize *value_location);
  297. void g_once_init_leave (volatile gsize *value_location,
  298. gsize initialization_value);
  299. #if defined (G_CAN_INLINE) || defined (__G_THREAD_C__)
  300. G_INLINE_FUNC gboolean
  301. g_once_init_enter (volatile gsize *value_location)
  302. {
  303. if G_LIKELY ((gpointer) g_atomic_pointer_get (value_location) != NULL)
  304. return FALSE;
  305. else
  306. return g_once_init_enter_impl (value_location);
  307. }
  308. #endif /* G_CAN_INLINE || __G_THREAD_C__ */
  309. /* these are some convenience macros that expand to nothing if GLib
  310. * was configured with --disable-threads. for using StaticMutexes,
  311. * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
  312. * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
  313. * declare such an globally defined lock. name is a unique identifier
  314. * for the protected varibale or code portion. locking, testing and
  315. * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
  316. * G_TRYLOCK() respectively.
  317. */
  318. extern void glib_dummy_decl (void);
  319. #define G_LOCK_NAME(name) g__ ## name ## _lock
  320. #ifdef G_THREADS_ENABLED
  321. # define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
  322. # define G_LOCK_DEFINE(name) \
  323. GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT
  324. # define G_LOCK_EXTERN(name) extern GStaticMutex G_LOCK_NAME (name)
  325. # ifdef G_DEBUG_LOCKS
  326. # define G_LOCK(name) G_STMT_START{ \
  327. g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
  328. "file %s: line %d (%s): locking: %s ", \
  329. __FILE__, __LINE__, G_STRFUNC, \
  330. #name); \
  331. g_static_mutex_lock (&G_LOCK_NAME (name)); \
  332. }G_STMT_END
  333. # define G_UNLOCK(name) G_STMT_START{ \
  334. g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
  335. "file %s: line %d (%s): unlocking: %s ", \
  336. __FILE__, __LINE__, G_STRFUNC, \
  337. #name); \
  338. g_static_mutex_unlock (&G_LOCK_NAME (name)); \
  339. }G_STMT_END
  340. # define G_TRYLOCK(name) \
  341. (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
  342. "file %s: line %d (%s): try locking: %s ", \
  343. __FILE__, __LINE__, G_STRFUNC, \
  344. #name), g_static_mutex_trylock (&G_LOCK_NAME (name)))
  345. # else /* !G_DEBUG_LOCKS */
  346. # define G_LOCK(name) g_static_mutex_lock (&G_LOCK_NAME (name))
  347. # define G_UNLOCK(name) g_static_mutex_unlock (&G_LOCK_NAME (name))
  348. # define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
  349. # endif /* !G_DEBUG_LOCKS */
  350. #else /* !G_THREADS_ENABLED */
  351. # define G_LOCK_DEFINE_STATIC(name) extern void glib_dummy_decl (void)
  352. # define G_LOCK_DEFINE(name) extern void glib_dummy_decl (void)
  353. # define G_LOCK_EXTERN(name) extern void glib_dummy_decl (void)
  354. # define G_LOCK(name)
  355. # define G_UNLOCK(name)
  356. # define G_TRYLOCK(name) (TRUE)
  357. #endif /* !G_THREADS_ENABLED */
  358. G_END_DECLS
  359. #endif /* __G_THREAD_H__ */