Call g_thread_impl_init(), as g_thread_init won't call it.
[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 #ifndef __G_THREAD_H__
28 #define __G_THREAD_H__
29
30 #include <gerror.h>
31
32 G_BEGIN_DECLS
33
34 #ifndef GLIB_VAR
35 #  ifdef G_OS_WIN32
36 #    ifdef GLIB_COMPILATION
37 #      define GLIB_VAR __declspec(dllexport)
38 #    else /* !GLIB_COMPILATION */
39 #      define GLIB_VAR extern __declspec(dllimport)
40 #    endif /* !GLIB_COMPILATION */
41 #  else /* !G_OS_WIN32 */
42 #    define GLIB_VAR extern
43 #  endif /* !G_OS_WIN32 */
44 #endif /* GLIB_VAR */
45
46 /* GLib Thread support
47  */
48
49 extern GQuark g_thread_error_quark();
50 #define G_THREAD_ERROR g_thread_error_quark()
51
52 typedef enum
53 {
54   G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
55 } GThreadError;
56
57 typedef void            (*GThreadFunc)          (gpointer       value);
58
59 typedef enum
60 {
61   G_THREAD_PRIORITY_LOW,
62   G_THREAD_PRIORITY_NORMAL,
63   G_THREAD_PRIORITY_HIGH,
64   G_THREAD_PRIORITY_URGENT
65 } GThreadPriority;
66
67 typedef struct _GThread         GThread;
68 struct  _GThread
69 {
70   GThreadPriority priority;
71   gboolean bound;
72   gboolean joinable;
73 };
74
75 typedef struct _GMutex          GMutex;
76 typedef struct _GCond           GCond;
77 typedef struct _GPrivate        GPrivate;
78 typedef struct _GStaticPrivate  GStaticPrivate;
79
80 typedef struct _GThreadFunctions GThreadFunctions;
81 struct _GThreadFunctions
82 {
83   GMutex*  (*mutex_new)           (void);
84   void     (*mutex_lock)          (GMutex               *mutex);
85   gboolean (*mutex_trylock)       (GMutex               *mutex);
86   void     (*mutex_unlock)        (GMutex               *mutex);
87   void     (*mutex_free)          (GMutex               *mutex);
88   GCond*   (*cond_new)            (void);
89   void     (*cond_signal)         (GCond                *cond);
90   void     (*cond_broadcast)      (GCond                *cond);
91   void     (*cond_wait)           (GCond                *cond,
92                                    GMutex               *mutex);
93   gboolean (*cond_timed_wait)     (GCond                *cond,
94                                    GMutex               *mutex,
95                                    GTimeVal             *end_time);
96   void      (*cond_free)          (GCond                *cond);
97   GPrivate* (*private_new)        (GDestroyNotify        destructor);
98   gpointer  (*private_get)        (GPrivate             *private_key);
99   void      (*private_set)        (GPrivate             *private_key,
100                                    gpointer              data);
101   void      (*thread_create)      (GThreadFunc           thread_func,
102                                    gpointer              arg,
103                                    gulong                stack_size,
104                                    gboolean              joinable,
105                                    gboolean              bound,
106                                    GThreadPriority       priority,
107                                    gpointer              thread,
108                                    GError              **error);
109   void      (*thread_yield)       (void);
110   void      (*thread_join)        (gpointer              thread);
111   void      (*thread_exit)        (void);
112   void      (*thread_set_priority)(gpointer              thread,
113                                    GThreadPriority       priority);
114   void      (*thread_self)        (gpointer              thread);
115 };
116
117 GLIB_VAR GThreadFunctions       g_thread_functions_for_glib_use;
118 GLIB_VAR gboolean               g_thread_use_default_impl;
119 GLIB_VAR gboolean               g_threads_got_initialized;
120
121 /* initializes the mutex/cond/private implementation for glib, might
122  * only be called once, and must not be called directly or indirectly
123  * from another glib-function, e.g. as a callback.
124  */
125 void    g_thread_init   (GThreadFunctions       *vtable);
126
127 /* Errorcheck mutexes. If you define G_ERRORCHECK_MUTEXES, then all
128  * mutexes will check for re-locking and re-unlocking */
129
130 /* Initialize thread system with errorcheck mutexes. vtable must be
131  * NULL. Do not call directly. Use #define G_ERRORCHECK_MUTEXES
132  * instead.
133  */
134 void    g_thread_init_with_errorcheck_mutexes (GThreadFunctions* vtable);
135
136 /* A random number to recognize debug calls to g_mutex_... */
137 #define G_MUTEX_DEBUG_MAGIC 0xf8e18ad7
138
139 #ifdef G_ERRORCHECK_MUTEXES
140 #define g_thread_init(vtable) g_thread_init_with_errorcheck_mutexes (vtable)
141 #endif
142
143 /* internal function for fallback static mutex implementation */
144 GMutex* g_static_mutex_get_mutex_impl   (GMutex **mutex);
145
146 /* shorthands for conditional and unconditional function calls */
147
148 #define G_THREAD_UF(op, arglist)                                        \
149     (*g_thread_functions_for_glib_use . op) arglist
150 #define G_THREAD_CF(op, fail, arg)                                      \
151     (g_thread_supported () ? G_THREAD_UF (op, arg) : (fail))
152 #define G_THREAD_ECF(op, fail, mutex, type)                             \
153     (g_thread_supported () ? ((type(*)(GMutex*, gulong, gchar*))        \
154       (*g_thread_functions_for_glib_use . op))                          \
155      (mutex, G_MUTEX_DEBUG_MAGIC, G_STRLOC) : (fail))
156
157 #ifndef G_ERRORCHECK_MUTEXES
158 # define g_mutex_lock(mutex)                                            \
159     G_THREAD_CF (mutex_lock,     (void)0, (mutex))
160 # define g_mutex_trylock(mutex)                                         \
161     G_THREAD_CF (mutex_trylock,  TRUE,    (mutex))
162 # define g_mutex_unlock(mutex)                                          \
163     G_THREAD_CF (mutex_unlock,   (void)0, (mutex))
164 # define g_mutex_free(mutex)                                            \
165     G_THREAD_CF (mutex_free,     (void)0, (mutex))
166 # define g_cond_wait(cond, mutex)                                       \
167     G_THREAD_CF (cond_wait,      (void)0, (cond, mutex))
168 # define g_cond_timed_wait(cond, mutex, abs_time)                       \
169     G_THREAD_CF (cond_timed_wait, TRUE,   (cond, mutex, abs_time))
170 #else /* G_ERRORCHECK_MUTEXES */
171 # define g_mutex_lock(mutex)                                            \
172     G_THREAD_ECF (mutex_lock,    (void)0, (mutex), void)
173 # define g_mutex_trylock(mutex)                                         \
174     G_THREAD_ECF (mutex_trylock, TRUE,    (mutex), gboolean)
175 # define g_mutex_unlock(mutex)                                          \
176     G_THREAD_ECF (mutex_unlock,  (void)0, (mutex), void)
177 # define g_mutex_free(mutex)                                            \
178     G_THREAD_ECF (mutex_free,    (void)0, (mutex), void)
179 # define g_cond_wait(cond, mutex)                                       \
180     (g_thread_supported () ? ((void(*)(GCond*, GMutex*, gulong, gchar*))\
181       g_thread_functions_for_glib_use.cond_wait)                        \
182         (cond, mutex, G_MUTEX_DEBUG_MAGIC, G_STRLOC) : (void) 0)
183 # define g_cond_timed_wait(cond, mutex, abs_time)                       \
184     (g_thread_supported () ?                                            \
185       ((gboolean(*)(GCond*, GMutex*, GTimeVal*, gulong, gchar*))        \
186         g_thread_functions_for_glib_use.cond_timed_wait)                \
187           (cond, mutex, abs_time, G_MUTEX_DEBUG_MAGIC, G_STRLOC) : TRUE)
188 #endif /* G_ERRORCHECK_MUTEXES */
189
190 #define g_thread_supported()    (g_threads_got_initialized)
191 #define g_mutex_new()            G_THREAD_UF (mutex_new,      ())
192 #define g_cond_new()             G_THREAD_UF (cond_new,       ())
193 #define g_cond_signal(cond)      G_THREAD_CF (cond_signal,    (void)0, (cond))
194 #define g_cond_broadcast(cond)   G_THREAD_CF (cond_broadcast, (void)0, (cond))
195 #define g_cond_free(cond)        G_THREAD_CF (cond_free,      (void)0, (cond))
196 #define g_private_new(destructor) G_THREAD_UF (private_new, (destructor))
197 #define g_private_get(private_key) G_THREAD_CF (private_get, \
198                                                 ((gpointer)private_key), \
199                                                 (private_key))
200 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
201                                                        (void) (private_key = \
202                                                         (GPrivate*) (value)), \
203                                                        (private_key, value))
204 #define g_thread_yield()              G_THREAD_CF (thread_yield, (void)0, ())
205 #define g_thread_exit()               G_THREAD_CF (thread_exit, (void)0, ())
206
207 GThread* g_thread_create (GThreadFunc            thread_func,
208                           gpointer               arg,
209                           gulong                 stack_size,
210                           gboolean               joinable,
211                           gboolean               bound,
212                           GThreadPriority        priority,
213                           GError               **error);
214 GThread* g_thread_self (void);
215 void g_thread_join (GThread *thread);
216 void g_thread_set_priority (GThread         *thread,
217                             GThreadPriority  priority);
218
219 /* GStaticMutexes can be statically initialized with the value
220  * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
221  * much easier, than having to explicitly allocate the mutex before
222  * use
223  */
224 #define g_static_mutex_lock(mutex) \
225     g_mutex_lock (g_static_mutex_get_mutex (mutex))
226 #define g_static_mutex_trylock(mutex) \
227     g_mutex_trylock (g_static_mutex_get_mutex (mutex))
228 #define g_static_mutex_unlock(mutex) \
229     g_mutex_unlock (g_static_mutex_get_mutex (mutex))
230 void g_static_mutex_free (GStaticMutex *mutex);
231
232 struct _GStaticPrivate
233 {
234   guint index;
235 };
236 #define G_STATIC_PRIVATE_INIT { 0 }
237 gpointer g_static_private_get (GStaticPrivate   *private_key);
238 void     g_static_private_set (GStaticPrivate   *private_key,
239                                gpointer          data,
240                                GDestroyNotify    notify);
241 gpointer g_static_private_get_for_thread (GStaticPrivate *private_key,
242                                           GThread        *thread);
243 void g_static_private_set_for_thread (GStaticPrivate *private_key,
244                                       GThread        *thread,
245                                       gpointer        data,
246                                       GDestroyNotify  notify);
247
248 typedef struct _GStaticRecMutex GStaticRecMutex;
249 struct _GStaticRecMutex
250 {
251   GStaticMutex mutex;
252   unsigned int depth;
253   GSystemThread owner;
254 };
255
256 #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT }
257 void     g_static_rec_mutex_lock        (GStaticRecMutex *mutex);
258 gboolean g_static_rec_mutex_trylock     (GStaticRecMutex *mutex);
259 void     g_static_rec_mutex_unlock      (GStaticRecMutex *mutex);
260 void     g_static_rec_mutex_lock_full   (GStaticRecMutex *mutex,
261                                          guint            depth);
262 guint    g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex);
263
264 typedef struct _GStaticRWLock GStaticRWLock;
265 struct _GStaticRWLock
266 {
267   GStaticMutex mutex;
268   GCond *read_cond;
269   GCond *write_cond;
270   guint read_counter;
271   gboolean write;
272   guint want_to_write;
273 };
274
275 #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, FALSE }
276
277 void      g_static_rw_lock_reader_lock    (GStaticRWLock* lock);
278 gboolean  g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
279 void      g_static_rw_lock_reader_unlock  (GStaticRWLock* lock);
280 void      g_static_rw_lock_writer_lock    (GStaticRWLock* lock);
281 gboolean  g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
282 void      g_static_rw_lock_writer_unlock  (GStaticRWLock* lock);
283 void      g_static_rw_lock_free (GStaticRWLock* lock);
284
285 /* these are some convenience macros that expand to nothing if GLib
286  * was configured with --disable-threads. for using StaticMutexes,
287  * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
288  * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
289  * declare such an globally defined lock. name is a unique identifier
290  * for the protected varibale or code portion. locking, testing and
291  * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
292  * G_TRYLOCK() respectively.
293  */
294 extern void glib_dummy_decl (void);
295 #define G_LOCK_NAME(name)               g__ ## name ## _lock
296 #ifdef  G_THREADS_ENABLED
297 #  define G_LOCK_DEFINE_STATIC(name)    static G_LOCK_DEFINE (name)
298 #  define G_LOCK_DEFINE(name)           \
299     GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT
300 #  define G_LOCK_EXTERN(name)           extern GStaticMutex G_LOCK_NAME (name)
301
302 #  ifdef G_DEBUG_LOCKS
303 #    define G_LOCK(name)                G_STMT_START{             \
304         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
305                "file %s: line %d (%s): locking: %s ",             \
306                __FILE__,        __LINE__, G_GNUC_PRETTY_FUNCTION, \
307                #name);                                            \
308         g_static_mutex_lock (&G_LOCK_NAME (name));                \
309      }G_STMT_END
310 #    define G_UNLOCK(name)              G_STMT_START{             \
311         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
312                "file %s: line %d (%s): unlocking: %s ",           \
313                __FILE__,        __LINE__, G_GNUC_PRETTY_FUNCTION, \
314                #name);                                            \
315        g_static_mutex_unlock (&G_LOCK_NAME (name));               \
316      }G_STMT_END
317 #    define G_TRYLOCK(name)                                       \
318         (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                  \
319                "file %s: line %d (%s): try locking: %s ",         \
320                __FILE__,        __LINE__, G_GNUC_PRETTY_FUNCTION, \
321                #name), g_static_mutex_trylock (&G_LOCK_NAME (name)))
322 #  else  /* !G_DEBUG_LOCKS */
323 #    define G_LOCK(name) g_static_mutex_lock       (&G_LOCK_NAME (name))
324 #    define G_UNLOCK(name) g_static_mutex_unlock   (&G_LOCK_NAME (name))
325 #    define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
326 #  endif /* !G_DEBUG_LOCKS */
327 #else   /* !G_THREADS_ENABLED */
328 #  define G_LOCK_DEFINE_STATIC(name)    extern void glib_dummy_decl (void)
329 #  define G_LOCK_DEFINE(name)           extern void glib_dummy_decl (void)
330 #  define G_LOCK_EXTERN(name)           extern void glib_dummy_decl (void)
331 #  define G_LOCK(name)
332 #  define G_UNLOCK(name)
333 #  define G_TRYLOCK(name)               (TRUE)
334 #endif  /* !G_THREADS_ENABLED */
335
336 G_END_DECLS
337
338 #endif /* __G_THREAD_H__ */
339