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__
37 # ifdef GLIB_COMPILATION
38 # define GLIB_VAR __declspec(dllexport)
39 # else /* !GLIB_COMPILATION */
40 # define GLIB_VAR extern __declspec(dllimport)
41 # endif /* !GLIB_COMPILATION */
42 # else /* !G_OS_WIN32 */
43 # define GLIB_VAR extern
44 # endif /* !G_OS_WIN32 */
47 /* GLib Thread support
50 extern GQuark g_thread_error_quark();
51 #define G_THREAD_ERROR g_thread_error_quark()
55 G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
58 typedef void (*GThreadFunc) (gpointer value);
62 G_THREAD_PRIORITY_LOW,
63 G_THREAD_PRIORITY_NORMAL,
64 G_THREAD_PRIORITY_HIGH,
65 G_THREAD_PRIORITY_URGENT
68 typedef struct _GThread GThread;
71 GThreadPriority priority;
76 typedef struct _GMutex GMutex;
77 typedef struct _GCond GCond;
78 typedef struct _GPrivate GPrivate;
79 typedef struct _GStaticPrivate GStaticPrivate;
81 typedef struct _GThreadFunctions GThreadFunctions;
82 struct _GThreadFunctions
84 GMutex* (*mutex_new) (void);
85 void (*mutex_lock) (GMutex *mutex);
86 gboolean (*mutex_trylock) (GMutex *mutex);
87 void (*mutex_unlock) (GMutex *mutex);
88 void (*mutex_free) (GMutex *mutex);
89 GCond* (*cond_new) (void);
90 void (*cond_signal) (GCond *cond);
91 void (*cond_broadcast) (GCond *cond);
92 void (*cond_wait) (GCond *cond,
94 gboolean (*cond_timed_wait) (GCond *cond,
97 void (*cond_free) (GCond *cond);
98 GPrivate* (*private_new) (GDestroyNotify destructor);
99 gpointer (*private_get) (GPrivate *private_key);
100 void (*private_set) (GPrivate *private_key,
102 void (*thread_create) (GThreadFunc thread_func,
107 GThreadPriority priority,
110 void (*thread_yield) (void);
111 void (*thread_join) (gpointer thread);
112 void (*thread_exit) (void);
113 void (*thread_set_priority)(gpointer thread,
114 GThreadPriority priority);
115 void (*thread_self) (gpointer thread);
118 GLIB_VAR GThreadFunctions g_thread_functions_for_glib_use;
119 GLIB_VAR gboolean g_thread_use_default_impl;
120 GLIB_VAR gboolean g_threads_got_initialized;
122 /* initializes the mutex/cond/private implementation for glib, might
123 * only be called once, and must not be called directly or indirectly
124 * from another glib-function, e.g. as a callback.
126 void g_thread_init (GThreadFunctions *vtable);
128 /* Errorcheck mutexes. If you define G_ERRORCHECK_MUTEXES, then all
129 * mutexes will check for re-locking and re-unlocking */
131 /* Initialize thread system with errorcheck mutexes. vtable must be
132 * NULL.Do not call directly. Use #define G_ERRORCHECK_MUTEXES
135 void g_thread_init_with_errorcheck_mutexes (GThreadFunctions* vtable);
137 /* A random number to recognize debug calls to g_mutex_... */
138 #define G_MUTEX_DEBUG_MAGIC 0xf8e18ad7
140 #ifdef G_ERRORCHECK_MUTEXES
141 #define g_thread_init(vtable) g_thread_init_with_errorcheck_mutexes (vtable)
144 /* internal function for fallback static mutex implementation */
145 GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex);
147 /* 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, name, type) \
153 (g_thread_supported () ? \
154 ((type(*)(GMutex*, gulong, gchar*, gchar*)) \
155 (*g_thread_functions_for_glib_use . op)) \
156 (mutex, G_MUTEX_DEBUG_MAGIC, G_STRINGIFY (name), G_STRLOC) : (fail))
157 #ifndef G_ERRORCHECK_MUTEXES
158 #define g_mutex_lock_with_debug_name(mutex, name) \
159 G_THREAD_CF (mutex_lock, (void)0, (mutex))
160 #define g_mutex_trylock_with_debug_name(mutex, name) \
161 G_THREAD_CF (mutex_trylock, TRUE, (mutex))
162 #define g_mutex_unlock_with_debug_name(mutex, name) \
163 G_THREAD_CF (mutex_unlock, (void)0, (mutex))
164 #else /* G_ERRORCHECK_MUTEXES */
165 #define g_mutex_lock_with_debug_name(mutex, name) \
166 G_THREAD_ECF (mutex_lock, (void)0, mutex, name, void)
167 #define g_mutex_trylock_with_debug_name(mutex, name) \
168 G_THREAD_ECF (mutex_trylock, TRUE, mutex, name, gboolean)
169 #define g_mutex_unlock_with_debug_name(mutex, name) \
170 G_THREAD_ECF (mutex_unlock, (void)0, mutex, name, void)
171 #endif /* G_ERRORCHECK_MUTEXES */
173 #define g_thread_supported() (g_threads_got_initialized)
174 #define g_mutex_new() G_THREAD_UF (mutex_new, ())
175 #define g_mutex_lock(mutex) g_mutex_lock_with_debug_name(mutex, mutex)
176 #define g_mutex_trylock(mutex) g_mutex_trylock_with_debug_name(mutex, mutex)
177 #define g_mutex_unlock(mutex) g_mutex_unlock_with_debug_name(mutex, mutex)
178 #define g_mutex_free(mutex) G_THREAD_CF (mutex_free, (void)0, (mutex))
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_wait(cond, mutex) G_THREAD_CF (cond_wait, (void)0, (cond, \
184 #define g_cond_free(cond) G_THREAD_CF (cond_free, (void)0, (cond))
185 #define g_cond_timed_wait(cond, mutex, abs_time) G_THREAD_CF (cond_timed_wait, \
189 #define g_private_new(destructor) G_THREAD_UF (private_new, (destructor))
190 #define g_private_get(private_key) G_THREAD_CF (private_get, \
191 ((gpointer)private_key), \
193 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
194 (void) (private_key = \
195 (GPrivate*) (value)), \
196 (private_key, value))
197 #define g_thread_yield() G_THREAD_CF (thread_yield, (void)0, ())
198 #define g_thread_exit() G_THREAD_CF (thread_exit, (void)0, ())
200 GThread* g_thread_create (GThreadFunc thread_func,
205 GThreadPriority priority,
207 GThread* g_thread_self ();
208 void g_thread_join (GThread *thread);
209 void g_thread_set_priority (GThread *thread,
210 GThreadPriority priority);
212 /* GStaticMutexes can be statically initialized with the value
213 * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
214 * much easier, than having to explicitly allocate the mutex before
217 #define g_static_mutex_lock(mutex) \
218 g_mutex_lock_with_debug_name (g_static_mutex_get_mutex (mutex), mutex)
219 #define g_static_mutex_trylock(mutex) \
220 g_mutex_trylock_with_debug_name (g_static_mutex_get_mutex (mutex), mutex)
221 #define g_static_mutex_unlock(mutex) \
222 g_mutex_unlock_with_debug_name (g_static_mutex_get_mutex (mutex), mutex)
224 struct _GStaticPrivate
228 #define G_STATIC_PRIVATE_INIT { 0 }
229 gpointer g_static_private_get (GStaticPrivate *private_key);
230 void g_static_private_set (GStaticPrivate *private_key,
232 GDestroyNotify notify);
233 gpointer g_static_private_get_for_thread (GStaticPrivate *private_key,
235 void g_static_private_set_for_thread (GStaticPrivate *private_key,
238 GDestroyNotify notify);
240 typedef struct _GStaticRecMutex GStaticRecMutex;
241 struct _GStaticRecMutex
248 #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT }
249 void g_static_rec_mutex_lock (GStaticRecMutex *mutex);
250 gboolean g_static_rec_mutex_trylock (GStaticRecMutex *mutex);
251 void g_static_rec_mutex_unlock (GStaticRecMutex *mutex);
252 void g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
254 guint g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex);
256 typedef struct _GStaticRWLock GStaticRWLock;
257 struct _GStaticRWLock
267 #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, FALSE }
269 void g_static_rw_lock_reader_lock (GStaticRWLock* lock);
270 gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
271 void g_static_rw_lock_reader_unlock (GStaticRWLock* lock);
272 void g_static_rw_lock_writer_lock (GStaticRWLock* lock);
273 gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
274 void g_static_rw_lock_writer_unlock (GStaticRWLock* lock);
275 void g_static_rw_lock_free (GStaticRWLock* lock);
277 /* these are some convenience macros that expand to nothing if GLib
278 * was configured with --disable-threads. for using StaticMutexes,
279 * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
280 * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
281 * declare such an globally defined lock. name is a unique identifier
282 * for the protected varibale or code portion. locking, testing and
283 * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
284 * G_TRYLOCK() respectively.
286 extern void glib_dummy_decl (void);
287 #define G_LOCK_NAME(name) g__ ## name ## _lock
288 #ifdef G_THREADS_ENABLED
289 # define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
290 # define G_LOCK_DEFINE(name) \
291 GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT
292 # define G_LOCK_EXTERN(name) extern GStaticMutex G_LOCK_NAME (name)
294 # ifdef G_DEBUG_LOCKS
295 # define G_LOCK(name) G_STMT_START{ \
296 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
297 "file %s: line %d (%s): locking: %s ", \
298 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
300 g_static_mutex_lock (&G_LOCK_NAME (name)); \
302 # define G_UNLOCK(name) G_STMT_START{ \
303 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
304 "file %s: line %d (%s): unlocking: %s ", \
305 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
307 g_static_mutex_unlock (&G_LOCK_NAME (name)); \
309 # define G_TRYLOCK(name) \
310 (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
311 "file %s: line %d (%s): try locking: %s ", \
312 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
313 #name), g_static_mutex_trylock (&G_LOCK_NAME (name)))
314 # else /* !G_DEBUG_LOCKS */
315 # define G_LOCK(name) g_static_mutex_lock (&G_LOCK_NAME (name))
316 # define G_UNLOCK(name) g_static_mutex_unlock (&G_LOCK_NAME (name))
317 # define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
318 # endif /* !G_DEBUG_LOCKS */
319 #else /* !G_THREADS_ENABLED */
320 # define G_LOCK_DEFINE_STATIC(name) extern void glib_dummy_decl (void)
321 # define G_LOCK_DEFINE(name) extern void glib_dummy_decl (void)
322 # define G_LOCK_EXTERN(name) extern void glib_dummy_decl (void)
323 # define G_LOCK(name)
324 # define G_UNLOCK(name)
325 # define G_TRYLOCK(name) (TRUE)
326 #endif /* !G_THREADS_ENABLED */
330 #endif /* __G_THREAD_H__ */