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 typedef struct _GThread GThread;
55 typedef struct _GMutex GMutex;
56 typedef struct _GCond GCond;
57 typedef struct _GPrivate GPrivate;
58 typedef struct _GStaticPrivate GStaticPrivate;
62 #define G_MUTEX_INIT { NULL }
68 #define G_COND_INIT { NULL }
77 #define G_MUTEX_INIT { PTHREAD_MUTEX_INITIALIZER }
83 #define G_COND_INIT { PTHREAD_COND_INITIALIZER }
91 /* initializes the mutex/cond/private implementation for glib, might
92 * only be called once, and must not be called directly or indirectly
93 * from another glib-function, e.g. as a callback.
95 void g_thread_init (gpointer vtable);
97 /* Checks if thread support is initialized. Identical to the
98 * g_thread_supported macro but provided for language bindings.
100 gboolean g_thread_get_initialized (void);
102 GLIB_VAR gboolean g_threads_got_initialized;
104 /* internal function for fallback static mutex implementation */
105 GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex);
107 #if defined(G_THREADS_MANDATORY)
108 #define g_thread_supported() 1
110 #define g_thread_supported() (g_threads_got_initialized)
113 GThread *g_thread_create (GThreadFunc func,
118 GThread *g_thread_create_with_stack_size (GThreadFunc func,
124 GThread* g_thread_self (void);
125 void g_thread_exit (gpointer retval);
126 gpointer g_thread_join (GThread *thread);
127 void g_thread_yield (void);
129 void g_thread_foreach (GFunc thread_func,
133 typedef GMutex * GStaticMutex;
134 #define G_STATIC_MUTEX_INIT NULL
135 #define g_static_mutex_get_mutex g_static_mutex_get_mutex_impl
136 #else /* G_OS_WIN32 */
138 struct _GMutex *unused;
141 #define G_STATIC_MUTEX_INIT { NULL, G_MUTEX_INIT }
142 #define g_static_mutex_get_mutex(s) (&(s)->mutex)
143 #endif /* G_OS_WIN32 */
145 #define g_static_mutex_lock(mutex) \
146 g_mutex_lock (g_static_mutex_get_mutex (mutex))
147 #define g_static_mutex_trylock(mutex) \
148 g_mutex_trylock (g_static_mutex_get_mutex (mutex))
149 #define g_static_mutex_unlock(mutex) \
150 g_mutex_unlock (g_static_mutex_get_mutex (mutex))
151 void g_static_mutex_init (GStaticMutex *mutex);
152 void g_static_mutex_free (GStaticMutex *mutex);
154 struct _GStaticPrivate
159 #define G_STATIC_PRIVATE_INIT { 0 }
160 void g_static_private_init (GStaticPrivate *private_key);
161 gpointer g_static_private_get (GStaticPrivate *private_key);
162 void g_static_private_set (GStaticPrivate *private_key,
164 GDestroyNotify notify);
165 void g_static_private_free (GStaticPrivate *private_key);
167 typedef struct _GStaticRecMutex GStaticRecMutex;
168 struct _GStaticRecMutex
176 #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT, 0, {{0, 0, 0, 0}} }
177 void g_static_rec_mutex_init (GStaticRecMutex *mutex);
178 void g_static_rec_mutex_lock (GStaticRecMutex *mutex);
179 gboolean g_static_rec_mutex_trylock (GStaticRecMutex *mutex);
180 void g_static_rec_mutex_unlock (GStaticRecMutex *mutex);
181 void g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
183 guint g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex);
184 void g_static_rec_mutex_free (GStaticRecMutex *mutex);
186 typedef struct _GStaticRWLock GStaticRWLock;
187 struct _GStaticRWLock
194 gboolean have_writer;
199 #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, 0, 0 }
201 void g_static_rw_lock_init (GStaticRWLock* lock);
202 void g_static_rw_lock_reader_lock (GStaticRWLock* lock);
203 gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
204 void g_static_rw_lock_reader_unlock (GStaticRWLock* lock);
205 void g_static_rw_lock_writer_lock (GStaticRWLock* lock);
206 gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
207 void g_static_rw_lock_writer_unlock (GStaticRWLock* lock);
208 void g_static_rw_lock_free (GStaticRWLock* lock);
212 G_ONCE_STATUS_NOTCALLED,
213 G_ONCE_STATUS_PROGRESS,
217 typedef struct _GOnce GOnce;
220 volatile GOnceStatus status;
221 volatile gpointer retval;
224 #define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
226 gpointer g_once_impl (GOnce *once, GThreadFunc func, gpointer arg);
228 #ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
229 # define g_once(once, func, arg) g_once_impl ((once), (func), (arg))
230 #else /* !G_ATOMIC_OP_MEMORY_BARRIER_NEEDED*/
231 # define g_once(once, func, arg) \
232 (((once)->status == G_ONCE_STATUS_READY) ? \
234 g_once_impl ((once), (func), (arg)))
235 #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
237 /* initialize-once guards, keyed by value_location */
238 G_INLINE_FUNC gboolean g_once_init_enter (volatile gsize *value_location);
239 gboolean g_once_init_enter_impl (volatile gsize *value_location);
240 void g_once_init_leave (volatile gsize *value_location,
241 gsize initialization_value);
242 #if defined (G_CAN_INLINE) || defined (__G_THREAD_C__)
243 G_INLINE_FUNC gboolean
244 g_once_init_enter (volatile gsize *value_location)
246 if G_LIKELY ((gpointer) g_atomic_pointer_get (value_location) != NULL)
249 return g_once_init_enter_impl (value_location);
251 #endif /* G_CAN_INLINE || __G_THREAD_C__ */
253 /* these are some convenience macros that expand to nothing if GLib
254 * was configured with --disable-threads. for using StaticMutexes,
255 * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
256 * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
257 * declare such an globally defined lock. name is a unique identifier
258 * for the protected varibale or code portion. locking, testing and
259 * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
260 * G_TRYLOCK() respectively.
262 extern void glib_dummy_decl (void);
263 #define G_LOCK_NAME(name) g__ ## name ## _lock
264 #define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
265 #define G_LOCK_DEFINE(name) \
266 GMutex G_LOCK_NAME (name) = G_MUTEX_INIT
267 #define G_LOCK_EXTERN(name) extern GMutex G_LOCK_NAME (name)
270 # define G_LOCK(name) G_STMT_START{ \
271 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
272 "file %s: line %d (%s): locking: %s ", \
273 __FILE__, __LINE__, G_STRFUNC, \
275 g_mutex_lock (&G_LOCK_NAME (name)); \
277 # define G_UNLOCK(name) G_STMT_START{ \
278 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
279 "file %s: line %d (%s): unlocking: %s ", \
280 __FILE__, __LINE__, G_STRFUNC, \
282 g_mutex_unlock (&G_LOCK_NAME (name)); \
284 # define G_TRYLOCK(name) \
285 (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
286 "file %s: line %d (%s): try locking: %s ", \
287 __FILE__, __LINE__, G_STRFUNC, \
288 #name), g_mutex_trylock (&G_LOCK_NAME (name)))
289 #else /* !G_DEBUG_LOCKS */
290 # define G_LOCK(name) g_mutex_lock (&G_LOCK_NAME (name))
291 # define G_UNLOCK(name) g_mutex_unlock (&G_LOCK_NAME (name))
292 # define G_TRYLOCK(name) g_mutex_trylock (&G_LOCK_NAME (name))
293 #endif /* !G_DEBUG_LOCKS */
296 GMutex * g_mutex_new (void);
297 void g_mutex_free (GMutex *mutex);
298 void g_mutex_init (GMutex *mutex);
299 void g_mutex_clear (GMutex *mutex);
301 void g_mutex_lock (GMutex *mutex);
302 void g_mutex_unlock (GMutex *mutex);
303 gboolean g_mutex_trylock (GMutex *mutex);
305 GCond * g_cond_new (void);
306 void g_cond_free (GCond *cond);
307 void g_cond_init (GCond *cond);
308 void g_cond_clear (GCond *cond);
310 void g_cond_wait (GCond *cond,
312 void g_cond_signal (GCond *cond);
313 void g_cond_broadcast (GCond *cond);
314 gboolean g_cond_timed_wait (GCond *cond,
317 gboolean g_cond_timedwait (GCond *cond,
321 GPrivate * g_private_new (GDestroyNotify notify);
322 gpointer g_private_get (GPrivate *key);
323 void g_private_set (GPrivate *key,
328 #endif /* __G_THREAD_H__ */