1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 #ifndef __G_THREAD_H__
28 #define __G_THREAD_H__
30 #include <glib/gerror.h>
31 #include <glib/gtypes.h>
32 #include <glib/gatomic.h> /* for g_atomic_pointer_get */
36 /* GLib Thread support
39 extern GQuark g_thread_error_quark (void);
40 #define G_THREAD_ERROR g_thread_error_quark ()
44 G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
47 typedef gpointer (*GThreadFunc) (gpointer data);
51 G_THREAD_PRIORITY_LOW,
52 G_THREAD_PRIORITY_NORMAL,
53 G_THREAD_PRIORITY_HIGH,
54 G_THREAD_PRIORITY_URGENT
57 typedef struct _GThread GThread;
64 GThreadPriority priority;
67 typedef struct _GMutex GMutex;
68 typedef struct _GCond GCond;
69 typedef struct _GPrivate GPrivate;
70 typedef struct _GStaticPrivate GStaticPrivate;
72 typedef struct _GThreadFunctions GThreadFunctions;
73 struct _GThreadFunctions
75 GMutex* (*mutex_new) (void);
76 void (*mutex_lock) (GMutex *mutex);
77 gboolean (*mutex_trylock) (GMutex *mutex);
78 void (*mutex_unlock) (GMutex *mutex);
79 void (*mutex_free) (GMutex *mutex);
80 GCond* (*cond_new) (void);
81 void (*cond_signal) (GCond *cond);
82 void (*cond_broadcast) (GCond *cond);
83 void (*cond_wait) (GCond *cond,
85 gboolean (*cond_timed_wait) (GCond *cond,
88 void (*cond_free) (GCond *cond);
89 GPrivate* (*private_new) (GDestroyNotify destructor);
90 gpointer (*private_get) (GPrivate *private_key);
91 void (*private_set) (GPrivate *private_key,
93 void (*thread_create) (GThreadFunc func,
98 GThreadPriority priority,
101 void (*thread_yield) (void);
102 void (*thread_join) (gpointer thread);
103 void (*thread_exit) (void);
104 void (*thread_set_priority)(gpointer thread,
105 GThreadPriority priority);
106 void (*thread_self) (gpointer thread);
107 gboolean (*thread_equal) (gpointer thread1,
111 GLIB_VAR GThreadFunctions g_thread_functions_for_glib_use;
112 GLIB_VAR gboolean g_thread_use_default_impl;
113 GLIB_VAR gboolean g_threads_got_initialized;
115 GLIB_VAR guint64 (*g_thread_gettime) (void);
117 /* initializes the mutex/cond/private implementation for glib, might
118 * only be called once, and must not be called directly or indirectly
119 * from another glib-function, e.g. as a callback.
121 void g_thread_init (GThreadFunctions *vtable);
123 /* Errorcheck mutexes. If you define G_ERRORCHECK_MUTEXES, then all
124 * mutexes will check for re-locking and re-unlocking */
126 /* Initialize thread system with errorcheck mutexes. vtable must be
127 * NULL. Do not call directly. Use #define G_ERRORCHECK_MUTEXES
130 void g_thread_init_with_errorcheck_mutexes (GThreadFunctions* vtable);
132 /* A random number to recognize debug calls to g_mutex_... */
133 #define G_MUTEX_DEBUG_MAGIC 0xf8e18ad7
135 #ifdef G_ERRORCHECK_MUTEXES
136 #define g_thread_init(vtable) g_thread_init_with_errorcheck_mutexes (vtable)
139 /* internal function for fallback static mutex implementation */
140 GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex);
142 #define g_static_mutex_get_mutex_impl_shortcut(mutex) \
143 (g_atomic_pointer_get ((gpointer*)(void*)mutex) ? *(mutex) : \
144 g_static_mutex_get_mutex_impl (mutex))
146 /* shorthands for conditional and unconditional function calls */
148 #define G_THREAD_UF(op, arglist) \
149 (*g_thread_functions_for_glib_use . op) arglist
150 #define G_THREAD_CF(op, fail, arg) \
151 (g_thread_supported () ? G_THREAD_UF (op, arg) : (fail))
152 #define G_THREAD_ECF(op, fail, mutex, type) \
153 (g_thread_supported () ? \
154 ((type(*)(GMutex*, const gulong, gchar const*)) \
155 (*g_thread_functions_for_glib_use . op)) \
156 (mutex, G_MUTEX_DEBUG_MAGIC, G_STRLOC) : (fail))
158 #ifndef G_ERRORCHECK_MUTEXES
159 # define g_mutex_lock(mutex) \
160 G_THREAD_CF (mutex_lock, (void)0, (mutex))
161 # define g_mutex_trylock(mutex) \
162 G_THREAD_CF (mutex_trylock, TRUE, (mutex))
163 # define g_mutex_unlock(mutex) \
164 G_THREAD_CF (mutex_unlock, (void)0, (mutex))
165 # define g_mutex_free(mutex) \
166 G_THREAD_CF (mutex_free, (void)0, (mutex))
167 # define g_cond_wait(cond, mutex) \
168 G_THREAD_CF (cond_wait, (void)0, (cond, mutex))
169 # define g_cond_timed_wait(cond, mutex, abs_time) \
170 G_THREAD_CF (cond_timed_wait, TRUE, (cond, mutex, abs_time))
171 #else /* G_ERRORCHECK_MUTEXES */
172 # define g_mutex_lock(mutex) \
173 G_THREAD_ECF (mutex_lock, (void)0, (mutex), void)
174 # define g_mutex_trylock(mutex) \
175 G_THREAD_ECF (mutex_trylock, TRUE, (mutex), gboolean)
176 # define g_mutex_unlock(mutex) \
177 G_THREAD_ECF (mutex_unlock, (void)0, (mutex), void)
178 # define g_mutex_free(mutex) \
179 G_THREAD_ECF (mutex_free, (void)0, (mutex), void)
180 # define g_cond_wait(cond, mutex) \
181 (g_thread_supported () ? ((void(*)(GCond*, GMutex*, gulong, gchar*))\
182 g_thread_functions_for_glib_use.cond_wait) \
183 (cond, mutex, G_MUTEX_DEBUG_MAGIC, G_STRLOC) : (void) 0)
184 # define g_cond_timed_wait(cond, mutex, abs_time) \
185 (g_thread_supported () ? \
186 ((gboolean(*)(GCond*, GMutex*, GTimeVal*, gulong, gchar*)) \
187 g_thread_functions_for_glib_use.cond_timed_wait) \
188 (cond, mutex, abs_time, G_MUTEX_DEBUG_MAGIC, G_STRLOC) : TRUE)
189 #endif /* G_ERRORCHECK_MUTEXES */
191 #define g_thread_supported() (g_threads_got_initialized)
192 #define g_mutex_new() G_THREAD_UF (mutex_new, ())
193 #define g_cond_new() G_THREAD_UF (cond_new, ())
194 #define g_cond_signal(cond) G_THREAD_CF (cond_signal, (void)0, (cond))
195 #define g_cond_broadcast(cond) G_THREAD_CF (cond_broadcast, (void)0, (cond))
196 #define g_cond_free(cond) G_THREAD_CF (cond_free, (void)0, (cond))
197 #define g_private_new(destructor) G_THREAD_UF (private_new, (destructor))
198 #define g_private_get(private_key) G_THREAD_CF (private_get, \
199 ((gpointer)private_key), \
201 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
202 (void) (private_key = \
203 (GPrivate*) (value)), \
204 (private_key, value))
205 #define g_thread_yield() G_THREAD_CF (thread_yield, (void)0, ())
207 #define g_thread_create(func, data, joinable, error) \
208 (g_thread_create_full (func, data, 0, joinable, FALSE, \
209 G_THREAD_PRIORITY_NORMAL, error))
211 GThread* g_thread_create_full (GThreadFunc func,
216 GThreadPriority priority,
218 GThread* g_thread_self (void);
219 void g_thread_exit (gpointer retval);
220 gpointer g_thread_join (GThread *thread);
222 void g_thread_set_priority (GThread *thread,
223 GThreadPriority priority);
225 /* GStaticMutexes can be statically initialized with the value
226 * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
227 * much easier, than having to explicitly allocate the mutex before
230 #define g_static_mutex_lock(mutex) \
231 g_mutex_lock (g_static_mutex_get_mutex (mutex))
232 #define g_static_mutex_trylock(mutex) \
233 g_mutex_trylock (g_static_mutex_get_mutex (mutex))
234 #define g_static_mutex_unlock(mutex) \
235 g_mutex_unlock (g_static_mutex_get_mutex (mutex))
236 void g_static_mutex_init (GStaticMutex *mutex);
237 void g_static_mutex_free (GStaticMutex *mutex);
239 struct _GStaticPrivate
244 #define G_STATIC_PRIVATE_INIT { 0 }
245 void g_static_private_init (GStaticPrivate *private_key);
246 gpointer g_static_private_get (GStaticPrivate *private_key);
247 void g_static_private_set (GStaticPrivate *private_key,
249 GDestroyNotify notify);
250 void g_static_private_free (GStaticPrivate *private_key);
252 typedef struct _GStaticRecMutex GStaticRecMutex;
253 struct _GStaticRecMutex
261 #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT }
262 void g_static_rec_mutex_init (GStaticRecMutex *mutex);
263 void g_static_rec_mutex_lock (GStaticRecMutex *mutex);
264 gboolean g_static_rec_mutex_trylock (GStaticRecMutex *mutex);
265 void g_static_rec_mutex_unlock (GStaticRecMutex *mutex);
266 void g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
268 guint g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex);
269 void g_static_rec_mutex_free (GStaticRecMutex *mutex);
271 typedef struct _GStaticRWLock GStaticRWLock;
272 struct _GStaticRWLock
279 gboolean have_writer;
284 #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, 0, 0 }
286 void g_static_rw_lock_init (GStaticRWLock* lock);
287 void g_static_rw_lock_reader_lock (GStaticRWLock* lock);
288 gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
289 void g_static_rw_lock_reader_unlock (GStaticRWLock* lock);
290 void g_static_rw_lock_writer_lock (GStaticRWLock* lock);
291 gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
292 void g_static_rw_lock_writer_unlock (GStaticRWLock* lock);
293 void g_static_rw_lock_free (GStaticRWLock* lock);
295 void g_thread_foreach (GFunc thread_func,
300 G_ONCE_STATUS_NOTCALLED,
301 G_ONCE_STATUS_PROGRESS,
305 typedef struct _GOnce GOnce;
308 volatile GOnceStatus status;
309 volatile gpointer retval;
312 #define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
314 gpointer g_once_impl (GOnce *once, GThreadFunc func, gpointer arg);
316 #ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
317 # define g_once(once, func, arg) g_once_impl ((once), (func), (arg))
318 #else /* !G_ATOMIC_OP_MEMORY_BARRIER_NEEDED*/
319 # define g_once(once, func, arg) \
320 (((once)->status == G_ONCE_STATUS_READY) ? \
322 g_once_impl ((once), (func), (arg)))
323 #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
325 /* these are some convenience macros that expand to nothing if GLib
326 * was configured with --disable-threads. for using StaticMutexes,
327 * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
328 * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
329 * declare such an globally defined lock. name is a unique identifier
330 * for the protected varibale or code portion. locking, testing and
331 * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
332 * G_TRYLOCK() respectively.
334 extern void glib_dummy_decl (void);
335 #define G_LOCK_NAME(name) g__ ## name ## _lock
336 #ifdef G_THREADS_ENABLED
337 # define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
338 # define G_LOCK_DEFINE(name) \
339 GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT
340 # define G_LOCK_EXTERN(name) extern GStaticMutex G_LOCK_NAME (name)
342 # ifdef G_DEBUG_LOCKS
343 # define G_LOCK(name) G_STMT_START{ \
344 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
345 "file %s: line %d (%s): locking: %s ", \
346 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
348 g_static_mutex_lock (&G_LOCK_NAME (name)); \
350 # define G_UNLOCK(name) G_STMT_START{ \
351 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
352 "file %s: line %d (%s): unlocking: %s ", \
353 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
355 g_static_mutex_unlock (&G_LOCK_NAME (name)); \
357 # define G_TRYLOCK(name) \
358 (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
359 "file %s: line %d (%s): try locking: %s ", \
360 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
361 #name), g_static_mutex_trylock (&G_LOCK_NAME (name)))
362 # else /* !G_DEBUG_LOCKS */
363 # define G_LOCK(name) g_static_mutex_lock (&G_LOCK_NAME (name))
364 # define G_UNLOCK(name) g_static_mutex_unlock (&G_LOCK_NAME (name))
365 # define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
366 # endif /* !G_DEBUG_LOCKS */
367 #else /* !G_THREADS_ENABLED */
368 # define G_LOCK_DEFINE_STATIC(name) extern void glib_dummy_decl (void)
369 # define G_LOCK_DEFINE(name) extern void glib_dummy_decl (void)
370 # define G_LOCK_EXTERN(name) extern void glib_dummy_decl (void)
371 # define G_LOCK(name)
372 # define G_UNLOCK(name)
373 # define G_TRYLOCK(name) (TRUE)
374 #endif /* !G_THREADS_ENABLED */
379 #endif /* __G_THREAD_H__ */