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_static_mutex);
203 G_LOCK_DEFINE_STATIC (g_thread);
205 /* Misc. GThread functions {{{1 */
208 * g_thread_set_priority:
209 * @thread: a #GThread.
212 * This function does nothing.
214 * Deprecated:2.32: Thread priorities no longer have any effect.
217 g_thread_set_priority (GThread *thread,
218 GThreadPriority priority)
224 * @thread_func: function to call for all #GThread structures
225 * @user_data: second argument to @thread_func
227 * Call @thread_func on all #GThreads that have been
228 * created with g_thread_create().
230 * Note that threads may decide to exit while @thread_func is
231 * running, so without intimate knowledge about the lifetime of
232 * foreign threads, @thread_func shouldn't access the GThread*
233 * pointer passed in as first argument. However, @thread_func will
234 * not be called for threads which are known to have exited already.
236 * Due to thread lifetime checks, this function has an execution complexity
237 * which is quadratic in the number of existing threads.
241 * Deprecated:2.32: There aren't many things you can do with a #GThread,
242 * except comparing it with one that was returned from g_thread_create().
243 * There are better ways to find out if your thread is still alive.
246 g_thread_foreach (GFunc thread_func,
249 GSList *slist = NULL;
251 g_return_if_fail (thread_func != NULL);
252 /* snapshot the list of threads for iteration */
254 slist = g_slist_copy (g_thread_all_threads);
256 /* walk the list, skipping non-existent threads */
259 GSList *node = slist;
261 /* check whether the current thread still exists */
263 if (g_slist_find (g_thread_all_threads, node->data))
269 thread_func (thread, user_data);
270 g_slist_free_1 (node);
275 g_enumerable_thread_remove (gpointer data)
277 GRealThread *thread = data;
280 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
284 GPrivate enumerable_thread_private = G_PRIVATE_INIT (g_enumerable_thread_remove);
287 g_enumerable_thread_add (GRealThread *thread)
290 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
293 g_private_set (&enumerable_thread_private, thread);
297 g_deprecated_thread_proxy (gpointer data)
299 GRealThread *real = data;
301 g_enumerable_thread_add (real);
303 return g_thread_proxy (data);
308 * @func: a function to execute in the new thread
309 * @data: an argument to supply to the new thread
310 * @joinable: should this thread be joinable?
311 * @error: return location for error, or %NULL
313 * This function creates a new thread.
315 * The new thread executes the function @func with the argument @data.
316 * If the thread was created successfully, it is returned.
318 * @error can be %NULL to ignore errors, or non-%NULL to report errors.
319 * The error is set, if and only if the function returns %NULL.
321 * This function returns a reference to the created thread only if
322 * @joinable is %TRUE. In that case, you must free this reference by
323 * calling g_thread_unref() or g_thread_join(). If @joinable is %FALSE
324 * then you should probably not touch the return value.
326 * Returns: the new #GThread on success
328 * Deprecated:2.32: Use g_thread_new() instead
331 g_thread_create (GThreadFunc func,
336 return g_thread_create_full (func, data, 0, joinable, 0, 0, error);
340 * g_thread_create_full:
341 * @func: a function to execute in the new thread.
342 * @data: an argument to supply to the new thread.
343 * @stack_size: a stack size for the new thread.
344 * @joinable: should this thread be joinable?
347 * @error: return location for error.
348 * @Returns: the new #GThread on success.
350 * This function creates a new thread.
352 * Deprecated:2.32: The @bound and @priority arguments are now ignored.
353 * Use g_thread_new() or g_thread_new_full() instead.
356 g_thread_create_full (GThreadFunc func,
361 GThreadPriority priority,
366 thread = g_thread_new_internal (NULL, g_deprecated_thread_proxy,
367 func, data, stack_size, error);
371 thread->joinable = FALSE;
372 g_thread_unref (thread);
378 /* GOnce {{{1 ------------------------------------------------------------- */
380 g_once_init_enter_impl (volatile gsize *location)
382 return (g_once_init_enter) (location);
385 /* GStaticMutex {{{1 ------------------------------------------------------ */
390 * A #GStaticMutex works like a #GMutex.
392 * Prior to GLib 2.32, GStaticMutex had the significant advantage
393 * that it doesn't need to be created at run-time, but can be defined
394 * at compile-time. Since 2.32, #GMutex can be statically allocated
395 * as well, and GStaticMutex has been deprecated.
397 * Here is a version of our give_me_next_number() example using
402 * Using <structname>GStaticMutex</structname>
403 * to simplify thread-safe programming
407 * give_me_next_number (void)
409 * static int current_number = 0;
411 * static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
413 * g_static_mutex_lock (&mutex);
414 * ret_val = current_number = calc_next_number (current_number);
415 * g_static_mutex_unlock (&mutex);
422 * Sometimes you would like to dynamically create a mutex. If you don't
423 * want to require prior calling to g_thread_init(), because your code
424 * should also be usable in non-threaded programs, you are not able to
425 * use g_mutex_new() and thus #GMutex, as that requires a prior call to
426 * g_thread_init(). In theses cases you can also use a #GStaticMutex.
427 * It must be initialized with g_static_mutex_init() before using it
428 * and freed with with g_static_mutex_free() when not needed anymore to
429 * free up any allocated resources.
431 * Even though #GStaticMutex is not opaque, it should only be used with
432 * the following functions, as it is defined differently on different
435 * All of the <function>g_static_mutex_*</function> functions apart
436 * from <function>g_static_mutex_get_mutex</function> can also be used
437 * even if g_thread_init() has not yet been called. Then they do
438 * nothing, apart from <function>g_static_mutex_trylock</function>,
439 * which does nothing but returning %TRUE.
441 * <note><para>All of the <function>g_static_mutex_*</function>
442 * functions are actually macros. Apart from taking their addresses, you
443 * can however use them as if they were functions.</para></note>
447 * G_STATIC_MUTEX_INIT:
449 * A #GStaticMutex must be initialized with this macro, before it can
450 * be used. This macro can used be to initialize a variable, but it
451 * cannot be assigned to a variable. In that case you have to use
452 * g_static_mutex_init().
455 * GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
460 * g_static_mutex_init:
461 * @mutex: a #GStaticMutex to be initialized.
463 * Initializes @mutex.
464 * Alternatively you can initialize it with #G_STATIC_MUTEX_INIT.
466 * Deprecated: 2.32: Use g_mutex_init()
469 g_static_mutex_init (GStaticMutex *mutex)
471 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
473 g_return_if_fail (mutex);
478 /* IMPLEMENTATION NOTE:
480 * On some platforms a GStaticMutex is actually a normal GMutex stored
481 * inside of a structure instead of being allocated dynamically. We can
482 * only do this for platforms on which we know, in advance, how to
483 * allocate (size) and initialise (value) that memory.
485 * On other platforms, a GStaticMutex is nothing more than a pointer to
486 * a GMutex. In that case, the first access we make to the static mutex
487 * must first allocate the normal GMutex and store it into the pointer.
489 * configure.ac writes macros into glibconfig.h to determine if
490 * g_static_mutex_get_mutex() accesses the structure in memory directly
491 * (on platforms where we are able to do that) or if it ends up here,
492 * where we may have to allocate the GMutex before returning it.
496 * g_static_mutex_get_mutex:
497 * @mutex: a #GStaticMutex.
498 * @Returns: the #GMutex corresponding to @mutex.
500 * For some operations (like g_cond_wait()) you must have a #GMutex
501 * instead of a #GStaticMutex. This function will return the
502 * corresponding #GMutex for @mutex.
504 * Deprecated: 2.32: Just use a #GMutex
507 g_static_mutex_get_mutex_impl (GStaticMutex* mutex)
511 if (!g_thread_supported ())
514 result = g_atomic_pointer_get (&mutex->mutex);
518 G_LOCK (g_static_mutex);
520 result = mutex->mutex;
523 result = g_mutex_new ();
524 g_atomic_pointer_set (&mutex->mutex, result);
527 G_UNLOCK (g_static_mutex);
533 /* IMPLEMENTATION NOTE:
535 * g_static_mutex_lock(), g_static_mutex_trylock() and
536 * g_static_mutex_unlock() are all preprocessor macros that wrap the
537 * corresponding g_mutex_*() function around a call to
538 * g_static_mutex_get_mutex().
542 * g_static_mutex_lock:
543 * @mutex: a #GStaticMutex.
545 * Works like g_mutex_lock(), but for a #GStaticMutex.
547 * Deprecated: 2.32: Use g_mutex_lock()
551 * g_static_mutex_trylock:
552 * @mutex: a #GStaticMutex.
553 * @Returns: %TRUE, if the #GStaticMutex could be locked.
555 * Works like g_mutex_trylock(), but for a #GStaticMutex.
557 * Deprecated: 2.32: Use g_mutex_trylock()
561 * g_static_mutex_unlock:
562 * @mutex: a #GStaticMutex.
564 * Works like g_mutex_unlock(), but for a #GStaticMutex.
566 * Deprecated: 2.32: Use g_mutex_unlock()
570 * g_static_mutex_free:
571 * @mutex: a #GStaticMutex to be freed.
573 * Releases all resources allocated to @mutex.
575 * You don't have to call this functions for a #GStaticMutex with an
576 * unbounded lifetime, i.e. objects declared 'static', but if you have
577 * a #GStaticMutex as a member of a structure and the structure is
578 * freed, you should also free the #GStaticMutex.
580 * <note><para>Calling g_static_mutex_free() on a locked mutex may
581 * result in undefined behaviour.</para></note>
583 * Deprecated: 2.32: Use g_mutex_free()
586 g_static_mutex_free (GStaticMutex* mutex)
588 GMutex **runtime_mutex;
590 g_return_if_fail (mutex);
592 /* The runtime_mutex is the first (or only) member of GStaticMutex,
593 * see both versions (of glibconfig.h) in configure.ac. Note, that
594 * this variable is NULL, if g_thread_init() hasn't been called or
595 * if we're using the default thread implementation and it provides
597 runtime_mutex = ((GMutex**)mutex);
600 g_mutex_free (*runtime_mutex);
602 *runtime_mutex = NULL;
605 /* {{{1 GStaticRecMutex */
610 * A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
611 * multiple times by one thread. If you enter it n times, you have to
612 * unlock it n times again to let other threads lock it. An exception
613 * is the function g_static_rec_mutex_unlock_full(): that allows you to
614 * unlock a #GStaticRecMutex completely returning the depth, (i.e. the
615 * number of times this mutex was locked). The depth can later be used
616 * to restore the state of the #GStaticRecMutex by calling
617 * g_static_rec_mutex_lock_full(). In GLib 2.32, #GStaticRecMutex has
618 * been deprecated in favor of #GRecMutex.
620 * Even though #GStaticRecMutex is not opaque, it should only be used
621 * with the following functions.
623 * All of the <function>g_static_rec_mutex_*</function> functions can
624 * be used even if g_thread_init() has not been called. Then they do
625 * nothing, apart from <function>g_static_rec_mutex_trylock</function>,
626 * which does nothing but returning %TRUE.
630 * G_STATIC_REC_MUTEX_INIT:
632 * A #GStaticRecMutex must be initialized with this macro before it can
633 * be used. This macro can used be to initialize a variable, but it
634 * cannot be assigned to a variable. In that case you have to use
635 * g_static_rec_mutex_init().
638 * GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
643 * g_static_rec_mutex_init:
644 * @mutex: a #GStaticRecMutex to be initialized.
646 * A #GStaticRecMutex must be initialized with this function before it
647 * can be used. Alternatively you can initialize it with
648 * #G_STATIC_REC_MUTEX_INIT.
650 * Deprecated: 2.32: Use g_rec_mutex_init()
653 g_static_rec_mutex_init (GStaticRecMutex *mutex)
655 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
657 g_return_if_fail (mutex);
663 g_static_rec_mutex_get_rec_mutex_impl (GStaticRecMutex* mutex)
667 if (!g_thread_supported ())
670 result = g_atomic_pointer_get (&mutex->mutex.mutex);
674 G_LOCK (g_static_mutex);
676 result = (GRecMutex *) mutex->mutex.mutex;
679 result = g_slice_new (GRecMutex);
680 g_rec_mutex_init (result);
681 g_atomic_pointer_set (&mutex->mutex.mutex, result);
684 G_UNLOCK (g_static_mutex);
691 * g_static_rec_mutex_lock:
692 * @mutex: a #GStaticRecMutex to lock.
694 * Locks @mutex. If @mutex is already locked by another thread, the
695 * current thread will block until @mutex is unlocked by the other
696 * thread. If @mutex is already locked by the calling thread, this
697 * functions increases the depth of @mutex and returns immediately.
699 * Deprecated: 2.32: Use g_rec_mutex_lock()
702 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
705 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
706 g_rec_mutex_lock (rm);
711 * g_static_rec_mutex_trylock:
712 * @mutex: a #GStaticRecMutex to lock.
713 * @Returns: %TRUE, if @mutex could be locked.
715 * Tries to lock @mutex. If @mutex is already locked by another thread,
716 * it immediately returns %FALSE. Otherwise it locks @mutex and returns
717 * %TRUE. If @mutex is already locked by the calling thread, this
718 * functions increases the depth of @mutex and immediately returns
721 * Deprecated: 2.32: Use g_rec_mutex_trylock()
724 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
727 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
729 if (g_rec_mutex_trylock (rm))
739 * g_static_rec_mutex_unlock:
740 * @mutex: a #GStaticRecMutex to unlock.
742 * Unlocks @mutex. Another thread will be allowed to lock @mutex only
743 * when it has been unlocked as many times as it had been locked
744 * before. If @mutex is completely unlocked and another thread is
745 * blocked in a g_static_rec_mutex_lock() call for @mutex, it will be
746 * woken and can lock @mutex itself.
748 * Deprecated: 2.32: Use g_rec_mutex_unlock()
751 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
754 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
756 g_rec_mutex_unlock (rm);
760 * g_static_rec_mutex_lock_full:
761 * @mutex: a #GStaticRecMutex to lock.
762 * @depth: number of times this mutex has to be unlocked to be
763 * completely unlocked.
765 * Works like calling g_static_rec_mutex_lock() for @mutex @depth times.
767 * Deprecated: 2.32: Use g_rec_mutex_lock()
770 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
775 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
778 g_rec_mutex_lock (rm);
784 * g_static_rec_mutex_unlock_full:
785 * @mutex: a #GStaticRecMutex to completely unlock.
786 * @Returns: number of times @mutex has been locked by the current
789 * Completely unlocks @mutex. If another thread is blocked in a
790 * g_static_rec_mutex_lock() call for @mutex, it will be woken and can
791 * lock @mutex itself. This function returns the number of times that
792 * @mutex has been locked by the current thread. To restore the state
793 * before the call to g_static_rec_mutex_unlock_full() you can call
794 * g_static_rec_mutex_lock_full() with the depth returned by this
797 * Deprecated: 2.32: Use g_rec_mutex_unlock()
800 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
805 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
806 depth = mutex->depth;
807 while (mutex->depth--)
808 g_rec_mutex_unlock (rm);
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 if (mutex->mutex.mutex)
833 GRecMutex *rm = (GRecMutex *) mutex->mutex.mutex;
835 g_rec_mutex_clear (rm);
836 g_slice_free (GRecMutex, rm);
840 /* GStaticRWLock {{{1 ----------------------------------------------------- */
845 * The #GStaticRWLock struct represents a read-write lock. A read-write
846 * lock can be used for protecting data that some portions of code only
847 * read from, while others also write. In such situations it is
848 * desirable that several readers can read at once, whereas of course
849 * only one writer may write at a time. Take a look at the following
853 * <title>An array with access functions</title>
855 * GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
859 * my_array_get (guint index)
861 * gpointer retval = NULL;
866 * g_static_rw_lock_reader_lock (&rwlock);
867 * if (index < array->len)
868 * retval = g_ptr_array_index (array, index);
869 * g_static_rw_lock_reader_unlock (&rwlock);
875 * my_array_set (guint index, gpointer data)
877 * g_static_rw_lock_writer_lock (&rwlock);
880 * array = g_ptr_array_new (<!-- -->);
882 * if (index >= array->len)
883 * g_ptr_array_set_size (array, index+1);
884 * g_ptr_array_index (array, index) = data;
886 * g_static_rw_lock_writer_unlock (&rwlock);
891 * This example shows an array which can be accessed by many readers
892 * (the <function>my_array_get()</function> function) simultaneously,
893 * whereas the writers (the <function>my_array_set()</function>
894 * function) will only be allowed once at a time and only if no readers
895 * currently access the array. This is because of the potentially
896 * dangerous resizing of the array. Using these functions is fully
897 * multi-thread safe now.
899 * Most of the time, writers should have precedence over readers. That
900 * means, for this implementation, that as soon as a writer wants to
901 * lock the data, no other reader is allowed to lock the data, whereas,
902 * of course, the readers that already have locked the data are allowed
903 * to finish their operation. As soon as the last reader unlocks the
904 * data, the writer will lock it.
906 * Even though #GStaticRWLock is not opaque, it should only be used
907 * with the following functions.
909 * All of the <function>g_static_rw_lock_*</function> functions can be
910 * used even if g_thread_init() has not been called. Then they do
911 * nothing, apart from <function>g_static_rw_lock_*_trylock</function>,
912 * which does nothing but returning %TRUE.
914 * <note><para>A read-write lock has a higher overhead than a mutex. For
915 * example, both g_static_rw_lock_reader_lock() and
916 * g_static_rw_lock_reader_unlock() have to lock and unlock a
917 * #GStaticMutex, so it takes at least twice the time to lock and unlock
918 * a #GStaticRWLock that it does to lock and unlock a #GStaticMutex. So
919 * only data structures that are accessed by multiple readers, and which
920 * keep the lock for a considerable time justify a #GStaticRWLock. The
921 * above example most probably would fare better with a
922 * #GStaticMutex.</para></note>
924 * Deprecated: 2.32: Use a #GRWLock instead
928 * G_STATIC_RW_LOCK_INIT:
930 * A #GStaticRWLock must be initialized with this macro before it can
931 * be used. This macro can used be to initialize a variable, but it
932 * cannot be assigned to a variable. In that case you have to use
933 * g_static_rw_lock_init().
936 * GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
941 * g_static_rw_lock_init:
942 * @lock: a #GStaticRWLock to be initialized.
944 * A #GStaticRWLock must be initialized with this function before it
945 * can be used. Alternatively you can initialize it with
946 * #G_STATIC_RW_LOCK_INIT.
948 * Deprecated: 2.32: Use g_rw_lock_init() instead
951 g_static_rw_lock_init (GStaticRWLock* lock)
953 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
955 g_return_if_fail (lock);
961 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
964 *cond = g_cond_new ();
965 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
969 g_static_rw_lock_signal (GStaticRWLock* lock)
971 if (lock->want_to_write && lock->write_cond)
972 g_cond_signal (lock->write_cond);
973 else if (lock->want_to_read && lock->read_cond)
974 g_cond_broadcast (lock->read_cond);
978 * g_static_rw_lock_reader_lock:
979 * @lock: a #GStaticRWLock to lock for reading.
981 * Locks @lock for reading. There may be unlimited concurrent locks for
982 * reading of a #GStaticRWLock at the same time. If @lock is already
983 * locked for writing by another thread or if another thread is already
984 * waiting to lock @lock for writing, this function will block until
985 * @lock is unlocked by the other writing thread and no other writing
986 * threads want to lock @lock. This lock has to be unlocked by
987 * g_static_rw_lock_reader_unlock().
989 * #GStaticRWLock is not recursive. It might seem to be possible to
990 * recursively lock for reading, but that can result in a deadlock, due
991 * to writer preference.
993 * Deprecated: 2.32: Use g_rw_lock_reader_lock() instead
996 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
998 g_return_if_fail (lock);
1000 if (!g_threads_got_initialized)
1003 g_static_mutex_lock (&lock->mutex);
1004 lock->want_to_read++;
1005 while (lock->have_writer || lock->want_to_write)
1006 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
1007 lock->want_to_read--;
1008 lock->read_counter++;
1009 g_static_mutex_unlock (&lock->mutex);
1013 * g_static_rw_lock_reader_trylock:
1014 * @lock: a #GStaticRWLock to lock for reading.
1015 * @Returns: %TRUE, if @lock could be locked for reading.
1017 * Tries to lock @lock for reading. If @lock is already locked for
1018 * writing by another thread or if another thread is already waiting to
1019 * lock @lock for writing, immediately returns %FALSE. Otherwise locks
1020 * @lock for reading and returns %TRUE. This lock has to be unlocked by
1021 * g_static_rw_lock_reader_unlock().
1023 * Deprectated: 2.32: Use g_rw_lock_reader_trylock() instead
1026 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
1028 gboolean ret_val = FALSE;
1030 g_return_val_if_fail (lock, FALSE);
1032 if (!g_threads_got_initialized)
1035 g_static_mutex_lock (&lock->mutex);
1036 if (!lock->have_writer && !lock->want_to_write)
1038 lock->read_counter++;
1041 g_static_mutex_unlock (&lock->mutex);
1046 * g_static_rw_lock_reader_unlock:
1047 * @lock: a #GStaticRWLock to unlock after reading.
1049 * Unlocks @lock. If a thread waits to lock @lock for writing and all
1050 * locks for reading have been unlocked, the waiting thread is woken up
1051 * and can lock @lock for writing.
1053 * Deprectated: 2.32: Use g_rw_lock_reader_unlock() instead
1056 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
1058 g_return_if_fail (lock);
1060 if (!g_threads_got_initialized)
1063 g_static_mutex_lock (&lock->mutex);
1064 lock->read_counter--;
1065 if (lock->read_counter == 0)
1066 g_static_rw_lock_signal (lock);
1067 g_static_mutex_unlock (&lock->mutex);
1071 * g_static_rw_lock_writer_lock:
1072 * @lock: a #GStaticRWLock to lock for writing.
1074 * Locks @lock for writing. If @lock is already locked for writing or
1075 * reading by other threads, this function will block until @lock is
1076 * completely unlocked and then lock @lock for writing. While this
1077 * functions waits to lock @lock, no other thread can lock @lock for
1078 * reading. When @lock is locked for writing, no other thread can lock
1079 * @lock (neither for reading nor writing). This lock has to be
1080 * unlocked by g_static_rw_lock_writer_unlock().
1082 * Deprectated: 2.32: Use g_rw_lock_writer_lock() instead
1085 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
1087 g_return_if_fail (lock);
1089 if (!g_threads_got_initialized)
1092 g_static_mutex_lock (&lock->mutex);
1093 lock->want_to_write++;
1094 while (lock->have_writer || lock->read_counter)
1095 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
1096 lock->want_to_write--;
1097 lock->have_writer = TRUE;
1098 g_static_mutex_unlock (&lock->mutex);
1102 * g_static_rw_lock_writer_trylock:
1103 * @lock: a #GStaticRWLock to lock for writing.
1104 * @Returns: %TRUE, if @lock could be locked for writing.
1106 * Tries to lock @lock for writing. If @lock is already locked (for
1107 * either reading or writing) by another thread, it immediately returns
1108 * %FALSE. Otherwise it locks @lock for writing and returns %TRUE. This
1109 * lock has to be unlocked by g_static_rw_lock_writer_unlock().
1111 * Deprectated: 2.32: Use g_rw_lock_writer_trylock() instead
1114 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
1116 gboolean ret_val = FALSE;
1118 g_return_val_if_fail (lock, FALSE);
1120 if (!g_threads_got_initialized)
1123 g_static_mutex_lock (&lock->mutex);
1124 if (!lock->have_writer && !lock->read_counter)
1126 lock->have_writer = TRUE;
1129 g_static_mutex_unlock (&lock->mutex);
1134 * g_static_rw_lock_writer_unlock:
1135 * @lock: a #GStaticRWLock to unlock after writing.
1137 * Unlocks @lock. If a thread is waiting to lock @lock for writing and
1138 * all locks for reading have been unlocked, the waiting thread is
1139 * woken up and can lock @lock for writing. If no thread is waiting to
1140 * lock @lock for writing, and some thread or threads are waiting to
1141 * lock @lock for reading, the waiting threads are woken up and can
1142 * lock @lock for reading.
1144 * Deprectated: 2.32: Use g_rw_lock_writer_unlock() instead
1147 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
1149 g_return_if_fail (lock);
1151 if (!g_threads_got_initialized)
1154 g_static_mutex_lock (&lock->mutex);
1155 lock->have_writer = FALSE;
1156 g_static_rw_lock_signal (lock);
1157 g_static_mutex_unlock (&lock->mutex);
1161 * g_static_rw_lock_free:
1162 * @lock: a #GStaticRWLock to be freed.
1164 * Releases all resources allocated to @lock.
1166 * You don't have to call this functions for a #GStaticRWLock with an
1167 * unbounded lifetime, i.e. objects declared 'static', but if you have
1168 * a #GStaticRWLock as a member of a structure, and the structure is
1169 * freed, you should also free the #GStaticRWLock.
1171 * Deprecated: 2.32: Use a #GRWLock instead
1174 g_static_rw_lock_free (GStaticRWLock* lock)
1176 g_return_if_fail (lock);
1178 if (lock->read_cond)
1180 g_cond_free (lock->read_cond);
1181 lock->read_cond = NULL;
1183 if (lock->write_cond)
1185 g_cond_free (lock->write_cond);
1186 lock->write_cond = NULL;
1188 g_static_mutex_free (&lock->mutex);
1191 /* GPrivate {{{1 ------------------------------------------------------ */
1195 * @notify: a #GDestroyNotify
1197 * Deprecated:2.32: dynamic allocation of #GPrivate is a bad idea. Use
1198 * static storage and G_PRIVATE_INIT() instead.
1200 * Returns: a newly allocated #GPrivate (which can never be destroyed)
1203 g_private_new (GDestroyNotify notify)
1205 GPrivate tmp = G_PRIVATE_INIT (notify);
1208 key = g_slice_new (GPrivate);
1214 /* {{{1 GStaticPrivate */
1216 typedef struct _GStaticPrivateNode GStaticPrivateNode;
1217 struct _GStaticPrivateNode
1220 GDestroyNotify destroy;
1221 GStaticPrivate *owner;
1225 g_static_private_cleanup (gpointer data)
1227 GArray *array = data;
1230 for (i = 0; i < array->len; i++ )
1232 GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
1234 node->destroy (node->data);
1237 g_array_free (array, TRUE);
1240 GPrivate static_private_private = G_PRIVATE_INIT (g_static_private_cleanup);
1245 * A #GStaticPrivate works almost like a #GPrivate, but it has one
1246 * significant advantage. It doesn't need to be created at run-time
1247 * like a #GPrivate, but can be defined at compile-time. This is
1248 * similar to the difference between #GMutex and #GStaticMutex. Now
1249 * look at our <function>give_me_next_number()</function> example with
1253 * <title>Using GStaticPrivate for per-thread data</title>
1256 * give_me_next_number (<!-- -->)
1258 * static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1259 * int *current_number = g_static_private_get (&current_number_key);
1261 * if (!current_number)
1263 * current_number = g_new (int,1);
1264 * *current_number = 0;
1265 * g_static_private_set (&current_number_key, current_number, g_free);
1268 * *current_number = calc_next_number (*current_number);
1270 * return *current_number;
1277 * G_STATIC_PRIVATE_INIT:
1279 * Every #GStaticPrivate must be initialized with this macro, before it
1283 * GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1288 * g_static_private_init:
1289 * @private_key: a #GStaticPrivate to be initialized
1291 * Initializes @private_key. Alternatively you can initialize it with
1292 * #G_STATIC_PRIVATE_INIT.
1295 g_static_private_init (GStaticPrivate *private_key)
1297 private_key->index = 0;
1301 * g_static_private_get:
1302 * @private_key: a #GStaticPrivate
1304 * Works like g_private_get() only for a #GStaticPrivate.
1306 * This function works even if g_thread_init() has not yet been called.
1308 * Returns: the corresponding pointer
1311 g_static_private_get (GStaticPrivate *private_key)
1314 gpointer ret = NULL;
1316 array = g_private_get (&static_private_private);
1318 if (array && private_key->index != 0 && private_key->index <= array->len)
1320 GStaticPrivateNode *node;
1322 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1324 /* Deal with the possibility that the GStaticPrivate which used
1325 * to have this index got freed and the index got allocated to
1326 * a new one. In this case, the data in the node is stale, so
1327 * free it and return NULL.
1329 if (G_UNLIKELY (node->owner != private_key))
1332 node->destroy (node->data);
1333 node->destroy = NULL;
1344 * g_static_private_set:
1345 * @private_key: a #GStaticPrivate
1346 * @data: the new pointer
1347 * @notify: a function to be called with the pointer whenever the
1348 * current thread ends or sets this pointer again
1350 * Sets the pointer keyed to @private_key for the current thread and
1351 * the function @notify to be called with that pointer (%NULL or
1352 * non-%NULL), whenever the pointer is set again or whenever the
1353 * current thread ends.
1355 * This function works even if g_thread_init() has not yet been called.
1356 * If g_thread_init() is called later, the @data keyed to @private_key
1357 * will be inherited only by the main thread, i.e. the one that called
1360 * <note><para>@notify is used quite differently from @destructor in
1361 * g_private_new().</para></note>
1364 g_static_private_set (GStaticPrivate *private_key,
1366 GDestroyNotify notify)
1369 static guint next_index = 0;
1370 GStaticPrivateNode *node;
1372 if (!private_key->index)
1376 if (!private_key->index)
1378 if (g_thread_free_indices)
1380 private_key->index = GPOINTER_TO_UINT (g_thread_free_indices->data);
1381 g_thread_free_indices = g_slist_delete_link (g_thread_free_indices,
1382 g_thread_free_indices);
1385 private_key->index = ++next_index;
1388 G_UNLOCK (g_thread);
1391 array = g_private_get (&static_private_private);
1394 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
1395 g_private_set (&static_private_private, array);
1397 if (private_key->index > array->len)
1398 g_array_set_size (array, private_key->index);
1400 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1403 node->destroy (node->data);
1406 node->destroy = notify;
1407 node->owner = private_key;
1411 * g_static_private_free:
1412 * @private_key: a #GStaticPrivate to be freed
1414 * Releases all resources allocated to @private_key.
1416 * You don't have to call this functions for a #GStaticPrivate with an
1417 * unbounded lifetime, i.e. objects declared 'static', but if you have
1418 * a #GStaticPrivate as a member of a structure and the structure is
1419 * freed, you should also free the #GStaticPrivate.
1422 g_static_private_free (GStaticPrivate *private_key)
1424 guint idx = private_key->index;
1429 private_key->index = 0;
1431 /* Freeing the per-thread data is deferred to either the
1432 * thread end or the next g_static_private_get() call for
1436 g_thread_free_indices = g_slist_prepend (g_thread_free_indices,
1437 GUINT_TO_POINTER (idx));
1438 G_UNLOCK (g_thread);
1441 /* GMutex {{{1 ------------------------------------------------------ */
1446 * Allocates and initializes a new #GMutex.
1448 * Returns: a newly allocated #GMutex. Use g_mutex_free() to free
1450 * Deprecated:3.32:GMutex can now be statically allocated, or embedded
1451 * in structures and initialised with g_mutex_init().
1458 mutex = g_slice_new (GMutex);
1459 g_mutex_init (mutex);
1468 * Destroys a @mutex that has been created with g_mutex_new().
1470 * Calling g_mutex_free() on a locked mutex may result
1471 * in undefined behaviour.
1473 * Deprecated:3.32:GMutex can now be statically allocated, or embedded
1474 * in structures and initialised with g_mutex_init().
1477 g_mutex_free (GMutex *mutex)
1479 g_mutex_clear (mutex);
1480 g_slice_free (GMutex, mutex);
1483 /* GCond {{{1 ------------------------------------------------------ */
1488 * Allocates and initializes a new #GCond.
1490 * Returns: a newly allocated #GCond. Free with g_cond_free()
1492 * Deprecated:3.32:GCond can now be statically allocated, or embedded
1493 * in structures and initialised with g_cond_init().
1500 cond = g_slice_new (GCond);
1510 * Destroys a #GCond that has been created with g_cond_new().
1512 * Calling g_cond_free() for a #GCond on which threads are
1513 * blocking leads to undefined behaviour.
1515 * Deprecated:3.32:GCond can now be statically allocated, or embedded
1516 * in structures and initialised with g_cond_init().
1519 g_cond_free (GCond *cond)
1521 g_cond_clear (cond);
1522 g_slice_free (GCond, cond);
1526 /* vim: set foldmethod=marker: */