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