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