1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gthread.c: MT safety related functions
5 * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
26 #include "gmessages.h"
30 #include "gthreadprivate.h"
31 #include "deprecated/gthread.h"
35 /* {{{1 Documentation */
39 * @G_THREAD_PRIORITY_LOW: a priority lower than normal
40 * @G_THREAD_PRIORITY_NORMAL: the default priority
41 * @G_THREAD_PRIORITY_HIGH: a priority higher than normal
42 * @G_THREAD_PRIORITY_URGENT: the highest priority
44 * Deprecated:2.32: Thread priorities no longer have any effect.
49 * @mutex_new: virtual function pointer for g_mutex_new()
50 * @mutex_lock: virtual function pointer for g_mutex_lock()
51 * @mutex_trylock: virtual function pointer for g_mutex_trylock()
52 * @mutex_unlock: virtual function pointer for g_mutex_unlock()
53 * @mutex_free: virtual function pointer for g_mutex_free()
54 * @cond_new: virtual function pointer for g_cond_new()
55 * @cond_signal: virtual function pointer for g_cond_signal()
56 * @cond_broadcast: virtual function pointer for g_cond_broadcast()
57 * @cond_wait: virtual function pointer for g_cond_wait()
58 * @cond_timed_wait: virtual function pointer for g_cond_timed_wait()
59 * @cond_free: virtual function pointer for g_cond_free()
60 * @private_new: virtual function pointer for g_private_new()
61 * @private_get: virtual function pointer for g_private_get()
62 * @private_set: virtual function pointer for g_private_set()
63 * @thread_create: virtual function pointer for g_thread_create()
64 * @thread_yield: virtual function pointer for g_thread_yield()
65 * @thread_join: virtual function pointer for g_thread_join()
66 * @thread_exit: virtual function pointer for g_thread_exit()
67 * @thread_set_priority: virtual function pointer for
68 * g_thread_set_priority()
69 * @thread_self: virtual function pointer for g_thread_self()
70 * @thread_equal: used internally by recursive mutex locks and by some
73 * This function table is no longer used by g_thread_init()
74 * to initialize the thread system.
78 * G_THREADS_IMPL_POSIX:
80 * This macro is defined if POSIX style threads are used.
82 * Deprecated:2.32:POSIX threads are in use on all non-Windows systems.
83 * Use G_OS_WIN32 to detect Windows.
87 * G_THREADS_IMPL_WIN32:
89 * This macro is defined if Windows style threads are used.
91 * Deprecated:2.32:Use G_OS_WIN32 to detect Windows.
95 /* {{{1 Exported Variables */
97 gboolean g_thread_use_default_impl = TRUE;
99 GThreadFunctions g_thread_functions_for_glib_use =
127 return g_get_monotonic_time () * 1000;
130 guint64 (*g_thread_gettime) (void) = gettime;
132 /* Initialisation {{{1 ---------------------------------------------------- */
133 gboolean g_threads_got_initialized = TRUE;
134 GSystemThread zero_thread; /* This is initialized to all zero */
139 * @vtable: a function table of type #GThreadFunctions, that provides
140 * the entry points to the thread system to be used. Since 2.32,
141 * this parameter is ignored and should always be %NULL
143 * If you use GLib from more than one thread, you must initialize the
144 * thread system by calling g_thread_init().
146 * Since version 2.24, calling g_thread_init() multiple times is allowed,
147 * but nothing happens except for the first call.
149 * Since version 2.32, GLib does not support custom thread implementations
150 * anymore and the @vtable parameter is ignored and you should pass %NULL.
152 * <note><para>g_thread_init() must not be called directly or indirectly
153 * in a callback from GLib. Also no mutexes may be currently locked while
154 * calling g_thread_init().</para></note>
156 * <note><para>To use g_thread_init() in your program, you have to link
157 * with the libraries that the command <command>pkg-config --libs
158 * gthread-2.0</command> outputs. This is not the case for all the
159 * other thread-related functions of GLib. Those can be used without
160 * having to link with the thread libraries.</para></note>
164 * g_thread_get_initialized:
166 * Indicates if g_thread_init() has been called.
168 * Returns: %TRUE if threads have been initialized.
173 g_thread_get_initialized (void)
175 return g_thread_supported ();
178 /* We need this for ABI compatibility */
179 void g_thread_init_glib (void) { }
181 /* Internal variables {{{1 */
183 static GRealThread *g_thread_all_threads = NULL;
184 static GSList *g_thread_free_indices = NULL;
186 /* Protects g_thread_all_threads and g_thread_free_indices */
187 G_LOCK_DEFINE_STATIC (g_thread);
189 /* Misc. GThread functions {{{1 */
192 * g_thread_set_priority:
193 * @thread: a #GThread.
196 * This function does nothing.
198 * Deprecated:2.32: Thread priorities no longer have any effect.
201 g_thread_set_priority (GThread *thread,
202 GThreadPriority priority)
208 * @func: a function to execute in the new thread
209 * @data: an argument to supply to the new thread
210 * @joinable: should this thread be joinable?
211 * @error: return location for error, or %NULL
213 * This function creates a new thread.
215 * If @joinable is %TRUE, you can wait for this threads termination
216 * calling g_thread_join(). Otherwise the thread will just disappear
217 * when it terminates.
219 * The new thread executes the function @func with the argument @data.
220 * If the thread was created successfully, it is returned.
222 * @error can be %NULL to ignore errors, or non-%NULL to report errors.
223 * The error is set, if and only if the function returns %NULL.
225 * Returns: the new #GThread on success
227 * Deprecated:2.32: Use g_thread_new() instead
230 g_thread_create (GThreadFunc func,
235 return g_thread_new_internal (NULL, func, data, joinable, 0, TRUE, error);
239 * g_thread_create_full:
240 * @func: a function to execute in the new thread.
241 * @data: an argument to supply to the new thread.
242 * @stack_size: a stack size for the new thread.
243 * @joinable: should this thread be joinable?
246 * @error: return location for error.
247 * @Returns: the new #GThread on success.
249 * This function creates a new thread.
251 * Deprecated:2.32: The @bound and @priority arguments are now ignored.
252 * Use g_thread_new() or g_thread_new_full() instead.
255 g_thread_create_full (GThreadFunc func,
260 GThreadPriority priority,
263 return g_thread_new_internal (NULL, func, data, joinable, stack_size, TRUE, error);
268 * @thread_func: function to call for all #GThread structures
269 * @user_data: second argument to @thread_func
271 * Call @thread_func on all #GThreads that have been
272 * created with g_thread_create().
274 * Note that threads may decide to exit while @thread_func is
275 * running, so without intimate knowledge about the lifetime of
276 * foreign threads, @thread_func shouldn't access the GThread*
277 * pointer passed in as first argument. However, @thread_func will
278 * not be called for threads which are known to have exited already.
280 * Due to thread lifetime checks, this function has an execution complexity
281 * which is quadratic in the number of existing threads.
285 * Deprecated:2.32: There aren't many things you can do with a #GThread,
286 * except comparing it with one that was returned from g_thread_create().
287 * There are better ways to find out if your thread is still alive.
290 g_thread_foreach (GFunc thread_func,
293 GSList *slist = NULL;
295 g_return_if_fail (thread_func != NULL);
296 /* snapshot the list of threads for iteration */
298 for (thread = g_thread_all_threads; thread; thread = thread->next)
299 slist = g_slist_prepend (slist, thread);
301 /* walk the list, skipping non-existent threads */
304 GSList *node = slist;
306 /* check whether the current thread still exists */
308 for (thread = g_thread_all_threads; thread; thread = thread->next)
309 if (thread == node->data)
313 thread_func (thread, user_data);
314 g_slist_free_1 (node);
319 g_enumerable_thread_add (GRealThread *thread)
322 thread->next = g_thread_all_threads;
323 g_thread_all_threads = thread;
328 g_enumerable_thread_remove (GRealThread *thread)
333 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
340 g_thread_all_threads = t->next;
347 /* GOnce {{{1 ------------------------------------------------------------- */
349 g_once_init_enter_impl (volatile gsize *location)
351 return (g_once_init_enter) (location);
354 /* GStaticMutex {{{1 ------------------------------------------------------ */
359 * A #GStaticMutex works like a #GMutex.
361 * Prior to GLib 2.32, GStaticMutex had the significant advantage
362 * that it doesn't need to be created at run-time, but can be defined
363 * at compile-time. Since 2.32, #GMutex can be statically allocated
364 * as well, and GStaticMutex has been deprecated.
366 * Here is a version of our give_me_next_number() example using
371 * Using <structname>GStaticMutex</structname>
372 * to simplify thread-safe programming
376 * give_me_next_number (void)
378 * static int current_number = 0;
380 * static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
382 * g_static_mutex_lock (&mutex);
383 * ret_val = current_number = calc_next_number (current_number);
384 * g_static_mutex_unlock (&mutex);
391 * Sometimes you would like to dynamically create a mutex. If you don't
392 * want to require prior calling to g_thread_init(), because your code
393 * should also be usable in non-threaded programs, you are not able to
394 * use g_mutex_new() and thus #GMutex, as that requires a prior call to
395 * g_thread_init(). In theses cases you can also use a #GStaticMutex.
396 * It must be initialized with g_static_mutex_init() before using it
397 * and freed with with g_static_mutex_free() when not needed anymore to
398 * free up any allocated resources.
400 * Even though #GStaticMutex is not opaque, it should only be used with
401 * the following functions, as it is defined differently on different
404 * All of the <function>g_static_mutex_*</function> functions apart
405 * from <function>g_static_mutex_get_mutex</function> can also be used
406 * even if g_thread_init() has not yet been called. Then they do
407 * nothing, apart from <function>g_static_mutex_trylock</function>,
408 * which does nothing but returning %TRUE.
410 * <note><para>All of the <function>g_static_mutex_*</function>
411 * functions are actually macros. Apart from taking their addresses, you
412 * can however use them as if they were functions.</para></note>
416 * G_STATIC_MUTEX_INIT:
418 * A #GStaticMutex must be initialized with this macro, before it can
419 * be used. This macro can used be to initialize a variable, but it
420 * cannot be assigned to a variable. In that case you have to use
421 * g_static_mutex_init().
424 * GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
429 * g_static_mutex_init:
430 * @mutex: a #GStaticMutex to be initialized.
432 * Initializes @mutex.
433 * Alternatively you can initialize it with #G_STATIC_MUTEX_INIT.
435 * Deprecated: 2.32: Use g_mutex_init()
438 g_static_mutex_init (GStaticMutex *mutex)
440 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
442 g_return_if_fail (mutex);
447 /* IMPLEMENTATION NOTE:
449 * On some platforms a GStaticMutex is actually a normal GMutex stored
450 * inside of a structure instead of being allocated dynamically. We can
451 * only do this for platforms on which we know, in advance, how to
452 * allocate (size) and initialise (value) that memory.
454 * On other platforms, a GStaticMutex is nothing more than a pointer to
455 * a GMutex. In that case, the first access we make to the static mutex
456 * must first allocate the normal GMutex and store it into the pointer.
458 * configure.ac writes macros into glibconfig.h to determine if
459 * g_static_mutex_get_mutex() accesses the structure in memory directly
460 * (on platforms where we are able to do that) or if it ends up here,
461 * where we may have to allocate the GMutex before returning it.
465 * g_static_mutex_get_mutex:
466 * @mutex: a #GStaticMutex.
467 * @Returns: the #GMutex corresponding to @mutex.
469 * For some operations (like g_cond_wait()) you must have a #GMutex
470 * instead of a #GStaticMutex. This function will return the
471 * corresponding #GMutex for @mutex.
473 * Deprecated: 2.32: Just use a #GMutex
476 g_static_mutex_get_mutex_impl (GMutex** mutex)
480 if (!g_thread_supported ())
483 result = g_atomic_pointer_get (mutex);
487 g_mutex_lock (&g_once_mutex);
492 result = g_mutex_new ();
493 g_atomic_pointer_set (mutex, result);
496 g_mutex_unlock (&g_once_mutex);
502 /* IMPLEMENTATION NOTE:
504 * g_static_mutex_lock(), g_static_mutex_trylock() and
505 * g_static_mutex_unlock() are all preprocessor macros that wrap the
506 * corresponding g_mutex_*() function around a call to
507 * g_static_mutex_get_mutex().
511 * g_static_mutex_lock:
512 * @mutex: a #GStaticMutex.
514 * Works like g_mutex_lock(), but for a #GStaticMutex.
516 * Deprecated: 2.32: Use g_mutex_lock()
520 * g_static_mutex_trylock:
521 * @mutex: a #GStaticMutex.
522 * @Returns: %TRUE, if the #GStaticMutex could be locked.
524 * Works like g_mutex_trylock(), but for a #GStaticMutex.
526 * Deprecated: 2.32: Use g_mutex_trylock()
530 * g_static_mutex_unlock:
531 * @mutex: a #GStaticMutex.
533 * Works like g_mutex_unlock(), but for a #GStaticMutex.
535 * Deprecated: 2.32: Use g_mutex_unlock()
539 * g_static_mutex_free:
540 * @mutex: a #GStaticMutex to be freed.
542 * Releases all resources allocated to @mutex.
544 * You don't have to call this functions for a #GStaticMutex with an
545 * unbounded lifetime, i.e. objects declared 'static', but if you have
546 * a #GStaticMutex as a member of a structure and the structure is
547 * freed, you should also free the #GStaticMutex.
549 * <note><para>Calling g_static_mutex_free() on a locked mutex may
550 * result in undefined behaviour.</para></note>
552 * Deprecated: 2.32: Use g_mutex_free()
555 g_static_mutex_free (GStaticMutex* mutex)
557 GMutex **runtime_mutex;
559 g_return_if_fail (mutex);
561 /* The runtime_mutex is the first (or only) member of GStaticMutex,
562 * see both versions (of glibconfig.h) in configure.ac. Note, that
563 * this variable is NULL, if g_thread_init() hasn't been called or
564 * if we're using the default thread implementation and it provides
566 runtime_mutex = ((GMutex**)mutex);
569 g_mutex_free (*runtime_mutex);
571 *runtime_mutex = NULL;
574 /* {{{1 GStaticRecMutex */
579 * A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
580 * multiple times by one thread. If you enter it n times, you have to
581 * unlock it n times again to let other threads lock it. An exception
582 * is the function g_static_rec_mutex_unlock_full(): that allows you to
583 * unlock a #GStaticRecMutex completely returning the depth, (i.e. the
584 * number of times this mutex was locked). The depth can later be used
585 * to restore the state of the #GStaticRecMutex by calling
586 * g_static_rec_mutex_lock_full(). In GLib 2.32, #GStaticRecMutex has
587 * been deprecated in favor of #GRecMutex.
589 * Even though #GStaticRecMutex is not opaque, it should only be used
590 * with the following functions.
592 * All of the <function>g_static_rec_mutex_*</function> functions can
593 * be used even if g_thread_init() has not been called. Then they do
594 * nothing, apart from <function>g_static_rec_mutex_trylock</function>,
595 * which does nothing but returning %TRUE.
599 * G_STATIC_REC_MUTEX_INIT:
601 * A #GStaticRecMutex must be initialized with this macro before it can
602 * be used. This macro can used be to initialize a variable, but it
603 * cannot be assigned to a variable. In that case you have to use
604 * g_static_rec_mutex_init().
607 * GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
612 * g_static_rec_mutex_init:
613 * @mutex: a #GStaticRecMutex to be initialized.
615 * A #GStaticRecMutex must be initialized with this function before it
616 * can be used. Alternatively you can initialize it with
617 * #G_STATIC_REC_MUTEX_INIT.
619 * Deprecated: 2.32: Use g_rec_mutex_init()
622 g_static_rec_mutex_init (GStaticRecMutex *mutex)
624 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
626 g_return_if_fail (mutex);
632 * g_static_rec_mutex_lock:
633 * @mutex: a #GStaticRecMutex to lock.
635 * Locks @mutex. If @mutex is already locked by another thread, the
636 * current thread will block until @mutex is unlocked by the other
637 * thread. If @mutex is already locked by the calling thread, this
638 * functions increases the depth of @mutex and returns immediately.
640 * Deprecated: 2.32: Use g_rec_mutex_lock()
643 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
647 g_return_if_fail (mutex);
649 if (!g_thread_supported ())
652 g_system_thread_self (&self);
654 if (g_system_thread_equal (&self, &mutex->owner))
659 g_static_mutex_lock (&mutex->mutex);
660 g_system_thread_assign (mutex->owner, self);
665 * g_static_rec_mutex_trylock:
666 * @mutex: a #GStaticRecMutex to lock.
667 * @Returns: %TRUE, if @mutex could be locked.
669 * Tries to lock @mutex. If @mutex is already locked by another thread,
670 * it immediately returns %FALSE. Otherwise it locks @mutex and returns
671 * %TRUE. If @mutex is already locked by the calling thread, this
672 * functions increases the depth of @mutex and immediately returns
675 * Deprecated: 2.32: Use g_rec_mutex_trylock()
678 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
682 g_return_val_if_fail (mutex, FALSE);
684 if (!g_thread_supported ())
687 g_system_thread_self (&self);
689 if (g_system_thread_equal (&self, &mutex->owner))
695 if (!g_static_mutex_trylock (&mutex->mutex))
698 g_system_thread_assign (mutex->owner, self);
704 * g_static_rec_mutex_unlock:
705 * @mutex: a #GStaticRecMutex to unlock.
707 * Unlocks @mutex. Another thread will be allowed to lock @mutex only
708 * when it has been unlocked as many times as it had been locked
709 * before. If @mutex is completely unlocked and another thread is
710 * blocked in a g_static_rec_mutex_lock() call for @mutex, it will be
711 * woken and can lock @mutex itself.
713 * Deprecated: 2.32: Use g_rec_mutex_unlock()
716 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
718 g_return_if_fail (mutex);
720 if (!g_thread_supported ())
723 if (mutex->depth > 1)
728 g_system_thread_assign (mutex->owner, zero_thread);
729 g_static_mutex_unlock (&mutex->mutex);
733 * g_static_rec_mutex_lock_full:
734 * @mutex: a #GStaticRecMutex to lock.
735 * @depth: number of times this mutex has to be unlocked to be
736 * completely unlocked.
738 * Works like calling g_static_rec_mutex_lock() for @mutex @depth times.
740 * Deprecated: 2.32: Use g_rec_mutex_lock()
743 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
747 g_return_if_fail (mutex);
749 if (!g_thread_supported ())
755 g_system_thread_self (&self);
757 if (g_system_thread_equal (&self, &mutex->owner))
759 mutex->depth += depth;
762 g_static_mutex_lock (&mutex->mutex);
763 g_system_thread_assign (mutex->owner, self);
764 mutex->depth = depth;
768 * g_static_rec_mutex_unlock_full:
769 * @mutex: a #GStaticRecMutex to completely unlock.
770 * @Returns: number of times @mutex has been locked by the current
773 * Completely unlocks @mutex. If another thread is blocked in a
774 * g_static_rec_mutex_lock() call for @mutex, it will be woken and can
775 * lock @mutex itself. This function returns the number of times that
776 * @mutex has been locked by the current thread. To restore the state
777 * before the call to g_static_rec_mutex_unlock_full() you can call
778 * g_static_rec_mutex_lock_full() with the depth returned by this
781 * Deprecated: 2.32: Use g_rec_mutex_unlock()
784 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
788 g_return_val_if_fail (mutex, 0);
790 if (!g_thread_supported ())
793 depth = mutex->depth;
795 g_system_thread_assign (mutex->owner, zero_thread);
797 g_static_mutex_unlock (&mutex->mutex);
803 * g_static_rec_mutex_free:
804 * @mutex: a #GStaticRecMutex to be freed.
806 * Releases all resources allocated to a #GStaticRecMutex.
808 * You don't have to call this functions for a #GStaticRecMutex with an
809 * unbounded lifetime, i.e. objects declared 'static', but if you have
810 * a #GStaticRecMutex as a member of a structure and the structure is
811 * freed, you should also free the #GStaticRecMutex.
813 * Deprecated: 2.32: Use g_rec_mutex_clear()
816 g_static_rec_mutex_free (GStaticRecMutex *mutex)
818 g_return_if_fail (mutex);
820 g_static_mutex_free (&mutex->mutex);
823 /* GStaticRWLock {{{1 ----------------------------------------------------- */
828 * The #GStaticRWLock struct represents a read-write lock. A read-write
829 * lock can be used for protecting data that some portions of code only
830 * read from, while others also write. In such situations it is
831 * desirable that several readers can read at once, whereas of course
832 * only one writer may write at a time. Take a look at the following
836 * <title>An array with access functions</title>
838 * GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
842 * my_array_get (guint index)
844 * gpointer retval = NULL;
849 * g_static_rw_lock_reader_lock (&rwlock);
850 * if (index < array->len)
851 * retval = g_ptr_array_index (array, index);
852 * g_static_rw_lock_reader_unlock (&rwlock);
858 * my_array_set (guint index, gpointer data)
860 * g_static_rw_lock_writer_lock (&rwlock);
863 * array = g_ptr_array_new (<!-- -->);
865 * if (index >= array->len)
866 * g_ptr_array_set_size (array, index+1);
867 * g_ptr_array_index (array, index) = data;
869 * g_static_rw_lock_writer_unlock (&rwlock);
874 * This example shows an array which can be accessed by many readers
875 * (the <function>my_array_get()</function> function) simultaneously,
876 * whereas the writers (the <function>my_array_set()</function>
877 * function) will only be allowed once at a time and only if no readers
878 * currently access the array. This is because of the potentially
879 * dangerous resizing of the array. Using these functions is fully
880 * multi-thread safe now.
882 * Most of the time, writers should have precedence over readers. That
883 * means, for this implementation, that as soon as a writer wants to
884 * lock the data, no other reader is allowed to lock the data, whereas,
885 * of course, the readers that already have locked the data are allowed
886 * to finish their operation. As soon as the last reader unlocks the
887 * data, the writer will lock it.
889 * Even though #GStaticRWLock is not opaque, it should only be used
890 * with the following functions.
892 * All of the <function>g_static_rw_lock_*</function> functions can be
893 * used even if g_thread_init() has not been called. Then they do
894 * nothing, apart from <function>g_static_rw_lock_*_trylock</function>,
895 * which does nothing but returning %TRUE.
897 * <note><para>A read-write lock has a higher overhead than a mutex. For
898 * example, both g_static_rw_lock_reader_lock() and
899 * g_static_rw_lock_reader_unlock() have to lock and unlock a
900 * #GStaticMutex, so it takes at least twice the time to lock and unlock
901 * a #GStaticRWLock that it does to lock and unlock a #GStaticMutex. So
902 * only data structures that are accessed by multiple readers, and which
903 * keep the lock for a considerable time justify a #GStaticRWLock. The
904 * above example most probably would fare better with a
905 * #GStaticMutex.</para></note>
907 * Deprecated: 2.32: Use a #GRWLock instead
911 * G_STATIC_RW_LOCK_INIT:
913 * A #GStaticRWLock must be initialized with this macro before it can
914 * be used. This macro can used be to initialize a variable, but it
915 * cannot be assigned to a variable. In that case you have to use
916 * g_static_rw_lock_init().
919 * GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
924 * g_static_rw_lock_init:
925 * @lock: a #GStaticRWLock to be initialized.
927 * A #GStaticRWLock must be initialized with this function before it
928 * can be used. Alternatively you can initialize it with
929 * #G_STATIC_RW_LOCK_INIT.
931 * Deprecated: 2.32: Use g_rw_lock_init() instead
934 g_static_rw_lock_init (GStaticRWLock* lock)
936 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
938 g_return_if_fail (lock);
944 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
947 *cond = g_cond_new ();
948 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
952 g_static_rw_lock_signal (GStaticRWLock* lock)
954 if (lock->want_to_write && lock->write_cond)
955 g_cond_signal (lock->write_cond);
956 else if (lock->want_to_read && lock->read_cond)
957 g_cond_broadcast (lock->read_cond);
961 * g_static_rw_lock_reader_lock:
962 * @lock: a #GStaticRWLock to lock for reading.
964 * Locks @lock for reading. There may be unlimited concurrent locks for
965 * reading of a #GStaticRWLock at the same time. If @lock is already
966 * locked for writing by another thread or if another thread is already
967 * waiting to lock @lock for writing, this function will block until
968 * @lock is unlocked by the other writing thread and no other writing
969 * threads want to lock @lock. This lock has to be unlocked by
970 * g_static_rw_lock_reader_unlock().
972 * #GStaticRWLock is not recursive. It might seem to be possible to
973 * recursively lock for reading, but that can result in a deadlock, due
974 * to writer preference.
976 * Deprecated: 2.32: Use g_rw_lock_reader_lock() instead
979 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
981 g_return_if_fail (lock);
983 if (!g_threads_got_initialized)
986 g_static_mutex_lock (&lock->mutex);
987 lock->want_to_read++;
988 while (lock->have_writer || lock->want_to_write)
989 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
990 lock->want_to_read--;
991 lock->read_counter++;
992 g_static_mutex_unlock (&lock->mutex);
996 * g_static_rw_lock_reader_trylock:
997 * @lock: a #GStaticRWLock to lock for reading.
998 * @Returns: %TRUE, if @lock could be locked for reading.
1000 * Tries to lock @lock for reading. If @lock is already locked for
1001 * writing by another thread or if another thread is already waiting to
1002 * lock @lock for writing, immediately returns %FALSE. Otherwise locks
1003 * @lock for reading and returns %TRUE. This lock has to be unlocked by
1004 * g_static_rw_lock_reader_unlock().
1006 * Deprectated: 2.32: Use g_rw_lock_reader_trylock() instead
1009 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
1011 gboolean ret_val = FALSE;
1013 g_return_val_if_fail (lock, FALSE);
1015 if (!g_threads_got_initialized)
1018 g_static_mutex_lock (&lock->mutex);
1019 if (!lock->have_writer && !lock->want_to_write)
1021 lock->read_counter++;
1024 g_static_mutex_unlock (&lock->mutex);
1029 * g_static_rw_lock_reader_unlock:
1030 * @lock: a #GStaticRWLock to unlock after reading.
1032 * Unlocks @lock. If a thread waits to lock @lock for writing and all
1033 * locks for reading have been unlocked, the waiting thread is woken up
1034 * and can lock @lock for writing.
1036 * Deprectated: 2.32: Use g_rw_lock_reader_unlock() instead
1039 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
1041 g_return_if_fail (lock);
1043 if (!g_threads_got_initialized)
1046 g_static_mutex_lock (&lock->mutex);
1047 lock->read_counter--;
1048 if (lock->read_counter == 0)
1049 g_static_rw_lock_signal (lock);
1050 g_static_mutex_unlock (&lock->mutex);
1054 * g_static_rw_lock_writer_lock:
1055 * @lock: a #GStaticRWLock to lock for writing.
1057 * Locks @lock for writing. If @lock is already locked for writing or
1058 * reading by other threads, this function will block until @lock is
1059 * completely unlocked and then lock @lock for writing. While this
1060 * functions waits to lock @lock, no other thread can lock @lock for
1061 * reading. When @lock is locked for writing, no other thread can lock
1062 * @lock (neither for reading nor writing). This lock has to be
1063 * unlocked by g_static_rw_lock_writer_unlock().
1065 * Deprectated: 2.32: Use g_rw_lock_writer_lock() instead
1068 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
1070 g_return_if_fail (lock);
1072 if (!g_threads_got_initialized)
1075 g_static_mutex_lock (&lock->mutex);
1076 lock->want_to_write++;
1077 while (lock->have_writer || lock->read_counter)
1078 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
1079 lock->want_to_write--;
1080 lock->have_writer = TRUE;
1081 g_static_mutex_unlock (&lock->mutex);
1085 * g_static_rw_lock_writer_trylock:
1086 * @lock: a #GStaticRWLock to lock for writing.
1087 * @Returns: %TRUE, if @lock could be locked for writing.
1089 * Tries to lock @lock for writing. If @lock is already locked (for
1090 * either reading or writing) by another thread, it immediately returns
1091 * %FALSE. Otherwise it locks @lock for writing and returns %TRUE. This
1092 * lock has to be unlocked by g_static_rw_lock_writer_unlock().
1094 * Deprectated: 2.32: Use g_rw_lock_writer_trylock() instead
1097 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
1099 gboolean ret_val = FALSE;
1101 g_return_val_if_fail (lock, FALSE);
1103 if (!g_threads_got_initialized)
1106 g_static_mutex_lock (&lock->mutex);
1107 if (!lock->have_writer && !lock->read_counter)
1109 lock->have_writer = TRUE;
1112 g_static_mutex_unlock (&lock->mutex);
1117 * g_static_rw_lock_writer_unlock:
1118 * @lock: a #GStaticRWLock to unlock after writing.
1120 * Unlocks @lock. If a thread is waiting to lock @lock for writing and
1121 * all locks for reading have been unlocked, the waiting thread is
1122 * woken up and can lock @lock for writing. If no thread is waiting to
1123 * lock @lock for writing, and some thread or threads are waiting to
1124 * lock @lock for reading, the waiting threads are woken up and can
1125 * lock @lock for reading.
1127 * Deprectated: 2.32: Use g_rw_lock_writer_unlock() instead
1130 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
1132 g_return_if_fail (lock);
1134 if (!g_threads_got_initialized)
1137 g_static_mutex_lock (&lock->mutex);
1138 lock->have_writer = FALSE;
1139 g_static_rw_lock_signal (lock);
1140 g_static_mutex_unlock (&lock->mutex);
1144 * g_static_rw_lock_free:
1145 * @lock: a #GStaticRWLock to be freed.
1147 * Releases all resources allocated to @lock.
1149 * You don't have to call this functions for a #GStaticRWLock with an
1150 * unbounded lifetime, i.e. objects declared 'static', but if you have
1151 * a #GStaticRWLock as a member of a structure, and the structure is
1152 * freed, you should also free the #GStaticRWLock.
1154 * Deprecated: 2.32: Use a #GRWLock instead
1157 g_static_rw_lock_free (GStaticRWLock* lock)
1159 g_return_if_fail (lock);
1161 if (lock->read_cond)
1163 g_cond_free (lock->read_cond);
1164 lock->read_cond = NULL;
1166 if (lock->write_cond)
1168 g_cond_free (lock->write_cond);
1169 lock->write_cond = NULL;
1171 g_static_mutex_free (&lock->mutex);
1174 /* GPrivate {{{1 ------------------------------------------------------ */
1178 * @notify: a #GDestroyNotify
1180 * Deprecated:2.32: dynamic allocation of #GPrivate is a bad idea. Use
1181 * static storage and G_PRIVATE_INIT() instead.
1183 * Returns: a newly allocated #GPrivate (which can never be destroyed)
1186 g_private_new (GDestroyNotify notify)
1188 GPrivate tmp = G_PRIVATE_INIT (notify);
1191 key = g_slice_new (GPrivate);
1197 /* {{{1 GStaticPrivate */
1199 typedef struct _GStaticPrivateNode GStaticPrivateNode;
1200 struct _GStaticPrivateNode
1203 GDestroyNotify destroy;
1204 GStaticPrivate *owner;
1210 * A #GStaticPrivate works almost like a #GPrivate, but it has one
1211 * significant advantage. It doesn't need to be created at run-time
1212 * like a #GPrivate, but can be defined at compile-time. This is
1213 * similar to the difference between #GMutex and #GStaticMutex. Now
1214 * look at our <function>give_me_next_number()</function> example with
1218 * <title>Using GStaticPrivate for per-thread data</title>
1221 * give_me_next_number (<!-- -->)
1223 * static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1224 * int *current_number = g_static_private_get (&current_number_key);
1226 * if (!current_number)
1228 * current_number = g_new (int,1);
1229 * *current_number = 0;
1230 * g_static_private_set (&current_number_key, current_number, g_free);
1233 * *current_number = calc_next_number (*current_number);
1235 * return *current_number;
1242 * G_STATIC_PRIVATE_INIT:
1244 * Every #GStaticPrivate must be initialized with this macro, before it
1248 * GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1253 * g_static_private_init:
1254 * @private_key: a #GStaticPrivate to be initialized
1256 * Initializes @private_key. Alternatively you can initialize it with
1257 * #G_STATIC_PRIVATE_INIT.
1260 g_static_private_init (GStaticPrivate *private_key)
1262 private_key->index = 0;
1266 * g_static_private_get:
1267 * @private_key: a #GStaticPrivate
1269 * Works like g_private_get() only for a #GStaticPrivate.
1271 * This function works even if g_thread_init() has not yet been called.
1273 * Returns: the corresponding pointer
1276 g_static_private_get (GStaticPrivate *private_key)
1278 GRealThread *self = (GRealThread*) g_thread_self ();
1280 gpointer ret = NULL;
1281 array = self->private_data;
1283 if (array && private_key->index != 0 && private_key->index <= array->len)
1285 GStaticPrivateNode *node;
1287 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1289 /* Deal with the possibility that the GStaticPrivate which used
1290 * to have this index got freed and the index got allocated to
1291 * a new one. In this case, the data in the node is stale, so
1292 * free it and return NULL.
1294 if (G_UNLIKELY (node->owner != private_key))
1297 node->destroy (node->data);
1298 node->destroy = NULL;
1309 * g_static_private_set:
1310 * @private_key: a #GStaticPrivate
1311 * @data: the new pointer
1312 * @notify: a function to be called with the pointer whenever the
1313 * current thread ends or sets this pointer again
1315 * Sets the pointer keyed to @private_key for the current thread and
1316 * the function @notify to be called with that pointer (%NULL or
1317 * non-%NULL), whenever the pointer is set again or whenever the
1318 * current thread ends.
1320 * This function works even if g_thread_init() has not yet been called.
1321 * If g_thread_init() is called later, the @data keyed to @private_key
1322 * will be inherited only by the main thread, i.e. the one that called
1325 * <note><para>@notify is used quite differently from @destructor in
1326 * g_private_new().</para></note>
1329 g_static_private_set (GStaticPrivate *private_key,
1331 GDestroyNotify notify)
1333 GRealThread *self = (GRealThread*) g_thread_self ();
1335 static guint next_index = 0;
1336 GStaticPrivateNode *node;
1338 if (!private_key->index)
1342 if (!private_key->index)
1344 if (g_thread_free_indices)
1346 private_key->index = GPOINTER_TO_UINT (g_thread_free_indices->data);
1347 g_thread_free_indices = g_slist_delete_link (g_thread_free_indices,
1348 g_thread_free_indices);
1351 private_key->index = ++next_index;
1354 G_UNLOCK (g_thread);
1357 array = self->private_data;
1360 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
1361 self->private_data = array;
1363 if (private_key->index > array->len)
1364 g_array_set_size (array, private_key->index);
1366 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1369 node->destroy (node->data);
1372 node->destroy = notify;
1373 node->owner = private_key;
1377 * g_static_private_free:
1378 * @private_key: a #GStaticPrivate to be freed
1380 * Releases all resources allocated to @private_key.
1382 * You don't have to call this functions for a #GStaticPrivate with an
1383 * unbounded lifetime, i.e. objects declared 'static', but if you have
1384 * a #GStaticPrivate as a member of a structure and the structure is
1385 * freed, you should also free the #GStaticPrivate.
1388 g_static_private_free (GStaticPrivate *private_key)
1390 guint idx = private_key->index;
1395 private_key->index = 0;
1397 /* Freeing the per-thread data is deferred to either the
1398 * thread end or the next g_static_private_get() call for
1402 g_thread_free_indices = g_slist_prepend (g_thread_free_indices,
1403 GUINT_TO_POINTER (idx));
1404 G_UNLOCK (g_thread);
1408 g_static_private_cleanup (GRealThread *thread)
1412 array = thread->private_data;
1413 thread->private_data = NULL;
1419 for (i = 0; i < array->len; i++ )
1421 GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
1423 node->destroy (node->data);
1425 g_array_free (array, TRUE);
1429 /* GMutex {{{1 ------------------------------------------------------ */
1434 * Allocates and initializes a new #GMutex.
1436 * Returns: a newly allocated #GMutex. Use g_mutex_free() to free
1438 * Deprecated:3.32:GMutex can now be statically allocated, or embedded
1439 * in structures and initialised with g_mutex_init().
1446 mutex = g_slice_new (GMutex);
1447 g_mutex_init (mutex);
1456 * Destroys a @mutex that has been created with g_mutex_new().
1458 * Calling g_mutex_free() on a locked mutex may result
1459 * in undefined behaviour.
1461 * Deprecated:3.32:GMutex can now be statically allocated, or embedded
1462 * in structures and initialised with g_mutex_init().
1465 g_mutex_free (GMutex *mutex)
1467 g_mutex_clear (mutex);
1468 g_slice_free (GMutex, mutex);
1471 /* GCond {{{1 ------------------------------------------------------ */
1476 * Allocates and initializes a new #GCond.
1478 * Returns: a newly allocated #GCond. Free with g_cond_free()
1480 * Deprecated:3.32:GCond can now be statically allocated, or embedded
1481 * in structures and initialised with g_cond_init().
1488 cond = g_slice_new (GCond);
1498 * Destroys a #GCond that has been created with g_cond_new().
1500 * Calling g_cond_free() for a #GCond on which threads are
1501 * blocking leads to undefined behaviour.
1503 * Deprecated:3.32:GCond can now be statically allocated, or embedded
1504 * in structures and initialised with g_cond_init().
1507 g_cond_free (GCond *cond)
1509 g_cond_clear (cond);
1510 g_slice_free (GCond, cond);
1514 /* vim: set foldmethod=marker: */