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