More GThread docs tweaks
[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 _GRecMutex       GRecMutex;
57 typedef struct _GRWLock         GRWLock;
58 typedef struct _GCond           GCond;
59 typedef struct _GPrivate        GPrivate;
60 typedef struct _GStaticPrivate  GStaticPrivate;
61
62 #ifdef G_OS_WIN32
63
64 #define G_MUTEX_INIT { NULL }
65 struct _GMutex
66 {
67   gpointer impl;
68 };
69
70 #define G_RW_LOCK_INIT { NULL }
71 struct _GRWLock
72 {
73   gpointer impl;
74 };
75
76 #define G_COND_INIT { NULL }
77 struct _GCond
78 {
79   gpointer impl;
80 };
81 #else
82
83 #include <pthread.h>
84
85 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
86 #define G_MUTEX_INIT { PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP }
87 #else
88 #define G_MUTEX_INIT { PTHREAD_MUTEX_INITIALIZER }
89 #endif
90 struct _GMutex
91 {
92   pthread_mutex_t impl;
93 };
94
95 #define G_RW_LOCK_INIT { PTHREAD_RWLOCK_INITIALIZER }
96 struct _GRWLock
97 {
98   pthread_rwlock_t impl;
99 };
100
101 #define G_COND_INIT { PTHREAD_COND_INITIALIZER }
102 struct _GCond
103 {
104   pthread_cond_t impl;
105 };
106
107 #endif
108
109 #define G_REC_MUTEX_INIT { NULL }
110 struct _GRecMutex
111 {
112   gpointer impl;
113 };
114
115 void     g_thread_init   (gpointer vtable);
116
117 gboolean g_thread_get_initialized (void);
118
119 GLIB_VAR gboolean g_threads_got_initialized;
120
121 #if defined(G_THREADS_MANDATORY)
122 #define g_thread_supported()     1
123 #else
124 #define g_thread_supported()    (g_threads_got_initialized)
125 #endif
126
127 GMutex* g_static_mutex_get_mutex_impl   (GMutex **mutex);
128
129 GThread *g_thread_create                 (GThreadFunc   func,
130                                           gpointer      data,
131                                           gboolean      joinable,
132                                           GError      **error);
133
134 GThread *g_thread_create_with_stack_size (GThreadFunc   func,
135                                           gpointer      data,
136                                           gboolean      joinable,
137                                           gsize         stack_size,
138                                           GError      **error);
139
140 GThread* g_thread_self                   (void);
141 void     g_thread_exit                   (gpointer      retval);
142 gpointer g_thread_join                   (GThread      *thread);
143 void     g_thread_yield                  (void);
144
145 void     g_thread_foreach                (GFunc         thread_func,
146                                           gpointer      user_data);
147
148 struct _GStaticPrivate
149 {
150   /*< private >*/
151   guint index;
152 };
153 #define G_STATIC_PRIVATE_INIT { 0 }
154 void     g_static_private_init           (GStaticPrivate   *private_key);
155 gpointer g_static_private_get            (GStaticPrivate   *private_key);
156 void     g_static_private_set            (GStaticPrivate   *private_key,
157                                           gpointer          data,
158                                           GDestroyNotify    notify);
159 void     g_static_private_free           (GStaticPrivate   *private_key);
160
161 typedef enum
162 {
163   G_ONCE_STATUS_NOTCALLED,
164   G_ONCE_STATUS_PROGRESS,
165   G_ONCE_STATUS_READY
166 } GOnceStatus;
167
168 typedef struct _GOnce GOnce;
169 struct _GOnce
170 {
171   volatile GOnceStatus status;
172   volatile gpointer retval;
173 };
174
175 #define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
176
177 gpointer g_once_impl (GOnce *once, GThreadFunc func, gpointer arg);
178
179 #ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
180 # define g_once(once, func, arg) g_once_impl ((once), (func), (arg))
181 #else /* !G_ATOMIC_OP_MEMORY_BARRIER_NEEDED*/
182 # define g_once(once, func, arg) \
183   (((once)->status == G_ONCE_STATUS_READY) ? \
184    (once)->retval : \
185    g_once_impl ((once), (func), (arg)))
186 #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
187
188 /* initialize-once guards, keyed by value_location */
189 G_INLINE_FUNC gboolean  g_once_init_enter       (volatile gsize *value_location);
190 gboolean                g_once_init_enter_impl  (volatile gsize *value_location);
191 void                    g_once_init_leave       (volatile gsize *value_location,
192                                                  gsize           initialization_value);
193 #if defined (G_CAN_INLINE) || defined (__G_THREAD_C__)
194 G_INLINE_FUNC gboolean
195 g_once_init_enter (volatile gsize *value_location)
196 {
197   if G_LIKELY ((gpointer) g_atomic_pointer_get (value_location) != NULL)
198     return FALSE;
199   else
200     return g_once_init_enter_impl (value_location);
201 }
202 #endif /* G_CAN_INLINE || __G_THREAD_C__ */
203
204 #define G_LOCK_NAME(name)               g__ ## name ## _lock
205 #define G_LOCK_DEFINE_STATIC(name)    static G_LOCK_DEFINE (name)
206 #define G_LOCK_DEFINE(name)           \
207   GMutex G_LOCK_NAME (name) = G_MUTEX_INIT
208 #define G_LOCK_EXTERN(name)           extern GMutex G_LOCK_NAME (name)
209
210 #ifdef G_DEBUG_LOCKS
211 #  define G_LOCK(name)                G_STMT_START{             \
212       g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
213              "file %s: line %d (%s): locking: %s ",             \
214              __FILE__,        __LINE__, G_STRFUNC,              \
215              #name);                                            \
216       g_mutex_lock (&G_LOCK_NAME (name));                       \
217    }G_STMT_END
218 #  define G_UNLOCK(name)              G_STMT_START{             \
219       g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
220              "file %s: line %d (%s): unlocking: %s ",           \
221              __FILE__,        __LINE__, G_STRFUNC,              \
222              #name);                                            \
223      g_mutex_unlock (&G_LOCK_NAME (name));                      \
224    }G_STMT_END
225 #  define G_TRYLOCK(name)                                       \
226       (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                  \
227              "file %s: line %d (%s): try locking: %s ",         \
228              __FILE__,        __LINE__, G_STRFUNC,              \
229              #name), g_mutex_trylock (&G_LOCK_NAME (name)))
230 #else  /* !G_DEBUG_LOCKS */
231 #  define G_LOCK(name) g_mutex_lock       (&G_LOCK_NAME (name))
232 #  define G_UNLOCK(name) g_mutex_unlock   (&G_LOCK_NAME (name))
233 #  define G_TRYLOCK(name) g_mutex_trylock (&G_LOCK_NAME (name))
234 #endif /* !G_DEBUG_LOCKS */
235
236
237 GMutex *                g_mutex_new                                     (void);
238 void                    g_mutex_free                                    (GMutex         *mutex);
239 void                    g_mutex_init                                    (GMutex         *mutex);
240 void                    g_mutex_clear                                   (GMutex         *mutex);
241
242 void                    g_mutex_lock                                    (GMutex         *mutex);
243 void                    g_mutex_unlock                                  (GMutex         *mutex);
244 gboolean                g_mutex_trylock                                 (GMutex         *mutex);
245
246 void                    g_rw_lock_init                                  (GRWLock        *lock);
247 void                    g_rw_lock_clear                                 (GRWLock        *lock);
248 void                    g_rw_lock_writer_lock                           (GRWLock        *lock);
249 gboolean                g_rw_lock_writer_trylock                        (GRWLock        *lock);
250 void                    g_rw_lock_writer_unlock                         (GRWLock        *lock);
251 void                    g_rw_lock_reader_lock                           (GRWLock        *lock);
252 gboolean                g_rw_lock_reader_trylock                        (GRWLock        *lock);
253 void                    g_rw_lock_reader_unlock                         (GRWLock        *lock);
254
255 void                    g_rec_mutex_init                                (GRecMutex      *rec_mutex);
256 void                    g_rec_mutex_clear                               (GRecMutex      *rec_mutex);
257 void                    g_rec_mutex_lock                                (GRecMutex      *rec_mutex);
258 gboolean                g_rec_mutex_trylock                             (GRecMutex      *rec_mutex);
259 void                    g_rec_mutex_unlock                              (GRecMutex      *rec_mutex);
260
261 GCond *                 g_cond_new                                      (void);
262 void                    g_cond_free                                     (GCond          *cond);
263 void                    g_cond_init                                     (GCond          *cond);
264 void                    g_cond_clear                                    (GCond          *cond);
265
266 void                    g_cond_wait                                     (GCond          *cond,
267                                                                          GMutex         *mutex);
268 void                    g_cond_signal                                   (GCond          *cond);
269 void                    g_cond_broadcast                                (GCond          *cond);
270 gboolean                g_cond_timed_wait                               (GCond          *cond,
271                                                                          GMutex         *mutex,
272                                                                          GTimeVal       *timeval);
273 gboolean                g_cond_timedwait                                (GCond          *cond,
274                                                                          GMutex         *mutex,
275                                                                          gint64          abs_time);
276
277 GPrivate *              g_private_new                                   (GDestroyNotify  notify);
278 gpointer                g_private_get                                   (GPrivate       *key);
279 void                    g_private_set                                   (GPrivate       *key,
280                                                                          gpointer        value);
281
282 G_END_DECLS
283
284 #endif /* __G_THREAD_H__ */