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 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
28 #error "Only <glib.h> can be included directly."
31 #ifndef __G_THREAD_H__
32 #define __G_THREAD_H__
34 #include <glib/gerror.h>
35 #include <glib/gutils.h> /* for G_INLINE_FUNC */
36 #include <glib/gatomic.h> /* for g_atomic_pointer_get */
40 /* GLib Thread support
43 extern GQuark g_thread_error_quark (void);
44 #define G_THREAD_ERROR g_thread_error_quark ()
48 G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
51 typedef gpointer (*GThreadFunc) (gpointer data);
53 /* This is "deprecated", but we can't actually remove it since
54 * g_thread_create_full takes it.
58 G_THREAD_PRIORITY_LOW,
59 G_THREAD_PRIORITY_NORMAL,
60 G_THREAD_PRIORITY_HIGH,
61 G_THREAD_PRIORITY_URGENT
64 typedef struct _GThread GThread;
71 GThreadPriority priority;
74 typedef struct _GMutex GMutex;
75 typedef struct _GCond GCond;
76 typedef struct _GPrivate GPrivate;
77 typedef struct _GStaticPrivate GStaticPrivate;
81 #define G_MUTEX_INIT { NULL }
87 #define G_COND_INIT { NULL }
96 #define G_MUTEX_INIT { PTHREAD_MUTEX_INITIALIZER }
102 #define G_COND_INIT { PTHREAD_COND_INITIALIZER }
110 typedef struct _GThreadFunctions GThreadFunctions;
111 struct _GThreadFunctions
113 GMutex* (*mutex_new) (void);
114 void (*mutex_lock) (GMutex *mutex);
115 gboolean (*mutex_trylock) (GMutex *mutex);
116 void (*mutex_unlock) (GMutex *mutex);
117 void (*mutex_free) (GMutex *mutex);
118 GCond* (*cond_new) (void);
119 void (*cond_signal) (GCond *cond);
120 void (*cond_broadcast) (GCond *cond);
121 void (*cond_wait) (GCond *cond,
123 gboolean (*cond_timed_wait) (GCond *cond,
126 void (*cond_free) (GCond *cond);
127 GPrivate* (*private_new) (GDestroyNotify destructor);
128 gpointer (*private_get) (GPrivate *private_key);
129 void (*private_set) (GPrivate *private_key,
131 void (*thread_create) (GThreadFunc func,
136 GThreadPriority priority,
139 void (*thread_yield) (void);
140 void (*thread_join) (gpointer thread);
141 void (*thread_exit) (void);
142 void (*thread_set_priority)(gpointer thread,
143 GThreadPriority priority);
144 void (*thread_self) (gpointer thread);
145 gboolean (*thread_equal) (gpointer thread1,
149 GLIB_VAR GThreadFunctions g_thread_functions_for_glib_use;
150 GLIB_VAR gboolean g_thread_use_default_impl;
151 GLIB_VAR gboolean g_threads_got_initialized;
153 #ifndef G_DISABLE_DEPRECATED
154 GLIB_VAR guint64 (*g_thread_gettime) (void);
157 /* initializes the mutex/cond/private implementation for glib, might
158 * only be called once, and must not be called directly or indirectly
159 * from another glib-function, e.g. as a callback.
161 void g_thread_init (GThreadFunctions *vtable);
163 /* Checks if thread support is initialized. Identical to the
164 * g_thread_supported macro but provided for language bindings.
166 gboolean g_thread_get_initialized (void);
168 /* A random number to recognize debug calls to g_mutex_... */
169 #define G_MUTEX_DEBUG_MAGIC 0xf8e18ad7
171 /* internal function for fallback static mutex implementation */
172 GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex);
174 #if defined(G_THREADS_MANDATORY)
175 #define g_thread_supported() 1
177 #define g_thread_supported() (g_threads_got_initialized)
180 #define g_thread_create(func, data, joinable, error) \
181 (g_thread_create_full (func, data, 0, joinable, FALSE, 0, error))
183 GThread* g_thread_create_full (GThreadFunc func,
188 GThreadPriority priority,
190 GThread* g_thread_self (void);
191 void g_thread_exit (gpointer retval);
192 gpointer g_thread_join (GThread *thread);
193 void g_thread_yield (void);
195 #ifndef G_DISABLE_DEPRECATED
196 void g_thread_set_priority (GThread *thread,
197 GThreadPriority priority);
201 typedef GMutex * GStaticMutex;
202 #define G_STATIC_MUTEX_INIT NULL
203 #define g_static_mutex_get_mutex g_static_mutex_get_mutex_impl
204 #else /* G_OS_WIN32 */
206 struct _GMutex *unused;
209 #define G_STATIC_MUTEX_INIT { NULL, G_MUTEX_INIT }
210 #define g_static_mutex_get_mutex(s) (&(s)->mutex)
211 #endif /* G_OS_WIN32 */
213 #define g_static_mutex_lock(mutex) \
214 g_mutex_lock (g_static_mutex_get_mutex (mutex))
215 #define g_static_mutex_trylock(mutex) \
216 g_mutex_trylock (g_static_mutex_get_mutex (mutex))
217 #define g_static_mutex_unlock(mutex) \
218 g_mutex_unlock (g_static_mutex_get_mutex (mutex))
219 void g_static_mutex_init (GStaticMutex *mutex);
220 void g_static_mutex_free (GStaticMutex *mutex);
222 struct _GStaticPrivate
227 #define G_STATIC_PRIVATE_INIT { 0 }
228 void g_static_private_init (GStaticPrivate *private_key);
229 gpointer g_static_private_get (GStaticPrivate *private_key);
230 void g_static_private_set (GStaticPrivate *private_key,
232 GDestroyNotify notify);
233 void g_static_private_free (GStaticPrivate *private_key);
235 typedef struct _GStaticRecMutex GStaticRecMutex;
236 struct _GStaticRecMutex
244 #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT, 0, {{0, 0, 0, 0}} }
245 void g_static_rec_mutex_init (GStaticRecMutex *mutex);
246 void g_static_rec_mutex_lock (GStaticRecMutex *mutex);
247 gboolean g_static_rec_mutex_trylock (GStaticRecMutex *mutex);
248 void g_static_rec_mutex_unlock (GStaticRecMutex *mutex);
249 void g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
251 guint g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex);
252 void g_static_rec_mutex_free (GStaticRecMutex *mutex);
254 typedef struct _GStaticRWLock GStaticRWLock;
255 struct _GStaticRWLock
262 gboolean have_writer;
267 #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, 0, 0 }
269 void g_static_rw_lock_init (GStaticRWLock* lock);
270 void g_static_rw_lock_reader_lock (GStaticRWLock* lock);
271 gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
272 void g_static_rw_lock_reader_unlock (GStaticRWLock* lock);
273 void g_static_rw_lock_writer_lock (GStaticRWLock* lock);
274 gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
275 void g_static_rw_lock_writer_unlock (GStaticRWLock* lock);
276 void g_static_rw_lock_free (GStaticRWLock* lock);
278 void g_thread_foreach (GFunc thread_func,
283 G_ONCE_STATUS_NOTCALLED,
284 G_ONCE_STATUS_PROGRESS,
288 typedef struct _GOnce GOnce;
291 volatile GOnceStatus status;
292 volatile gpointer retval;
295 #define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
297 gpointer g_once_impl (GOnce *once, GThreadFunc func, gpointer arg);
299 #ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
300 # define g_once(once, func, arg) g_once_impl ((once), (func), (arg))
301 #else /* !G_ATOMIC_OP_MEMORY_BARRIER_NEEDED*/
302 # define g_once(once, func, arg) \
303 (((once)->status == G_ONCE_STATUS_READY) ? \
305 g_once_impl ((once), (func), (arg)))
306 #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
308 /* initialize-once guards, keyed by value_location */
309 G_INLINE_FUNC gboolean g_once_init_enter (volatile gsize *value_location);
310 gboolean g_once_init_enter_impl (volatile gsize *value_location);
311 void g_once_init_leave (volatile gsize *value_location,
312 gsize initialization_value);
313 #if defined (G_CAN_INLINE) || defined (__G_THREAD_C__)
314 G_INLINE_FUNC gboolean
315 g_once_init_enter (volatile gsize *value_location)
317 if G_LIKELY ((gpointer) g_atomic_pointer_get (value_location) != NULL)
320 return g_once_init_enter_impl (value_location);
322 #endif /* G_CAN_INLINE || __G_THREAD_C__ */
324 /* these are some convenience macros that expand to nothing if GLib
325 * was configured with --disable-threads. for using StaticMutexes,
326 * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
327 * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
328 * declare such an globally defined lock. name is a unique identifier
329 * for the protected varibale or code portion. locking, testing and
330 * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
331 * G_TRYLOCK() respectively.
333 extern void glib_dummy_decl (void);
334 #define G_LOCK_NAME(name) g__ ## name ## _lock
335 #define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
336 #define G_LOCK_DEFINE(name) \
337 GMutex G_LOCK_NAME (name) = G_MUTEX_INIT
338 #define G_LOCK_EXTERN(name) extern GMutex G_LOCK_NAME (name)
341 # define G_LOCK(name) G_STMT_START{ \
342 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
343 "file %s: line %d (%s): locking: %s ", \
344 __FILE__, __LINE__, G_STRFUNC, \
346 g_mutex_lock (&G_LOCK_NAME (name)); \
348 # define G_UNLOCK(name) G_STMT_START{ \
349 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
350 "file %s: line %d (%s): unlocking: %s ", \
351 __FILE__, __LINE__, G_STRFUNC, \
353 g_mutex_unlock (&G_LOCK_NAME (name)); \
355 # define G_TRYLOCK(name) \
356 (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
357 "file %s: line %d (%s): try locking: %s ", \
358 __FILE__, __LINE__, G_STRFUNC, \
359 #name), g_mutex_trylock (&G_LOCK_NAME (name)))
360 #else /* !G_DEBUG_LOCKS */
361 # define G_LOCK(name) g_mutex_lock (&G_LOCK_NAME (name))
362 # define G_UNLOCK(name) g_mutex_unlock (&G_LOCK_NAME (name))
363 # define G_TRYLOCK(name) g_mutex_trylock (&G_LOCK_NAME (name))
364 #endif /* !G_DEBUG_LOCKS */
367 void g_mutex_init (GMutex *mutex);
368 void g_mutex_clear (GMutex *mutex);
370 void g_mutex_lock (GMutex *mutex);
371 void g_mutex_unlock (GMutex *mutex);
372 gboolean g_mutex_trylock (GMutex *mutex);
374 void g_cond_init (GCond *cond);
375 void g_cond_clear (GCond *cond);
377 void g_cond_wait (GCond *cond,
379 void g_cond_signal (GCond *cond);
380 void g_cond_broadcast (GCond *cond);
381 gboolean g_cond_timed_wait (GCond *cond,
384 gboolean g_cond_timedwait (GCond *cond,
388 GMutex * g_mutex_new (void);
389 void g_mutex_free (GMutex *mutex);
390 GCond * g_cond_new (void);
391 void g_cond_free (GCond *cond);
393 GPrivate * g_private_new (GDestroyNotify notify);
394 gpointer g_private_get (GPrivate *key);
395 void g_private_set (GPrivate *key,
400 #endif /* __G_THREAD_H__ */