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