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 */
38 * SECTION:threads-deprecated
39 * @title: Deprecated thread API
40 * @short_description: old thread APIs (for reference only)
43 * These APIs are deprecated. You should not use them in new code.
44 * This section remains only to assist with understanding code that was
45 * written to use these APIs at some point in the past.
50 * @G_THREAD_PRIORITY_LOW: a priority lower than normal
51 * @G_THREAD_PRIORITY_NORMAL: the default priority
52 * @G_THREAD_PRIORITY_HIGH: a priority higher than normal
53 * @G_THREAD_PRIORITY_URGENT: the highest priority
55 * Deprecated:2.32: Thread priorities no longer have any effect.
60 * @mutex_new: virtual function pointer for g_mutex_new()
61 * @mutex_lock: virtual function pointer for g_mutex_lock()
62 * @mutex_trylock: virtual function pointer for g_mutex_trylock()
63 * @mutex_unlock: virtual function pointer for g_mutex_unlock()
64 * @mutex_free: virtual function pointer for g_mutex_free()
65 * @cond_new: virtual function pointer for g_cond_new()
66 * @cond_signal: virtual function pointer for g_cond_signal()
67 * @cond_broadcast: virtual function pointer for g_cond_broadcast()
68 * @cond_wait: virtual function pointer for g_cond_wait()
69 * @cond_timed_wait: virtual function pointer for g_cond_timed_wait()
70 * @cond_free: virtual function pointer for g_cond_free()
71 * @private_new: virtual function pointer for g_private_new()
72 * @private_get: virtual function pointer for g_private_get()
73 * @private_set: virtual function pointer for g_private_set()
74 * @thread_create: virtual function pointer for g_thread_create()
75 * @thread_yield: virtual function pointer for g_thread_yield()
76 * @thread_join: virtual function pointer for g_thread_join()
77 * @thread_exit: virtual function pointer for g_thread_exit()
78 * @thread_set_priority: virtual function pointer for
79 * g_thread_set_priority()
80 * @thread_self: virtual function pointer for g_thread_self()
81 * @thread_equal: used internally by recursive mutex locks and by some
84 * This function table is no longer used by g_thread_init()
85 * to initialize the thread system.
89 * G_THREADS_IMPL_POSIX:
91 * This macro is defined if POSIX style threads are used.
93 * Deprecated:2.32:POSIX threads are in use on all non-Windows systems.
94 * Use G_OS_WIN32 to detect Windows.
98 * G_THREADS_IMPL_WIN32:
100 * This macro is defined if Windows style threads are used.
102 * Deprecated:2.32:Use G_OS_WIN32 to detect Windows.
106 /* {{{1 Exported Variables */
108 gboolean g_thread_use_default_impl = TRUE;
110 GThreadFunctions g_thread_functions_for_glib_use =
138 return g_get_monotonic_time () * 1000;
141 guint64 (*g_thread_gettime) (void) = gettime;
143 /* Initialisation {{{1 ---------------------------------------------------- */
144 gboolean g_threads_got_initialized = TRUE;
145 GSystemThread zero_thread; /* This is initialized to all zero */
150 * @vtable: a function table of type #GThreadFunctions, that provides
151 * the entry points to the thread system to be used. Since 2.32,
152 * this parameter is ignored and should always be %NULL
154 * If you use GLib from more than one thread, you must initialize the
155 * thread system by calling g_thread_init().
157 * Since version 2.24, calling g_thread_init() multiple times is allowed,
158 * but nothing happens except for the first call.
160 * Since version 2.32, GLib does not support custom thread implementations
161 * anymore and the @vtable parameter is ignored and you should pass %NULL.
163 * <note><para>g_thread_init() must not be called directly or indirectly
164 * in a callback from GLib. Also no mutexes may be currently locked while
165 * calling g_thread_init().</para></note>
167 * <note><para>To use g_thread_init() in your program, you have to link
168 * with the libraries that the command <command>pkg-config --libs
169 * gthread-2.0</command> outputs. This is not the case for all the
170 * other thread-related functions of GLib. Those can be used without
171 * having to link with the thread libraries.</para></note>
175 * g_thread_get_initialized:
177 * Indicates if g_thread_init() has been called.
179 * Returns: %TRUE if threads have been initialized.
184 g_thread_get_initialized (void)
186 return g_thread_supported ();
189 /* We need this for ABI compatibility */
190 void g_thread_init_glib (void) { }
192 /* Internal variables {{{1 */
194 static GRealThread *g_thread_all_threads = NULL;
195 static GSList *g_thread_free_indices = NULL;
197 /* Protects g_thread_all_threads and g_thread_free_indices */
198 G_LOCK_DEFINE_STATIC (g_thread);
200 /* Misc. GThread functions {{{1 */
203 * g_thread_set_priority:
204 * @thread: a #GThread.
207 * This function does nothing.
209 * Deprecated:2.32: Thread priorities no longer have any effect.
212 g_thread_set_priority (GThread *thread,
213 GThreadPriority priority)
219 * @func: a function to execute in the new thread
220 * @data: an argument to supply to the new thread
221 * @joinable: should this thread be joinable?
222 * @error: return location for error, or %NULL
224 * This function creates a new thread.
226 * If @joinable is %TRUE, you can wait for this threads termination
227 * calling g_thread_join(). Otherwise the thread will just disappear
228 * when it terminates.
230 * The new thread executes the function @func with the argument @data.
231 * If the thread was created successfully, it is returned.
233 * @error can be %NULL to ignore errors, or non-%NULL to report errors.
234 * The error is set, if and only if the function returns %NULL.
236 * Returns: the new #GThread on success
238 * Deprecated:2.32: Use g_thread_new() instead
241 g_thread_create (GThreadFunc func,
246 return g_thread_new_internal (NULL, func, data, joinable, 0, TRUE, error);
250 * g_thread_create_full:
251 * @func: a function to execute in the new thread.
252 * @data: an argument to supply to the new thread.
253 * @stack_size: a stack size for the new thread.
254 * @joinable: should this thread be joinable?
257 * @error: return location for error.
258 * @Returns: the new #GThread on success.
260 * This function creates a new thread.
262 * Deprecated:2.32: The @bound and @priority arguments are now ignored.
263 * Use g_thread_new() or g_thread_new_full() instead.
266 g_thread_create_full (GThreadFunc func,
271 GThreadPriority priority,
274 return g_thread_new_internal (NULL, func, data, joinable, stack_size, TRUE, error);
279 * @thread_func: function to call for all #GThread structures
280 * @user_data: second argument to @thread_func
282 * Call @thread_func on all #GThreads that have been
283 * created with g_thread_create().
285 * Note that threads may decide to exit while @thread_func is
286 * running, so without intimate knowledge about the lifetime of
287 * foreign threads, @thread_func shouldn't access the GThread*
288 * pointer passed in as first argument. However, @thread_func will
289 * not be called for threads which are known to have exited already.
291 * Due to thread lifetime checks, this function has an execution complexity
292 * which is quadratic in the number of existing threads.
296 * Deprecated:2.32: There aren't many things you can do with a #GThread,
297 * except comparing it with one that was returned from g_thread_create().
298 * There are better ways to find out if your thread is still alive.
301 g_thread_foreach (GFunc thread_func,
304 GSList *slist = NULL;
306 g_return_if_fail (thread_func != NULL);
307 /* snapshot the list of threads for iteration */
309 for (thread = g_thread_all_threads; thread; thread = thread->next)
310 slist = g_slist_prepend (slist, thread);
312 /* walk the list, skipping non-existent threads */
315 GSList *node = slist;
317 /* check whether the current thread still exists */
319 for (thread = g_thread_all_threads; thread; thread = thread->next)
320 if (thread == node->data)
324 thread_func (thread, user_data);
325 g_slist_free_1 (node);
330 g_enumerable_thread_add (GRealThread *thread)
333 thread->next = g_thread_all_threads;
334 g_thread_all_threads = thread;
339 g_enumerable_thread_remove (GRealThread *thread)
344 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
351 g_thread_all_threads = t->next;
358 /* GOnce {{{1 ------------------------------------------------------------- */
360 g_once_init_enter_impl (volatile gsize *location)
362 return (g_once_init_enter) (location);
365 /* GStaticMutex {{{1 ------------------------------------------------------ */
370 * A #GStaticMutex works like a #GMutex.
372 * Prior to GLib 2.32, GStaticMutex had the significant advantage
373 * that it doesn't need to be created at run-time, but can be defined
374 * at compile-time. Since 2.32, #GMutex can be statically allocated
375 * as well, and GStaticMutex has been deprecated.
377 * Here is a version of our give_me_next_number() example using
382 * Using <structname>GStaticMutex</structname>
383 * to simplify thread-safe programming
387 * give_me_next_number (void)
389 * static int current_number = 0;
391 * static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
393 * g_static_mutex_lock (&mutex);
394 * ret_val = current_number = calc_next_number (current_number);
395 * g_static_mutex_unlock (&mutex);
402 * Sometimes you would like to dynamically create a mutex. If you don't
403 * want to require prior calling to g_thread_init(), because your code
404 * should also be usable in non-threaded programs, you are not able to
405 * use g_mutex_new() and thus #GMutex, as that requires a prior call to
406 * g_thread_init(). In theses cases you can also use a #GStaticMutex.
407 * It must be initialized with g_static_mutex_init() before using it
408 * and freed with with g_static_mutex_free() when not needed anymore to
409 * free up any allocated resources.
411 * Even though #GStaticMutex is not opaque, it should only be used with
412 * the following functions, as it is defined differently on different
415 * All of the <function>g_static_mutex_*</function> functions apart
416 * from <function>g_static_mutex_get_mutex</function> can also be used
417 * even if g_thread_init() has not yet been called. Then they do
418 * nothing, apart from <function>g_static_mutex_trylock</function>,
419 * which does nothing but returning %TRUE.
421 * <note><para>All of the <function>g_static_mutex_*</function>
422 * functions are actually macros. Apart from taking their addresses, you
423 * can however use them as if they were functions.</para></note>
427 * G_STATIC_MUTEX_INIT:
429 * A #GStaticMutex must be initialized with this macro, before it can
430 * be used. This macro can used be to initialize a variable, but it
431 * cannot be assigned to a variable. In that case you have to use
432 * g_static_mutex_init().
435 * GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
440 * g_static_mutex_init:
441 * @mutex: a #GStaticMutex to be initialized.
443 * Initializes @mutex.
444 * Alternatively you can initialize it with #G_STATIC_MUTEX_INIT.
446 * Deprecated: 2.32: Use g_mutex_init()
449 g_static_mutex_init (GStaticMutex *mutex)
451 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
453 g_return_if_fail (mutex);
458 /* IMPLEMENTATION NOTE:
460 * On some platforms a GStaticMutex is actually a normal GMutex stored
461 * inside of a structure instead of being allocated dynamically. We can
462 * only do this for platforms on which we know, in advance, how to
463 * allocate (size) and initialise (value) that memory.
465 * On other platforms, a GStaticMutex is nothing more than a pointer to
466 * a GMutex. In that case, the first access we make to the static mutex
467 * must first allocate the normal GMutex and store it into the pointer.
469 * configure.ac writes macros into glibconfig.h to determine if
470 * g_static_mutex_get_mutex() accesses the structure in memory directly
471 * (on platforms where we are able to do that) or if it ends up here,
472 * where we may have to allocate the GMutex before returning it.
476 * g_static_mutex_get_mutex:
477 * @mutex: a #GStaticMutex.
478 * @Returns: the #GMutex corresponding to @mutex.
480 * For some operations (like g_cond_wait()) you must have a #GMutex
481 * instead of a #GStaticMutex. This function will return the
482 * corresponding #GMutex for @mutex.
484 * Deprecated: 2.32: Just use a #GMutex
487 g_static_mutex_get_mutex_impl (GMutex** mutex)
491 if (!g_thread_supported ())
494 result = g_atomic_pointer_get (mutex);
498 g_mutex_lock (&g_once_mutex);
503 result = g_mutex_new ();
504 g_atomic_pointer_set (mutex, result);
507 g_mutex_unlock (&g_once_mutex);
513 /* IMPLEMENTATION NOTE:
515 * g_static_mutex_lock(), g_static_mutex_trylock() and
516 * g_static_mutex_unlock() are all preprocessor macros that wrap the
517 * corresponding g_mutex_*() function around a call to
518 * g_static_mutex_get_mutex().
522 * g_static_mutex_lock:
523 * @mutex: a #GStaticMutex.
525 * Works like g_mutex_lock(), but for a #GStaticMutex.
527 * Deprecated: 2.32: Use g_mutex_lock()
531 * g_static_mutex_trylock:
532 * @mutex: a #GStaticMutex.
533 * @Returns: %TRUE, if the #GStaticMutex could be locked.
535 * Works like g_mutex_trylock(), but for a #GStaticMutex.
537 * Deprecated: 2.32: Use g_mutex_trylock()
541 * g_static_mutex_unlock:
542 * @mutex: a #GStaticMutex.
544 * Works like g_mutex_unlock(), but for a #GStaticMutex.
546 * Deprecated: 2.32: Use g_mutex_unlock()
550 * g_static_mutex_free:
551 * @mutex: a #GStaticMutex to be freed.
553 * Releases all resources allocated to @mutex.
555 * You don't have to call this functions for a #GStaticMutex with an
556 * unbounded lifetime, i.e. objects declared 'static', but if you have
557 * a #GStaticMutex as a member of a structure and the structure is
558 * freed, you should also free the #GStaticMutex.
560 * <note><para>Calling g_static_mutex_free() on a locked mutex may
561 * result in undefined behaviour.</para></note>
563 * Deprecated: 2.32: Use g_mutex_free()
566 g_static_mutex_free (GStaticMutex* mutex)
568 GMutex **runtime_mutex;
570 g_return_if_fail (mutex);
572 /* The runtime_mutex is the first (or only) member of GStaticMutex,
573 * see both versions (of glibconfig.h) in configure.ac. Note, that
574 * this variable is NULL, if g_thread_init() hasn't been called or
575 * if we're using the default thread implementation and it provides
577 runtime_mutex = ((GMutex**)mutex);
580 g_mutex_free (*runtime_mutex);
582 *runtime_mutex = NULL;
585 /* {{{1 GStaticRecMutex */
590 * A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
591 * multiple times by one thread. If you enter it n times, you have to
592 * unlock it n times again to let other threads lock it. An exception
593 * is the function g_static_rec_mutex_unlock_full(): that allows you to
594 * unlock a #GStaticRecMutex completely returning the depth, (i.e. the
595 * number of times this mutex was locked). The depth can later be used
596 * to restore the state of the #GStaticRecMutex by calling
597 * g_static_rec_mutex_lock_full(). In GLib 2.32, #GStaticRecMutex has
598 * been deprecated in favor of #GRecMutex.
600 * Even though #GStaticRecMutex is not opaque, it should only be used
601 * with the following functions.
603 * All of the <function>g_static_rec_mutex_*</function> functions can
604 * be used even if g_thread_init() has not been called. Then they do
605 * nothing, apart from <function>g_static_rec_mutex_trylock</function>,
606 * which does nothing but returning %TRUE.
610 * G_STATIC_REC_MUTEX_INIT:
612 * A #GStaticRecMutex must be initialized with this macro before it can
613 * be used. This macro can used be to initialize a variable, but it
614 * cannot be assigned to a variable. In that case you have to use
615 * g_static_rec_mutex_init().
618 * GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
623 * g_static_rec_mutex_init:
624 * @mutex: a #GStaticRecMutex to be initialized.
626 * A #GStaticRecMutex must be initialized with this function before it
627 * can be used. Alternatively you can initialize it with
628 * #G_STATIC_REC_MUTEX_INIT.
630 * Deprecated: 2.32: Use g_rec_mutex_init()
633 g_static_rec_mutex_init (GStaticRecMutex *mutex)
635 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
637 g_return_if_fail (mutex);
643 * g_static_rec_mutex_lock:
644 * @mutex: a #GStaticRecMutex to lock.
646 * Locks @mutex. If @mutex is already locked by another thread, the
647 * current thread will block until @mutex is unlocked by the other
648 * thread. If @mutex is already locked by the calling thread, this
649 * functions increases the depth of @mutex and returns immediately.
651 * Deprecated: 2.32: Use g_rec_mutex_lock()
654 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
658 g_return_if_fail (mutex);
660 if (!g_thread_supported ())
663 g_system_thread_self (&self);
665 if (g_system_thread_equal (&self, &mutex->owner))
670 g_static_mutex_lock (&mutex->mutex);
671 g_system_thread_assign (mutex->owner, self);
676 * g_static_rec_mutex_trylock:
677 * @mutex: a #GStaticRecMutex to lock.
678 * @Returns: %TRUE, if @mutex could be locked.
680 * Tries to lock @mutex. If @mutex is already locked by another thread,
681 * it immediately returns %FALSE. Otherwise it locks @mutex and returns
682 * %TRUE. If @mutex is already locked by the calling thread, this
683 * functions increases the depth of @mutex and immediately returns
686 * Deprecated: 2.32: Use g_rec_mutex_trylock()
689 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
693 g_return_val_if_fail (mutex, FALSE);
695 if (!g_thread_supported ())
698 g_system_thread_self (&self);
700 if (g_system_thread_equal (&self, &mutex->owner))
706 if (!g_static_mutex_trylock (&mutex->mutex))
709 g_system_thread_assign (mutex->owner, self);
715 * g_static_rec_mutex_unlock:
716 * @mutex: a #GStaticRecMutex to unlock.
718 * Unlocks @mutex. Another thread will be allowed to lock @mutex only
719 * when it has been unlocked as many times as it had been locked
720 * before. If @mutex is completely unlocked and another thread is
721 * blocked in a g_static_rec_mutex_lock() call for @mutex, it will be
722 * woken and can lock @mutex itself.
724 * Deprecated: 2.32: Use g_rec_mutex_unlock()
727 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
729 g_return_if_fail (mutex);
731 if (!g_thread_supported ())
734 if (mutex->depth > 1)
739 g_system_thread_assign (mutex->owner, zero_thread);
740 g_static_mutex_unlock (&mutex->mutex);
744 * g_static_rec_mutex_lock_full:
745 * @mutex: a #GStaticRecMutex to lock.
746 * @depth: number of times this mutex has to be unlocked to be
747 * completely unlocked.
749 * Works like calling g_static_rec_mutex_lock() for @mutex @depth times.
751 * Deprecated: 2.32: Use g_rec_mutex_lock()
754 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
758 g_return_if_fail (mutex);
760 if (!g_thread_supported ())
766 g_system_thread_self (&self);
768 if (g_system_thread_equal (&self, &mutex->owner))
770 mutex->depth += depth;
773 g_static_mutex_lock (&mutex->mutex);
774 g_system_thread_assign (mutex->owner, self);
775 mutex->depth = depth;
779 * g_static_rec_mutex_unlock_full:
780 * @mutex: a #GStaticRecMutex to completely unlock.
781 * @Returns: number of times @mutex has been locked by the current
784 * Completely unlocks @mutex. If another thread is blocked in a
785 * g_static_rec_mutex_lock() call for @mutex, it will be woken and can
786 * lock @mutex itself. This function returns the number of times that
787 * @mutex has been locked by the current thread. To restore the state
788 * before the call to g_static_rec_mutex_unlock_full() you can call
789 * g_static_rec_mutex_lock_full() with the depth returned by this
792 * Deprecated: 2.32: Use g_rec_mutex_unlock()
795 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
799 g_return_val_if_fail (mutex, 0);
801 if (!g_thread_supported ())
804 depth = mutex->depth;
806 g_system_thread_assign (mutex->owner, zero_thread);
808 g_static_mutex_unlock (&mutex->mutex);
814 * g_static_rec_mutex_free:
815 * @mutex: a #GStaticRecMutex to be freed.
817 * Releases all resources allocated to a #GStaticRecMutex.
819 * You don't have to call this functions for a #GStaticRecMutex with an
820 * unbounded lifetime, i.e. objects declared 'static', but if you have
821 * a #GStaticRecMutex as a member of a structure and the structure is
822 * freed, you should also free the #GStaticRecMutex.
824 * Deprecated: 2.32: Use g_rec_mutex_clear()
827 g_static_rec_mutex_free (GStaticRecMutex *mutex)
829 g_return_if_fail (mutex);
831 g_static_mutex_free (&mutex->mutex);
834 /* GStaticRWLock {{{1 ----------------------------------------------------- */
839 * The #GStaticRWLock struct represents a read-write lock. A read-write
840 * lock can be used for protecting data that some portions of code only
841 * read from, while others also write. In such situations it is
842 * desirable that several readers can read at once, whereas of course
843 * only one writer may write at a time. Take a look at the following
847 * <title>An array with access functions</title>
849 * GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
853 * my_array_get (guint index)
855 * gpointer retval = NULL;
860 * g_static_rw_lock_reader_lock (&rwlock);
861 * if (index < array->len)
862 * retval = g_ptr_array_index (array, index);
863 * g_static_rw_lock_reader_unlock (&rwlock);
869 * my_array_set (guint index, gpointer data)
871 * g_static_rw_lock_writer_lock (&rwlock);
874 * array = g_ptr_array_new (<!-- -->);
876 * if (index >= array->len)
877 * g_ptr_array_set_size (array, index+1);
878 * g_ptr_array_index (array, index) = data;
880 * g_static_rw_lock_writer_unlock (&rwlock);
885 * This example shows an array which can be accessed by many readers
886 * (the <function>my_array_get()</function> function) simultaneously,
887 * whereas the writers (the <function>my_array_set()</function>
888 * function) will only be allowed once at a time and only if no readers
889 * currently access the array. This is because of the potentially
890 * dangerous resizing of the array. Using these functions is fully
891 * multi-thread safe now.
893 * Most of the time, writers should have precedence over readers. That
894 * means, for this implementation, that as soon as a writer wants to
895 * lock the data, no other reader is allowed to lock the data, whereas,
896 * of course, the readers that already have locked the data are allowed
897 * to finish their operation. As soon as the last reader unlocks the
898 * data, the writer will lock it.
900 * Even though #GStaticRWLock is not opaque, it should only be used
901 * with the following functions.
903 * All of the <function>g_static_rw_lock_*</function> functions can be
904 * used even if g_thread_init() has not been called. Then they do
905 * nothing, apart from <function>g_static_rw_lock_*_trylock</function>,
906 * which does nothing but returning %TRUE.
908 * <note><para>A read-write lock has a higher overhead than a mutex. For
909 * example, both g_static_rw_lock_reader_lock() and
910 * g_static_rw_lock_reader_unlock() have to lock and unlock a
911 * #GStaticMutex, so it takes at least twice the time to lock and unlock
912 * a #GStaticRWLock that it does to lock and unlock a #GStaticMutex. So
913 * only data structures that are accessed by multiple readers, and which
914 * keep the lock for a considerable time justify a #GStaticRWLock. The
915 * above example most probably would fare better with a
916 * #GStaticMutex.</para></note>
918 * Deprecated: 2.32: Use a #GRWLock instead
922 * G_STATIC_RW_LOCK_INIT:
924 * A #GStaticRWLock must be initialized with this macro before it can
925 * be used. This macro can used be to initialize a variable, but it
926 * cannot be assigned to a variable. In that case you have to use
927 * g_static_rw_lock_init().
930 * GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
935 * g_static_rw_lock_init:
936 * @lock: a #GStaticRWLock to be initialized.
938 * A #GStaticRWLock must be initialized with this function before it
939 * can be used. Alternatively you can initialize it with
940 * #G_STATIC_RW_LOCK_INIT.
942 * Deprecated: 2.32: Use g_rw_lock_init() instead
945 g_static_rw_lock_init (GStaticRWLock* lock)
947 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
949 g_return_if_fail (lock);
955 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
958 *cond = g_cond_new ();
959 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
963 g_static_rw_lock_signal (GStaticRWLock* lock)
965 if (lock->want_to_write && lock->write_cond)
966 g_cond_signal (lock->write_cond);
967 else if (lock->want_to_read && lock->read_cond)
968 g_cond_broadcast (lock->read_cond);
972 * g_static_rw_lock_reader_lock:
973 * @lock: a #GStaticRWLock to lock for reading.
975 * Locks @lock for reading. There may be unlimited concurrent locks for
976 * reading of a #GStaticRWLock at the same time. If @lock is already
977 * locked for writing by another thread or if another thread is already
978 * waiting to lock @lock for writing, this function will block until
979 * @lock is unlocked by the other writing thread and no other writing
980 * threads want to lock @lock. This lock has to be unlocked by
981 * g_static_rw_lock_reader_unlock().
983 * #GStaticRWLock is not recursive. It might seem to be possible to
984 * recursively lock for reading, but that can result in a deadlock, due
985 * to writer preference.
987 * Deprecated: 2.32: Use g_rw_lock_reader_lock() instead
990 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
992 g_return_if_fail (lock);
994 if (!g_threads_got_initialized)
997 g_static_mutex_lock (&lock->mutex);
998 lock->want_to_read++;
999 while (lock->have_writer || lock->want_to_write)
1000 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
1001 lock->want_to_read--;
1002 lock->read_counter++;
1003 g_static_mutex_unlock (&lock->mutex);
1007 * g_static_rw_lock_reader_trylock:
1008 * @lock: a #GStaticRWLock to lock for reading.
1009 * @Returns: %TRUE, if @lock could be locked for reading.
1011 * Tries to lock @lock for reading. If @lock is already locked for
1012 * writing by another thread or if another thread is already waiting to
1013 * lock @lock for writing, immediately returns %FALSE. Otherwise locks
1014 * @lock for reading and returns %TRUE. This lock has to be unlocked by
1015 * g_static_rw_lock_reader_unlock().
1017 * Deprectated: 2.32: Use g_rw_lock_reader_trylock() instead
1020 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
1022 gboolean ret_val = FALSE;
1024 g_return_val_if_fail (lock, FALSE);
1026 if (!g_threads_got_initialized)
1029 g_static_mutex_lock (&lock->mutex);
1030 if (!lock->have_writer && !lock->want_to_write)
1032 lock->read_counter++;
1035 g_static_mutex_unlock (&lock->mutex);
1040 * g_static_rw_lock_reader_unlock:
1041 * @lock: a #GStaticRWLock to unlock after reading.
1043 * Unlocks @lock. If a thread waits to lock @lock for writing and all
1044 * locks for reading have been unlocked, the waiting thread is woken up
1045 * and can lock @lock for writing.
1047 * Deprectated: 2.32: Use g_rw_lock_reader_unlock() instead
1050 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
1052 g_return_if_fail (lock);
1054 if (!g_threads_got_initialized)
1057 g_static_mutex_lock (&lock->mutex);
1058 lock->read_counter--;
1059 if (lock->read_counter == 0)
1060 g_static_rw_lock_signal (lock);
1061 g_static_mutex_unlock (&lock->mutex);
1065 * g_static_rw_lock_writer_lock:
1066 * @lock: a #GStaticRWLock to lock for writing.
1068 * Locks @lock for writing. If @lock is already locked for writing or
1069 * reading by other threads, this function will block until @lock is
1070 * completely unlocked and then lock @lock for writing. While this
1071 * functions waits to lock @lock, no other thread can lock @lock for
1072 * reading. When @lock is locked for writing, no other thread can lock
1073 * @lock (neither for reading nor writing). This lock has to be
1074 * unlocked by g_static_rw_lock_writer_unlock().
1076 * Deprectated: 2.32: Use g_rw_lock_writer_lock() instead
1079 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
1081 g_return_if_fail (lock);
1083 if (!g_threads_got_initialized)
1086 g_static_mutex_lock (&lock->mutex);
1087 lock->want_to_write++;
1088 while (lock->have_writer || lock->read_counter)
1089 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
1090 lock->want_to_write--;
1091 lock->have_writer = TRUE;
1092 g_static_mutex_unlock (&lock->mutex);
1096 * g_static_rw_lock_writer_trylock:
1097 * @lock: a #GStaticRWLock to lock for writing.
1098 * @Returns: %TRUE, if @lock could be locked for writing.
1100 * Tries to lock @lock for writing. If @lock is already locked (for
1101 * either reading or writing) by another thread, it immediately returns
1102 * %FALSE. Otherwise it locks @lock for writing and returns %TRUE. This
1103 * lock has to be unlocked by g_static_rw_lock_writer_unlock().
1105 * Deprectated: 2.32: Use g_rw_lock_writer_trylock() instead
1108 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
1110 gboolean ret_val = FALSE;
1112 g_return_val_if_fail (lock, FALSE);
1114 if (!g_threads_got_initialized)
1117 g_static_mutex_lock (&lock->mutex);
1118 if (!lock->have_writer && !lock->read_counter)
1120 lock->have_writer = TRUE;
1123 g_static_mutex_unlock (&lock->mutex);
1128 * g_static_rw_lock_writer_unlock:
1129 * @lock: a #GStaticRWLock to unlock after writing.
1131 * Unlocks @lock. If a thread is waiting to lock @lock for writing and
1132 * all locks for reading have been unlocked, the waiting thread is
1133 * woken up and can lock @lock for writing. If no thread is waiting to
1134 * lock @lock for writing, and some thread or threads are waiting to
1135 * lock @lock for reading, the waiting threads are woken up and can
1136 * lock @lock for reading.
1138 * Deprectated: 2.32: Use g_rw_lock_writer_unlock() instead
1141 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
1143 g_return_if_fail (lock);
1145 if (!g_threads_got_initialized)
1148 g_static_mutex_lock (&lock->mutex);
1149 lock->have_writer = FALSE;
1150 g_static_rw_lock_signal (lock);
1151 g_static_mutex_unlock (&lock->mutex);
1155 * g_static_rw_lock_free:
1156 * @lock: a #GStaticRWLock to be freed.
1158 * Releases all resources allocated to @lock.
1160 * You don't have to call this functions for a #GStaticRWLock with an
1161 * unbounded lifetime, i.e. objects declared 'static', but if you have
1162 * a #GStaticRWLock as a member of a structure, and the structure is
1163 * freed, you should also free the #GStaticRWLock.
1165 * Deprecated: 2.32: Use a #GRWLock instead
1168 g_static_rw_lock_free (GStaticRWLock* lock)
1170 g_return_if_fail (lock);
1172 if (lock->read_cond)
1174 g_cond_free (lock->read_cond);
1175 lock->read_cond = NULL;
1177 if (lock->write_cond)
1179 g_cond_free (lock->write_cond);
1180 lock->write_cond = NULL;
1182 g_static_mutex_free (&lock->mutex);
1185 /* GPrivate {{{1 ------------------------------------------------------ */
1189 * @notify: a #GDestroyNotify
1191 * Deprecated:2.32: dynamic allocation of #GPrivate is a bad idea. Use
1192 * static storage and G_PRIVATE_INIT() instead.
1194 * Returns: a newly allocated #GPrivate (which can never be destroyed)
1197 g_private_new (GDestroyNotify notify)
1199 GPrivate tmp = G_PRIVATE_INIT (notify);
1202 key = g_slice_new (GPrivate);
1208 /* {{{1 GStaticPrivate */
1210 typedef struct _GStaticPrivateNode GStaticPrivateNode;
1211 struct _GStaticPrivateNode
1214 GDestroyNotify destroy;
1215 GStaticPrivate *owner;
1221 * A #GStaticPrivate works almost like a #GPrivate, but it has one
1222 * significant advantage. It doesn't need to be created at run-time
1223 * like a #GPrivate, but can be defined at compile-time. This is
1224 * similar to the difference between #GMutex and #GStaticMutex. Now
1225 * look at our <function>give_me_next_number()</function> example with
1229 * <title>Using GStaticPrivate for per-thread data</title>
1232 * give_me_next_number (<!-- -->)
1234 * static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1235 * int *current_number = g_static_private_get (&current_number_key);
1237 * if (!current_number)
1239 * current_number = g_new (int,1);
1240 * *current_number = 0;
1241 * g_static_private_set (&current_number_key, current_number, g_free);
1244 * *current_number = calc_next_number (*current_number);
1246 * return *current_number;
1253 * G_STATIC_PRIVATE_INIT:
1255 * Every #GStaticPrivate must be initialized with this macro, before it
1259 * GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1264 * g_static_private_init:
1265 * @private_key: a #GStaticPrivate to be initialized
1267 * Initializes @private_key. Alternatively you can initialize it with
1268 * #G_STATIC_PRIVATE_INIT.
1271 g_static_private_init (GStaticPrivate *private_key)
1273 private_key->index = 0;
1277 * g_static_private_get:
1278 * @private_key: a #GStaticPrivate
1280 * Works like g_private_get() only for a #GStaticPrivate.
1282 * This function works even if g_thread_init() has not yet been called.
1284 * Returns: the corresponding pointer
1287 g_static_private_get (GStaticPrivate *private_key)
1289 GRealThread *self = (GRealThread*) g_thread_self ();
1291 gpointer ret = NULL;
1292 array = self->private_data;
1294 if (array && private_key->index != 0 && private_key->index <= array->len)
1296 GStaticPrivateNode *node;
1298 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1300 /* Deal with the possibility that the GStaticPrivate which used
1301 * to have this index got freed and the index got allocated to
1302 * a new one. In this case, the data in the node is stale, so
1303 * free it and return NULL.
1305 if (G_UNLIKELY (node->owner != private_key))
1308 node->destroy (node->data);
1309 node->destroy = NULL;
1320 * g_static_private_set:
1321 * @private_key: a #GStaticPrivate
1322 * @data: the new pointer
1323 * @notify: a function to be called with the pointer whenever the
1324 * current thread ends or sets this pointer again
1326 * Sets the pointer keyed to @private_key for the current thread and
1327 * the function @notify to be called with that pointer (%NULL or
1328 * non-%NULL), whenever the pointer is set again or whenever the
1329 * current thread ends.
1331 * This function works even if g_thread_init() has not yet been called.
1332 * If g_thread_init() is called later, the @data keyed to @private_key
1333 * will be inherited only by the main thread, i.e. the one that called
1336 * <note><para>@notify is used quite differently from @destructor in
1337 * g_private_new().</para></note>
1340 g_static_private_set (GStaticPrivate *private_key,
1342 GDestroyNotify notify)
1344 GRealThread *self = (GRealThread*) g_thread_self ();
1346 static guint next_index = 0;
1347 GStaticPrivateNode *node;
1349 if (!private_key->index)
1353 if (!private_key->index)
1355 if (g_thread_free_indices)
1357 private_key->index = GPOINTER_TO_UINT (g_thread_free_indices->data);
1358 g_thread_free_indices = g_slist_delete_link (g_thread_free_indices,
1359 g_thread_free_indices);
1362 private_key->index = ++next_index;
1365 G_UNLOCK (g_thread);
1368 array = self->private_data;
1371 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
1372 self->private_data = array;
1374 if (private_key->index > array->len)
1375 g_array_set_size (array, private_key->index);
1377 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1380 node->destroy (node->data);
1383 node->destroy = notify;
1384 node->owner = private_key;
1388 * g_static_private_free:
1389 * @private_key: a #GStaticPrivate to be freed
1391 * Releases all resources allocated to @private_key.
1393 * You don't have to call this functions for a #GStaticPrivate with an
1394 * unbounded lifetime, i.e. objects declared 'static', but if you have
1395 * a #GStaticPrivate as a member of a structure and the structure is
1396 * freed, you should also free the #GStaticPrivate.
1399 g_static_private_free (GStaticPrivate *private_key)
1401 guint idx = private_key->index;
1406 private_key->index = 0;
1408 /* Freeing the per-thread data is deferred to either the
1409 * thread end or the next g_static_private_get() call for
1413 g_thread_free_indices = g_slist_prepend (g_thread_free_indices,
1414 GUINT_TO_POINTER (idx));
1415 G_UNLOCK (g_thread);
1419 g_static_private_cleanup (GRealThread *thread)
1423 array = thread->private_data;
1424 thread->private_data = NULL;
1430 for (i = 0; i < array->len; i++ )
1432 GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
1434 node->destroy (node->data);
1436 g_array_free (array, TRUE);
1440 /* GMutex {{{1 ------------------------------------------------------ */
1445 * Allocates and initializes a new #GMutex.
1447 * Returns: a newly allocated #GMutex. Use g_mutex_free() to free
1449 * Deprecated:3.32:GMutex can now be statically allocated, or embedded
1450 * in structures and initialised with g_mutex_init().
1457 mutex = g_slice_new (GMutex);
1458 g_mutex_init (mutex);
1467 * Destroys a @mutex that has been created with g_mutex_new().
1469 * Calling g_mutex_free() on a locked mutex may result
1470 * in undefined behaviour.
1472 * Deprecated:3.32:GMutex can now be statically allocated, or embedded
1473 * in structures and initialised with g_mutex_init().
1476 g_mutex_free (GMutex *mutex)
1478 g_mutex_clear (mutex);
1479 g_slice_free (GMutex, mutex);
1482 /* GCond {{{1 ------------------------------------------------------ */
1487 * Allocates and initializes a new #GCond.
1489 * Returns: a newly allocated #GCond. Free with g_cond_free()
1491 * Deprecated:3.32:GCond can now be statically allocated, or embedded
1492 * in structures and initialised with g_cond_init().
1499 cond = g_slice_new (GCond);
1509 * Destroys a #GCond that has been created with g_cond_new().
1511 * Calling g_cond_free() for a #GCond on which threads are
1512 * blocking leads to undefined behaviour.
1514 * Deprecated:3.32:GCond can now be statically allocated, or embedded
1515 * in structures and initialised with g_cond_init().
1518 g_cond_free (GCond *cond)
1520 g_cond_clear (cond);
1521 g_slice_free (GCond, cond);
1525 /* vim: set foldmethod=marker: */