GThread: remove errorcheck mutex support
[platform/upstream/glib.git] / glib / gthread.h
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/.
25  */
26
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
30
31 #ifndef __G_THREAD_H__
32 #define __G_THREAD_H__
33
34 #include <glib/gerror.h>
35 #include <glib/gutils.h>        /* for G_INLINE_FUNC */
36 #include <glib/gatomic.h>       /* for g_atomic_pointer_get */
37
38 G_BEGIN_DECLS
39
40 /* GLib Thread support
41  */
42
43 extern GQuark g_thread_error_quark (void);
44 #define G_THREAD_ERROR g_thread_error_quark ()
45
46 typedef enum
47 {
48   G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
49 } GThreadError;
50
51 typedef gpointer (*GThreadFunc) (gpointer data);
52
53 typedef enum
54 {
55   G_THREAD_PRIORITY_LOW,
56   G_THREAD_PRIORITY_NORMAL,
57   G_THREAD_PRIORITY_HIGH,
58   G_THREAD_PRIORITY_URGENT
59 } GThreadPriority;
60
61 typedef struct _GThread         GThread;
62 struct  _GThread
63 {
64   /*< private >*/
65   GThreadFunc func;
66   gpointer data;
67   gboolean joinable;
68   GThreadPriority priority;
69 };
70
71 typedef struct _GMutex          GMutex;
72 typedef struct _GCond           GCond;
73 typedef struct _GPrivate        GPrivate;
74 typedef struct _GStaticPrivate  GStaticPrivate;
75
76 typedef struct _GThreadFunctions GThreadFunctions;
77 struct _GThreadFunctions
78 {
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);
113 };
114
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;
118
119 #ifndef G_DISABLE_DEPRECATED
120 GLIB_VAR guint64   (*g_thread_gettime) (void);
121 #endif
122
123 /* initializes the mutex/cond/private implementation for glib, might
124  * only be called once, and must not be called directly or indirectly
125  * from another glib-function, e.g. as a callback.
126  */
127 void    g_thread_init   (GThreadFunctions       *vtable);
128
129 /* Checks if thread support is initialized.  Identical to the
130  * g_thread_supported macro but provided for language bindings.
131  */
132 gboolean g_thread_get_initialized (void);
133
134 /* A random number to recognize debug calls to g_mutex_... */
135 #define G_MUTEX_DEBUG_MAGIC 0xf8e18ad7
136
137 /* internal function for fallback static mutex implementation */
138 GMutex* g_static_mutex_get_mutex_impl   (GMutex **mutex);
139
140 #define g_static_mutex_get_mutex_impl_shortcut(mutex) \
141   (g_atomic_pointer_get (mutex) ? *(mutex) : \
142    g_static_mutex_get_mutex_impl (mutex))
143
144 /* shorthands for conditional and unconditional function calls */
145
146 #define G_THREAD_UF(op, arglist)                                        \
147     (*g_thread_functions_for_glib_use . op) arglist
148 #define G_THREAD_CF(op, fail, arg)                                      \
149     (g_thread_supported () ? G_THREAD_UF (op, arg) : (fail))
150 #define G_THREAD_ECF(op, fail, mutex, type)                             \
151     (g_thread_supported () ?                                            \
152       ((type(*)(GMutex*, const gulong, gchar const*))                   \
153       (*g_thread_functions_for_glib_use . op))                          \
154      (mutex, G_MUTEX_DEBUG_MAGIC, G_STRLOC) : (fail))
155
156 #ifndef G_ERRORCHECK_MUTEXES
157 # define g_mutex_lock(mutex)                                            \
158     G_THREAD_CF (mutex_lock,     (void)0, (mutex))
159 # define g_mutex_trylock(mutex)                                         \
160     G_THREAD_CF (mutex_trylock,  TRUE,    (mutex))
161 # define g_mutex_unlock(mutex)                                          \
162     G_THREAD_CF (mutex_unlock,   (void)0, (mutex))
163 # define g_mutex_free(mutex)                                            \
164     G_THREAD_CF (mutex_free,     (void)0, (mutex))
165 # define g_cond_wait(cond, mutex)                                       \
166     G_THREAD_CF (cond_wait,      (void)0, (cond, mutex))
167 # define g_cond_timed_wait(cond, mutex, abs_time)                       \
168     G_THREAD_CF (cond_timed_wait, TRUE,   (cond, mutex, abs_time))
169 #else /* G_ERRORCHECK_MUTEXES */
170 # define g_mutex_lock(mutex)                                            \
171     G_THREAD_ECF (mutex_lock,    (void)0, (mutex), void)
172 # define g_mutex_trylock(mutex)                                         \
173     G_THREAD_ECF (mutex_trylock, TRUE,    (mutex), gboolean)
174 # define g_mutex_unlock(mutex)                                          \
175     G_THREAD_ECF (mutex_unlock,  (void)0, (mutex), void)
176 # define g_mutex_free(mutex)                                            \
177     G_THREAD_ECF (mutex_free,    (void)0, (mutex), void)
178 # define g_cond_wait(cond, mutex)                                       \
179     (g_thread_supported () ? ((void(*)(GCond*, GMutex*, gulong, gchar*))\
180       g_thread_functions_for_glib_use.cond_wait)                        \
181         (cond, mutex, G_MUTEX_DEBUG_MAGIC, G_STRLOC) : (void) 0)
182 # define g_cond_timed_wait(cond, mutex, abs_time)                       \
183     (g_thread_supported () ?                                            \
184       ((gboolean(*)(GCond*, GMutex*, GTimeVal*, gulong, gchar*))        \
185         g_thread_functions_for_glib_use.cond_timed_wait)                \
186           (cond, mutex, abs_time, G_MUTEX_DEBUG_MAGIC, G_STRLOC) : TRUE)
187 #endif /* G_ERRORCHECK_MUTEXES */
188
189 #if defined(G_THREADS_MANDATORY)
190 #define g_thread_supported()     1
191 #else
192 #define g_thread_supported()    (g_threads_got_initialized)
193 #endif
194 #define g_mutex_new()            G_THREAD_UF (mutex_new,      ())
195 #define g_cond_new()             G_THREAD_UF (cond_new,       ())
196 #define g_cond_signal(cond)      G_THREAD_CF (cond_signal,    (void)0, (cond))
197 #define g_cond_broadcast(cond)   G_THREAD_CF (cond_broadcast, (void)0, (cond))
198 #define g_cond_free(cond)        G_THREAD_CF (cond_free,      (void)0, (cond))
199 #define g_private_new(destructor) G_THREAD_UF (private_new, (destructor))
200 #define g_private_get(private_key) G_THREAD_CF (private_get, \
201                                                 ((gpointer)private_key), \
202                                                 (private_key))
203 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
204                                                        (void) (private_key = \
205                                                         (GPrivate*) (value)), \
206                                                        (private_key, value))
207 #define g_thread_yield()              G_THREAD_CF (thread_yield, (void)0, ())
208
209 #define g_thread_create(func, data, joinable, error)                    \
210   (g_thread_create_full (func, data, 0, joinable, FALSE,                \
211                          G_THREAD_PRIORITY_NORMAL, error))
212
213 GThread* g_thread_create_full  (GThreadFunc            func,
214                                 gpointer               data,
215                                 gulong                 stack_size,
216                                 gboolean               joinable,
217                                 gboolean               bound,
218                                 GThreadPriority        priority,
219                                 GError               **error);
220 GThread* g_thread_self         (void);
221 void     g_thread_exit         (gpointer               retval);
222 gpointer g_thread_join         (GThread               *thread);
223
224 void     g_thread_set_priority (GThread               *thread,
225                                 GThreadPriority        priority);
226
227 /* GStaticMutexes can be statically initialized with the value
228  * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
229  * much easier, than having to explicitly allocate the mutex before
230  * use
231  */
232 #define g_static_mutex_lock(mutex) \
233     g_mutex_lock (g_static_mutex_get_mutex (mutex))
234 #define g_static_mutex_trylock(mutex) \
235     g_mutex_trylock (g_static_mutex_get_mutex (mutex))
236 #define g_static_mutex_unlock(mutex) \
237     g_mutex_unlock (g_static_mutex_get_mutex (mutex))
238 void g_static_mutex_init (GStaticMutex *mutex);
239 void g_static_mutex_free (GStaticMutex *mutex);
240
241 struct _GStaticPrivate
242 {
243   /*< private >*/
244   guint index;
245 };
246 #define G_STATIC_PRIVATE_INIT { 0 }
247 void     g_static_private_init           (GStaticPrivate   *private_key);
248 gpointer g_static_private_get            (GStaticPrivate   *private_key);
249 void     g_static_private_set            (GStaticPrivate   *private_key,
250                                           gpointer          data,
251                                           GDestroyNotify    notify);
252 void     g_static_private_free           (GStaticPrivate   *private_key);
253
254 typedef struct _GStaticRecMutex GStaticRecMutex;
255 struct _GStaticRecMutex
256 {
257   /*< private >*/
258   GStaticMutex mutex;
259   guint depth;
260   GSystemThread owner;
261 };
262
263 #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT, 0, {{0, 0, 0, 0}} }
264 void     g_static_rec_mutex_init        (GStaticRecMutex *mutex);
265 void     g_static_rec_mutex_lock        (GStaticRecMutex *mutex);
266 gboolean g_static_rec_mutex_trylock     (GStaticRecMutex *mutex);
267 void     g_static_rec_mutex_unlock      (GStaticRecMutex *mutex);
268 void     g_static_rec_mutex_lock_full   (GStaticRecMutex *mutex,
269                                          guint            depth);
270 guint    g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex);
271 void     g_static_rec_mutex_free        (GStaticRecMutex *mutex);
272
273 typedef struct _GStaticRWLock GStaticRWLock;
274 struct _GStaticRWLock
275 {
276   /*< private >*/
277   GStaticMutex mutex;
278   GCond *read_cond;
279   GCond *write_cond;
280   guint read_counter;
281   gboolean have_writer;
282   guint want_to_read;
283   guint want_to_write;
284 };
285
286 #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, 0, 0 }
287
288 void      g_static_rw_lock_init           (GStaticRWLock* lock);
289 void      g_static_rw_lock_reader_lock    (GStaticRWLock* lock);
290 gboolean  g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
291 void      g_static_rw_lock_reader_unlock  (GStaticRWLock* lock);
292 void      g_static_rw_lock_writer_lock    (GStaticRWLock* lock);
293 gboolean  g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
294 void      g_static_rw_lock_writer_unlock  (GStaticRWLock* lock);
295 void      g_static_rw_lock_free           (GStaticRWLock* lock);
296
297 void      g_thread_foreach                (GFunc          thread_func,
298                                            gpointer       user_data);
299
300 typedef enum
301 {
302   G_ONCE_STATUS_NOTCALLED,
303   G_ONCE_STATUS_PROGRESS,
304   G_ONCE_STATUS_READY  
305 } GOnceStatus;
306
307 typedef struct _GOnce GOnce;
308 struct _GOnce
309 {
310   volatile GOnceStatus status;
311   volatile gpointer retval;
312 };
313
314 #define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
315
316 gpointer g_once_impl (GOnce *once, GThreadFunc func, gpointer arg);
317
318 #ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
319 # define g_once(once, func, arg) g_once_impl ((once), (func), (arg))
320 #else /* !G_ATOMIC_OP_MEMORY_BARRIER_NEEDED*/
321 # define g_once(once, func, arg) \
322   (((once)->status == G_ONCE_STATUS_READY) ? \
323    (once)->retval : \
324    g_once_impl ((once), (func), (arg)))
325 #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
326
327 /* initialize-once guards, keyed by value_location */
328 G_INLINE_FUNC gboolean  g_once_init_enter       (volatile gsize *value_location);
329 gboolean                g_once_init_enter_impl  (volatile gsize *value_location);
330 void                    g_once_init_leave       (volatile gsize *value_location,
331                                                  gsize           initialization_value);
332 #if defined (G_CAN_INLINE) || defined (__G_THREAD_C__)
333 G_INLINE_FUNC gboolean
334 g_once_init_enter (volatile gsize *value_location)
335 {
336   if G_LIKELY ((gpointer) g_atomic_pointer_get (value_location) != NULL)
337     return FALSE;
338   else
339     return g_once_init_enter_impl (value_location);
340 }
341 #endif /* G_CAN_INLINE || __G_THREAD_C__ */
342
343 /* these are some convenience macros that expand to nothing if GLib
344  * was configured with --disable-threads. for using StaticMutexes,
345  * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
346  * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
347  * declare such an globally defined lock. name is a unique identifier
348  * for the protected varibale or code portion. locking, testing and
349  * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
350  * G_TRYLOCK() respectively.
351  */
352 extern void glib_dummy_decl (void);
353 #define G_LOCK_NAME(name)               g__ ## name ## _lock
354 #define G_LOCK_DEFINE_STATIC(name)    static G_LOCK_DEFINE (name)
355 #define G_LOCK_DEFINE(name)           \
356   GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT
357 #define G_LOCK_EXTERN(name)           extern GStaticMutex G_LOCK_NAME (name)
358
359 #ifdef G_DEBUG_LOCKS
360 #  define G_LOCK(name)                G_STMT_START{             \
361       g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
362              "file %s: line %d (%s): locking: %s ",             \
363              __FILE__,        __LINE__, G_STRFUNC,              \
364              #name);                                            \
365       g_static_mutex_lock (&G_LOCK_NAME (name));                \
366    }G_STMT_END
367 #  define G_UNLOCK(name)              G_STMT_START{             \
368       g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
369              "file %s: line %d (%s): unlocking: %s ",           \
370              __FILE__,        __LINE__, G_STRFUNC,              \
371              #name);                                            \
372      g_static_mutex_unlock (&G_LOCK_NAME (name));               \
373    }G_STMT_END
374 #  define G_TRYLOCK(name)                                       \
375       (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                  \
376              "file %s: line %d (%s): try locking: %s ",         \
377              __FILE__,        __LINE__, G_STRFUNC,              \
378              #name), g_static_mutex_trylock (&G_LOCK_NAME (name)))
379 #else  /* !G_DEBUG_LOCKS */
380 #  define G_LOCK(name) g_static_mutex_lock       (&G_LOCK_NAME (name))
381 #  define G_UNLOCK(name) g_static_mutex_unlock   (&G_LOCK_NAME (name))
382 #  define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
383 #endif /* !G_DEBUG_LOCKS */
384
385 G_END_DECLS
386
387 #endif /* __G_THREAD_H__ */