macro wrappers for g_once_init_enter/leave
[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 union  _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 union _GMutex
63 {
64   /*< private >*/
65   gpointer p;
66   guint i[2];
67 };
68
69 struct _GRWLock
70 {
71   /*< private >*/
72   gpointer p;
73   guint i[2];
74 };
75
76 struct _GCond
77 {
78   /*< private >*/
79   gpointer p;
80   guint i[2];
81 };
82
83 struct _GRecMutex
84 {
85   /*< private >*/
86   gpointer p;
87   guint i[2];
88 };
89
90 #define G_PRIVATE_INIT(notify) { NULL, (notify), { NULL, NULL } }
91 struct _GPrivate
92 {
93   gpointer       p;
94   GDestroyNotify notify;
95   gpointer future[2];
96 };
97
98 void     g_thread_init   (gpointer vtable);
99
100 gboolean g_thread_get_initialized (void);
101
102 GLIB_VAR gboolean g_threads_got_initialized;
103
104 #if defined(G_THREADS_MANDATORY)
105 #define g_thread_supported()     1
106 #else
107 #define g_thread_supported()    (g_threads_got_initialized)
108 #endif
109
110 GMutex* g_static_mutex_get_mutex_impl   (GMutex **mutex);
111
112 GThread *g_thread_new                    (const gchar  *name,
113                                           GThreadFunc   func,
114                                           gpointer      data,
115                                           gboolean      joinable,
116                                           GError      **error);
117
118 GThread *g_thread_new_full               (const gchar  *name,
119                                           GThreadFunc   func,
120                                           gpointer      data,
121                                           gboolean      joinable,
122                                           gsize         stack_size,
123                                           GError      **error);
124
125 GThread* g_thread_self                   (void);
126 void     g_thread_exit                   (gpointer      retval);
127 gpointer g_thread_join                   (GThread      *thread);
128 void     g_thread_yield                  (void);
129
130 typedef enum
131 {
132   G_ONCE_STATUS_NOTCALLED,
133   G_ONCE_STATUS_PROGRESS,
134   G_ONCE_STATUS_READY
135 } GOnceStatus;
136
137 typedef struct _GOnce GOnce;
138 struct _GOnce
139 {
140   volatile GOnceStatus status;
141   volatile gpointer retval;
142 };
143
144 #define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
145
146 gpointer g_once_impl (GOnce *once, GThreadFunc func, gpointer arg);
147
148 #ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
149 # define g_once(once, func, arg) g_once_impl ((once), (func), (arg))
150 #else /* !G_ATOMIC_OP_MEMORY_BARRIER_NEEDED*/
151 # define g_once(once, func, arg) \
152   (((once)->status == G_ONCE_STATUS_READY) ? \
153    (once)->retval : \
154    g_once_impl ((once), (func), (arg)))
155 #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
156
157 /* initialize-once guards, keyed by location */
158 gboolean        g_once_init_enter       (volatile void *location);
159 void            g_once_init_leave       (volatile void *location,
160                                          gsize          result);
161
162 #ifdef __GNUC__
163 # define g_once_init_enter(location) \
164   (G_GNUC_EXTENSION ({                                               \
165     G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer));       \
166     (void) (0 ? (gpointer) *(location) : 0);                         \
167     (!g_atomic_pointer_get (location) &&                             \
168      g_once_init_enter (location));                                  \
169   }))
170 # define g_once_init_leave(location, result) \
171   (G_GNUC_EXTENSION ({                                               \
172     G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer));       \
173     (void) (0 ? *(location) = (result) : 0);                         \
174     g_once_init_leave ((location), (gsize) (result));                \
175   }))
176 #else
177 # define g_once_init_enter(location) \
178   (g_once_init_enter((location)))
179 # define g_once_init_leave(location, result) \
180   (g_once_init_leave((location), (gsize) (result)))
181 #endif
182
183 #define G_LOCK_NAME(name)               g__ ## name ## _lock
184 #define G_LOCK_DEFINE_STATIC(name)    static G_LOCK_DEFINE (name)
185 #define G_LOCK_DEFINE(name)           GMutex G_LOCK_NAME (name)
186 #define G_LOCK_EXTERN(name)           extern GMutex G_LOCK_NAME (name)
187
188 #ifdef G_DEBUG_LOCKS
189 #  define G_LOCK(name)                G_STMT_START{             \
190       g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
191              "file %s: line %d (%s): locking: %s ",             \
192              __FILE__,        __LINE__, G_STRFUNC,              \
193              #name);                                            \
194       g_mutex_lock (&G_LOCK_NAME (name));                       \
195    }G_STMT_END
196 #  define G_UNLOCK(name)              G_STMT_START{             \
197       g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
198              "file %s: line %d (%s): unlocking: %s ",           \
199              __FILE__,        __LINE__, G_STRFUNC,              \
200              #name);                                            \
201      g_mutex_unlock (&G_LOCK_NAME (name));                      \
202    }G_STMT_END
203 #  define G_TRYLOCK(name)                                       \
204       (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                  \
205              "file %s: line %d (%s): try locking: %s ",         \
206              __FILE__,        __LINE__, G_STRFUNC,              \
207              #name), g_mutex_trylock (&G_LOCK_NAME (name)))
208 #else  /* !G_DEBUG_LOCKS */
209 #  define G_LOCK(name) g_mutex_lock       (&G_LOCK_NAME (name))
210 #  define G_UNLOCK(name) g_mutex_unlock   (&G_LOCK_NAME (name))
211 #  define G_TRYLOCK(name) g_mutex_trylock (&G_LOCK_NAME (name))
212 #endif /* !G_DEBUG_LOCKS */
213
214
215 GMutex *                g_mutex_new                                     (void);
216 void                    g_mutex_free                                    (GMutex         *mutex);
217 void                    g_mutex_init                                    (GMutex         *mutex);
218 void                    g_mutex_clear                                   (GMutex         *mutex);
219
220 void                    g_mutex_lock                                    (GMutex         *mutex);
221 void                    g_mutex_unlock                                  (GMutex         *mutex);
222 gboolean                g_mutex_trylock                                 (GMutex         *mutex);
223
224 void                    g_rw_lock_init                                  (GRWLock        *rw_lock);
225 void                    g_rw_lock_clear                                 (GRWLock        *rw_lock);
226 void                    g_rw_lock_writer_lock                           (GRWLock        *rw_lock);
227 gboolean                g_rw_lock_writer_trylock                        (GRWLock        *rw_lock);
228 void                    g_rw_lock_writer_unlock                         (GRWLock        *rw_lock);
229 void                    g_rw_lock_reader_lock                           (GRWLock        *rw_lock);
230 gboolean                g_rw_lock_reader_trylock                        (GRWLock        *rw_lock);
231 void                    g_rw_lock_reader_unlock                         (GRWLock        *rw_lock);
232
233 void                    g_rec_mutex_init                                (GRecMutex      *rec_mutex);
234 void                    g_rec_mutex_clear                               (GRecMutex      *rec_mutex);
235 void                    g_rec_mutex_lock                                (GRecMutex      *rec_mutex);
236 gboolean                g_rec_mutex_trylock                             (GRecMutex      *rec_mutex);
237 void                    g_rec_mutex_unlock                              (GRecMutex      *rec_mutex);
238
239 GCond *                 g_cond_new                                      (void);
240 void                    g_cond_free                                     (GCond          *cond);
241 void                    g_cond_init                                     (GCond          *cond);
242 void                    g_cond_clear                                    (GCond          *cond);
243
244 void                    g_cond_wait                                     (GCond          *cond,
245                                                                          GMutex         *mutex);
246 void                    g_cond_signal                                   (GCond          *cond);
247 void                    g_cond_broadcast                                (GCond          *cond);
248 gboolean                g_cond_timed_wait                               (GCond          *cond,
249                                                                          GMutex         *mutex,
250                                                                          GTimeVal       *timeval);
251 gboolean                g_cond_timedwait                                (GCond          *cond,
252                                                                          GMutex         *mutex,
253                                                                          gint64          abs_time);
254
255 gpointer                g_private_get                                   (GPrivate       *key);
256 void                    g_private_set                                   (GPrivate       *key,
257                                                                          gpointer        value);
258 void                    g_private_replace                               (GPrivate       *key,
259                                                                          gpointer        value);
260
261 G_END_DECLS
262
263 #endif /* __G_THREAD_H__ */