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.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * 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, see <http://www.gnu.org/licenses/>.
16 */
17
18/*
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
23 */
24
25#ifndef __G_THREAD_H__
26#define __G_THREAD_H__
27
28#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
29#error "Only <glib.h> can be included directly."
30#endif
31
32#include <glib/gatomic.h>
33#include <glib/gerror.h>
34#include <glib/gutils.h>
35
36G_BEGIN_DECLS
37
38#define G_THREAD_ERROR g_thread_error_quark ()
39GLIB_AVAILABLE_IN_ALL
40GQuark g_thread_error_quark (void);
41
42typedef enum
43{
44 G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
45} GThreadError;
46
47typedef gpointer (*GThreadFunc) (gpointer data);
48
49typedef struct _GThread GThread;
50
51typedef union _GMutex GMutex;
52typedef struct _GRecMutex GRecMutex;
53typedef struct _GRWLock GRWLock;
54typedef struct _GCond GCond;
55typedef struct _GPrivate GPrivate;
56typedef struct _GOnce GOnce;
57
58union _GMutex
59{
60 /*< private >*/
61 gpointer p;
62 guint i[2];
63};
64
65struct _GRWLock
66{
67 /*< private >*/
68 gpointer p;
69 guint i[2];
70};
71
72struct _GCond
73{
74 /*< private >*/
75 gpointer p;
76 guint i[2];
77};
78
79struct _GRecMutex
80{
81 /*< private >*/
82 gpointer p;
83 guint i[2];
84};
85
86#define G_PRIVATE_INIT(notify) { NULL, (notify), { NULL, NULL } }
87struct _GPrivate
88{
89 /*< private >*/
90 gpointer p;
91 GDestroyNotify notify;
92 gpointer future[2];
93};
94
95typedef enum
96{
97 G_ONCE_STATUS_NOTCALLED,
98 G_ONCE_STATUS_PROGRESS,
99 G_ONCE_STATUS_READY
100} GOnceStatus;
101
102#define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
103struct _GOnce
104{
105 volatile GOnceStatus status;
106 volatile gpointer retval;
107};
108
109#define G_LOCK_NAME(name) g__ ## name ## _lock
110#define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
111#define G_LOCK_DEFINE(name) GMutex G_LOCK_NAME (name)
112#define G_LOCK_EXTERN(name) extern GMutex G_LOCK_NAME (name)
113
114#ifdef G_DEBUG_LOCKS
115# define G_LOCK(name) G_STMT_START{ \
116 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
117 "file %s: line %d (%s): locking: %s ", \
118 __FILE__, __LINE__, G_STRFUNC, \
119 #name); \
120 g_mutex_lock (&G_LOCK_NAME (name)); \
121 }G_STMT_END
122# define G_UNLOCK(name) G_STMT_START{ \
123 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
124 "file %s: line %d (%s): unlocking: %s ", \
125 __FILE__, __LINE__, G_STRFUNC, \
126 #name); \
127 g_mutex_unlock (&G_LOCK_NAME (name)); \
128 }G_STMT_END
129# define G_TRYLOCK(name) \
130 (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
131 "file %s: line %d (%s): try locking: %s ", \
132 __FILE__, __LINE__, G_STRFUNC, \
133 #name), g_mutex_trylock (&G_LOCK_NAME (name)))
134#else /* !G_DEBUG_LOCKS */
135# define G_LOCK(name) g_mutex_lock (&G_LOCK_NAME (name))
136# define G_UNLOCK(name) g_mutex_unlock (&G_LOCK_NAME (name))
137# define G_TRYLOCK(name) g_mutex_trylock (&G_LOCK_NAME (name))
138#endif /* !G_DEBUG_LOCKS */
139
140GLIB_AVAILABLE_IN_2_32
141GThread * g_thread_ref (GThread *thread);
142GLIB_AVAILABLE_IN_2_32
143void g_thread_unref (GThread *thread);
144GLIB_AVAILABLE_IN_2_32
145GThread * g_thread_new (const gchar *name,
146 GThreadFunc func,
147 gpointer data);
148GLIB_AVAILABLE_IN_2_32
149GThread * g_thread_try_new (const gchar *name,
150 GThreadFunc func,
151 gpointer data,
152 GError **error);
153GLIB_AVAILABLE_IN_ALL
154GThread * g_thread_self (void);
155GLIB_AVAILABLE_IN_ALL
156void g_thread_exit (gpointer retval);
157GLIB_AVAILABLE_IN_ALL
158gpointer g_thread_join (GThread *thread);
159GLIB_AVAILABLE_IN_ALL
160void g_thread_yield (void);
161
162
163GLIB_AVAILABLE_IN_2_32
164void g_mutex_init (GMutex *mutex);
165GLIB_AVAILABLE_IN_2_32
166void g_mutex_clear (GMutex *mutex);
167GLIB_AVAILABLE_IN_ALL
168void g_mutex_lock (GMutex *mutex);
169GLIB_AVAILABLE_IN_ALL
170gboolean g_mutex_trylock (GMutex *mutex);
171GLIB_AVAILABLE_IN_ALL
172void g_mutex_unlock (GMutex *mutex);
173
174GLIB_AVAILABLE_IN_2_32
175void g_rw_lock_init (GRWLock *rw_lock);
176GLIB_AVAILABLE_IN_2_32
177void g_rw_lock_clear (GRWLock *rw_lock);
178GLIB_AVAILABLE_IN_2_32
179void g_rw_lock_writer_lock (GRWLock *rw_lock);
180GLIB_AVAILABLE_IN_2_32
181gboolean g_rw_lock_writer_trylock (GRWLock *rw_lock);
182GLIB_AVAILABLE_IN_2_32
183void g_rw_lock_writer_unlock (GRWLock *rw_lock);
184GLIB_AVAILABLE_IN_2_32
185void g_rw_lock_reader_lock (GRWLock *rw_lock);
186GLIB_AVAILABLE_IN_2_32
187gboolean g_rw_lock_reader_trylock (GRWLock *rw_lock);
188GLIB_AVAILABLE_IN_2_32
189void g_rw_lock_reader_unlock (GRWLock *rw_lock);
190
191GLIB_AVAILABLE_IN_2_32
192void g_rec_mutex_init (GRecMutex *rec_mutex);
193GLIB_AVAILABLE_IN_2_32
194void g_rec_mutex_clear (GRecMutex *rec_mutex);
195GLIB_AVAILABLE_IN_2_32
196void g_rec_mutex_lock (GRecMutex *rec_mutex);
197GLIB_AVAILABLE_IN_2_32
198gboolean g_rec_mutex_trylock (GRecMutex *rec_mutex);
199GLIB_AVAILABLE_IN_2_32
200void g_rec_mutex_unlock (GRecMutex *rec_mutex);
201
202GLIB_AVAILABLE_IN_2_32
203void g_cond_init (GCond *cond);
204GLIB_AVAILABLE_IN_2_32
205void g_cond_clear (GCond *cond);
206GLIB_AVAILABLE_IN_ALL
207void g_cond_wait (GCond *cond,
208 GMutex *mutex);
209GLIB_AVAILABLE_IN_ALL
210void g_cond_signal (GCond *cond);
211GLIB_AVAILABLE_IN_ALL
212void g_cond_broadcast (GCond *cond);
213GLIB_AVAILABLE_IN_2_32
214gboolean g_cond_wait_until (GCond *cond,
215 GMutex *mutex,
216 gint64 end_time);
217
218GLIB_AVAILABLE_IN_ALL
219gpointer g_private_get (GPrivate *key);
220GLIB_AVAILABLE_IN_ALL
221void g_private_set (GPrivate *key,
222 gpointer value);
223GLIB_AVAILABLE_IN_2_32
224void g_private_replace (GPrivate *key,
225 gpointer value);
226
227GLIB_AVAILABLE_IN_ALL
228gpointer g_once_impl (GOnce *once,
229 GThreadFunc func,
230 gpointer arg);
231GLIB_AVAILABLE_IN_ALL
232gboolean g_once_init_enter (volatile void *location);
233GLIB_AVAILABLE_IN_ALL
234void g_once_init_leave (volatile void *location,
235 gsize result);
236
237#ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
238# define g_once(once, func, arg) g_once_impl ((once), (func), (arg))
239#else /* !G_ATOMIC_OP_MEMORY_BARRIER_NEEDED*/
240# define g_once(once, func, arg) \
241 (((once)->status == G_ONCE_STATUS_READY) ? \
242 (once)->retval : \
243 g_once_impl ((once), (func), (arg)))
244#endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
245
246#ifdef __GNUC__
247# define g_once_init_enter(location) \
248 (G_GNUC_EXTENSION ({ \
249 G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer)); \
250 (void) (0 ? (gpointer) *(location) : 0); \
251 (!g_atomic_pointer_get (location) && \
252 g_once_init_enter (location)); \
253 }))
254# define g_once_init_leave(location, result) \
255 (G_GNUC_EXTENSION ({ \
256 G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer)); \
257 (void) (0 ? *(location) = (result) : 0); \
258 g_once_init_leave ((location), (gsize) (result)); \
259 }))
260#else
261# define g_once_init_enter(location) \
262 (g_once_init_enter((location)))
263# define g_once_init_leave(location, result) \
264 (g_once_init_leave((location), (gsize) (result)))
265#endif
266
267GLIB_AVAILABLE_IN_2_36
268guint g_get_num_processors (void);
269
270/**
271 * GMutexLocker:
272 *
273 * Opaque type. See g_mutex_locker_new() for details.
274 * Since: 2.44
275 */
276typedef void GMutexLocker;
277
278/**
279 * g_mutex_locker_new:
280 * @mutex: a mutex to lock
281 *
282 * Lock @mutex and return a new #GMutexLocker. Unlock with
283 * g_mutex_locker_free(). Using g_mutex_unlock() on @mutex
284 * while a #GMutexLocker exists can lead to undefined behaviour.
285 *
286 * This is intended to be used with g_autoptr(). Note that g_autoptr()
287 * is only available when using GCC or clang, so the following example
288 * will only work with those compilers:
289 * |[
290 * typedef struct
291 * {
292 * ...
293 * GMutex mutex;
294 * ...
295 * } MyObject;
296 *
297 * static void
298 * my_object_do_stuff (MyObject *self)
299 * {
300 * g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&self->mutex);
301 *
302 * // Code with mutex locked here
303 *
304 * if (cond)
305 * // No need to unlock
306 * return;
307 *
308 * // Optionally early unlock
309 * g_clear_pointer (&locker, g_mutex_locker_free);
310 *
311 * // Code with mutex unlocked here
312 * }
313 * ]|
314 *
315 * Returns: a #GMutexLocker
316 * Since: 2.44
317 */
318static inline GMutexLocker *
319g_mutex_locker_new (GMutex *mutex)
320{
321 g_mutex_lock (mutex);
322 return (GMutexLocker *) mutex;
323}
324
325/**
326 * g_mutex_locker_free:
327 * @locker: a GMutexLocker
328 *
329 * Unlock @locker's mutex. See g_mutex_locker_new() for details.
330 *
331 * Since: 2.44
332 */
333static inline void
334g_mutex_locker_free (GMutexLocker *locker)
335{
336 g_mutex_unlock ((GMutex *) locker);
337}
338
339/**
340 * GRecMutexLocker:
341 *
342 * Opaque type. See g_rec_mutex_locker_new() for details.
343 * Since: 2.60
344 */
345typedef void GRecMutexLocker;
346
347/**
348 * g_rec_mutex_locker_new:
349 * @rec_mutex: a recursive mutex to lock
350 *
351 * Lock @rec_mutex and return a new #GRecMutexLocker. Unlock with
352 * g_rec_mutex_locker_free(). Using g_rec_mutex_unlock() on @rec_mutex
353 * while a #GRecMutexLocker exists can lead to undefined behaviour.
354 *
355 * This is intended to be used with g_autoptr(). Note that g_autoptr()
356 * is only available when using GCC or clang, so the following example
357 * will only work with those compilers:
358 * |[
359 * typedef struct
360 * {
361 * ...
362 * GRecMutex rec_mutex;
363 * ...
364 * } MyObject;
365 *
366 * static void
367 * my_object_do_stuff (MyObject *self)
368 * {
369 * g_autoptr(GRecMutexLocker) locker = g_rec_mutex_locker_new (&self->rec_mutex);
370 *
371 * // Code with rec_mutex locked here
372 *
373 * if (cond)
374 * // No need to unlock
375 * return;
376 *
377 * // Optionally early unlock
378 * g_clear_pointer (&locker, g_rec_mutex_locker_free);
379 *
380 * // Code with rec_mutex unlocked here
381 * }
382 * ]|
383 *
384 * Returns: a #GRecMutexLocker
385 * Since: 2.60
386 */
387static inline GRecMutexLocker *
388g_rec_mutex_locker_new (GRecMutex *rec_mutex)
389{
390 g_rec_mutex_lock (rec_mutex);
391 return (GRecMutexLocker *) rec_mutex;
392}
393
394/**
395 * g_rec_mutex_locker_free:
396 * @locker: a GRecMutexLocker
397 *
398 * Unlock @locker's recursive mutex. See g_rec_mutex_locker_new() for details.
399 *
400 * Since: 2.60
401 */
402static inline void
403g_rec_mutex_locker_free (GRecMutexLocker *locker)
404{
405 g_rec_mutex_unlock ((GRecMutex *) locker);
406}
407
408/**
409 * GRWLockWriterLocker:
410 *
411 * Opaque type. See g_rw_lock_writer_locker_new() for details.
412 * Since: 2.62
413 */
414typedef void GRWLockWriterLocker;
415
416/**
417 * g_rw_lock_writer_locker_new:
418 * @rw_lock: a #GRWLock
419 *
420 * Obtain a write lock on @rw_lock and return a new #GRWLockWriterLocker.
421 * Unlock with g_rw_lock_writer_locker_free(). Using g_rw_lock_writer_unlock()
422 * on @rw_lock while a #GRWLockWriterLocker exists can lead to undefined
423 * behaviour.
424 *
425 * This is intended to be used with g_autoptr(). Note that g_autoptr()
426 * is only available when using GCC or clang, so the following example
427 * will only work with those compilers:
428 * |[
429 * typedef struct
430 * {
431 * ...
432 * GRWLock rw_lock;
433 * GPtrArray *array;
434 * ...
435 * } MyObject;
436 *
437 * static gchar *
438 * my_object_get_data (MyObject *self, guint index)
439 * {
440 * g_autoptr(GRWLockReaderLocker) locker = g_rw_lock_reader_locker_new (&self->rw_lock);
441 *
442 * // Code with a read lock obtained on rw_lock here
443 *
444 * if (self->array == NULL)
445 * // No need to unlock
446 * return NULL;
447 *
448 * if (index < self->array->len)
449 * // No need to unlock
450 * return g_ptr_array_index (self->array, index);
451 *
452 * // Optionally early unlock
453 * g_clear_pointer (&locker, g_rw_lock_reader_locker_free);
454 *
455 * // Code with rw_lock unlocked here
456 * return NULL;
457 * }
458 *
459 * static void
460 * my_object_set_data (MyObject *self, guint index, gpointer data)
461 * {
462 * g_autoptr(GRWLockWriterLocker) locker = g_rw_lock_writer_locker_new (&self->rw_lock);
463 *
464 * // Code with a write lock obtained on rw_lock here
465 *
466 * if (self->array == NULL)
467 * self->array = g_ptr_array_new ();
468 *
469 * if (cond)
470 * // No need to unlock
471 * return;
472 *
473 * if (index >= self->array->len)
474 * g_ptr_array_set_size (self->array, index+1);
475 * g_ptr_array_index (self->array, index) = data;
476 *
477 * // Optionally early unlock
478 * g_clear_pointer (&locker, g_rw_lock_writer_locker_free);
479 *
480 * // Code with rw_lock unlocked here
481 * }
482 * ]|
483 *
484 * Returns: a #GRWLockWriterLocker
485 * Since: 2.62
486 */
487static inline GRWLockWriterLocker *
488g_rw_lock_writer_locker_new (GRWLock *rw_lock)
489{
490 g_rw_lock_writer_lock (rw_lock);
491 return (GRWLockWriterLocker *) rw_lock;
492}
493
494/**
495 * g_rw_lock_writer_locker_free:
496 * @locker: a GRWLockWriterLocker
497 *
498 * Release a write lock on @locker's read-write lock. See
499 * g_rw_lock_writer_locker_new() for details.
500 *
501 * Since: 2.62
502 */
503static inline void
504g_rw_lock_writer_locker_free (GRWLockWriterLocker *locker)
505{
506 g_rw_lock_writer_unlock ((GRWLock *) locker);
507}
508
509/**
510 * GRWLockReaderLocker:
511 *
512 * Opaque type. See g_rw_lock_reader_locker_new() for details.
513 * Since: 2.62
514 */
515typedef void GRWLockReaderLocker;
516
517/**
518 * g_rw_lock_reader_locker_new:
519 * @rw_lock: a #GRWLock
520 *
521 * Obtain a read lock on @rw_lock and return a new #GRWLockReaderLocker.
522 * Unlock with g_rw_lock_reader_locker_free(). Using g_rw_lock_reader_unlock()
523 * on @rw_lock while a #GRWLockReaderLocker exists can lead to undefined
524 * behaviour.
525 *
526 * This is intended to be used with g_autoptr(). For a code sample, see
527 * g_rw_lock_writer_locker_new().
528 *
529 * Returns: a #GRWLockReaderLocker
530 * Since: 2.62
531 */
532static inline GRWLockReaderLocker *
533g_rw_lock_reader_locker_new (GRWLock *rw_lock)
534{
535 g_rw_lock_reader_lock (rw_lock);
536 return (GRWLockReaderLocker *) rw_lock;
537}
538
539/**
540 * g_rw_lock_reader_locker_free:
541 * @locker: a GRWLockReaderLocker
542 *
543 * Release a read lock on @locker's read-write lock. See
544 * g_rw_lock_reader_locker_new() for details.
545 *
546 * Since: 2.62
547 */
548static inline void
549g_rw_lock_reader_locker_free (GRWLockReaderLocker *locker)
550{
551 g_rw_lock_reader_unlock ((GRWLock *) locker);
552}
553
554G_END_DECLS
555
556#endif /* __G_THREAD_H__ */
557