Add a new recursive mutex type, GRecMutex
[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 struct _GThread         GThread;
54
55 typedef struct _GMutex          GMutex;
56 typedef struct _GRecMutex       GRecMutex;
57 typedef struct _GRWLock         GRWLock;
58 typedef struct _GCond           GCond;
59 typedef struct _GPrivate        GPrivate;
60 typedef struct _GStaticPrivate  GStaticPrivate;
61
62 #ifdef G_OS_WIN32
63
64 #define G_MUTEX_INIT { NULL }
65 struct _GMutex
66 {
67   gpointer impl;
68 };
69
70 #define G_RW_LOCK_INIT { NULL }
71 struct _GRWLock
72 {
73   gpointer impl;
74 };
75
76 #define G_COND_INIT { NULL }
77 struct _GCond
78 {
79   gpointer impl;
80 };
81 #else
82
83 #include <pthread.h>
84
85 #define G_MUTEX_INIT { PTHREAD_MUTEX_INITIALIZER }
86 struct _GMutex
87 {
88   pthread_mutex_t impl;
89 };
90
91 #define G_RW_LOCK_INIT { PTHREAD_RWLOCK_INITIALIZER }
92 struct _GRWLock
93 {
94   pthread_rwlock_t impl;
95 };
96
97 #define G_COND_INIT { PTHREAD_COND_INITIALIZER }
98 struct _GCond
99 {
100   pthread_cond_t impl;
101 };
102
103 #endif
104
105 #define G_REC_MUTEX_INIT { NULL }
106 struct _GRecMutex
107 {
108   gpointer impl;
109 };
110
111 /* initializes the mutex/cond/private implementation for glib, might
112  * only be called once, and must not be called directly or indirectly
113  * from another glib-function, e.g. as a callback.
114  */
115 void    g_thread_init   (gpointer vtable);
116
117 /* Checks if thread support is initialized.  Identical to the
118  * g_thread_supported macro but provided for language bindings.
119  */
120 gboolean g_thread_get_initialized (void);
121
122 GLIB_VAR gboolean               g_threads_got_initialized;
123
124 /* internal function for fallback static mutex implementation */
125 GMutex* g_static_mutex_get_mutex_impl   (GMutex **mutex);
126
127 #if defined(G_THREADS_MANDATORY)
128 #define g_thread_supported()     1
129 #else
130 #define g_thread_supported()    (g_threads_got_initialized)
131 #endif
132
133 GThread *g_thread_create                 (GThreadFunc   func,
134                                           gpointer      data,
135                                           gboolean      joinable,
136                                           GError      **error);
137
138 GThread *g_thread_create_with_stack_size (GThreadFunc   func,
139                                           gpointer      data,
140                                           gboolean      joinable,
141                                           gsize         stack_size,
142                                           GError      **error);
143
144 GThread* g_thread_self                   (void);
145 void     g_thread_exit                   (gpointer      retval);
146 gpointer g_thread_join                   (GThread      *thread);
147 void     g_thread_yield                  (void);
148
149 void     g_thread_foreach                (GFunc         thread_func,
150                                           gpointer      user_data);
151
152 #ifdef G_OS_WIN32
153 typedef GMutex * GStaticMutex;
154 #define G_STATIC_MUTEX_INIT NULL
155 #define g_static_mutex_get_mutex g_static_mutex_get_mutex_impl
156 #else /* G_OS_WIN32 */
157 typedef struct {
158   struct _GMutex *unused;
159   GMutex mutex;
160 } GStaticMutex;
161 #define G_STATIC_MUTEX_INIT { NULL, G_MUTEX_INIT }
162 #define g_static_mutex_get_mutex(s) (&(s)->mutex)
163 #endif /* G_OS_WIN32 */
164
165 #define g_static_mutex_lock(mutex) \
166     g_mutex_lock (g_static_mutex_get_mutex (mutex))
167 #define g_static_mutex_trylock(mutex) \
168     g_mutex_trylock (g_static_mutex_get_mutex (mutex))
169 #define g_static_mutex_unlock(mutex) \
170     g_mutex_unlock (g_static_mutex_get_mutex (mutex))
171 void g_static_mutex_init (GStaticMutex *mutex);
172 void g_static_mutex_free (GStaticMutex *mutex);
173
174 struct _GStaticPrivate
175 {
176   /*< private >*/
177   guint index;
178 };
179 #define G_STATIC_PRIVATE_INIT { 0 }
180 void     g_static_private_init           (GStaticPrivate   *private_key);
181 gpointer g_static_private_get            (GStaticPrivate   *private_key);
182 void     g_static_private_set            (GStaticPrivate   *private_key,
183                                           gpointer          data,
184                                           GDestroyNotify    notify);
185 void     g_static_private_free           (GStaticPrivate   *private_key);
186
187 typedef struct _GStaticRecMutex GStaticRecMutex;
188 struct _GStaticRecMutex
189 {
190   /*< private >*/
191   GStaticMutex mutex;
192   guint depth;
193   GSystemThread owner;
194 };
195
196 #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT, 0, {{0, 0, 0, 0}} }
197 void     g_static_rec_mutex_init        (GStaticRecMutex *mutex);
198 void     g_static_rec_mutex_lock        (GStaticRecMutex *mutex);
199 gboolean g_static_rec_mutex_trylock     (GStaticRecMutex *mutex);
200 void     g_static_rec_mutex_unlock      (GStaticRecMutex *mutex);
201 void     g_static_rec_mutex_lock_full   (GStaticRecMutex *mutex,
202                                          guint            depth);
203 guint    g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex);
204 void     g_static_rec_mutex_free        (GStaticRecMutex *mutex);
205
206 typedef struct _GStaticRWLock GStaticRWLock;
207 struct _GStaticRWLock
208 {
209   /*< private >*/
210   GStaticMutex mutex;
211   GCond *read_cond;
212   GCond *write_cond;
213   guint read_counter;
214   gboolean have_writer;
215   guint want_to_read;
216   guint want_to_write;
217 };
218
219 #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, 0, 0 }
220
221 void      g_static_rw_lock_init           (GStaticRWLock* lock);
222 void      g_static_rw_lock_reader_lock    (GStaticRWLock* lock);
223 gboolean  g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
224 void      g_static_rw_lock_reader_unlock  (GStaticRWLock* lock);
225 void      g_static_rw_lock_writer_lock    (GStaticRWLock* lock);
226 gboolean  g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
227 void      g_static_rw_lock_writer_unlock  (GStaticRWLock* lock);
228 void      g_static_rw_lock_free           (GStaticRWLock* lock);
229
230 typedef enum
231 {
232   G_ONCE_STATUS_NOTCALLED,
233   G_ONCE_STATUS_PROGRESS,
234   G_ONCE_STATUS_READY  
235 } GOnceStatus;
236
237 typedef struct _GOnce GOnce;
238 struct _GOnce
239 {
240   volatile GOnceStatus status;
241   volatile gpointer retval;
242 };
243
244 #define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
245
246 gpointer g_once_impl (GOnce *once, GThreadFunc func, gpointer arg);
247
248 #ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
249 # define g_once(once, func, arg) g_once_impl ((once), (func), (arg))
250 #else /* !G_ATOMIC_OP_MEMORY_BARRIER_NEEDED*/
251 # define g_once(once, func, arg) \
252   (((once)->status == G_ONCE_STATUS_READY) ? \
253    (once)->retval : \
254    g_once_impl ((once), (func), (arg)))
255 #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
256
257 /* initialize-once guards, keyed by value_location */
258 G_INLINE_FUNC gboolean  g_once_init_enter       (volatile gsize *value_location);
259 gboolean                g_once_init_enter_impl  (volatile gsize *value_location);
260 void                    g_once_init_leave       (volatile gsize *value_location,
261                                                  gsize           initialization_value);
262 #if defined (G_CAN_INLINE) || defined (__G_THREAD_C__)
263 G_INLINE_FUNC gboolean
264 g_once_init_enter (volatile gsize *value_location)
265 {
266   if G_LIKELY ((gpointer) g_atomic_pointer_get (value_location) != NULL)
267     return FALSE;
268   else
269     return g_once_init_enter_impl (value_location);
270 }
271 #endif /* G_CAN_INLINE || __G_THREAD_C__ */
272
273 #define G_LOCK_NAME(name)               g__ ## name ## _lock
274 #define G_LOCK_DEFINE_STATIC(name)    static G_LOCK_DEFINE (name)
275 #define G_LOCK_DEFINE(name)           \
276   GMutex G_LOCK_NAME (name) = G_MUTEX_INIT
277 #define G_LOCK_EXTERN(name)           extern GMutex G_LOCK_NAME (name)
278
279 #ifdef G_DEBUG_LOCKS
280 #  define G_LOCK(name)                G_STMT_START{             \
281       g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
282              "file %s: line %d (%s): locking: %s ",             \
283              __FILE__,        __LINE__, G_STRFUNC,              \
284              #name);                                            \
285       g_mutex_lock (&G_LOCK_NAME (name));                       \
286    }G_STMT_END
287 #  define G_UNLOCK(name)              G_STMT_START{             \
288       g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
289              "file %s: line %d (%s): unlocking: %s ",           \
290              __FILE__,        __LINE__, G_STRFUNC,              \
291              #name);                                            \
292      g_mutex_unlock (&G_LOCK_NAME (name));                      \
293    }G_STMT_END
294 #  define G_TRYLOCK(name)                                       \
295       (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                  \
296              "file %s: line %d (%s): try locking: %s ",         \
297              __FILE__,        __LINE__, G_STRFUNC,              \
298              #name), g_mutex_trylock (&G_LOCK_NAME (name)))
299 #else  /* !G_DEBUG_LOCKS */
300 #  define G_LOCK(name) g_mutex_lock       (&G_LOCK_NAME (name))
301 #  define G_UNLOCK(name) g_mutex_unlock   (&G_LOCK_NAME (name))
302 #  define G_TRYLOCK(name) g_mutex_trylock (&G_LOCK_NAME (name))
303 #endif /* !G_DEBUG_LOCKS */
304
305
306 GMutex *                g_mutex_new                                     (void);
307 void                    g_mutex_free                                    (GMutex         *mutex);
308 void                    g_mutex_init                                    (GMutex         *mutex);
309 void                    g_mutex_clear                                   (GMutex         *mutex);
310
311 void                    g_mutex_lock                                    (GMutex         *mutex);
312 void                    g_mutex_unlock                                  (GMutex         *mutex);
313 gboolean                g_mutex_trylock                                 (GMutex         *mutex);
314
315 void                    g_rw_lock_init                                  (GRWLock        *lock);
316 void                    g_rw_lock_clear                                 (GRWLock        *lock);
317 void                    g_rw_lock_writer_lock                           (GRWLock        *lock);
318 gboolean                g_rw_lock_writer_trylock                        (GRWLock        *lock);
319 void                    g_rw_lock_writer_unlock                         (GRWLock        *lock);
320 void                    g_rw_lock_reader_lock                           (GRWLock        *lock);
321 gboolean                g_rw_lock_reader_trylock                        (GRWLock        *lock);
322 void                    g_rw_lock_reader_unlock                         (GRWLock        *lock);
323
324 void                    g_rec_mutex_init                                (GRecMutex      *rec_mutex);
325 void                    g_rec_mutex_clear                               (GRecMutex      *rec_mutex);
326 void                    g_rec_mutex_lock                                (GRecMutex      *rec_mutex);
327 gboolean                g_rec_mutex_trylock                             (GRecMutex      *rec_mutex);
328 void                    g_rec_mutex_unlock                              (GRecMutex      *rec_mutex);
329
330 GCond *                 g_cond_new                                      (void);
331 void                    g_cond_free                                     (GCond          *cond);
332 void                    g_cond_init                                     (GCond          *cond);
333 void                    g_cond_clear                                    (GCond          *cond);
334
335 void                    g_cond_wait                                     (GCond          *cond,
336                                                                          GMutex         *mutex);
337 void                    g_cond_signal                                   (GCond          *cond);
338 void                    g_cond_broadcast                                (GCond          *cond);
339 gboolean                g_cond_timed_wait                               (GCond          *cond,
340                                                                          GMutex         *mutex,
341                                                                          GTimeVal       *timeval);
342 gboolean                g_cond_timedwait                                (GCond          *cond,
343                                                                          GMutex         *mutex,
344                                                                          gint64          abs_time);
345
346 GPrivate *              g_private_new                                   (GDestroyNotify  notify);
347 gpointer                g_private_get                                   (GPrivate       *key);
348 void                    g_private_set                                   (GPrivate       *key,
349                                                                          gpointer        value);
350
351 G_END_DECLS
352
353 #endif /* __G_THREAD_H__ */