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