Updated British English translation
[glib.git] / glib / gthread.h
blob9f5b34b547658c0a23fa9432ca9fda30ca003f17
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."
29 #endif
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 */
38 G_BEGIN_DECLS
40 /* GLib Thread support
43 extern GQuark g_thread_error_quark (void);
44 #define G_THREAD_ERROR g_thread_error_quark ()
46 typedef enum
48 G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
49 } GThreadError;
51 typedef gpointer (*GThreadFunc) (gpointer data);
53 typedef enum
55 G_THREAD_PRIORITY_LOW,
56 G_THREAD_PRIORITY_NORMAL,
57 G_THREAD_PRIORITY_HIGH,
58 G_THREAD_PRIORITY_URGENT
59 } GThreadPriority;
61 typedef struct _GThread GThread;
62 struct _GThread
64 /*< private >*/
65 GThreadFunc func;
66 gpointer data;
67 gboolean joinable;
68 GThreadPriority priority;
71 typedef struct _GMutex GMutex;
72 typedef struct _GCond GCond;
73 typedef struct _GPrivate GPrivate;
74 typedef struct _GStaticPrivate GStaticPrivate;
76 typedef struct _GThreadFunctions GThreadFunctions;
77 struct _GThreadFunctions
79 GMutex* (*mutex_new) (void);
80 void (*mutex_lock) (GMutex *mutex);
81 gboolean (*mutex_trylock) (GMutex *mutex);
82 void (*mutex_unlock) (GMutex *mutex);
83 void (*mutex_free) (GMutex *mutex);
84 GCond* (*cond_new) (void);
85 void (*cond_signal) (GCond *cond);
86 void (*cond_broadcast) (GCond *cond);
87 void (*cond_wait) (GCond *cond,
88 GMutex *mutex);
89 gboolean (*cond_timed_wait) (GCond *cond,
90 GMutex *mutex,
91 GTimeVal *end_time);
92 void (*cond_free) (GCond *cond);
93 GPrivate* (*private_new) (GDestroyNotify destructor);
94 gpointer (*private_get) (GPrivate *private_key);
95 void (*private_set) (GPrivate *private_key,
96 gpointer data);
97 void (*thread_create) (GThreadFunc func,
98 gpointer data,
99 gulong stack_size,
100 gboolean joinable,
101 gboolean bound,
102 GThreadPriority priority,
103 gpointer thread,
104 GError **error);
105 void (*thread_yield) (void);
106 void (*thread_join) (gpointer thread);
107 void (*thread_exit) (void);
108 void (*thread_set_priority)(gpointer thread,
109 GThreadPriority priority);
110 void (*thread_self) (gpointer thread);
111 gboolean (*thread_equal) (gpointer thread1,
112 gpointer thread2);
115 GLIB_VAR GThreadFunctions g_thread_functions_for_glib_use;
116 GLIB_VAR gboolean g_thread_use_default_impl;
117 GLIB_VAR gboolean g_threads_got_initialized;
119 GLIB_VAR guint64 (*g_thread_gettime) (void);
121 /* initializes the mutex/cond/private implementation for glib, might
122 * only be called once, and must not be called directly or indirectly
123 * from another glib-function, e.g. as a callback.
125 void g_thread_init (GThreadFunctions *vtable);
127 /* Errorcheck mutexes. If you define G_ERRORCHECK_MUTEXES, then all
128 * mutexes will check for re-locking and re-unlocking */
130 /* Initialize thread system with errorcheck mutexes. vtable must be
131 * NULL. Do not call directly. Use #define G_ERRORCHECK_MUTEXES
132 * instead.
134 void g_thread_init_with_errorcheck_mutexes (GThreadFunctions* vtable);
136 /* Checks if thread support is initialized. Identical to the
137 * g_thread_supported macro but provided for language bindings.
139 gboolean g_thread_get_initialized (void);
141 /* A random number to recognize debug calls to g_mutex_... */
142 #define G_MUTEX_DEBUG_MAGIC 0xf8e18ad7
144 #ifdef G_ERRORCHECK_MUTEXES
145 #define g_thread_init(vtable) g_thread_init_with_errorcheck_mutexes (vtable)
146 #endif
148 /* internal function for fallback static mutex implementation */
149 GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex);
151 #define g_static_mutex_get_mutex_impl_shortcut(mutex) \
152 (g_atomic_pointer_get (mutex) ? *(mutex) : \
153 g_static_mutex_get_mutex_impl (mutex))
155 /* shorthands for conditional and unconditional function calls */
157 #define G_THREAD_UF(op, arglist) \
158 (*g_thread_functions_for_glib_use . op) arglist
159 #define G_THREAD_CF(op, fail, arg) \
160 (g_thread_supported () ? G_THREAD_UF (op, arg) : (fail))
161 #define G_THREAD_ECF(op, fail, mutex, type) \
162 (g_thread_supported () ? \
163 ((type(*)(GMutex*, const gulong, gchar const*)) \
164 (*g_thread_functions_for_glib_use . op)) \
165 (mutex, G_MUTEX_DEBUG_MAGIC, G_STRLOC) : (fail))
167 #ifndef G_ERRORCHECK_MUTEXES
168 # define g_mutex_lock(mutex) \
169 G_THREAD_CF (mutex_lock, (void)0, (mutex))
170 # define g_mutex_trylock(mutex) \
171 G_THREAD_CF (mutex_trylock, TRUE, (mutex))
172 # define g_mutex_unlock(mutex) \
173 G_THREAD_CF (mutex_unlock, (void)0, (mutex))
174 # define g_mutex_free(mutex) \
175 G_THREAD_CF (mutex_free, (void)0, (mutex))
176 # define g_cond_wait(cond, mutex) \
177 G_THREAD_CF (cond_wait, (void)0, (cond, mutex))
178 # define g_cond_timed_wait(cond, mutex, abs_time) \
179 G_THREAD_CF (cond_timed_wait, TRUE, (cond, mutex, abs_time))
180 #else /* G_ERRORCHECK_MUTEXES */
181 # define g_mutex_lock(mutex) \
182 G_THREAD_ECF (mutex_lock, (void)0, (mutex), void)
183 # define g_mutex_trylock(mutex) \
184 G_THREAD_ECF (mutex_trylock, TRUE, (mutex), gboolean)
185 # define g_mutex_unlock(mutex) \
186 G_THREAD_ECF (mutex_unlock, (void)0, (mutex), void)
187 # define g_mutex_free(mutex) \
188 G_THREAD_ECF (mutex_free, (void)0, (mutex), void)
189 # define g_cond_wait(cond, mutex) \
190 (g_thread_supported () ? ((void(*)(GCond*, GMutex*, gulong, gchar*))\
191 g_thread_functions_for_glib_use.cond_wait) \
192 (cond, mutex, G_MUTEX_DEBUG_MAGIC, G_STRLOC) : (void) 0)
193 # define g_cond_timed_wait(cond, mutex, abs_time) \
194 (g_thread_supported () ? \
195 ((gboolean(*)(GCond*, GMutex*, GTimeVal*, gulong, gchar*)) \
196 g_thread_functions_for_glib_use.cond_timed_wait) \
197 (cond, mutex, abs_time, G_MUTEX_DEBUG_MAGIC, G_STRLOC) : TRUE)
198 #endif /* G_ERRORCHECK_MUTEXES */
200 #if defined(G_THREADS_ENABLED) && defined(G_THREADS_MANDATORY)
201 #define g_thread_supported() 1
202 #else
203 #define g_thread_supported() (g_threads_got_initialized)
204 #endif
205 #define g_mutex_new() G_THREAD_UF (mutex_new, ())
206 #define g_cond_new() G_THREAD_UF (cond_new, ())
207 #define g_cond_signal(cond) G_THREAD_CF (cond_signal, (void)0, (cond))
208 #define g_cond_broadcast(cond) G_THREAD_CF (cond_broadcast, (void)0, (cond))
209 #define g_cond_free(cond) G_THREAD_CF (cond_free, (void)0, (cond))
210 #define g_private_new(destructor) G_THREAD_UF (private_new, (destructor))
211 #define g_private_get(private_key) G_THREAD_CF (private_get, \
212 ((gpointer)private_key), \
213 (private_key))
214 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
215 (void) (private_key = \
216 (GPrivate*) (value)), \
217 (private_key, value))
218 #define g_thread_yield() G_THREAD_CF (thread_yield, (void)0, ())
220 #define g_thread_create(func, data, joinable, error) \
221 (g_thread_create_full (func, data, 0, joinable, FALSE, \
222 G_THREAD_PRIORITY_NORMAL, error))
224 GThread* g_thread_create_full (GThreadFunc func,
225 gpointer data,
226 gulong stack_size,
227 gboolean joinable,
228 gboolean bound,
229 GThreadPriority priority,
230 GError **error);
231 GThread* g_thread_self (void);
232 void g_thread_exit (gpointer retval);
233 gpointer g_thread_join (GThread *thread);
235 void g_thread_set_priority (GThread *thread,
236 GThreadPriority priority);
238 /* GStaticMutexes can be statically initialized with the value
239 * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
240 * much easier, than having to explicitly allocate the mutex before
241 * use
243 #define g_static_mutex_lock(mutex) \
244 g_mutex_lock (g_static_mutex_get_mutex (mutex))
245 #define g_static_mutex_trylock(mutex) \
246 g_mutex_trylock (g_static_mutex_get_mutex (mutex))
247 #define g_static_mutex_unlock(mutex) \
248 g_mutex_unlock (g_static_mutex_get_mutex (mutex))
249 void g_static_mutex_init (GStaticMutex *mutex);
250 void g_static_mutex_free (GStaticMutex *mutex);
252 struct _GStaticPrivate
254 /*< private >*/
255 guint index;
257 #define G_STATIC_PRIVATE_INIT { 0 }
258 void g_static_private_init (GStaticPrivate *private_key);
259 gpointer g_static_private_get (GStaticPrivate *private_key);
260 void g_static_private_set (GStaticPrivate *private_key,
261 gpointer data,
262 GDestroyNotify notify);
263 void g_static_private_free (GStaticPrivate *private_key);
265 typedef struct _GStaticRecMutex GStaticRecMutex;
266 struct _GStaticRecMutex
268 /*< private >*/
269 GStaticMutex mutex;
270 guint depth;
271 GSystemThread owner;
274 #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT, 0, {{0, 0, 0, 0}} }
275 void g_static_rec_mutex_init (GStaticRecMutex *mutex);
276 void g_static_rec_mutex_lock (GStaticRecMutex *mutex);
277 gboolean g_static_rec_mutex_trylock (GStaticRecMutex *mutex);
278 void g_static_rec_mutex_unlock (GStaticRecMutex *mutex);
279 void g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
280 guint depth);
281 guint g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex);
282 void g_static_rec_mutex_free (GStaticRecMutex *mutex);
284 typedef struct _GStaticRWLock GStaticRWLock;
285 struct _GStaticRWLock
287 /*< private >*/
288 GStaticMutex mutex;
289 GCond *read_cond;
290 GCond *write_cond;
291 guint read_counter;
292 gboolean have_writer;
293 guint want_to_read;
294 guint want_to_write;
297 #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, 0, 0 }
299 void g_static_rw_lock_init (GStaticRWLock* lock);
300 void g_static_rw_lock_reader_lock (GStaticRWLock* lock);
301 gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
302 void g_static_rw_lock_reader_unlock (GStaticRWLock* lock);
303 void g_static_rw_lock_writer_lock (GStaticRWLock* lock);
304 gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
305 void g_static_rw_lock_writer_unlock (GStaticRWLock* lock);
306 void g_static_rw_lock_free (GStaticRWLock* lock);
308 void g_thread_foreach (GFunc thread_func,
309 gpointer user_data);
311 typedef enum
313 G_ONCE_STATUS_NOTCALLED,
314 G_ONCE_STATUS_PROGRESS,
315 G_ONCE_STATUS_READY
316 } GOnceStatus;
318 typedef struct _GOnce GOnce;
319 struct _GOnce
321 volatile GOnceStatus status;
322 volatile gpointer retval;
325 #define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
327 gpointer g_once_impl (GOnce *once, GThreadFunc func, gpointer arg);
329 #ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
330 # define g_once(once, func, arg) g_once_impl ((once), (func), (arg))
331 #else /* !G_ATOMIC_OP_MEMORY_BARRIER_NEEDED*/
332 # define g_once(once, func, arg) \
333 (((once)->status == G_ONCE_STATUS_READY) ? \
334 (once)->retval : \
335 g_once_impl ((once), (func), (arg)))
336 #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
338 /* initialize-once guards, keyed by value_location */
339 G_INLINE_FUNC gboolean g_once_init_enter (volatile gsize *value_location);
340 gboolean g_once_init_enter_impl (volatile gsize *value_location);
341 void g_once_init_leave (volatile gsize *value_location,
342 gsize initialization_value);
343 #if defined (G_CAN_INLINE) || defined (__G_THREAD_C__)
344 G_INLINE_FUNC gboolean
345 g_once_init_enter (volatile gsize *value_location)
347 if G_LIKELY ((gpointer) g_atomic_pointer_get (value_location) != NULL)
348 return FALSE;
349 else
350 return g_once_init_enter_impl (value_location);
352 #endif /* G_CAN_INLINE || __G_THREAD_C__ */
354 /* these are some convenience macros that expand to nothing if GLib
355 * was configured with --disable-threads. for using StaticMutexes,
356 * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
357 * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
358 * declare such an globally defined lock. name is a unique identifier
359 * for the protected varibale or code portion. locking, testing and
360 * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
361 * G_TRYLOCK() respectively.
363 extern void glib_dummy_decl (void);
364 #define G_LOCK_NAME(name) g__ ## name ## _lock
365 #ifdef G_THREADS_ENABLED
366 # define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
367 # define G_LOCK_DEFINE(name) \
368 GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT
369 # define G_LOCK_EXTERN(name) extern GStaticMutex G_LOCK_NAME (name)
371 # ifdef G_DEBUG_LOCKS
372 # define G_LOCK(name) G_STMT_START{ \
373 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
374 "file %s: line %d (%s): locking: %s ", \
375 __FILE__, __LINE__, G_STRFUNC, \
376 #name); \
377 g_static_mutex_lock (&G_LOCK_NAME (name)); \
378 }G_STMT_END
379 # define G_UNLOCK(name) G_STMT_START{ \
380 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
381 "file %s: line %d (%s): unlocking: %s ", \
382 __FILE__, __LINE__, G_STRFUNC, \
383 #name); \
384 g_static_mutex_unlock (&G_LOCK_NAME (name)); \
385 }G_STMT_END
386 # define G_TRYLOCK(name) \
387 (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
388 "file %s: line %d (%s): try locking: %s ", \
389 __FILE__, __LINE__, G_STRFUNC, \
390 #name), g_static_mutex_trylock (&G_LOCK_NAME (name)))
391 # else /* !G_DEBUG_LOCKS */
392 # define G_LOCK(name) g_static_mutex_lock (&G_LOCK_NAME (name))
393 # define G_UNLOCK(name) g_static_mutex_unlock (&G_LOCK_NAME (name))
394 # define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
395 # endif /* !G_DEBUG_LOCKS */
396 #else /* !G_THREADS_ENABLED */
397 # define G_LOCK_DEFINE_STATIC(name) extern void glib_dummy_decl (void)
398 # define G_LOCK_DEFINE(name) extern void glib_dummy_decl (void)
399 # define G_LOCK_EXTERN(name) extern void glib_dummy_decl (void)
400 # define G_LOCK(name)
401 # define G_UNLOCK(name)
402 # define G_TRYLOCK(name) (TRUE)
403 #endif /* !G_THREADS_ENABLED */
405 G_END_DECLS
407 #endif /* __G_THREAD_H__ */