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