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