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 _GRecMutex GRecMutex;
57 typedef struct _GRWLock GRWLock;
58 typedef struct _GCond GCond;
59 typedef struct _GPrivate GPrivate;
60 typedef struct _GStaticPrivate GStaticPrivate;
64 #define G_MUTEX_INIT { NULL }
70 #define G_RW_LOCK_INIT { NULL }
76 #define G_COND_INIT { NULL }
85 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
86 #define G_MUTEX_INIT { PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP }
88 #define G_MUTEX_INIT { PTHREAD_MUTEX_INITIALIZER }
95 #define G_RW_LOCK_INIT { PTHREAD_RWLOCK_INITIALIZER }
98 pthread_rwlock_t impl;
101 #define G_COND_INIT { PTHREAD_COND_INITIALIZER }
109 #define G_REC_MUTEX_INIT { NULL }
115 #define G_PRIVATE_INIT(notify) { NULL, (notify), { NULL, NULL } }
119 GDestroyNotify notify;
123 void g_thread_init (gpointer vtable);
125 gboolean g_thread_get_initialized (void);
127 GLIB_VAR gboolean g_threads_got_initialized;
129 #if defined(G_THREADS_MANDATORY)
130 #define g_thread_supported() 1
132 #define g_thread_supported() (g_threads_got_initialized)
135 GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex);
137 GThread *g_thread_new (const gchar *name,
143 GThread *g_thread_new_full (const gchar *name,
150 GThread* g_thread_self (void);
151 void g_thread_exit (gpointer retval);
152 gpointer g_thread_join (GThread *thread);
153 void g_thread_yield (void);
155 void g_thread_foreach (GFunc thread_func,
158 struct _GStaticPrivate
163 #define G_STATIC_PRIVATE_INIT { 0 }
164 void g_static_private_init (GStaticPrivate *private_key);
165 gpointer g_static_private_get (GStaticPrivate *private_key);
166 void g_static_private_set (GStaticPrivate *private_key,
168 GDestroyNotify notify);
169 void g_static_private_free (GStaticPrivate *private_key);
173 G_ONCE_STATUS_NOTCALLED,
174 G_ONCE_STATUS_PROGRESS,
178 typedef struct _GOnce GOnce;
181 volatile GOnceStatus status;
182 volatile gpointer retval;
185 #define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
187 gpointer g_once_impl (GOnce *once, GThreadFunc func, gpointer arg);
189 #ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
190 # define g_once(once, func, arg) g_once_impl ((once), (func), (arg))
191 #else /* !G_ATOMIC_OP_MEMORY_BARRIER_NEEDED*/
192 # define g_once(once, func, arg) \
193 (((once)->status == G_ONCE_STATUS_READY) ? \
195 g_once_impl ((once), (func), (arg)))
196 #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
198 /* initialize-once guards, keyed by value_location */
199 G_INLINE_FUNC gboolean g_once_init_enter (volatile gsize *value_location);
200 gboolean g_once_init_enter_impl (volatile gsize *value_location);
201 void g_once_init_leave (volatile gsize *value_location,
202 gsize initialization_value);
203 #if defined (G_CAN_INLINE) || defined (__G_THREAD_C__)
204 G_INLINE_FUNC gboolean
205 g_once_init_enter (volatile gsize *value_location)
207 if G_LIKELY ((gpointer) g_atomic_pointer_get (value_location) != NULL)
210 return g_once_init_enter_impl (value_location);
212 #endif /* G_CAN_INLINE || __G_THREAD_C__ */
214 #define G_LOCK_NAME(name) g__ ## name ## _lock
215 #define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
216 #define G_LOCK_DEFINE(name) \
217 GMutex G_LOCK_NAME (name) = G_MUTEX_INIT
218 #define G_LOCK_EXTERN(name) extern GMutex G_LOCK_NAME (name)
221 # define G_LOCK(name) G_STMT_START{ \
222 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
223 "file %s: line %d (%s): locking: %s ", \
224 __FILE__, __LINE__, G_STRFUNC, \
226 g_mutex_lock (&G_LOCK_NAME (name)); \
228 # define G_UNLOCK(name) G_STMT_START{ \
229 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
230 "file %s: line %d (%s): unlocking: %s ", \
231 __FILE__, __LINE__, G_STRFUNC, \
233 g_mutex_unlock (&G_LOCK_NAME (name)); \
235 # define G_TRYLOCK(name) \
236 (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
237 "file %s: line %d (%s): try locking: %s ", \
238 __FILE__, __LINE__, G_STRFUNC, \
239 #name), g_mutex_trylock (&G_LOCK_NAME (name)))
240 #else /* !G_DEBUG_LOCKS */
241 # define G_LOCK(name) g_mutex_lock (&G_LOCK_NAME (name))
242 # define G_UNLOCK(name) g_mutex_unlock (&G_LOCK_NAME (name))
243 # define G_TRYLOCK(name) g_mutex_trylock (&G_LOCK_NAME (name))
244 #endif /* !G_DEBUG_LOCKS */
247 GMutex * g_mutex_new (void);
248 void g_mutex_free (GMutex *mutex);
249 void g_mutex_init (GMutex *mutex);
250 void g_mutex_clear (GMutex *mutex);
252 void g_mutex_lock (GMutex *mutex);
253 void g_mutex_unlock (GMutex *mutex);
254 gboolean g_mutex_trylock (GMutex *mutex);
256 void g_rw_lock_init (GRWLock *rw_lock);
257 void g_rw_lock_clear (GRWLock *rw_lock);
258 void g_rw_lock_writer_lock (GRWLock *rw_lock);
259 gboolean g_rw_lock_writer_trylock (GRWLock *rw_lock);
260 void g_rw_lock_writer_unlock (GRWLock *rw_lock);
261 void g_rw_lock_reader_lock (GRWLock *rw_lock);
262 gboolean g_rw_lock_reader_trylock (GRWLock *rw_lock);
263 void g_rw_lock_reader_unlock (GRWLock *rw_lock);
265 void g_rec_mutex_init (GRecMutex *rec_mutex);
266 void g_rec_mutex_clear (GRecMutex *rec_mutex);
267 void g_rec_mutex_lock (GRecMutex *rec_mutex);
268 gboolean g_rec_mutex_trylock (GRecMutex *rec_mutex);
269 void g_rec_mutex_unlock (GRecMutex *rec_mutex);
271 GCond * g_cond_new (void);
272 void g_cond_free (GCond *cond);
273 void g_cond_init (GCond *cond);
274 void g_cond_clear (GCond *cond);
276 void g_cond_wait (GCond *cond,
278 void g_cond_signal (GCond *cond);
279 void g_cond_broadcast (GCond *cond);
280 gboolean g_cond_timed_wait (GCond *cond,
283 gboolean g_cond_timedwait (GCond *cond,
287 gpointer g_private_get (GPrivate *key);
288 void g_private_set (GPrivate *key,
290 void g_private_replace (GPrivate *key,
295 #endif /* __G_THREAD_H__ */