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.
28 /* we know we are deprecated here, no need for warnings */
29 #undef G_GNUC_DEPRECATED
30 #define G_GNUC_DEPRECATED
31 #undef G_GNUC_DEPRECATED_FOR
32 #define G_GNUC_DEPRECATED_FOR(f)
34 #include "gmessages.h"
38 #include "gthreadprivate.h"
39 #include "deprecated/gthread.h"
43 /* {{{1 Documentation */
46 * SECTION:threads-deprecated
47 * @title: Deprecated thread API
48 * @short_description: old thread APIs (for reference only)
51 * These APIs are deprecated. You should not use them in new code.
52 * This section remains only to assist with understanding code that was
53 * written to use these APIs at some point in the past.
58 * @G_THREAD_PRIORITY_LOW: a priority lower than normal
59 * @G_THREAD_PRIORITY_NORMAL: the default priority
60 * @G_THREAD_PRIORITY_HIGH: a priority higher than normal
61 * @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;
156 GSystemThread zero_thread; /* This is initialized to all zero */
161 * @vtable: a function table of type #GThreadFunctions, that provides
162 * the entry points to the thread system to be used. Since 2.32,
163 * this parameter is ignored and should always be %NULL
165 * If you use GLib from more than one thread, you must initialize the
166 * thread system by calling g_thread_init().
168 * Since version 2.24, calling g_thread_init() multiple times is allowed,
169 * but nothing happens except for the first call.
171 * Since version 2.32, GLib does not support custom thread implementations
172 * anymore and the @vtable parameter is ignored and you should pass %NULL.
174 * <note><para>g_thread_init() must not be called directly or indirectly
175 * in a callback from GLib. Also no mutexes may be currently locked while
176 * calling g_thread_init().</para></note>
178 * <note><para>To use g_thread_init() in your program, you have to link
179 * with the libraries that the command <command>pkg-config --libs
180 * gthread-2.0</command> outputs. This is not the case for all the
181 * other thread-related functions of GLib. Those can be used without
182 * having to link with the thread libraries.</para></note>
186 * g_thread_get_initialized:
188 * Indicates if g_thread_init() has been called.
190 * Returns: %TRUE if threads have been initialized.
195 g_thread_get_initialized (void)
197 return g_thread_supported ();
200 /* We need this for ABI compatibility */
201 void g_thread_init_glib (void) { }
203 /* Internal variables {{{1 */
205 static GRealThread *g_thread_all_threads = NULL;
206 static GSList *g_thread_free_indices = NULL;
208 /* Protects g_thread_all_threads and g_thread_free_indices */
209 G_LOCK_DEFINE_STATIC (g_thread);
211 /* Misc. GThread functions {{{1 */
214 * g_thread_set_priority:
215 * @thread: a #GThread.
218 * This function does nothing.
220 * Deprecated:2.32: Thread priorities no longer have any effect.
223 g_thread_set_priority (GThread *thread,
224 GThreadPriority priority)
230 * @func: a function to execute in the new thread
231 * @data: an argument to supply to the new thread
232 * @joinable: should this thread be joinable?
233 * @error: return location for error, or %NULL
235 * This function creates a new thread.
237 * If @joinable is %TRUE, you can wait for this threads termination
238 * calling g_thread_join(). Otherwise the thread will just disappear
239 * when it terminates.
241 * The new thread executes the function @func with the argument @data.
242 * If the thread was created successfully, it is returned.
244 * @error can be %NULL to ignore errors, or non-%NULL to report errors.
245 * The error is set, if and only if the function returns %NULL.
247 * Returns: the new #GThread on success
249 * Deprecated:2.32: Use g_thread_new() instead
252 g_thread_create (GThreadFunc func,
257 return g_thread_new_internal (NULL, func, data, joinable, 0, TRUE, error);
261 * g_thread_create_full:
262 * @func: a function to execute in the new thread.
263 * @data: an argument to supply to the new thread.
264 * @stack_size: a stack size for the new thread.
265 * @joinable: should this thread be joinable?
268 * @error: return location for error.
269 * @Returns: the new #GThread on success.
271 * This function creates a new thread.
273 * Deprecated:2.32: The @bound and @priority arguments are now ignored.
274 * Use g_thread_new() or g_thread_new_full() instead.
277 g_thread_create_full (GThreadFunc func,
282 GThreadPriority priority,
285 return g_thread_new_internal (NULL, func, data, joinable, stack_size, TRUE, error);
290 * @thread_func: function to call for all #GThread structures
291 * @user_data: second argument to @thread_func
293 * Call @thread_func on all #GThreads that have been
294 * created with g_thread_create().
296 * Note that threads may decide to exit while @thread_func is
297 * running, so without intimate knowledge about the lifetime of
298 * foreign threads, @thread_func shouldn't access the GThread*
299 * pointer passed in as first argument. However, @thread_func will
300 * not be called for threads which are known to have exited already.
302 * Due to thread lifetime checks, this function has an execution complexity
303 * which is quadratic in the number of existing threads.
307 * Deprecated:2.32: There aren't many things you can do with a #GThread,
308 * except comparing it with one that was returned from g_thread_create().
309 * There are better ways to find out if your thread is still alive.
312 g_thread_foreach (GFunc thread_func,
315 GSList *slist = NULL;
317 g_return_if_fail (thread_func != NULL);
318 /* snapshot the list of threads for iteration */
320 for (thread = g_thread_all_threads; thread; thread = thread->next)
321 slist = g_slist_prepend (slist, thread);
323 /* walk the list, skipping non-existent threads */
326 GSList *node = slist;
328 /* check whether the current thread still exists */
330 for (thread = g_thread_all_threads; thread; thread = thread->next)
331 if (thread == node->data)
335 thread_func (thread, user_data);
336 g_slist_free_1 (node);
341 g_enumerable_thread_add (GRealThread *thread)
344 thread->next = g_thread_all_threads;
345 g_thread_all_threads = thread;
350 g_enumerable_thread_remove (GRealThread *thread)
355 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
362 g_thread_all_threads = t->next;
369 /* GOnce {{{1 ------------------------------------------------------------- */
371 g_once_init_enter_impl (volatile gsize *location)
373 return (g_once_init_enter) (location);
376 /* GStaticMutex {{{1 ------------------------------------------------------ */
381 * A #GStaticMutex works like a #GMutex.
383 * Prior to GLib 2.32, GStaticMutex had the significant advantage
384 * that it doesn't need to be created at run-time, but can be defined
385 * at compile-time. Since 2.32, #GMutex can be statically allocated
386 * as well, and GStaticMutex has been deprecated.
388 * Here is a version of our give_me_next_number() example using
393 * Using <structname>GStaticMutex</structname>
394 * to simplify thread-safe programming
398 * give_me_next_number (void)
400 * static int current_number = 0;
402 * static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
404 * g_static_mutex_lock (&mutex);
405 * ret_val = current_number = calc_next_number (current_number);
406 * g_static_mutex_unlock (&mutex);
413 * Sometimes you would like to dynamically create a mutex. If you don't
414 * want to require prior calling to g_thread_init(), because your code
415 * should also be usable in non-threaded programs, you are not able to
416 * use g_mutex_new() and thus #GMutex, as that requires a prior call to
417 * g_thread_init(). In theses cases you can also use a #GStaticMutex.
418 * It must be initialized with g_static_mutex_init() before using it
419 * and freed with with g_static_mutex_free() when not needed anymore to
420 * free up any allocated resources.
422 * Even though #GStaticMutex is not opaque, it should only be used with
423 * the following functions, as it is defined differently on different
426 * All of the <function>g_static_mutex_*</function> functions apart
427 * from <function>g_static_mutex_get_mutex</function> can also be used
428 * even if g_thread_init() has not yet been called. Then they do
429 * nothing, apart from <function>g_static_mutex_trylock</function>,
430 * which does nothing but returning %TRUE.
432 * <note><para>All of the <function>g_static_mutex_*</function>
433 * functions are actually macros. Apart from taking their addresses, you
434 * can however use them as if they were functions.</para></note>
438 * G_STATIC_MUTEX_INIT:
440 * A #GStaticMutex must be initialized with this macro, before it can
441 * be used. This macro can used be to initialize a variable, but it
442 * cannot be assigned to a variable. In that case you have to use
443 * g_static_mutex_init().
446 * GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
451 * g_static_mutex_init:
452 * @mutex: a #GStaticMutex to be initialized.
454 * Initializes @mutex.
455 * Alternatively you can initialize it with #G_STATIC_MUTEX_INIT.
457 * Deprecated: 2.32: Use g_mutex_init()
460 g_static_mutex_init (GStaticMutex *mutex)
462 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
464 g_return_if_fail (mutex);
469 /* IMPLEMENTATION NOTE:
471 * On some platforms a GStaticMutex is actually a normal GMutex stored
472 * inside of a structure instead of being allocated dynamically. We can
473 * only do this for platforms on which we know, in advance, how to
474 * allocate (size) and initialise (value) that memory.
476 * On other platforms, a GStaticMutex is nothing more than a pointer to
477 * a GMutex. In that case, the first access we make to the static mutex
478 * must first allocate the normal GMutex and store it into the pointer.
480 * configure.ac writes macros into glibconfig.h to determine if
481 * g_static_mutex_get_mutex() accesses the structure in memory directly
482 * (on platforms where we are able to do that) or if it ends up here,
483 * where we may have to allocate the GMutex before returning it.
487 * g_static_mutex_get_mutex:
488 * @mutex: a #GStaticMutex.
489 * @Returns: the #GMutex corresponding to @mutex.
491 * For some operations (like g_cond_wait()) you must have a #GMutex
492 * instead of a #GStaticMutex. This function will return the
493 * corresponding #GMutex for @mutex.
495 * Deprecated: 2.32: Just use a #GMutex
498 g_static_mutex_get_mutex_impl (GStaticMutex* mutex)
502 if (!g_thread_supported ())
505 result = g_atomic_pointer_get (&mutex->mutex);
509 g_mutex_lock (&g_once_mutex);
511 result = mutex->mutex;
514 result = g_mutex_new ();
515 g_atomic_pointer_set (&mutex->mutex, result);
518 g_mutex_unlock (&g_once_mutex);
524 /* IMPLEMENTATION NOTE:
526 * g_static_mutex_lock(), g_static_mutex_trylock() and
527 * g_static_mutex_unlock() are all preprocessor macros that wrap the
528 * corresponding g_mutex_*() function around a call to
529 * g_static_mutex_get_mutex().
533 * g_static_mutex_lock:
534 * @mutex: a #GStaticMutex.
536 * Works like g_mutex_lock(), but for a #GStaticMutex.
538 * Deprecated: 2.32: Use g_mutex_lock()
542 * g_static_mutex_trylock:
543 * @mutex: a #GStaticMutex.
544 * @Returns: %TRUE, if the #GStaticMutex could be locked.
546 * Works like g_mutex_trylock(), but for a #GStaticMutex.
548 * Deprecated: 2.32: Use g_mutex_trylock()
552 * g_static_mutex_unlock:
553 * @mutex: a #GStaticMutex.
555 * Works like g_mutex_unlock(), but for a #GStaticMutex.
557 * Deprecated: 2.32: Use g_mutex_unlock()
561 * g_static_mutex_free:
562 * @mutex: a #GStaticMutex to be freed.
564 * Releases all resources allocated to @mutex.
566 * You don't have to call this functions for a #GStaticMutex with an
567 * unbounded lifetime, i.e. objects declared 'static', but if you have
568 * a #GStaticMutex as a member of a structure and the structure is
569 * freed, you should also free the #GStaticMutex.
571 * <note><para>Calling g_static_mutex_free() on a locked mutex may
572 * result in undefined behaviour.</para></note>
574 * Deprecated: 2.32: Use g_mutex_free()
577 g_static_mutex_free (GStaticMutex* mutex)
579 GMutex **runtime_mutex;
581 g_return_if_fail (mutex);
583 /* The runtime_mutex is the first (or only) member of GStaticMutex,
584 * see both versions (of glibconfig.h) in configure.ac. Note, that
585 * this variable is NULL, if g_thread_init() hasn't been called or
586 * if we're using the default thread implementation and it provides
588 runtime_mutex = ((GMutex**)mutex);
591 g_mutex_free (*runtime_mutex);
593 *runtime_mutex = NULL;
596 /* {{{1 GStaticRecMutex */
601 * A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
602 * multiple times by one thread. If you enter it n times, you have to
603 * unlock it n times again to let other threads lock it. An exception
604 * is the function g_static_rec_mutex_unlock_full(): that allows you to
605 * unlock a #GStaticRecMutex completely returning the depth, (i.e. the
606 * number of times this mutex was locked). The depth can later be used
607 * to restore the state of the #GStaticRecMutex by calling
608 * g_static_rec_mutex_lock_full(). In GLib 2.32, #GStaticRecMutex has
609 * been deprecated in favor of #GRecMutex.
611 * Even though #GStaticRecMutex is not opaque, it should only be used
612 * with the following functions.
614 * All of the <function>g_static_rec_mutex_*</function> functions can
615 * be used even if g_thread_init() has not been called. Then they do
616 * nothing, apart from <function>g_static_rec_mutex_trylock</function>,
617 * which does nothing but returning %TRUE.
621 * G_STATIC_REC_MUTEX_INIT:
623 * A #GStaticRecMutex must be initialized with this macro before it can
624 * be used. This macro can used be to initialize a variable, but it
625 * cannot be assigned to a variable. In that case you have to use
626 * g_static_rec_mutex_init().
629 * GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
634 * g_static_rec_mutex_init:
635 * @mutex: a #GStaticRecMutex to be initialized.
637 * A #GStaticRecMutex must be initialized with this function before it
638 * can be used. Alternatively you can initialize it with
639 * #G_STATIC_REC_MUTEX_INIT.
641 * Deprecated: 2.32: Use g_rec_mutex_init()
644 g_static_rec_mutex_init (GStaticRecMutex *mutex)
646 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
648 g_return_if_fail (mutex);
654 * g_static_rec_mutex_lock:
655 * @mutex: a #GStaticRecMutex to lock.
657 * Locks @mutex. If @mutex is already locked by another thread, the
658 * current thread will block until @mutex is unlocked by the other
659 * thread. If @mutex is already locked by the calling thread, this
660 * functions increases the depth of @mutex and returns immediately.
662 * Deprecated: 2.32: Use g_rec_mutex_lock()
665 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
669 g_return_if_fail (mutex);
671 if (!g_thread_supported ())
674 g_system_thread_self (&self);
676 if (g_system_thread_equal (&self, &mutex->owner))
681 g_static_mutex_lock (&mutex->mutex);
682 g_system_thread_assign (mutex->owner, self);
687 * g_static_rec_mutex_trylock:
688 * @mutex: a #GStaticRecMutex to lock.
689 * @Returns: %TRUE, if @mutex could be locked.
691 * Tries to lock @mutex. If @mutex is already locked by another thread,
692 * it immediately returns %FALSE. Otherwise it locks @mutex and returns
693 * %TRUE. If @mutex is already locked by the calling thread, this
694 * functions increases the depth of @mutex and immediately returns
697 * Deprecated: 2.32: Use g_rec_mutex_trylock()
700 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
704 g_return_val_if_fail (mutex, FALSE);
706 if (!g_thread_supported ())
709 g_system_thread_self (&self);
711 if (g_system_thread_equal (&self, &mutex->owner))
717 if (!g_static_mutex_trylock (&mutex->mutex))
720 g_system_thread_assign (mutex->owner, self);
726 * g_static_rec_mutex_unlock:
727 * @mutex: a #GStaticRecMutex to unlock.
729 * Unlocks @mutex. Another thread will be allowed to lock @mutex only
730 * when it has been unlocked as many times as it had been locked
731 * before. If @mutex is completely unlocked and another thread is
732 * blocked in a g_static_rec_mutex_lock() call for @mutex, it will be
733 * woken and can lock @mutex itself.
735 * Deprecated: 2.32: Use g_rec_mutex_unlock()
738 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
740 g_return_if_fail (mutex);
742 if (!g_thread_supported ())
745 if (mutex->depth > 1)
750 g_system_thread_assign (mutex->owner, zero_thread);
751 g_static_mutex_unlock (&mutex->mutex);
755 * g_static_rec_mutex_lock_full:
756 * @mutex: a #GStaticRecMutex to lock.
757 * @depth: number of times this mutex has to be unlocked to be
758 * completely unlocked.
760 * Works like calling g_static_rec_mutex_lock() for @mutex @depth times.
762 * Deprecated: 2.32: Use g_rec_mutex_lock()
765 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
769 g_return_if_fail (mutex);
771 if (!g_thread_supported ())
777 g_system_thread_self (&self);
779 if (g_system_thread_equal (&self, &mutex->owner))
781 mutex->depth += depth;
784 g_static_mutex_lock (&mutex->mutex);
785 g_system_thread_assign (mutex->owner, self);
786 mutex->depth = depth;
790 * g_static_rec_mutex_unlock_full:
791 * @mutex: a #GStaticRecMutex to completely unlock.
792 * @Returns: number of times @mutex has been locked by the current
795 * Completely unlocks @mutex. If another thread is blocked in a
796 * g_static_rec_mutex_lock() call for @mutex, it will be woken and can
797 * lock @mutex itself. This function returns the number of times that
798 * @mutex has been locked by the current thread. To restore the state
799 * before the call to g_static_rec_mutex_unlock_full() you can call
800 * g_static_rec_mutex_lock_full() with the depth returned by this
803 * Deprecated: 2.32: Use g_rec_mutex_unlock()
806 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
810 g_return_val_if_fail (mutex, 0);
812 if (!g_thread_supported ())
815 depth = mutex->depth;
817 g_system_thread_assign (mutex->owner, zero_thread);
819 g_static_mutex_unlock (&mutex->mutex);
825 * g_static_rec_mutex_free:
826 * @mutex: a #GStaticRecMutex to be freed.
828 * Releases all resources allocated to a #GStaticRecMutex.
830 * You don't have to call this functions for a #GStaticRecMutex with an
831 * unbounded lifetime, i.e. objects declared 'static', but if you have
832 * a #GStaticRecMutex as a member of a structure and the structure is
833 * freed, you should also free the #GStaticRecMutex.
835 * Deprecated: 2.32: Use g_rec_mutex_clear()
838 g_static_rec_mutex_free (GStaticRecMutex *mutex)
840 g_return_if_fail (mutex);
842 g_static_mutex_free (&mutex->mutex);
845 /* GStaticRWLock {{{1 ----------------------------------------------------- */
850 * The #GStaticRWLock struct represents a read-write lock. A read-write
851 * lock can be used for protecting data that some portions of code only
852 * read from, while others also write. In such situations it is
853 * desirable that several readers can read at once, whereas of course
854 * only one writer may write at a time. Take a look at the following
858 * <title>An array with access functions</title>
860 * GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
864 * my_array_get (guint index)
866 * gpointer retval = NULL;
871 * g_static_rw_lock_reader_lock (&rwlock);
872 * if (index < array->len)
873 * retval = g_ptr_array_index (array, index);
874 * g_static_rw_lock_reader_unlock (&rwlock);
880 * my_array_set (guint index, gpointer data)
882 * g_static_rw_lock_writer_lock (&rwlock);
885 * array = g_ptr_array_new (<!-- -->);
887 * if (index >= array->len)
888 * g_ptr_array_set_size (array, index+1);
889 * g_ptr_array_index (array, index) = data;
891 * g_static_rw_lock_writer_unlock (&rwlock);
896 * This example shows an array which can be accessed by many readers
897 * (the <function>my_array_get()</function> function) simultaneously,
898 * whereas the writers (the <function>my_array_set()</function>
899 * function) will only be allowed once at a time and only if no readers
900 * currently access the array. This is because of the potentially
901 * dangerous resizing of the array. Using these functions is fully
902 * multi-thread safe now.
904 * Most of the time, writers should have precedence over readers. That
905 * means, for this implementation, that as soon as a writer wants to
906 * lock the data, no other reader is allowed to lock the data, whereas,
907 * of course, the readers that already have locked the data are allowed
908 * to finish their operation. As soon as the last reader unlocks the
909 * data, the writer will lock it.
911 * Even though #GStaticRWLock is not opaque, it should only be used
912 * with the following functions.
914 * All of the <function>g_static_rw_lock_*</function> functions can be
915 * used even if g_thread_init() has not been called. Then they do
916 * nothing, apart from <function>g_static_rw_lock_*_trylock</function>,
917 * which does nothing but returning %TRUE.
919 * <note><para>A read-write lock has a higher overhead than a mutex. For
920 * example, both g_static_rw_lock_reader_lock() and
921 * g_static_rw_lock_reader_unlock() have to lock and unlock a
922 * #GStaticMutex, so it takes at least twice the time to lock and unlock
923 * a #GStaticRWLock that it does to lock and unlock a #GStaticMutex. So
924 * only data structures that are accessed by multiple readers, and which
925 * keep the lock for a considerable time justify a #GStaticRWLock. The
926 * above example most probably would fare better with a
927 * #GStaticMutex.</para></note>
929 * Deprecated: 2.32: Use a #GRWLock instead
933 * G_STATIC_RW_LOCK_INIT:
935 * A #GStaticRWLock must be initialized with this macro before it can
936 * be used. This macro can used be to initialize a variable, but it
937 * cannot be assigned to a variable. In that case you have to use
938 * g_static_rw_lock_init().
941 * GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
946 * g_static_rw_lock_init:
947 * @lock: a #GStaticRWLock to be initialized.
949 * A #GStaticRWLock must be initialized with this function before it
950 * can be used. Alternatively you can initialize it with
951 * #G_STATIC_RW_LOCK_INIT.
953 * Deprecated: 2.32: Use g_rw_lock_init() instead
956 g_static_rw_lock_init (GStaticRWLock* lock)
958 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
960 g_return_if_fail (lock);
966 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
969 *cond = g_cond_new ();
970 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
974 g_static_rw_lock_signal (GStaticRWLock* lock)
976 if (lock->want_to_write && lock->write_cond)
977 g_cond_signal (lock->write_cond);
978 else if (lock->want_to_read && lock->read_cond)
979 g_cond_broadcast (lock->read_cond);
983 * g_static_rw_lock_reader_lock:
984 * @lock: a #GStaticRWLock to lock for reading.
986 * Locks @lock for reading. There may be unlimited concurrent locks for
987 * reading of a #GStaticRWLock at the same time. If @lock is already
988 * locked for writing by another thread or if another thread is already
989 * waiting to lock @lock for writing, this function will block until
990 * @lock is unlocked by the other writing thread and no other writing
991 * threads want to lock @lock. This lock has to be unlocked by
992 * g_static_rw_lock_reader_unlock().
994 * #GStaticRWLock is not recursive. It might seem to be possible to
995 * recursively lock for reading, but that can result in a deadlock, due
996 * to writer preference.
998 * Deprecated: 2.32: Use g_rw_lock_reader_lock() instead
1001 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
1003 g_return_if_fail (lock);
1005 if (!g_threads_got_initialized)
1008 g_static_mutex_lock (&lock->mutex);
1009 lock->want_to_read++;
1010 while (lock->have_writer || lock->want_to_write)
1011 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
1012 lock->want_to_read--;
1013 lock->read_counter++;
1014 g_static_mutex_unlock (&lock->mutex);
1018 * g_static_rw_lock_reader_trylock:
1019 * @lock: a #GStaticRWLock to lock for reading.
1020 * @Returns: %TRUE, if @lock could be locked for reading.
1022 * Tries to lock @lock for reading. If @lock is already locked for
1023 * writing by another thread or if another thread is already waiting to
1024 * lock @lock for writing, immediately returns %FALSE. Otherwise locks
1025 * @lock for reading and returns %TRUE. This lock has to be unlocked by
1026 * g_static_rw_lock_reader_unlock().
1028 * Deprectated: 2.32: Use g_rw_lock_reader_trylock() instead
1031 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
1033 gboolean ret_val = FALSE;
1035 g_return_val_if_fail (lock, FALSE);
1037 if (!g_threads_got_initialized)
1040 g_static_mutex_lock (&lock->mutex);
1041 if (!lock->have_writer && !lock->want_to_write)
1043 lock->read_counter++;
1046 g_static_mutex_unlock (&lock->mutex);
1051 * g_static_rw_lock_reader_unlock:
1052 * @lock: a #GStaticRWLock to unlock after reading.
1054 * Unlocks @lock. If a thread waits to lock @lock for writing and all
1055 * locks for reading have been unlocked, the waiting thread is woken up
1056 * and can lock @lock for writing.
1058 * Deprectated: 2.32: Use g_rw_lock_reader_unlock() instead
1061 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
1063 g_return_if_fail (lock);
1065 if (!g_threads_got_initialized)
1068 g_static_mutex_lock (&lock->mutex);
1069 lock->read_counter--;
1070 if (lock->read_counter == 0)
1071 g_static_rw_lock_signal (lock);
1072 g_static_mutex_unlock (&lock->mutex);
1076 * g_static_rw_lock_writer_lock:
1077 * @lock: a #GStaticRWLock to lock for writing.
1079 * Locks @lock for writing. If @lock is already locked for writing or
1080 * reading by other threads, this function will block until @lock is
1081 * completely unlocked and then lock @lock for writing. While this
1082 * functions waits to lock @lock, no other thread can lock @lock for
1083 * reading. When @lock is locked for writing, no other thread can lock
1084 * @lock (neither for reading nor writing). This lock has to be
1085 * unlocked by g_static_rw_lock_writer_unlock().
1087 * Deprectated: 2.32: Use g_rw_lock_writer_lock() instead
1090 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
1092 g_return_if_fail (lock);
1094 if (!g_threads_got_initialized)
1097 g_static_mutex_lock (&lock->mutex);
1098 lock->want_to_write++;
1099 while (lock->have_writer || lock->read_counter)
1100 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
1101 lock->want_to_write--;
1102 lock->have_writer = TRUE;
1103 g_static_mutex_unlock (&lock->mutex);
1107 * g_static_rw_lock_writer_trylock:
1108 * @lock: a #GStaticRWLock to lock for writing.
1109 * @Returns: %TRUE, if @lock could be locked for writing.
1111 * Tries to lock @lock for writing. If @lock is already locked (for
1112 * either reading or writing) by another thread, it immediately returns
1113 * %FALSE. Otherwise it locks @lock for writing and returns %TRUE. This
1114 * lock has to be unlocked by g_static_rw_lock_writer_unlock().
1116 * Deprectated: 2.32: Use g_rw_lock_writer_trylock() instead
1119 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
1121 gboolean ret_val = FALSE;
1123 g_return_val_if_fail (lock, FALSE);
1125 if (!g_threads_got_initialized)
1128 g_static_mutex_lock (&lock->mutex);
1129 if (!lock->have_writer && !lock->read_counter)
1131 lock->have_writer = TRUE;
1134 g_static_mutex_unlock (&lock->mutex);
1139 * g_static_rw_lock_writer_unlock:
1140 * @lock: a #GStaticRWLock to unlock after writing.
1142 * Unlocks @lock. If a thread is waiting to lock @lock for writing and
1143 * all locks for reading have been unlocked, the waiting thread is
1144 * woken up and can lock @lock for writing. If no thread is waiting to
1145 * lock @lock for writing, and some thread or threads are waiting to
1146 * lock @lock for reading, the waiting threads are woken up and can
1147 * lock @lock for reading.
1149 * Deprectated: 2.32: Use g_rw_lock_writer_unlock() instead
1152 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
1154 g_return_if_fail (lock);
1156 if (!g_threads_got_initialized)
1159 g_static_mutex_lock (&lock->mutex);
1160 lock->have_writer = FALSE;
1161 g_static_rw_lock_signal (lock);
1162 g_static_mutex_unlock (&lock->mutex);
1166 * g_static_rw_lock_free:
1167 * @lock: a #GStaticRWLock to be freed.
1169 * Releases all resources allocated to @lock.
1171 * You don't have to call this functions for a #GStaticRWLock with an
1172 * unbounded lifetime, i.e. objects declared 'static', but if you have
1173 * a #GStaticRWLock as a member of a structure, and the structure is
1174 * freed, you should also free the #GStaticRWLock.
1176 * Deprecated: 2.32: Use a #GRWLock instead
1179 g_static_rw_lock_free (GStaticRWLock* lock)
1181 g_return_if_fail (lock);
1183 if (lock->read_cond)
1185 g_cond_free (lock->read_cond);
1186 lock->read_cond = NULL;
1188 if (lock->write_cond)
1190 g_cond_free (lock->write_cond);
1191 lock->write_cond = NULL;
1193 g_static_mutex_free (&lock->mutex);
1196 /* GPrivate {{{1 ------------------------------------------------------ */
1200 * @notify: a #GDestroyNotify
1202 * Deprecated:2.32: dynamic allocation of #GPrivate is a bad idea. Use
1203 * static storage and G_PRIVATE_INIT() instead.
1205 * Returns: a newly allocated #GPrivate (which can never be destroyed)
1208 g_private_new (GDestroyNotify notify)
1210 GPrivate tmp = G_PRIVATE_INIT (notify);
1213 key = g_slice_new (GPrivate);
1219 /* {{{1 GStaticPrivate */
1221 typedef struct _GStaticPrivateNode GStaticPrivateNode;
1222 struct _GStaticPrivateNode
1225 GDestroyNotify destroy;
1226 GStaticPrivate *owner;
1232 * A #GStaticPrivate works almost like a #GPrivate, but it has one
1233 * significant advantage. It doesn't need to be created at run-time
1234 * like a #GPrivate, but can be defined at compile-time. This is
1235 * similar to the difference between #GMutex and #GStaticMutex. Now
1236 * look at our <function>give_me_next_number()</function> example with
1240 * <title>Using GStaticPrivate for per-thread data</title>
1243 * give_me_next_number (<!-- -->)
1245 * static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1246 * int *current_number = g_static_private_get (&current_number_key);
1248 * if (!current_number)
1250 * current_number = g_new (int,1);
1251 * *current_number = 0;
1252 * g_static_private_set (&current_number_key, current_number, g_free);
1255 * *current_number = calc_next_number (*current_number);
1257 * return *current_number;
1264 * G_STATIC_PRIVATE_INIT:
1266 * Every #GStaticPrivate must be initialized with this macro, before it
1270 * GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1275 * g_static_private_init:
1276 * @private_key: a #GStaticPrivate to be initialized
1278 * Initializes @private_key. Alternatively you can initialize it with
1279 * #G_STATIC_PRIVATE_INIT.
1282 g_static_private_init (GStaticPrivate *private_key)
1284 private_key->index = 0;
1288 * g_static_private_get:
1289 * @private_key: a #GStaticPrivate
1291 * Works like g_private_get() only for a #GStaticPrivate.
1293 * This function works even if g_thread_init() has not yet been called.
1295 * Returns: the corresponding pointer
1298 g_static_private_get (GStaticPrivate *private_key)
1300 GRealThread *self = (GRealThread*) g_thread_self ();
1302 gpointer ret = NULL;
1303 array = self->private_data;
1305 if (array && private_key->index != 0 && private_key->index <= array->len)
1307 GStaticPrivateNode *node;
1309 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1311 /* Deal with the possibility that the GStaticPrivate which used
1312 * to have this index got freed and the index got allocated to
1313 * a new one. In this case, the data in the node is stale, so
1314 * free it and return NULL.
1316 if (G_UNLIKELY (node->owner != private_key))
1319 node->destroy (node->data);
1320 node->destroy = NULL;
1331 * g_static_private_set:
1332 * @private_key: a #GStaticPrivate
1333 * @data: the new pointer
1334 * @notify: a function to be called with the pointer whenever the
1335 * current thread ends or sets this pointer again
1337 * Sets the pointer keyed to @private_key for the current thread and
1338 * the function @notify to be called with that pointer (%NULL or
1339 * non-%NULL), whenever the pointer is set again or whenever the
1340 * current thread ends.
1342 * This function works even if g_thread_init() has not yet been called.
1343 * If g_thread_init() is called later, the @data keyed to @private_key
1344 * will be inherited only by the main thread, i.e. the one that called
1347 * <note><para>@notify is used quite differently from @destructor in
1348 * g_private_new().</para></note>
1351 g_static_private_set (GStaticPrivate *private_key,
1353 GDestroyNotify notify)
1355 GRealThread *self = (GRealThread*) g_thread_self ();
1357 static guint next_index = 0;
1358 GStaticPrivateNode *node;
1360 if (!private_key->index)
1364 if (!private_key->index)
1366 if (g_thread_free_indices)
1368 private_key->index = GPOINTER_TO_UINT (g_thread_free_indices->data);
1369 g_thread_free_indices = g_slist_delete_link (g_thread_free_indices,
1370 g_thread_free_indices);
1373 private_key->index = ++next_index;
1376 G_UNLOCK (g_thread);
1379 array = self->private_data;
1382 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
1383 self->private_data = array;
1385 if (private_key->index > array->len)
1386 g_array_set_size (array, private_key->index);
1388 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1391 node->destroy (node->data);
1394 node->destroy = notify;
1395 node->owner = private_key;
1399 * g_static_private_free:
1400 * @private_key: a #GStaticPrivate to be freed
1402 * Releases all resources allocated to @private_key.
1404 * You don't have to call this functions for a #GStaticPrivate with an
1405 * unbounded lifetime, i.e. objects declared 'static', but if you have
1406 * a #GStaticPrivate as a member of a structure and the structure is
1407 * freed, you should also free the #GStaticPrivate.
1410 g_static_private_free (GStaticPrivate *private_key)
1412 guint idx = private_key->index;
1417 private_key->index = 0;
1419 /* Freeing the per-thread data is deferred to either the
1420 * thread end or the next g_static_private_get() call for
1424 g_thread_free_indices = g_slist_prepend (g_thread_free_indices,
1425 GUINT_TO_POINTER (idx));
1426 G_UNLOCK (g_thread);
1430 g_static_private_cleanup (GRealThread *thread)
1434 array = thread->private_data;
1435 thread->private_data = NULL;
1441 for (i = 0; i < array->len; i++ )
1443 GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
1445 node->destroy (node->data);
1447 g_array_free (array, TRUE);
1451 /* GMutex {{{1 ------------------------------------------------------ */
1456 * Allocates and initializes a new #GMutex.
1458 * Returns: a newly allocated #GMutex. Use g_mutex_free() to free
1460 * Deprecated:3.32:GMutex can now be statically allocated, or embedded
1461 * in structures and initialised with g_mutex_init().
1468 mutex = g_slice_new (GMutex);
1469 g_mutex_init (mutex);
1478 * Destroys a @mutex that has been created with g_mutex_new().
1480 * Calling g_mutex_free() on a locked mutex may result
1481 * in undefined behaviour.
1483 * Deprecated:3.32:GMutex can now be statically allocated, or embedded
1484 * in structures and initialised with g_mutex_init().
1487 g_mutex_free (GMutex *mutex)
1489 g_mutex_clear (mutex);
1490 g_slice_free (GMutex, mutex);
1493 /* GCond {{{1 ------------------------------------------------------ */
1498 * Allocates and initializes a new #GCond.
1500 * Returns: a newly allocated #GCond. Free with g_cond_free()
1502 * Deprecated:3.32:GCond can now be statically allocated, or embedded
1503 * in structures and initialised with g_cond_init().
1510 cond = g_slice_new (GCond);
1520 * Destroys a #GCond that has been created with g_cond_new().
1522 * Calling g_cond_free() for a #GCond on which threads are
1523 * blocking leads to undefined behaviour.
1525 * Deprecated:3.32:GCond can now be statically allocated, or embedded
1526 * in structures and initialised with g_cond_init().
1529 g_cond_free (GCond *cond)
1531 g_cond_clear (cond);
1532 g_slice_free (GCond, cond);
1536 /* vim: set foldmethod=marker: */