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