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 /* we know we are deprecated here, no need for warnings */
27 #define GLIB_DISABLE_DEPRECATION_WARNINGS
29 #include "gmessages.h"
33 #include "gthreadprivate.h"
34 #include "deprecated/gthread.h"
38 /* {{{1 Documentation */
41 * SECTION:threads-deprecated
42 * @title: Deprecated thread API
43 * @short_description: old thread APIs (for reference only)
46 * These APIs are deprecated. You should not use them in new code.
47 * This section remains only to assist with understanding code that was
48 * written to use these APIs at some point in the past.
53 * @G_THREAD_PRIORITY_LOW: a priority lower than normal
54 * @G_THREAD_PRIORITY_NORMAL: the default priority
55 * @G_THREAD_PRIORITY_HIGH: a priority higher than normal
56 * @G_THREAD_PRIORITY_URGENT: the highest priority
58 * Deprecated:2.32: Thread priorities no longer have any effect.
63 * @mutex_new: virtual function pointer for g_mutex_new()
64 * @mutex_lock: virtual function pointer for g_mutex_lock()
65 * @mutex_trylock: virtual function pointer for g_mutex_trylock()
66 * @mutex_unlock: virtual function pointer for g_mutex_unlock()
67 * @mutex_free: virtual function pointer for g_mutex_free()
68 * @cond_new: virtual function pointer for g_cond_new()
69 * @cond_signal: virtual function pointer for g_cond_signal()
70 * @cond_broadcast: virtual function pointer for g_cond_broadcast()
71 * @cond_wait: virtual function pointer for g_cond_wait()
72 * @cond_timed_wait: virtual function pointer for g_cond_timed_wait()
73 * @cond_free: virtual function pointer for g_cond_free()
74 * @private_new: virtual function pointer for g_private_new()
75 * @private_get: virtual function pointer for g_private_get()
76 * @private_set: virtual function pointer for g_private_set()
77 * @thread_create: virtual function pointer for g_thread_create()
78 * @thread_yield: virtual function pointer for g_thread_yield()
79 * @thread_join: virtual function pointer for g_thread_join()
80 * @thread_exit: virtual function pointer for g_thread_exit()
81 * @thread_set_priority: virtual function pointer for
82 * g_thread_set_priority()
83 * @thread_self: virtual function pointer for g_thread_self()
84 * @thread_equal: used internally by recursive mutex locks and by some
87 * This function table is no longer used by g_thread_init()
88 * to initialize the thread system.
92 * G_THREADS_IMPL_POSIX:
94 * This macro is defined if POSIX style threads are used.
96 * Deprecated:2.32:POSIX threads are in use on all non-Windows systems.
97 * Use G_OS_WIN32 to detect Windows.
101 * G_THREADS_IMPL_WIN32:
103 * This macro is defined if Windows style threads are used.
105 * Deprecated:2.32:Use G_OS_WIN32 to detect Windows.
109 /* {{{1 Exported Variables */
111 /* Set this FALSE to have previously-compiled GStaticMutex code use the
112 * slow path (ie: call into us) to avoid compatibility problems.
114 gboolean g_thread_use_default_impl = FALSE;
116 GThreadFunctions g_thread_functions_for_glib_use =
144 return g_get_monotonic_time () * 1000;
147 guint64 (*g_thread_gettime) (void) = gettime;
149 /* Initialisation {{{1 ---------------------------------------------------- */
150 gboolean g_threads_got_initialized = TRUE;
154 * @vtable: a function table of type #GThreadFunctions, that provides
155 * the entry points to the thread system to be used. Since 2.32,
156 * this parameter is ignored and should always be %NULL
158 * If you use GLib from more than one thread, you must initialize the
159 * thread system by calling g_thread_init().
161 * Since version 2.24, calling g_thread_init() multiple times is allowed,
162 * but nothing happens except for the first call.
164 * Since version 2.32, GLib does not support custom thread implementations
165 * anymore and the @vtable parameter is ignored and you should pass %NULL.
167 * <note><para>g_thread_init() must not be called directly or indirectly
168 * in a callback from GLib. Also no mutexes may be currently locked while
169 * calling g_thread_init().</para></note>
171 * <note><para>To use g_thread_init() in your program, you have to link
172 * with the libraries that the command <command>pkg-config --libs
173 * gthread-2.0</command> outputs. This is not the case for all the
174 * other thread-related functions of GLib. Those can be used without
175 * having to link with the thread libraries.</para></note>
179 * g_thread_get_initialized:
181 * Indicates if g_thread_init() has been called.
183 * Returns: %TRUE if threads have been initialized.
188 g_thread_get_initialized (void)
190 return g_thread_supported ();
193 /* We need this for ABI compatibility */
194 void g_thread_init_glib (void) { }
196 /* Internal variables {{{1 */
198 static GSList *g_thread_all_threads = NULL;
199 static GSList *g_thread_free_indices = NULL;
201 /* Protects g_thread_all_threads and g_thread_free_indices */
202 G_LOCK_DEFINE_STATIC (g_thread);
204 /* Misc. GThread functions {{{1 */
207 * g_thread_set_priority:
208 * @thread: a #GThread.
211 * This function does nothing.
213 * Deprecated:2.32: Thread priorities no longer have any effect.
216 g_thread_set_priority (GThread *thread,
217 GThreadPriority priority)
223 * @thread_func: function to call for all #GThread structures
224 * @user_data: second argument to @thread_func
226 * Call @thread_func on all #GThreads that have been
227 * created with g_thread_create().
229 * Note that threads may decide to exit while @thread_func is
230 * running, so without intimate knowledge about the lifetime of
231 * foreign threads, @thread_func shouldn't access the GThread*
232 * pointer passed in as first argument. However, @thread_func will
233 * not be called for threads which are known to have exited already.
235 * Due to thread lifetime checks, this function has an execution complexity
236 * which is quadratic in the number of existing threads.
240 * Deprecated:2.32: There aren't many things you can do with a #GThread,
241 * except comparing it with one that was returned from g_thread_create().
242 * There are better ways to find out if your thread is still alive.
245 g_thread_foreach (GFunc thread_func,
248 GSList *slist = NULL;
250 g_return_if_fail (thread_func != NULL);
251 /* snapshot the list of threads for iteration */
253 slist = g_slist_copy (g_thread_all_threads);
255 /* walk the list, skipping non-existent threads */
258 GSList *node = slist;
260 /* check whether the current thread still exists */
262 if (g_slist_find (g_thread_all_threads, node->data))
268 thread_func (thread, user_data);
269 g_slist_free_1 (node);
274 g_enumerable_thread_remove (gpointer data)
276 GRealThread *thread = data;
279 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
283 GPrivate enumerable_thread_private = G_PRIVATE_INIT (g_enumerable_thread_remove);
286 g_enumerable_thread_add (GRealThread *thread)
289 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
292 g_private_set (&enumerable_thread_private, thread);
296 * @func: a function to execute in the new thread
297 * @data: an argument to supply to the new thread
298 * @joinable: should this thread be joinable?
299 * @error: return location for error, or %NULL
301 * This function creates a new thread.
303 * If @joinable is %TRUE, you can wait for this threads termination
304 * calling g_thread_join(). Otherwise the thread will just disappear
305 * when it terminates.
307 * The new thread executes the function @func with the argument @data.
308 * If the thread was created successfully, it is returned.
310 * @error can be %NULL to ignore errors, or non-%NULL to report errors.
311 * The error is set, if and only if the function returns %NULL.
313 * Returns: the new #GThread on success
315 * Deprecated:2.32: Use g_thread_new() instead
318 g_thread_create (GThreadFunc func,
323 return g_thread_new_internal (NULL, func, data, joinable, 0, g_enumerable_thread_add, error);
327 * g_thread_create_full:
328 * @func: a function to execute in the new thread.
329 * @data: an argument to supply to the new thread.
330 * @stack_size: a stack size for the new thread.
331 * @joinable: should this thread be joinable?
334 * @error: return location for error.
335 * @Returns: the new #GThread on success.
337 * This function creates a new thread.
339 * Deprecated:2.32: The @bound and @priority arguments are now ignored.
340 * Use g_thread_new() or g_thread_new_full() instead.
343 g_thread_create_full (GThreadFunc func,
348 GThreadPriority priority,
351 return g_thread_new_internal (NULL, func, data, joinable, stack_size, g_enumerable_thread_add, error);
356 /* GOnce {{{1 ------------------------------------------------------------- */
358 g_once_init_enter_impl (volatile gsize *location)
360 return (g_once_init_enter) (location);
363 /* GStaticMutex {{{1 ------------------------------------------------------ */
368 * A #GStaticMutex works like a #GMutex.
370 * Prior to GLib 2.32, GStaticMutex had the significant advantage
371 * that it doesn't need to be created at run-time, but can be defined
372 * at compile-time. Since 2.32, #GMutex can be statically allocated
373 * as well, and GStaticMutex has been deprecated.
375 * Here is a version of our give_me_next_number() example using
380 * Using <structname>GStaticMutex</structname>
381 * to simplify thread-safe programming
385 * give_me_next_number (void)
387 * static int current_number = 0;
389 * static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
391 * g_static_mutex_lock (&mutex);
392 * ret_val = current_number = calc_next_number (current_number);
393 * g_static_mutex_unlock (&mutex);
400 * Sometimes you would like to dynamically create a mutex. If you don't
401 * want to require prior calling to g_thread_init(), because your code
402 * should also be usable in non-threaded programs, you are not able to
403 * use g_mutex_new() and thus #GMutex, as that requires a prior call to
404 * g_thread_init(). In theses cases you can also use a #GStaticMutex.
405 * It must be initialized with g_static_mutex_init() before using it
406 * and freed with with g_static_mutex_free() when not needed anymore to
407 * free up any allocated resources.
409 * Even though #GStaticMutex is not opaque, it should only be used with
410 * the following functions, as it is defined differently on different
413 * All of the <function>g_static_mutex_*</function> functions apart
414 * from <function>g_static_mutex_get_mutex</function> can also be used
415 * even if g_thread_init() has not yet been called. Then they do
416 * nothing, apart from <function>g_static_mutex_trylock</function>,
417 * which does nothing but returning %TRUE.
419 * <note><para>All of the <function>g_static_mutex_*</function>
420 * functions are actually macros. Apart from taking their addresses, you
421 * can however use them as if they were functions.</para></note>
425 * G_STATIC_MUTEX_INIT:
427 * A #GStaticMutex must be initialized with this macro, before it can
428 * be used. This macro can used be to initialize a variable, but it
429 * cannot be assigned to a variable. In that case you have to use
430 * g_static_mutex_init().
433 * GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
438 * g_static_mutex_init:
439 * @mutex: a #GStaticMutex to be initialized.
441 * Initializes @mutex.
442 * Alternatively you can initialize it with #G_STATIC_MUTEX_INIT.
444 * Deprecated: 2.32: Use g_mutex_init()
447 g_static_mutex_init (GStaticMutex *mutex)
449 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
451 g_return_if_fail (mutex);
456 /* IMPLEMENTATION NOTE:
458 * On some platforms a GStaticMutex is actually a normal GMutex stored
459 * inside of a structure instead of being allocated dynamically. We can
460 * only do this for platforms on which we know, in advance, how to
461 * allocate (size) and initialise (value) that memory.
463 * On other platforms, a GStaticMutex is nothing more than a pointer to
464 * a GMutex. In that case, the first access we make to the static mutex
465 * must first allocate the normal GMutex and store it into the pointer.
467 * configure.ac writes macros into glibconfig.h to determine if
468 * g_static_mutex_get_mutex() accesses the structure in memory directly
469 * (on platforms where we are able to do that) or if it ends up here,
470 * where we may have to allocate the GMutex before returning it.
474 * g_static_mutex_get_mutex:
475 * @mutex: a #GStaticMutex.
476 * @Returns: the #GMutex corresponding to @mutex.
478 * For some operations (like g_cond_wait()) you must have a #GMutex
479 * instead of a #GStaticMutex. This function will return the
480 * corresponding #GMutex for @mutex.
482 * Deprecated: 2.32: Just use a #GMutex
485 g_static_mutex_get_mutex_impl (GStaticMutex* mutex)
489 if (!g_thread_supported ())
492 result = g_atomic_pointer_get (&mutex->mutex);
496 g_mutex_lock (&g_once_mutex);
498 result = mutex->mutex;
501 result = g_mutex_new ();
502 g_atomic_pointer_set (&mutex->mutex, result);
505 g_mutex_unlock (&g_once_mutex);
511 /* IMPLEMENTATION NOTE:
513 * g_static_mutex_lock(), g_static_mutex_trylock() and
514 * g_static_mutex_unlock() are all preprocessor macros that wrap the
515 * corresponding g_mutex_*() function around a call to
516 * g_static_mutex_get_mutex().
520 * g_static_mutex_lock:
521 * @mutex: a #GStaticMutex.
523 * Works like g_mutex_lock(), but for a #GStaticMutex.
525 * Deprecated: 2.32: Use g_mutex_lock()
529 * g_static_mutex_trylock:
530 * @mutex: a #GStaticMutex.
531 * @Returns: %TRUE, if the #GStaticMutex could be locked.
533 * Works like g_mutex_trylock(), but for a #GStaticMutex.
535 * Deprecated: 2.32: Use g_mutex_trylock()
539 * g_static_mutex_unlock:
540 * @mutex: a #GStaticMutex.
542 * Works like g_mutex_unlock(), but for a #GStaticMutex.
544 * Deprecated: 2.32: Use g_mutex_unlock()
548 * g_static_mutex_free:
549 * @mutex: a #GStaticMutex to be freed.
551 * Releases all resources allocated to @mutex.
553 * You don't have to call this functions for a #GStaticMutex with an
554 * unbounded lifetime, i.e. objects declared 'static', but if you have
555 * a #GStaticMutex as a member of a structure and the structure is
556 * freed, you should also free the #GStaticMutex.
558 * <note><para>Calling g_static_mutex_free() on a locked mutex may
559 * result in undefined behaviour.</para></note>
561 * Deprecated: 2.32: Use g_mutex_free()
564 g_static_mutex_free (GStaticMutex* mutex)
566 GMutex **runtime_mutex;
568 g_return_if_fail (mutex);
570 /* The runtime_mutex is the first (or only) member of GStaticMutex,
571 * see both versions (of glibconfig.h) in configure.ac. Note, that
572 * this variable is NULL, if g_thread_init() hasn't been called or
573 * if we're using the default thread implementation and it provides
575 runtime_mutex = ((GMutex**)mutex);
578 g_mutex_free (*runtime_mutex);
580 *runtime_mutex = NULL;
583 /* {{{1 GStaticRecMutex */
588 * A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
589 * multiple times by one thread. If you enter it n times, you have to
590 * unlock it n times again to let other threads lock it. An exception
591 * is the function g_static_rec_mutex_unlock_full(): that allows you to
592 * unlock a #GStaticRecMutex completely returning the depth, (i.e. the
593 * number of times this mutex was locked). The depth can later be used
594 * to restore the state of the #GStaticRecMutex by calling
595 * g_static_rec_mutex_lock_full(). In GLib 2.32, #GStaticRecMutex has
596 * been deprecated in favor of #GRecMutex.
598 * Even though #GStaticRecMutex is not opaque, it should only be used
599 * with the following functions.
601 * All of the <function>g_static_rec_mutex_*</function> functions can
602 * be used even if g_thread_init() has not been called. Then they do
603 * nothing, apart from <function>g_static_rec_mutex_trylock</function>,
604 * which does nothing but returning %TRUE.
608 * G_STATIC_REC_MUTEX_INIT:
610 * A #GStaticRecMutex must be initialized with this macro before it can
611 * be used. This macro can used be to initialize a variable, but it
612 * cannot be assigned to a variable. In that case you have to use
613 * g_static_rec_mutex_init().
616 * GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
621 * g_static_rec_mutex_init:
622 * @mutex: a #GStaticRecMutex to be initialized.
624 * A #GStaticRecMutex must be initialized with this function before it
625 * can be used. Alternatively you can initialize it with
626 * #G_STATIC_REC_MUTEX_INIT.
628 * Deprecated: 2.32: Use g_rec_mutex_init()
631 g_static_rec_mutex_init (GStaticRecMutex *mutex)
633 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
635 g_return_if_fail (mutex);
641 g_static_rec_mutex_get_rec_mutex_impl (GStaticRecMutex* mutex)
645 if (!g_thread_supported ())
648 result = g_atomic_pointer_get (&mutex->mutex.mutex);
652 g_mutex_lock (&g_once_mutex);
654 result = (GRecMutex *) mutex->mutex.mutex;
657 result = g_slice_new (GRecMutex);
658 g_rec_mutex_init (result);
659 g_atomic_pointer_set (&mutex->mutex.mutex, result);
662 g_mutex_unlock (&g_once_mutex);
669 * g_static_rec_mutex_lock:
670 * @mutex: a #GStaticRecMutex to lock.
672 * Locks @mutex. If @mutex is already locked by another thread, the
673 * current thread will block until @mutex is unlocked by the other
674 * thread. If @mutex is already locked by the calling thread, this
675 * functions increases the depth of @mutex and returns immediately.
677 * Deprecated: 2.32: Use g_rec_mutex_lock()
680 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
683 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
684 g_rec_mutex_lock (rm);
689 * g_static_rec_mutex_trylock:
690 * @mutex: a #GStaticRecMutex to lock.
691 * @Returns: %TRUE, if @mutex could be locked.
693 * Tries to lock @mutex. If @mutex is already locked by another thread,
694 * it immediately returns %FALSE. Otherwise it locks @mutex and returns
695 * %TRUE. If @mutex is already locked by the calling thread, this
696 * functions increases the depth of @mutex and immediately returns
699 * Deprecated: 2.32: Use g_rec_mutex_trylock()
702 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
705 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
707 if (g_rec_mutex_trylock (rm))
717 * g_static_rec_mutex_unlock:
718 * @mutex: a #GStaticRecMutex to unlock.
720 * Unlocks @mutex. Another thread will be allowed to lock @mutex only
721 * when it has been unlocked as many times as it had been locked
722 * before. If @mutex is completely unlocked and another thread is
723 * blocked in a g_static_rec_mutex_lock() call for @mutex, it will be
724 * woken and can lock @mutex itself.
726 * Deprecated: 2.32: Use g_rec_mutex_unlock()
729 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
732 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
734 g_rec_mutex_unlock (rm);
738 * g_static_rec_mutex_lock_full:
739 * @mutex: a #GStaticRecMutex to lock.
740 * @depth: number of times this mutex has to be unlocked to be
741 * completely unlocked.
743 * Works like calling g_static_rec_mutex_lock() for @mutex @depth times.
745 * Deprecated: 2.32: Use g_rec_mutex_lock()
748 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
753 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
756 g_rec_mutex_lock (rm);
762 * g_static_rec_mutex_unlock_full:
763 * @mutex: a #GStaticRecMutex to completely unlock.
764 * @Returns: number of times @mutex has been locked by the current
767 * Completely unlocks @mutex. If another thread is blocked in a
768 * g_static_rec_mutex_lock() call for @mutex, it will be woken and can
769 * lock @mutex itself. This function returns the number of times that
770 * @mutex has been locked by the current thread. To restore the state
771 * before the call to g_static_rec_mutex_unlock_full() you can call
772 * g_static_rec_mutex_lock_full() with the depth returned by this
775 * Deprecated: 2.32: Use g_rec_mutex_unlock()
778 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
783 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
784 depth = mutex->depth;
785 while (mutex->depth--)
786 g_rec_mutex_unlock (rm);
792 * g_static_rec_mutex_free:
793 * @mutex: a #GStaticRecMutex to be freed.
795 * Releases all resources allocated to a #GStaticRecMutex.
797 * You don't have to call this functions for a #GStaticRecMutex with an
798 * unbounded lifetime, i.e. objects declared 'static', but if you have
799 * a #GStaticRecMutex as a member of a structure and the structure is
800 * freed, you should also free the #GStaticRecMutex.
802 * Deprecated: 2.32: Use g_rec_mutex_clear()
805 g_static_rec_mutex_free (GStaticRecMutex *mutex)
807 g_return_if_fail (mutex);
809 if (mutex->mutex.mutex)
811 GRecMutex *rm = (GRecMutex *) mutex->mutex.mutex;
813 g_rec_mutex_clear (rm);
814 g_slice_free (GRecMutex, rm);
818 /* GStaticRWLock {{{1 ----------------------------------------------------- */
823 * The #GStaticRWLock struct represents a read-write lock. A read-write
824 * lock can be used for protecting data that some portions of code only
825 * read from, while others also write. In such situations it is
826 * desirable that several readers can read at once, whereas of course
827 * only one writer may write at a time. Take a look at the following
831 * <title>An array with access functions</title>
833 * GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
837 * my_array_get (guint index)
839 * gpointer retval = NULL;
844 * g_static_rw_lock_reader_lock (&rwlock);
845 * if (index < array->len)
846 * retval = g_ptr_array_index (array, index);
847 * g_static_rw_lock_reader_unlock (&rwlock);
853 * my_array_set (guint index, gpointer data)
855 * g_static_rw_lock_writer_lock (&rwlock);
858 * array = g_ptr_array_new (<!-- -->);
860 * if (index >= array->len)
861 * g_ptr_array_set_size (array, index+1);
862 * g_ptr_array_index (array, index) = data;
864 * g_static_rw_lock_writer_unlock (&rwlock);
869 * This example shows an array which can be accessed by many readers
870 * (the <function>my_array_get()</function> function) simultaneously,
871 * whereas the writers (the <function>my_array_set()</function>
872 * function) will only be allowed once at a time and only if no readers
873 * currently access the array. This is because of the potentially
874 * dangerous resizing of the array. Using these functions is fully
875 * multi-thread safe now.
877 * Most of the time, writers should have precedence over readers. That
878 * means, for this implementation, that as soon as a writer wants to
879 * lock the data, no other reader is allowed to lock the data, whereas,
880 * of course, the readers that already have locked the data are allowed
881 * to finish their operation. As soon as the last reader unlocks the
882 * data, the writer will lock it.
884 * Even though #GStaticRWLock is not opaque, it should only be used
885 * with the following functions.
887 * All of the <function>g_static_rw_lock_*</function> functions can be
888 * used even if g_thread_init() has not been called. Then they do
889 * nothing, apart from <function>g_static_rw_lock_*_trylock</function>,
890 * which does nothing but returning %TRUE.
892 * <note><para>A read-write lock has a higher overhead than a mutex. For
893 * example, both g_static_rw_lock_reader_lock() and
894 * g_static_rw_lock_reader_unlock() have to lock and unlock a
895 * #GStaticMutex, so it takes at least twice the time to lock and unlock
896 * a #GStaticRWLock that it does to lock and unlock a #GStaticMutex. So
897 * only data structures that are accessed by multiple readers, and which
898 * keep the lock for a considerable time justify a #GStaticRWLock. The
899 * above example most probably would fare better with a
900 * #GStaticMutex.</para></note>
902 * Deprecated: 2.32: Use a #GRWLock instead
906 * G_STATIC_RW_LOCK_INIT:
908 * A #GStaticRWLock must be initialized with this macro before it can
909 * be used. This macro can used be to initialize a variable, but it
910 * cannot be assigned to a variable. In that case you have to use
911 * g_static_rw_lock_init().
914 * GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
919 * g_static_rw_lock_init:
920 * @lock: a #GStaticRWLock to be initialized.
922 * A #GStaticRWLock must be initialized with this function before it
923 * can be used. Alternatively you can initialize it with
924 * #G_STATIC_RW_LOCK_INIT.
926 * Deprecated: 2.32: Use g_rw_lock_init() instead
929 g_static_rw_lock_init (GStaticRWLock* lock)
931 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
933 g_return_if_fail (lock);
939 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
942 *cond = g_cond_new ();
943 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
947 g_static_rw_lock_signal (GStaticRWLock* lock)
949 if (lock->want_to_write && lock->write_cond)
950 g_cond_signal (lock->write_cond);
951 else if (lock->want_to_read && lock->read_cond)
952 g_cond_broadcast (lock->read_cond);
956 * g_static_rw_lock_reader_lock:
957 * @lock: a #GStaticRWLock to lock for reading.
959 * Locks @lock for reading. There may be unlimited concurrent locks for
960 * reading of a #GStaticRWLock at the same time. If @lock is already
961 * locked for writing by another thread or if another thread is already
962 * waiting to lock @lock for writing, this function will block until
963 * @lock is unlocked by the other writing thread and no other writing
964 * threads want to lock @lock. This lock has to be unlocked by
965 * g_static_rw_lock_reader_unlock().
967 * #GStaticRWLock is not recursive. It might seem to be possible to
968 * recursively lock for reading, but that can result in a deadlock, due
969 * to writer preference.
971 * Deprecated: 2.32: Use g_rw_lock_reader_lock() instead
974 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
976 g_return_if_fail (lock);
978 if (!g_threads_got_initialized)
981 g_static_mutex_lock (&lock->mutex);
982 lock->want_to_read++;
983 while (lock->have_writer || lock->want_to_write)
984 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
985 lock->want_to_read--;
986 lock->read_counter++;
987 g_static_mutex_unlock (&lock->mutex);
991 * g_static_rw_lock_reader_trylock:
992 * @lock: a #GStaticRWLock to lock for reading.
993 * @Returns: %TRUE, if @lock could be locked for reading.
995 * Tries to lock @lock for reading. If @lock is already locked for
996 * writing by another thread or if another thread is already waiting to
997 * lock @lock for writing, immediately returns %FALSE. Otherwise locks
998 * @lock for reading and returns %TRUE. This lock has to be unlocked by
999 * g_static_rw_lock_reader_unlock().
1001 * Deprectated: 2.32: Use g_rw_lock_reader_trylock() instead
1004 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
1006 gboolean ret_val = FALSE;
1008 g_return_val_if_fail (lock, FALSE);
1010 if (!g_threads_got_initialized)
1013 g_static_mutex_lock (&lock->mutex);
1014 if (!lock->have_writer && !lock->want_to_write)
1016 lock->read_counter++;
1019 g_static_mutex_unlock (&lock->mutex);
1024 * g_static_rw_lock_reader_unlock:
1025 * @lock: a #GStaticRWLock to unlock after reading.
1027 * Unlocks @lock. If a thread waits to lock @lock for writing and all
1028 * locks for reading have been unlocked, the waiting thread is woken up
1029 * and can lock @lock for writing.
1031 * Deprectated: 2.32: Use g_rw_lock_reader_unlock() instead
1034 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
1036 g_return_if_fail (lock);
1038 if (!g_threads_got_initialized)
1041 g_static_mutex_lock (&lock->mutex);
1042 lock->read_counter--;
1043 if (lock->read_counter == 0)
1044 g_static_rw_lock_signal (lock);
1045 g_static_mutex_unlock (&lock->mutex);
1049 * g_static_rw_lock_writer_lock:
1050 * @lock: a #GStaticRWLock to lock for writing.
1052 * Locks @lock for writing. If @lock is already locked for writing or
1053 * reading by other threads, this function will block until @lock is
1054 * completely unlocked and then lock @lock for writing. While this
1055 * functions waits to lock @lock, no other thread can lock @lock for
1056 * reading. When @lock is locked for writing, no other thread can lock
1057 * @lock (neither for reading nor writing). This lock has to be
1058 * unlocked by g_static_rw_lock_writer_unlock().
1060 * Deprectated: 2.32: Use g_rw_lock_writer_lock() instead
1063 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
1065 g_return_if_fail (lock);
1067 if (!g_threads_got_initialized)
1070 g_static_mutex_lock (&lock->mutex);
1071 lock->want_to_write++;
1072 while (lock->have_writer || lock->read_counter)
1073 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
1074 lock->want_to_write--;
1075 lock->have_writer = TRUE;
1076 g_static_mutex_unlock (&lock->mutex);
1080 * g_static_rw_lock_writer_trylock:
1081 * @lock: a #GStaticRWLock to lock for writing.
1082 * @Returns: %TRUE, if @lock could be locked for writing.
1084 * Tries to lock @lock for writing. If @lock is already locked (for
1085 * either reading or writing) by another thread, it immediately returns
1086 * %FALSE. Otherwise it locks @lock for writing and returns %TRUE. This
1087 * lock has to be unlocked by g_static_rw_lock_writer_unlock().
1089 * Deprectated: 2.32: Use g_rw_lock_writer_trylock() instead
1092 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
1094 gboolean ret_val = FALSE;
1096 g_return_val_if_fail (lock, FALSE);
1098 if (!g_threads_got_initialized)
1101 g_static_mutex_lock (&lock->mutex);
1102 if (!lock->have_writer && !lock->read_counter)
1104 lock->have_writer = TRUE;
1107 g_static_mutex_unlock (&lock->mutex);
1112 * g_static_rw_lock_writer_unlock:
1113 * @lock: a #GStaticRWLock to unlock after writing.
1115 * Unlocks @lock. If a thread is waiting to lock @lock for writing and
1116 * all locks for reading have been unlocked, the waiting thread is
1117 * woken up and can lock @lock for writing. If no thread is waiting to
1118 * lock @lock for writing, and some thread or threads are waiting to
1119 * lock @lock for reading, the waiting threads are woken up and can
1120 * lock @lock for reading.
1122 * Deprectated: 2.32: Use g_rw_lock_writer_unlock() instead
1125 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
1127 g_return_if_fail (lock);
1129 if (!g_threads_got_initialized)
1132 g_static_mutex_lock (&lock->mutex);
1133 lock->have_writer = FALSE;
1134 g_static_rw_lock_signal (lock);
1135 g_static_mutex_unlock (&lock->mutex);
1139 * g_static_rw_lock_free:
1140 * @lock: a #GStaticRWLock to be freed.
1142 * Releases all resources allocated to @lock.
1144 * You don't have to call this functions for a #GStaticRWLock with an
1145 * unbounded lifetime, i.e. objects declared 'static', but if you have
1146 * a #GStaticRWLock as a member of a structure, and the structure is
1147 * freed, you should also free the #GStaticRWLock.
1149 * Deprecated: 2.32: Use a #GRWLock instead
1152 g_static_rw_lock_free (GStaticRWLock* lock)
1154 g_return_if_fail (lock);
1156 if (lock->read_cond)
1158 g_cond_free (lock->read_cond);
1159 lock->read_cond = NULL;
1161 if (lock->write_cond)
1163 g_cond_free (lock->write_cond);
1164 lock->write_cond = NULL;
1166 g_static_mutex_free (&lock->mutex);
1169 /* GPrivate {{{1 ------------------------------------------------------ */
1173 * @notify: a #GDestroyNotify
1175 * Deprecated:2.32: dynamic allocation of #GPrivate is a bad idea. Use
1176 * static storage and G_PRIVATE_INIT() instead.
1178 * Returns: a newly allocated #GPrivate (which can never be destroyed)
1181 g_private_new (GDestroyNotify notify)
1183 GPrivate tmp = G_PRIVATE_INIT (notify);
1186 key = g_slice_new (GPrivate);
1192 /* {{{1 GStaticPrivate */
1194 typedef struct _GStaticPrivateNode GStaticPrivateNode;
1195 struct _GStaticPrivateNode
1198 GDestroyNotify destroy;
1199 GStaticPrivate *owner;
1203 g_static_private_cleanup (gpointer data)
1205 GArray *array = data;
1208 for (i = 0; i < array->len; i++ )
1210 GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
1212 node->destroy (node->data);
1215 g_array_free (array, TRUE);
1218 GPrivate static_private_private = G_PRIVATE_INIT (g_static_private_cleanup);
1223 * A #GStaticPrivate works almost like a #GPrivate, but it has one
1224 * significant advantage. It doesn't need to be created at run-time
1225 * like a #GPrivate, but can be defined at compile-time. This is
1226 * similar to the difference between #GMutex and #GStaticMutex. Now
1227 * look at our <function>give_me_next_number()</function> example with
1231 * <title>Using GStaticPrivate for per-thread data</title>
1234 * give_me_next_number (<!-- -->)
1236 * static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1237 * int *current_number = g_static_private_get (&current_number_key);
1239 * if (!current_number)
1241 * current_number = g_new (int,1);
1242 * *current_number = 0;
1243 * g_static_private_set (&current_number_key, current_number, g_free);
1246 * *current_number = calc_next_number (*current_number);
1248 * return *current_number;
1255 * G_STATIC_PRIVATE_INIT:
1257 * Every #GStaticPrivate must be initialized with this macro, before it
1261 * GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1266 * g_static_private_init:
1267 * @private_key: a #GStaticPrivate to be initialized
1269 * Initializes @private_key. Alternatively you can initialize it with
1270 * #G_STATIC_PRIVATE_INIT.
1273 g_static_private_init (GStaticPrivate *private_key)
1275 private_key->index = 0;
1279 * g_static_private_get:
1280 * @private_key: a #GStaticPrivate
1282 * Works like g_private_get() only for a #GStaticPrivate.
1284 * This function works even if g_thread_init() has not yet been called.
1286 * Returns: the corresponding pointer
1289 g_static_private_get (GStaticPrivate *private_key)
1292 gpointer ret = NULL;
1294 array = g_private_get (&static_private_private);
1296 if (array && private_key->index != 0 && private_key->index <= array->len)
1298 GStaticPrivateNode *node;
1300 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1302 /* Deal with the possibility that the GStaticPrivate which used
1303 * to have this index got freed and the index got allocated to
1304 * a new one. In this case, the data in the node is stale, so
1305 * free it and return NULL.
1307 if (G_UNLIKELY (node->owner != private_key))
1310 node->destroy (node->data);
1311 node->destroy = NULL;
1322 * g_static_private_set:
1323 * @private_key: a #GStaticPrivate
1324 * @data: the new pointer
1325 * @notify: a function to be called with the pointer whenever the
1326 * current thread ends or sets this pointer again
1328 * Sets the pointer keyed to @private_key for the current thread and
1329 * the function @notify to be called with that pointer (%NULL or
1330 * non-%NULL), whenever the pointer is set again or whenever the
1331 * current thread ends.
1333 * This function works even if g_thread_init() has not yet been called.
1334 * If g_thread_init() is called later, the @data keyed to @private_key
1335 * will be inherited only by the main thread, i.e. the one that called
1338 * <note><para>@notify is used quite differently from @destructor in
1339 * g_private_new().</para></note>
1342 g_static_private_set (GStaticPrivate *private_key,
1344 GDestroyNotify notify)
1347 static guint next_index = 0;
1348 GStaticPrivateNode *node;
1350 if (!private_key->index)
1354 if (!private_key->index)
1356 if (g_thread_free_indices)
1358 private_key->index = GPOINTER_TO_UINT (g_thread_free_indices->data);
1359 g_thread_free_indices = g_slist_delete_link (g_thread_free_indices,
1360 g_thread_free_indices);
1363 private_key->index = ++next_index;
1366 G_UNLOCK (g_thread);
1369 array = g_private_get (&static_private_private);
1372 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
1373 g_private_set (&static_private_private, array);
1375 if (private_key->index > array->len)
1376 g_array_set_size (array, private_key->index);
1378 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1381 node->destroy (node->data);
1384 node->destroy = notify;
1385 node->owner = private_key;
1389 * g_static_private_free:
1390 * @private_key: a #GStaticPrivate to be freed
1392 * Releases all resources allocated to @private_key.
1394 * You don't have to call this functions for a #GStaticPrivate with an
1395 * unbounded lifetime, i.e. objects declared 'static', but if you have
1396 * a #GStaticPrivate as a member of a structure and the structure is
1397 * freed, you should also free the #GStaticPrivate.
1400 g_static_private_free (GStaticPrivate *private_key)
1402 guint idx = private_key->index;
1407 private_key->index = 0;
1409 /* Freeing the per-thread data is deferred to either the
1410 * thread end or the next g_static_private_get() call for
1414 g_thread_free_indices = g_slist_prepend (g_thread_free_indices,
1415 GUINT_TO_POINTER (idx));
1416 G_UNLOCK (g_thread);
1419 /* GMutex {{{1 ------------------------------------------------------ */
1424 * Allocates and initializes a new #GMutex.
1426 * Returns: a newly allocated #GMutex. Use g_mutex_free() to free
1428 * Deprecated:3.32:GMutex can now be statically allocated, or embedded
1429 * in structures and initialised with g_mutex_init().
1436 mutex = g_slice_new (GMutex);
1437 g_mutex_init (mutex);
1446 * Destroys a @mutex that has been created with g_mutex_new().
1448 * Calling g_mutex_free() on a locked mutex may result
1449 * in undefined behaviour.
1451 * Deprecated:3.32:GMutex can now be statically allocated, or embedded
1452 * in structures and initialised with g_mutex_init().
1455 g_mutex_free (GMutex *mutex)
1457 g_mutex_clear (mutex);
1458 g_slice_free (GMutex, mutex);
1461 /* GCond {{{1 ------------------------------------------------------ */
1466 * Allocates and initializes a new #GCond.
1468 * Returns: a newly allocated #GCond. Free with g_cond_free()
1470 * Deprecated:3.32:GCond can now be statically allocated, or embedded
1471 * in structures and initialised with g_cond_init().
1478 cond = g_slice_new (GCond);
1488 * Destroys a #GCond that has been created with g_cond_new().
1490 * Calling g_cond_free() for a #GCond on which threads are
1491 * blocking leads to undefined behaviour.
1493 * Deprecated:3.32:GCond can now be statically allocated, or embedded
1494 * in structures and initialised with g_cond_init().
1497 g_cond_free (GCond *cond)
1499 g_cond_clear (cond);
1500 g_slice_free (GCond, cond);
1504 /* vim: set foldmethod=marker: */