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 * SPDX-License-Identifier: LGPL-2.1-or-later
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 /* we know we are deprecated here, no need for warnings */
27 #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
28 #define GLIB_DISABLE_DEPRECATION_WARNINGS
31 #include "gmessages.h"
35 #include "gthreadprivate.h"
36 #include "deprecated/gthread.h"
41 /* {{{1 Documentation */
44 * SECTION:threads-deprecated
45 * @title: Deprecated thread API
46 * @short_description: old thread APIs (for reference only)
49 * These APIs are deprecated. You should not use them in new code.
50 * This section remains only to assist with understanding code that was
51 * written to use these APIs at some point in the past.
56 * @G_THREAD_PRIORITY_LOW: a priority lower than normal
57 * @G_THREAD_PRIORITY_NORMAL: the default priority
58 * @G_THREAD_PRIORITY_HIGH: a priority higher than normal
59 * @G_THREAD_PRIORITY_URGENT: the highest priority
63 * Deprecated:2.32: Thread priorities no longer have any effect.
68 * @mutex_new: virtual function pointer for g_mutex_new()
69 * @mutex_lock: virtual function pointer for g_mutex_lock()
70 * @mutex_trylock: virtual function pointer for g_mutex_trylock()
71 * @mutex_unlock: virtual function pointer for g_mutex_unlock()
72 * @mutex_free: virtual function pointer for g_mutex_free()
73 * @cond_new: virtual function pointer for g_cond_new()
74 * @cond_signal: virtual function pointer for g_cond_signal()
75 * @cond_broadcast: virtual function pointer for g_cond_broadcast()
76 * @cond_wait: virtual function pointer for g_cond_wait()
77 * @cond_timed_wait: virtual function pointer for g_cond_timed_wait()
78 * @cond_free: virtual function pointer for g_cond_free()
79 * @private_new: virtual function pointer for g_private_new()
80 * @private_get: virtual function pointer for g_private_get()
81 * @private_set: virtual function pointer for g_private_set()
82 * @thread_create: virtual function pointer for g_thread_create()
83 * @thread_yield: virtual function pointer for g_thread_yield()
84 * @thread_join: virtual function pointer for g_thread_join()
85 * @thread_exit: virtual function pointer for g_thread_exit()
86 * @thread_set_priority: virtual function pointer for
87 * g_thread_set_priority()
88 * @thread_self: virtual function pointer for g_thread_self()
89 * @thread_equal: used internally by recursive mutex locks and by some
92 * This function table is no longer used by g_thread_init()
93 * to initialize the thread system.
97 * G_THREADS_IMPL_POSIX:
99 * This macro is defined if POSIX style threads are used.
101 * Deprecated:2.32:POSIX threads are in use on all non-Windows systems.
102 * Use %G_OS_WIN32 to detect Windows.
106 * G_THREADS_IMPL_WIN32:
108 * This macro is defined if Windows style threads are used.
110 * Deprecated:2.32:Use %G_OS_WIN32 to detect Windows.
114 /* {{{1 Exported Variables */
116 /* Set this FALSE to have previously-compiled GStaticMutex code use the
117 * slow path (ie: call into us) to avoid compatibility problems.
119 gboolean g_thread_use_default_impl = FALSE;
121 GThreadFunctions g_thread_functions_for_glib_use =
149 return g_get_monotonic_time () * 1000;
152 guint64 (*g_thread_gettime) (void) = gettime;
154 /* Initialisation {{{1 ---------------------------------------------------- */
155 gboolean g_threads_got_initialized = TRUE;
159 * @vtable: a function table of type #GThreadFunctions, that provides
160 * the entry points to the thread system to be used. Since 2.32,
161 * this parameter is ignored and should always be %NULL
163 * If you use GLib from more than one thread, you must initialize the
164 * thread system by calling g_thread_init().
166 * Since version 2.24, calling g_thread_init() multiple times is allowed,
167 * but nothing happens except for the first call.
169 * Since version 2.32, GLib does not support custom thread implementations
170 * anymore and the @vtable parameter is ignored and you should pass %NULL.
172 * <note><para>g_thread_init() must not be called directly or indirectly
173 * in a callback from GLib. Also no mutexes may be currently locked while
174 * calling g_thread_init().</para></note>
176 * <note><para>To use g_thread_init() in your program, you have to link
177 * with the libraries that the command <command>pkg-config --libs
178 * gthread-2.0</command> outputs. This is not the case for all the
179 * other thread-related functions of GLib. Those can be used without
180 * having to link with the thread libraries.</para></note>
182 * Deprecated:2.32: This function is no longer necessary. The GLib
183 * threading system is automatically initialized at the start
188 * g_thread_get_initialized:
190 * Indicates if g_thread_init() has been called.
192 * Returns: %TRUE if threads have been initialized.
197 g_thread_get_initialized (void)
199 return g_thread_supported ();
202 /* We need this for ABI compatibility */
203 GLIB_AVAILABLE_IN_ALL
204 void g_thread_init_glib (void);
205 void g_thread_init_glib (void) { }
207 /* Internal variables {{{1 */
209 static GSList *g_thread_all_threads = NULL;
210 static GSList *g_thread_free_indices = NULL;
212 /* Protects g_thread_all_threads and g_thread_free_indices */
213 G_LOCK_DEFINE_STATIC (g_static_mutex);
214 G_LOCK_DEFINE_STATIC (g_thread);
216 /* Misc. GThread functions {{{1 */
219 * g_thread_set_priority:
220 * @thread: a #GThread.
223 * This function does nothing.
225 * Deprecated:2.32: Thread priorities no longer have any effect.
228 g_thread_set_priority (GThread *thread,
229 GThreadPriority priority)
235 * @thread_func: function to call for all #GThread structures
236 * @user_data: second argument to @thread_func
238 * Call @thread_func on all #GThreads that have been
239 * created with g_thread_create().
241 * Note that threads may decide to exit while @thread_func is
242 * running, so without intimate knowledge about the lifetime of
243 * foreign threads, @thread_func shouldn't access the GThread*
244 * pointer passed in as first argument. However, @thread_func will
245 * not be called for threads which are known to have exited already.
247 * Due to thread lifetime checks, this function has an execution complexity
248 * which is quadratic in the number of existing threads.
252 * Deprecated:2.32: There aren't many things you can do with a #GThread,
253 * except comparing it with one that was returned from g_thread_create().
254 * There are better ways to find out if your thread is still alive.
257 g_thread_foreach (GFunc thread_func,
260 GSList *slist = NULL;
262 g_return_if_fail (thread_func != NULL);
263 /* snapshot the list of threads for iteration */
265 slist = g_slist_copy (g_thread_all_threads);
267 /* walk the list, skipping non-existent threads */
270 GSList *node = slist;
272 /* check whether the current thread still exists */
274 if (g_slist_find (g_thread_all_threads, node->data))
280 thread_func (thread, user_data);
281 g_slist_free_1 (node);
286 g_enumerable_thread_remove (gpointer data)
288 GRealThread *thread = data;
291 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
295 GPrivate enumerable_thread_private = G_PRIVATE_INIT (g_enumerable_thread_remove);
298 g_enumerable_thread_add (GRealThread *thread)
301 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
304 g_private_set (&enumerable_thread_private, thread);
308 g_deprecated_thread_proxy (gpointer data)
310 GRealThread *real = data;
312 g_enumerable_thread_add (real);
314 return g_thread_proxy (data);
319 * @func: a function to execute in the new thread
320 * @data: an argument to supply to the new thread
321 * @joinable: should this thread be joinable?
322 * @error: return location for error, or %NULL
324 * This function creates a new thread.
326 * The new thread executes the function @func with the argument @data.
327 * If the thread was created successfully, it is returned.
329 * @error can be %NULL to ignore errors, or non-%NULL to report errors.
330 * The error is set, if and only if the function returns %NULL.
332 * This function returns a reference to the created thread only if
333 * @joinable is %TRUE. In that case, you must free this reference by
334 * calling g_thread_unref() or g_thread_join(). If @joinable is %FALSE
335 * then you should probably not touch the return value.
337 * Returns: the new #GThread on success
339 * Deprecated:2.32: Use g_thread_new() instead
342 g_thread_create (GThreadFunc func,
347 return g_thread_create_full (func, data, 0, joinable, 0, 0, error);
351 * g_thread_create_full:
352 * @func: a function to execute in the new thread.
353 * @data: an argument to supply to the new thread.
354 * @stack_size: a stack size for the new thread.
355 * @joinable: should this thread be joinable?
358 * @error: return location for error.
360 * This function creates a new thread.
362 * Returns: the new #GThread on success.
364 * Deprecated:2.32: The @bound and @priority arguments are now ignored.
365 * Use g_thread_new().
368 g_thread_create_full (GThreadFunc func,
373 GThreadPriority priority,
378 thread = g_thread_new_internal (NULL, g_deprecated_thread_proxy,
379 func, data, stack_size, error);
381 if (thread && !joinable)
383 thread->joinable = FALSE;
384 g_thread_unref (thread);
390 /* GOnce {{{1 ------------------------------------------------------------- */
392 g_once_init_enter_impl (volatile gsize *location)
394 return (g_once_init_enter) (location);
397 /* GStaticMutex {{{1 ------------------------------------------------------ */
402 * A #GStaticMutex works like a #GMutex.
404 * Prior to GLib 2.32, GStaticMutex had the significant advantage
405 * that it doesn't need to be created at run-time, but can be defined
406 * at compile-time. Since 2.32, #GMutex can be statically allocated
407 * as well, and GStaticMutex has been deprecated.
409 * Here is a version of our give_me_next_number() example using
413 * give_me_next_number (void)
415 * static int current_number = 0;
417 * static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
419 * g_static_mutex_lock (&mutex);
420 * ret_val = current_number = calc_next_number (current_number);
421 * g_static_mutex_unlock (&mutex);
427 * Sometimes you would like to dynamically create a mutex. If you don't
428 * want to require prior calling to g_thread_init(), because your code
429 * should also be usable in non-threaded programs, you are not able to
430 * use g_mutex_new() and thus #GMutex, as that requires a prior call to
431 * g_thread_init(). In these cases you can also use a #GStaticMutex.
432 * It must be initialized with g_static_mutex_init() before using it
433 * and freed with with g_static_mutex_free() when not needed anymore to
434 * free up any allocated resources.
436 * Even though #GStaticMutex is not opaque, it should only be used with
437 * the following functions, as it is defined differently on different
440 * All of the g_static_mutex_* functions apart from
441 * g_static_mutex_get_mutex() can also be used even if g_thread_init()
442 * has not yet been called. Then they do nothing, apart from
443 * g_static_mutex_trylock() which does nothing but returning %TRUE.
445 * All of the g_static_mutex_* functions are actually macros. Apart from
446 * taking their addresses, you can however use them as if they were
451 * G_STATIC_MUTEX_INIT:
453 * A #GStaticMutex must be initialized with this macro, before it can
454 * be used. This macro can used be to initialize a variable, but it
455 * cannot be assigned to a variable. In that case you have to use
456 * g_static_mutex_init().
459 * GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
464 * g_static_mutex_init:
465 * @mutex: a #GStaticMutex to be initialized.
467 * Initializes @mutex.
468 * Alternatively you can initialize it with %G_STATIC_MUTEX_INIT.
470 * Deprecated: 2.32: Use g_mutex_init()
473 g_static_mutex_init (GStaticMutex *mutex)
475 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
477 g_return_if_fail (mutex);
482 /* IMPLEMENTATION NOTE:
484 * On some platforms a GStaticMutex is actually a normal GMutex stored
485 * inside of a structure instead of being allocated dynamically. We can
486 * only do this for platforms on which we know, in advance, how to
487 * allocate (size) and initialise (value) that memory.
489 * On other platforms, a GStaticMutex is nothing more than a pointer to
490 * a GMutex. In that case, the first access we make to the static mutex
491 * must first allocate the normal GMutex and store it into the pointer.
493 * configure.ac writes macros into glibconfig.h to determine if
494 * g_static_mutex_get_mutex() accesses the structure in memory directly
495 * (on platforms where we are able to do that) or if it ends up here,
496 * where we may have to allocate the GMutex before returning it.
500 * g_static_mutex_get_mutex:
501 * @mutex: a #GStaticMutex.
503 * For some operations (like g_cond_wait()) you must have a #GMutex
504 * instead of a #GStaticMutex. This function will return the
505 * corresponding #GMutex for @mutex.
507 * Returns: the #GMutex corresponding to @mutex.
509 * Deprecated: 2.32: Just use a #GMutex
512 g_static_mutex_get_mutex_impl (GStaticMutex* mutex)
516 if (!g_thread_supported ())
519 result = g_atomic_pointer_get (&mutex->mutex);
523 G_LOCK (g_static_mutex);
525 result = mutex->mutex;
528 result = g_mutex_new ();
529 g_atomic_pointer_set (&mutex->mutex, result);
532 G_UNLOCK (g_static_mutex);
538 /* IMPLEMENTATION NOTE:
540 * g_static_mutex_lock(), g_static_mutex_trylock() and
541 * g_static_mutex_unlock() are all preprocessor macros that wrap the
542 * corresponding g_mutex_*() function around a call to
543 * g_static_mutex_get_mutex().
547 * g_static_mutex_lock:
548 * @mutex: a #GStaticMutex.
550 * Works like g_mutex_lock(), but for a #GStaticMutex.
552 * Deprecated: 2.32: Use g_mutex_lock()
556 * g_static_mutex_trylock:
557 * @mutex: a #GStaticMutex.
559 * Works like g_mutex_trylock(), but for a #GStaticMutex.
561 * Returns: %TRUE, if the #GStaticMutex could be locked.
563 * Deprecated: 2.32: Use g_mutex_trylock()
567 * g_static_mutex_unlock:
568 * @mutex: a #GStaticMutex.
570 * Works like g_mutex_unlock(), but for a #GStaticMutex.
572 * Deprecated: 2.32: Use g_mutex_unlock()
576 * g_static_mutex_free:
577 * @mutex: a #GStaticMutex to be freed.
579 * Releases all resources allocated to @mutex.
581 * You don't have to call this functions for a #GStaticMutex with an
582 * unbounded lifetime, i.e. objects declared 'static', but if you have
583 * a #GStaticMutex as a member of a structure and the structure is
584 * freed, you should also free the #GStaticMutex.
586 * Calling g_static_mutex_free() on a locked mutex may result in
587 * undefined behaviour.
589 * Deprecated: 2.32: Use g_mutex_clear()
592 g_static_mutex_free (GStaticMutex* mutex)
594 GMutex **runtime_mutex;
596 g_return_if_fail (mutex);
598 /* The runtime_mutex is the first (or only) member of GStaticMutex,
599 * see both versions (of glibconfig.h) in configure.ac. Note, that
600 * this variable is NULL, if g_thread_init() hasn't been called or
601 * if we're using the default thread implementation and it provides
603 runtime_mutex = ((GMutex**)mutex);
606 g_mutex_free (*runtime_mutex);
608 *runtime_mutex = NULL;
611 /* {{{1 GStaticRecMutex */
616 * A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
617 * multiple times by one thread. If you enter it n times, you have to
618 * unlock it n times again to let other threads lock it. An exception
619 * is the function g_static_rec_mutex_unlock_full(): that allows you to
620 * unlock a #GStaticRecMutex completely returning the depth, (i.e. the
621 * number of times this mutex was locked). The depth can later be used
622 * to restore the state of the #GStaticRecMutex by calling
623 * g_static_rec_mutex_lock_full(). In GLib 2.32, #GStaticRecMutex has
624 * been deprecated in favor of #GRecMutex.
626 * Even though #GStaticRecMutex is not opaque, it should only be used
627 * with the following functions.
629 * All of the g_static_rec_mutex_* functions can be used even if
630 * g_thread_init() has not been called. Then they do nothing, apart
631 * from g_static_rec_mutex_trylock(), which does nothing but returning
636 * G_STATIC_REC_MUTEX_INIT:
638 * A #GStaticRecMutex must be initialized with this macro before it can
639 * be used. This macro can used be to initialize a variable, but it
640 * cannot be assigned to a variable. In that case you have to use
641 * g_static_rec_mutex_init().
644 * GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
649 * g_static_rec_mutex_init:
650 * @mutex: a #GStaticRecMutex to be initialized.
652 * A #GStaticRecMutex must be initialized with this function before it
653 * can be used. Alternatively you can initialize it with
654 * %G_STATIC_REC_MUTEX_INIT.
656 * Deprecated: 2.32: Use g_rec_mutex_init()
659 g_static_rec_mutex_init (GStaticRecMutex *mutex)
661 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
663 g_return_if_fail (mutex);
669 g_static_rec_mutex_get_rec_mutex_impl (GStaticRecMutex* mutex)
673 if (!g_thread_supported ())
676 result = (GRecMutex *) g_atomic_pointer_get (&mutex->mutex.mutex);
680 G_LOCK (g_static_mutex);
682 result = (GRecMutex *) mutex->mutex.mutex;
685 result = g_slice_new (GRecMutex);
686 g_rec_mutex_init (result);
687 g_atomic_pointer_set (&mutex->mutex.mutex, (GMutex *) result);
690 G_UNLOCK (g_static_mutex);
697 * g_static_rec_mutex_lock:
698 * @mutex: a #GStaticRecMutex to lock.
700 * Locks @mutex. If @mutex is already locked by another thread, the
701 * current thread will block until @mutex is unlocked by the other
702 * thread. If @mutex is already locked by the calling thread, this
703 * functions increases the depth of @mutex and returns immediately.
705 * Deprecated: 2.32: Use g_rec_mutex_lock()
708 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
711 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
712 g_rec_mutex_lock (rm);
717 * g_static_rec_mutex_trylock:
718 * @mutex: a #GStaticRecMutex to lock.
720 * Tries to lock @mutex. If @mutex is already locked by another thread,
721 * it immediately returns %FALSE. Otherwise it locks @mutex and returns
722 * %TRUE. If @mutex is already locked by the calling thread, this
723 * functions increases the depth of @mutex and immediately returns
726 * Returns: %TRUE, if @mutex could be locked.
728 * Deprecated: 2.32: Use g_rec_mutex_trylock()
731 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
734 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
736 if (g_rec_mutex_trylock (rm))
746 * g_static_rec_mutex_unlock:
747 * @mutex: a #GStaticRecMutex to unlock.
749 * Unlocks @mutex. Another thread will be allowed to lock @mutex only
750 * when it has been unlocked as many times as it had been locked
751 * before. If @mutex is completely unlocked and another thread is
752 * blocked in a g_static_rec_mutex_lock() call for @mutex, it will be
753 * woken and can lock @mutex itself.
755 * Deprecated: 2.32: Use g_rec_mutex_unlock()
758 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
761 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
763 g_rec_mutex_unlock (rm);
767 * g_static_rec_mutex_lock_full:
768 * @mutex: a #GStaticRecMutex to lock.
769 * @depth: number of times this mutex has to be unlocked to be
770 * completely unlocked.
772 * Works like calling g_static_rec_mutex_lock() for @mutex @depth times.
774 * Deprecated: 2.32: Use g_rec_mutex_lock()
777 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
782 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
785 g_rec_mutex_lock (rm);
791 * g_static_rec_mutex_unlock_full:
792 * @mutex: a #GStaticRecMutex to completely unlock.
794 * Completely unlocks @mutex. If another thread is blocked in a
795 * g_static_rec_mutex_lock() call for @mutex, it will be woken and can
796 * lock @mutex itself. This function returns the number of times that
797 * @mutex has been locked by the current thread. To restore the state
798 * before the call to g_static_rec_mutex_unlock_full() you can call
799 * g_static_rec_mutex_lock_full() with the depth returned by this
802 * Returns: number of times @mutex has been locked by the current
805 * Deprecated: 2.32: Use g_rec_mutex_unlock()
808 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
814 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
816 /* all access to mutex->depth done while still holding the lock */
817 depth = mutex->depth;
822 g_rec_mutex_unlock (rm);
828 * g_static_rec_mutex_free:
829 * @mutex: a #GStaticRecMutex to be freed.
831 * Releases all resources allocated to a #GStaticRecMutex.
833 * You don't have to call this functions for a #GStaticRecMutex with an
834 * unbounded lifetime, i.e. objects declared 'static', but if you have
835 * a #GStaticRecMutex as a member of a structure and the structure is
836 * freed, you should also free the #GStaticRecMutex.
838 * Deprecated: 2.32: Use g_rec_mutex_clear()
841 g_static_rec_mutex_free (GStaticRecMutex *mutex)
843 g_return_if_fail (mutex);
845 if (mutex->mutex.mutex)
847 GRecMutex *rm = (GRecMutex *) mutex->mutex.mutex;
849 g_rec_mutex_clear (rm);
850 g_slice_free (GRecMutex, rm);
854 /* GStaticRWLock {{{1 ----------------------------------------------------- */
859 * The #GStaticRWLock struct represents a read-write lock. A read-write
860 * lock can be used for protecting data that some portions of code only
861 * read from, while others also write. In such situations it is
862 * desirable that several readers can read at once, whereas of course
863 * only one writer may write at a time.
865 * Take a look at the following example:
867 * GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
871 * my_array_get (guint index)
873 * gpointer retval = NULL;
878 * g_static_rw_lock_reader_lock (&rwlock);
879 * if (index < array->len)
880 * retval = g_ptr_array_index (array, index);
881 * g_static_rw_lock_reader_unlock (&rwlock);
887 * my_array_set (guint index, gpointer data)
889 * g_static_rw_lock_writer_lock (&rwlock);
892 * array = g_ptr_array_new ();
894 * if (index >= array->len)
895 * g_ptr_array_set_size (array, index + 1);
896 * g_ptr_array_index (array, index) = data;
898 * g_static_rw_lock_writer_unlock (&rwlock);
902 * This example shows an array which can be accessed by many readers
903 * (the my_array_get() function) simultaneously, whereas the writers
904 * (the my_array_set() function) will only be allowed once at a time
905 * and only if no readers currently access the array. This is because
906 * of the potentially dangerous resizing of the array. Using these
907 * functions is fully multi-thread safe now.
909 * Most of the time, writers should have precedence over readers. That
910 * means, for this implementation, that as soon as a writer wants to
911 * lock the data, no other reader is allowed to lock the data, whereas,
912 * of course, the readers that already have locked the data are allowed
913 * to finish their operation. As soon as the last reader unlocks the
914 * data, the writer will lock it.
916 * Even though #GStaticRWLock is not opaque, it should only be used
917 * with the following functions.
919 * All of the g_static_rw_lock_* functions can be used even if
920 * g_thread_init() has not been called. Then they do nothing, apart
921 * from g_static_rw_lock_*_trylock, which does nothing but returning %TRUE.
923 * A read-write lock has a higher overhead than a mutex. For example, both
924 * g_static_rw_lock_reader_lock() and g_static_rw_lock_reader_unlock() have
925 * to lock and unlock a #GStaticMutex, so it takes at least twice the time
926 * to lock and unlock a #GStaticRWLock that it does to lock and unlock a
927 * #GStaticMutex. So only data structures that are accessed by multiple
928 * readers, and which keep the lock for a considerable time justify a
929 * #GStaticRWLock. The above example most probably would fare better with a
932 * Deprecated: 2.32: Use a #GRWLock instead
936 * G_STATIC_RW_LOCK_INIT:
938 * A #GStaticRWLock must be initialized with this macro before it can
939 * be used. This macro can used be to initialize a variable, but it
940 * cannot be assigned to a variable. In that case you have to use
941 * g_static_rw_lock_init().
944 * GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
949 * g_static_rw_lock_init:
950 * @lock: a #GStaticRWLock to be initialized.
952 * A #GStaticRWLock must be initialized with this function before it
953 * can be used. Alternatively you can initialize it with
954 * %G_STATIC_RW_LOCK_INIT.
956 * Deprecated: 2.32: Use g_rw_lock_init() instead
959 g_static_rw_lock_init (GStaticRWLock* lock)
961 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
963 g_return_if_fail (lock);
969 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
972 *cond = g_cond_new ();
973 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
977 g_static_rw_lock_signal (GStaticRWLock* lock)
979 if (lock->want_to_write && lock->write_cond)
980 g_cond_signal (lock->write_cond);
981 else if (lock->want_to_read && lock->read_cond)
982 g_cond_broadcast (lock->read_cond);
986 * g_static_rw_lock_reader_lock:
987 * @lock: a #GStaticRWLock to lock for reading.
989 * Locks @lock for reading. There may be unlimited concurrent locks for
990 * reading of a #GStaticRWLock at the same time. If @lock is already
991 * locked for writing by another thread or if another thread is already
992 * waiting to lock @lock for writing, this function will block until
993 * @lock is unlocked by the other writing thread and no other writing
994 * threads want to lock @lock. This lock has to be unlocked by
995 * g_static_rw_lock_reader_unlock().
997 * #GStaticRWLock is not recursive. It might seem to be possible to
998 * recursively lock for reading, but that can result in a deadlock, due
999 * to writer preference.
1001 * Deprecated: 2.32: Use g_rw_lock_reader_lock() instead
1004 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
1006 g_return_if_fail (lock);
1008 if (!g_threads_got_initialized)
1011 g_static_mutex_lock (&lock->mutex);
1012 lock->want_to_read++;
1013 while (lock->have_writer || lock->want_to_write)
1014 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
1015 lock->want_to_read--;
1016 lock->read_counter++;
1017 g_static_mutex_unlock (&lock->mutex);
1021 * g_static_rw_lock_reader_trylock:
1022 * @lock: a #GStaticRWLock to lock for reading
1024 * Tries to lock @lock for reading. If @lock is already locked for
1025 * writing by another thread or if another thread is already waiting to
1026 * lock @lock for writing, immediately returns %FALSE. Otherwise locks
1027 * @lock for reading and returns %TRUE. This lock has to be unlocked by
1028 * g_static_rw_lock_reader_unlock().
1030 * Returns: %TRUE, if @lock could be locked for reading
1032 * Deprecated: 2.32: Use g_rw_lock_reader_trylock() instead
1035 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
1037 gboolean ret_val = FALSE;
1039 g_return_val_if_fail (lock, FALSE);
1041 if (!g_threads_got_initialized)
1044 g_static_mutex_lock (&lock->mutex);
1045 if (!lock->have_writer && !lock->want_to_write)
1047 lock->read_counter++;
1050 g_static_mutex_unlock (&lock->mutex);
1055 * g_static_rw_lock_reader_unlock:
1056 * @lock: a #GStaticRWLock to unlock after reading
1058 * Unlocks @lock. If a thread waits to lock @lock for writing and all
1059 * locks for reading have been unlocked, the waiting thread is woken up
1060 * and can lock @lock for writing.
1062 * Deprecated: 2.32: Use g_rw_lock_reader_unlock() instead
1065 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
1067 g_return_if_fail (lock);
1069 if (!g_threads_got_initialized)
1072 g_static_mutex_lock (&lock->mutex);
1073 lock->read_counter--;
1074 if (lock->read_counter == 0)
1075 g_static_rw_lock_signal (lock);
1076 g_static_mutex_unlock (&lock->mutex);
1080 * g_static_rw_lock_writer_lock:
1081 * @lock: a #GStaticRWLock to lock for writing
1083 * Locks @lock for writing. If @lock is already locked for writing or
1084 * reading by other threads, this function will block until @lock is
1085 * completely unlocked and then lock @lock for writing. While this
1086 * functions waits to lock @lock, no other thread can lock @lock for
1087 * reading. When @lock is locked for writing, no other thread can lock
1088 * @lock (neither for reading nor writing). This lock has to be
1089 * unlocked by g_static_rw_lock_writer_unlock().
1091 * Deprecated: 2.32: Use g_rw_lock_writer_lock() instead
1094 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
1096 g_return_if_fail (lock);
1098 if (!g_threads_got_initialized)
1101 g_static_mutex_lock (&lock->mutex);
1102 lock->want_to_write++;
1103 while (lock->have_writer || lock->read_counter)
1104 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
1105 lock->want_to_write--;
1106 lock->have_writer = TRUE;
1107 g_static_mutex_unlock (&lock->mutex);
1111 * g_static_rw_lock_writer_trylock:
1112 * @lock: a #GStaticRWLock to lock for writing
1114 * Tries to lock @lock for writing. If @lock is already locked (for
1115 * either reading or writing) by another thread, it immediately returns
1116 * %FALSE. Otherwise it locks @lock for writing and returns %TRUE. This
1117 * lock has to be unlocked by g_static_rw_lock_writer_unlock().
1119 * Returns: %TRUE, if @lock could be locked for writing
1121 * Deprecated: 2.32: Use g_rw_lock_writer_trylock() instead
1124 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
1126 gboolean ret_val = FALSE;
1128 g_return_val_if_fail (lock, FALSE);
1130 if (!g_threads_got_initialized)
1133 g_static_mutex_lock (&lock->mutex);
1134 if (!lock->have_writer && !lock->read_counter)
1136 lock->have_writer = TRUE;
1139 g_static_mutex_unlock (&lock->mutex);
1144 * g_static_rw_lock_writer_unlock:
1145 * @lock: a #GStaticRWLock to unlock after writing.
1147 * Unlocks @lock. If a thread is waiting to lock @lock for writing and
1148 * all locks for reading have been unlocked, the waiting thread is
1149 * woken up and can lock @lock for writing. If no thread is waiting to
1150 * lock @lock for writing, and some thread or threads are waiting to
1151 * lock @lock for reading, the waiting threads are woken up and can
1152 * lock @lock for reading.
1154 * Deprecated: 2.32: Use g_rw_lock_writer_unlock() instead
1157 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
1159 g_return_if_fail (lock);
1161 if (!g_threads_got_initialized)
1164 g_static_mutex_lock (&lock->mutex);
1165 lock->have_writer = FALSE;
1166 g_static_rw_lock_signal (lock);
1167 g_static_mutex_unlock (&lock->mutex);
1171 * g_static_rw_lock_free:
1172 * @lock: a #GStaticRWLock to be freed.
1174 * Releases all resources allocated to @lock.
1176 * You don't have to call this functions for a #GStaticRWLock with an
1177 * unbounded lifetime, i.e. objects declared 'static', but if you have
1178 * a #GStaticRWLock as a member of a structure, and the structure is
1179 * freed, you should also free the #GStaticRWLock.
1181 * Deprecated: 2.32: Use a #GRWLock instead
1184 g_static_rw_lock_free (GStaticRWLock* lock)
1186 g_return_if_fail (lock);
1188 if (lock->read_cond)
1190 g_cond_free (lock->read_cond);
1191 lock->read_cond = NULL;
1193 if (lock->write_cond)
1195 g_cond_free (lock->write_cond);
1196 lock->write_cond = NULL;
1198 g_static_mutex_free (&lock->mutex);
1201 /* GPrivate {{{1 ------------------------------------------------------ */
1205 * @notify: a #GDestroyNotify
1207 * Creates a new #GPrivate.
1209 * Deprecated:2.32: dynamic allocation of #GPrivate is a bad idea. Use
1210 * static storage and G_PRIVATE_INIT() instead.
1212 * Returns: a newly allocated #GPrivate (which can never be destroyed)
1215 g_private_new (GDestroyNotify notify)
1217 GPrivate tmp = G_PRIVATE_INIT (notify);
1220 key = g_slice_new (GPrivate);
1226 /* {{{1 GStaticPrivate */
1228 typedef struct _GStaticPrivateNode GStaticPrivateNode;
1229 struct _GStaticPrivateNode
1232 GDestroyNotify destroy;
1233 GStaticPrivate *owner;
1237 g_static_private_cleanup (gpointer data)
1239 GArray *array = data;
1242 for (i = 0; i < array->len; i++ )
1244 GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
1246 node->destroy (node->data);
1249 g_array_free (array, TRUE);
1252 GPrivate static_private_private = G_PRIVATE_INIT (g_static_private_cleanup);
1257 * A #GStaticPrivate works almost like a #GPrivate, but it has one
1258 * significant advantage. It doesn't need to be created at run-time
1259 * like a #GPrivate, but can be defined at compile-time. This is
1260 * similar to the difference between #GMutex and #GStaticMutex.
1262 * Now look at our give_me_next_number() example with #GStaticPrivate:
1265 * give_me_next_number ()
1267 * static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1268 * int *current_number = g_static_private_get (¤t_number_key);
1270 * if (!current_number)
1272 * current_number = g_new (int, 1);
1273 * *current_number = 0;
1274 * g_static_private_set (¤t_number_key, current_number, g_free);
1277 * *current_number = calc_next_number (*current_number);
1279 * return *current_number;
1285 * G_STATIC_PRIVATE_INIT:
1287 * Every #GStaticPrivate must be initialized with this macro, before it
1291 * GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1296 * g_static_private_init:
1297 * @private_key: a #GStaticPrivate to be initialized
1299 * Initializes @private_key. Alternatively you can initialize it with
1300 * %G_STATIC_PRIVATE_INIT.
1303 g_static_private_init (GStaticPrivate *private_key)
1305 private_key->index = 0;
1309 * g_static_private_get:
1310 * @private_key: a #GStaticPrivate
1312 * Works like g_private_get() only for a #GStaticPrivate.
1314 * This function works even if g_thread_init() has not yet been called.
1316 * Returns: the corresponding pointer
1319 g_static_private_get (GStaticPrivate *private_key)
1322 gpointer ret = NULL;
1324 array = g_private_get (&static_private_private);
1326 if (array && private_key->index != 0 && private_key->index <= array->len)
1328 GStaticPrivateNode *node;
1330 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1332 /* Deal with the possibility that the GStaticPrivate which used
1333 * to have this index got freed and the index got allocated to
1334 * a new one. In this case, the data in the node is stale, so
1335 * free it and return NULL.
1337 if (G_UNLIKELY (node->owner != private_key))
1340 node->destroy (node->data);
1341 node->destroy = NULL;
1352 * g_static_private_set:
1353 * @private_key: a #GStaticPrivate
1354 * @data: the new pointer
1355 * @notify: a function to be called with the pointer whenever the
1356 * current thread ends or sets this pointer again
1358 * Sets the pointer keyed to @private_key for the current thread and
1359 * the function @notify to be called with that pointer (%NULL or
1360 * non-%NULL), whenever the pointer is set again or whenever the
1361 * current thread ends.
1363 * This function works even if g_thread_init() has not yet been called.
1364 * If g_thread_init() is called later, the @data keyed to @private_key
1365 * will be inherited only by the main thread, i.e. the one that called
1368 * @notify is used quite differently from @destructor in g_private_new().
1371 g_static_private_set (GStaticPrivate *private_key,
1373 GDestroyNotify notify)
1376 static guint next_index = 0;
1377 GStaticPrivateNode *node;
1379 if (!private_key->index)
1383 if (!private_key->index)
1385 if (g_thread_free_indices)
1387 private_key->index = GPOINTER_TO_UINT (g_thread_free_indices->data);
1388 g_thread_free_indices = g_slist_delete_link (g_thread_free_indices,
1389 g_thread_free_indices);
1392 private_key->index = ++next_index;
1395 G_UNLOCK (g_thread);
1398 array = g_private_get (&static_private_private);
1401 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
1402 g_private_set (&static_private_private, array);
1404 if (private_key->index > array->len)
1405 g_array_set_size (array, private_key->index);
1407 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1410 node->destroy (node->data);
1413 node->destroy = notify;
1414 node->owner = private_key;
1418 * g_static_private_free:
1419 * @private_key: a #GStaticPrivate to be freed
1421 * Releases all resources allocated to @private_key.
1423 * You don't have to call this functions for a #GStaticPrivate with an
1424 * unbounded lifetime, i.e. objects declared 'static', but if you have
1425 * a #GStaticPrivate as a member of a structure and the structure is
1426 * freed, you should also free the #GStaticPrivate.
1429 g_static_private_free (GStaticPrivate *private_key)
1431 guint idx = private_key->index;
1436 private_key->index = 0;
1438 /* Freeing the per-thread data is deferred to either the
1439 * thread end or the next g_static_private_get() call for
1443 g_thread_free_indices = g_slist_prepend (g_thread_free_indices,
1444 GUINT_TO_POINTER (idx));
1445 G_UNLOCK (g_thread);
1448 /* GMutex {{{1 ------------------------------------------------------ */
1453 * Allocates and initializes a new #GMutex.
1455 * Returns: a newly allocated #GMutex. Use g_mutex_free() to free
1457 * Deprecated: 2.32: GMutex can now be statically allocated, or embedded
1458 * in structures and initialised with g_mutex_init().
1465 mutex = g_slice_new (GMutex);
1466 g_mutex_init (mutex);
1475 * Destroys a @mutex that has been created with g_mutex_new().
1477 * Calling g_mutex_free() on a locked mutex may result
1478 * in undefined behaviour.
1480 * Deprecated: 2.32: GMutex can now be statically allocated, or embedded
1481 * in structures and initialised with g_mutex_init().
1484 g_mutex_free (GMutex *mutex)
1486 g_mutex_clear (mutex);
1487 g_slice_free (GMutex, mutex);
1490 /* GCond {{{1 ------------------------------------------------------ */
1495 * Allocates and initializes a new #GCond.
1497 * Returns: a newly allocated #GCond. Free with g_cond_free()
1499 * Deprecated: 2.32: GCond can now be statically allocated, or embedded
1500 * in structures and initialised with g_cond_init().
1507 cond = g_slice_new (GCond);
1517 * Destroys a #GCond that has been created with g_cond_new().
1519 * Calling g_cond_free() for a #GCond on which threads are
1520 * blocking leads to undefined behaviour.
1522 * Deprecated: 2.32: GCond can now be statically allocated, or embedded
1523 * in structures and initialised with g_cond_init().
1526 g_cond_free (GCond *cond)
1528 g_cond_clear (cond);
1529 g_slice_free (GCond, cond);
1533 * g_cond_timed_wait:
1535 * @mutex: a #GMutex that is currently locked
1536 * @abs_time: a #GTimeVal, determining the final time
1538 * Waits until this thread is woken up on @cond, but not longer than
1539 * until the time specified by @abs_time. The @mutex is unlocked before
1540 * falling asleep and locked again before resuming.
1542 * If @abs_time is %NULL, g_cond_timed_wait() acts like g_cond_wait().
1544 * This function can be used even if g_thread_init() has not yet been
1545 * called, and, in that case, will immediately return %TRUE.
1547 * To easily calculate @abs_time a combination of g_get_real_time()
1548 * and g_time_val_add() can be used.
1550 * Returns: %TRUE if @cond was signalled, or %FALSE on timeout
1552 * Deprecated:2.32: Use g_cond_wait_until() instead.
1555 g_cond_timed_wait (GCond *cond,
1561 if (abs_time == NULL)
1563 g_cond_wait (cond, mutex);
1567 end_time = abs_time->tv_sec;
1568 end_time *= 1000000;
1569 end_time += abs_time->tv_usec;
1571 /* would be nice if we had clock_rtoffset, but that didn't seem to
1572 * make it into the kernel yet...
1574 end_time += g_get_monotonic_time () - g_get_real_time ();
1576 return g_cond_wait_until (cond, mutex, end_time);
1580 /* vim: set foldmethod=marker: */