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 gboolean g_thread_use_default_impl = TRUE;
118 GThreadFunctions g_thread_functions_for_glib_use =
146 return g_get_monotonic_time () * 1000;
149 guint64 (*g_thread_gettime) (void) = gettime;
151 /* Initialisation {{{1 ---------------------------------------------------- */
152 gboolean g_threads_got_initialized = TRUE;
153 GSystemThread zero_thread; /* This is initialized to all zero */
158 * @vtable: a function table of type #GThreadFunctions, that provides
159 * the entry points to the thread system to be used. Since 2.32,
160 * this parameter is ignored and should always be %NULL
162 * If you use GLib from more than one thread, you must initialize the
163 * thread system by calling g_thread_init().
165 * Since version 2.24, calling g_thread_init() multiple times is allowed,
166 * but nothing happens except for the first call.
168 * Since version 2.32, GLib does not support custom thread implementations
169 * anymore and the @vtable parameter is ignored and you should pass %NULL.
171 * <note><para>g_thread_init() must not be called directly or indirectly
172 * in a callback from GLib. Also no mutexes may be currently locked while
173 * calling g_thread_init().</para></note>
175 * <note><para>To use g_thread_init() in your program, you have to link
176 * with the libraries that the command <command>pkg-config --libs
177 * gthread-2.0</command> outputs. This is not the case for all the
178 * other thread-related functions of GLib. Those can be used without
179 * having to link with the thread libraries.</para></note>
183 * g_thread_get_initialized:
185 * Indicates if g_thread_init() has been called.
187 * Returns: %TRUE if threads have been initialized.
192 g_thread_get_initialized (void)
194 return g_thread_supported ();
197 /* We need this for ABI compatibility */
198 void g_thread_init_glib (void) { }
200 /* Internal variables {{{1 */
202 static GRealThread *g_thread_all_threads = NULL;
203 static GSList *g_thread_free_indices = NULL;
205 /* Protects g_thread_all_threads and g_thread_free_indices */
206 G_LOCK_DEFINE_STATIC (g_thread);
208 /* Misc. GThread functions {{{1 */
211 * g_thread_set_priority:
212 * @thread: a #GThread.
215 * This function does nothing.
217 * Deprecated:2.32: Thread priorities no longer have any effect.
220 g_thread_set_priority (GThread *thread,
221 GThreadPriority priority)
227 * @func: a function to execute in the new thread
228 * @data: an argument to supply to the new thread
229 * @joinable: should this thread be joinable?
230 * @error: return location for error, or %NULL
232 * This function creates a new thread.
234 * If @joinable is %TRUE, you can wait for this threads termination
235 * calling g_thread_join(). Otherwise the thread will just disappear
236 * when it terminates.
238 * The new thread executes the function @func with the argument @data.
239 * If the thread was created successfully, it is returned.
241 * @error can be %NULL to ignore errors, or non-%NULL to report errors.
242 * The error is set, if and only if the function returns %NULL.
244 * Returns: the new #GThread on success
246 * Deprecated:2.32: Use g_thread_new() instead
249 g_thread_create (GThreadFunc func,
254 return g_thread_new_internal (NULL, func, data, joinable, 0, TRUE, error);
258 * g_thread_create_full:
259 * @func: a function to execute in the new thread.
260 * @data: an argument to supply to the new thread.
261 * @stack_size: a stack size for the new thread.
262 * @joinable: should this thread be joinable?
265 * @error: return location for error.
266 * @Returns: the new #GThread on success.
268 * This function creates a new thread.
270 * Deprecated:2.32: The @bound and @priority arguments are now ignored.
271 * Use g_thread_new() or g_thread_new_full() instead.
274 g_thread_create_full (GThreadFunc func,
279 GThreadPriority priority,
282 return g_thread_new_internal (NULL, func, data, joinable, stack_size, TRUE, error);
287 * @thread_func: function to call for all #GThread structures
288 * @user_data: second argument to @thread_func
290 * Call @thread_func on all #GThreads that have been
291 * created with g_thread_create().
293 * Note that threads may decide to exit while @thread_func is
294 * running, so without intimate knowledge about the lifetime of
295 * foreign threads, @thread_func shouldn't access the GThread*
296 * pointer passed in as first argument. However, @thread_func will
297 * not be called for threads which are known to have exited already.
299 * Due to thread lifetime checks, this function has an execution complexity
300 * which is quadratic in the number of existing threads.
304 * Deprecated:2.32: There aren't many things you can do with a #GThread,
305 * except comparing it with one that was returned from g_thread_create().
306 * There are better ways to find out if your thread is still alive.
309 g_thread_foreach (GFunc thread_func,
312 GSList *slist = NULL;
314 g_return_if_fail (thread_func != NULL);
315 /* snapshot the list of threads for iteration */
317 for (thread = g_thread_all_threads; thread; thread = thread->next)
318 slist = g_slist_prepend (slist, thread);
320 /* walk the list, skipping non-existent threads */
323 GSList *node = slist;
325 /* check whether the current thread still exists */
327 for (thread = g_thread_all_threads; thread; thread = thread->next)
328 if (thread == node->data)
332 thread_func (thread, user_data);
333 g_slist_free_1 (node);
338 g_enumerable_thread_add (GRealThread *thread)
341 thread->next = g_thread_all_threads;
342 g_thread_all_threads = thread;
347 g_enumerable_thread_remove (GRealThread *thread)
352 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
359 g_thread_all_threads = t->next;
366 /* GOnce {{{1 ------------------------------------------------------------- */
368 g_once_init_enter_impl (volatile gsize *location)
370 return (g_once_init_enter) (location);
373 /* GStaticMutex {{{1 ------------------------------------------------------ */
378 * A #GStaticMutex works like a #GMutex.
380 * Prior to GLib 2.32, GStaticMutex had the significant advantage
381 * that it doesn't need to be created at run-time, but can be defined
382 * at compile-time. Since 2.32, #GMutex can be statically allocated
383 * as well, and GStaticMutex has been deprecated.
385 * Here is a version of our give_me_next_number() example using
390 * Using <structname>GStaticMutex</structname>
391 * to simplify thread-safe programming
395 * give_me_next_number (void)
397 * static int current_number = 0;
399 * static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
401 * g_static_mutex_lock (&mutex);
402 * ret_val = current_number = calc_next_number (current_number);
403 * g_static_mutex_unlock (&mutex);
410 * Sometimes you would like to dynamically create a mutex. If you don't
411 * want to require prior calling to g_thread_init(), because your code
412 * should also be usable in non-threaded programs, you are not able to
413 * use g_mutex_new() and thus #GMutex, as that requires a prior call to
414 * g_thread_init(). In theses cases you can also use a #GStaticMutex.
415 * It must be initialized with g_static_mutex_init() before using it
416 * and freed with with g_static_mutex_free() when not needed anymore to
417 * free up any allocated resources.
419 * Even though #GStaticMutex is not opaque, it should only be used with
420 * the following functions, as it is defined differently on different
423 * All of the <function>g_static_mutex_*</function> functions apart
424 * from <function>g_static_mutex_get_mutex</function> can also be used
425 * even if g_thread_init() has not yet been called. Then they do
426 * nothing, apart from <function>g_static_mutex_trylock</function>,
427 * which does nothing but returning %TRUE.
429 * <note><para>All of the <function>g_static_mutex_*</function>
430 * functions are actually macros. Apart from taking their addresses, you
431 * can however use them as if they were functions.</para></note>
435 * G_STATIC_MUTEX_INIT:
437 * A #GStaticMutex must be initialized with this macro, before it can
438 * be used. This macro can used be to initialize a variable, but it
439 * cannot be assigned to a variable. In that case you have to use
440 * g_static_mutex_init().
443 * GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
448 * g_static_mutex_init:
449 * @mutex: a #GStaticMutex to be initialized.
451 * Initializes @mutex.
452 * Alternatively you can initialize it with #G_STATIC_MUTEX_INIT.
454 * Deprecated: 2.32: Use g_mutex_init()
457 g_static_mutex_init (GStaticMutex *mutex)
459 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
461 g_return_if_fail (mutex);
466 /* IMPLEMENTATION NOTE:
468 * On some platforms a GStaticMutex is actually a normal GMutex stored
469 * inside of a structure instead of being allocated dynamically. We can
470 * only do this for platforms on which we know, in advance, how to
471 * allocate (size) and initialise (value) that memory.
473 * On other platforms, a GStaticMutex is nothing more than a pointer to
474 * a GMutex. In that case, the first access we make to the static mutex
475 * must first allocate the normal GMutex and store it into the pointer.
477 * configure.ac writes macros into glibconfig.h to determine if
478 * g_static_mutex_get_mutex() accesses the structure in memory directly
479 * (on platforms where we are able to do that) or if it ends up here,
480 * where we may have to allocate the GMutex before returning it.
484 * g_static_mutex_get_mutex:
485 * @mutex: a #GStaticMutex.
486 * @Returns: the #GMutex corresponding to @mutex.
488 * For some operations (like g_cond_wait()) you must have a #GMutex
489 * instead of a #GStaticMutex. This function will return the
490 * corresponding #GMutex for @mutex.
492 * Deprecated: 2.32: Just use a #GMutex
495 g_static_mutex_get_mutex_impl (GMutex** mutex)
499 if (!g_thread_supported ())
502 result = g_atomic_pointer_get (mutex);
506 g_mutex_lock (&g_once_mutex);
511 result = g_mutex_new ();
512 g_atomic_pointer_set (mutex, result);
515 g_mutex_unlock (&g_once_mutex);
521 /* IMPLEMENTATION NOTE:
523 * g_static_mutex_lock(), g_static_mutex_trylock() and
524 * g_static_mutex_unlock() are all preprocessor macros that wrap the
525 * corresponding g_mutex_*() function around a call to
526 * g_static_mutex_get_mutex().
530 * g_static_mutex_lock:
531 * @mutex: a #GStaticMutex.
533 * Works like g_mutex_lock(), but for a #GStaticMutex.
535 * Deprecated: 2.32: Use g_mutex_lock()
539 * g_static_mutex_trylock:
540 * @mutex: a #GStaticMutex.
541 * @Returns: %TRUE, if the #GStaticMutex could be locked.
543 * Works like g_mutex_trylock(), but for a #GStaticMutex.
545 * Deprecated: 2.32: Use g_mutex_trylock()
549 * g_static_mutex_unlock:
550 * @mutex: a #GStaticMutex.
552 * Works like g_mutex_unlock(), but for a #GStaticMutex.
554 * Deprecated: 2.32: Use g_mutex_unlock()
558 * g_static_mutex_free:
559 * @mutex: a #GStaticMutex to be freed.
561 * Releases all resources allocated to @mutex.
563 * You don't have to call this functions for a #GStaticMutex with an
564 * unbounded lifetime, i.e. objects declared 'static', but if you have
565 * a #GStaticMutex as a member of a structure and the structure is
566 * freed, you should also free the #GStaticMutex.
568 * <note><para>Calling g_static_mutex_free() on a locked mutex may
569 * result in undefined behaviour.</para></note>
571 * Deprecated: 2.32: Use g_mutex_free()
574 g_static_mutex_free (GStaticMutex* mutex)
576 GMutex **runtime_mutex;
578 g_return_if_fail (mutex);
580 /* The runtime_mutex is the first (or only) member of GStaticMutex,
581 * see both versions (of glibconfig.h) in configure.ac. Note, that
582 * this variable is NULL, if g_thread_init() hasn't been called or
583 * if we're using the default thread implementation and it provides
585 runtime_mutex = ((GMutex**)mutex);
588 g_mutex_free (*runtime_mutex);
590 *runtime_mutex = NULL;
593 /* {{{1 GStaticRecMutex */
598 * A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
599 * multiple times by one thread. If you enter it n times, you have to
600 * unlock it n times again to let other threads lock it. An exception
601 * is the function g_static_rec_mutex_unlock_full(): that allows you to
602 * unlock a #GStaticRecMutex completely returning the depth, (i.e. the
603 * number of times this mutex was locked). The depth can later be used
604 * to restore the state of the #GStaticRecMutex by calling
605 * g_static_rec_mutex_lock_full(). In GLib 2.32, #GStaticRecMutex has
606 * been deprecated in favor of #GRecMutex.
608 * Even though #GStaticRecMutex is not opaque, it should only be used
609 * with the following functions.
611 * All of the <function>g_static_rec_mutex_*</function> functions can
612 * be used even if g_thread_init() has not been called. Then they do
613 * nothing, apart from <function>g_static_rec_mutex_trylock</function>,
614 * which does nothing but returning %TRUE.
618 * G_STATIC_REC_MUTEX_INIT:
620 * A #GStaticRecMutex must be initialized with this macro before it can
621 * be used. This macro can used be to initialize a variable, but it
622 * cannot be assigned to a variable. In that case you have to use
623 * g_static_rec_mutex_init().
626 * GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
631 * g_static_rec_mutex_init:
632 * @mutex: a #GStaticRecMutex to be initialized.
634 * A #GStaticRecMutex must be initialized with this function before it
635 * can be used. Alternatively you can initialize it with
636 * #G_STATIC_REC_MUTEX_INIT.
638 * Deprecated: 2.32: Use g_rec_mutex_init()
641 g_static_rec_mutex_init (GStaticRecMutex *mutex)
643 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
645 g_return_if_fail (mutex);
651 * g_static_rec_mutex_lock:
652 * @mutex: a #GStaticRecMutex to lock.
654 * Locks @mutex. If @mutex is already locked by another thread, the
655 * current thread will block until @mutex is unlocked by the other
656 * thread. If @mutex is already locked by the calling thread, this
657 * functions increases the depth of @mutex and returns immediately.
659 * Deprecated: 2.32: Use g_rec_mutex_lock()
662 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
666 g_return_if_fail (mutex);
668 if (!g_thread_supported ())
671 g_system_thread_self (&self);
673 if (g_system_thread_equal (&self, &mutex->owner))
678 g_static_mutex_lock (&mutex->mutex);
679 g_system_thread_assign (mutex->owner, self);
684 * g_static_rec_mutex_trylock:
685 * @mutex: a #GStaticRecMutex to lock.
686 * @Returns: %TRUE, if @mutex could be locked.
688 * Tries to lock @mutex. If @mutex is already locked by another thread,
689 * it immediately returns %FALSE. Otherwise it locks @mutex and returns
690 * %TRUE. If @mutex is already locked by the calling thread, this
691 * functions increases the depth of @mutex and immediately returns
694 * Deprecated: 2.32: Use g_rec_mutex_trylock()
697 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
701 g_return_val_if_fail (mutex, FALSE);
703 if (!g_thread_supported ())
706 g_system_thread_self (&self);
708 if (g_system_thread_equal (&self, &mutex->owner))
714 if (!g_static_mutex_trylock (&mutex->mutex))
717 g_system_thread_assign (mutex->owner, self);
723 * g_static_rec_mutex_unlock:
724 * @mutex: a #GStaticRecMutex to unlock.
726 * Unlocks @mutex. Another thread will be allowed to lock @mutex only
727 * when it has been unlocked as many times as it had been locked
728 * before. If @mutex is completely unlocked and another thread is
729 * blocked in a g_static_rec_mutex_lock() call for @mutex, it will be
730 * woken and can lock @mutex itself.
732 * Deprecated: 2.32: Use g_rec_mutex_unlock()
735 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
737 g_return_if_fail (mutex);
739 if (!g_thread_supported ())
742 if (mutex->depth > 1)
747 g_system_thread_assign (mutex->owner, zero_thread);
748 g_static_mutex_unlock (&mutex->mutex);
752 * g_static_rec_mutex_lock_full:
753 * @mutex: a #GStaticRecMutex to lock.
754 * @depth: number of times this mutex has to be unlocked to be
755 * completely unlocked.
757 * Works like calling g_static_rec_mutex_lock() for @mutex @depth times.
759 * Deprecated: 2.32: Use g_rec_mutex_lock()
762 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
766 g_return_if_fail (mutex);
768 if (!g_thread_supported ())
774 g_system_thread_self (&self);
776 if (g_system_thread_equal (&self, &mutex->owner))
778 mutex->depth += depth;
781 g_static_mutex_lock (&mutex->mutex);
782 g_system_thread_assign (mutex->owner, self);
783 mutex->depth = depth;
787 * g_static_rec_mutex_unlock_full:
788 * @mutex: a #GStaticRecMutex to completely unlock.
789 * @Returns: number of times @mutex has been locked by the current
792 * Completely unlocks @mutex. If another thread is blocked in a
793 * g_static_rec_mutex_lock() call for @mutex, it will be woken and can
794 * lock @mutex itself. This function returns the number of times that
795 * @mutex has been locked by the current thread. To restore the state
796 * before the call to g_static_rec_mutex_unlock_full() you can call
797 * g_static_rec_mutex_lock_full() with the depth returned by this
800 * Deprecated: 2.32: Use g_rec_mutex_unlock()
803 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
807 g_return_val_if_fail (mutex, 0);
809 if (!g_thread_supported ())
812 depth = mutex->depth;
814 g_system_thread_assign (mutex->owner, zero_thread);
816 g_static_mutex_unlock (&mutex->mutex);
822 * g_static_rec_mutex_free:
823 * @mutex: a #GStaticRecMutex to be freed.
825 * Releases all resources allocated to a #GStaticRecMutex.
827 * You don't have to call this functions for a #GStaticRecMutex with an
828 * unbounded lifetime, i.e. objects declared 'static', but if you have
829 * a #GStaticRecMutex as a member of a structure and the structure is
830 * freed, you should also free the #GStaticRecMutex.
832 * Deprecated: 2.32: Use g_rec_mutex_clear()
835 g_static_rec_mutex_free (GStaticRecMutex *mutex)
837 g_return_if_fail (mutex);
839 g_static_mutex_free (&mutex->mutex);
842 /* GStaticRWLock {{{1 ----------------------------------------------------- */
847 * The #GStaticRWLock struct represents a read-write lock. A read-write
848 * lock can be used for protecting data that some portions of code only
849 * read from, while others also write. In such situations it is
850 * desirable that several readers can read at once, whereas of course
851 * only one writer may write at a time. Take a look at the following
855 * <title>An array with access functions</title>
857 * GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
861 * my_array_get (guint index)
863 * gpointer retval = NULL;
868 * g_static_rw_lock_reader_lock (&rwlock);
869 * if (index < array->len)
870 * retval = g_ptr_array_index (array, index);
871 * g_static_rw_lock_reader_unlock (&rwlock);
877 * my_array_set (guint index, gpointer data)
879 * g_static_rw_lock_writer_lock (&rwlock);
882 * array = g_ptr_array_new (<!-- -->);
884 * if (index >= array->len)
885 * g_ptr_array_set_size (array, index+1);
886 * g_ptr_array_index (array, index) = data;
888 * g_static_rw_lock_writer_unlock (&rwlock);
893 * This example shows an array which can be accessed by many readers
894 * (the <function>my_array_get()</function> function) simultaneously,
895 * whereas the writers (the <function>my_array_set()</function>
896 * function) will only be allowed once at a time and only if no readers
897 * currently access the array. This is because of the potentially
898 * dangerous resizing of the array. Using these functions is fully
899 * multi-thread safe now.
901 * Most of the time, writers should have precedence over readers. That
902 * means, for this implementation, that as soon as a writer wants to
903 * lock the data, no other reader is allowed to lock the data, whereas,
904 * of course, the readers that already have locked the data are allowed
905 * to finish their operation. As soon as the last reader unlocks the
906 * data, the writer will lock it.
908 * Even though #GStaticRWLock is not opaque, it should only be used
909 * with the following functions.
911 * All of the <function>g_static_rw_lock_*</function> functions can be
912 * used even if g_thread_init() has not been called. Then they do
913 * nothing, apart from <function>g_static_rw_lock_*_trylock</function>,
914 * which does nothing but returning %TRUE.
916 * <note><para>A read-write lock has a higher overhead than a mutex. For
917 * example, both g_static_rw_lock_reader_lock() and
918 * g_static_rw_lock_reader_unlock() have to lock and unlock a
919 * #GStaticMutex, so it takes at least twice the time to lock and unlock
920 * a #GStaticRWLock that it does to lock and unlock a #GStaticMutex. So
921 * only data structures that are accessed by multiple readers, and which
922 * keep the lock for a considerable time justify a #GStaticRWLock. The
923 * above example most probably would fare better with a
924 * #GStaticMutex.</para></note>
926 * Deprecated: 2.32: Use a #GRWLock instead
930 * G_STATIC_RW_LOCK_INIT:
932 * A #GStaticRWLock must be initialized with this macro before it can
933 * be used. This macro can used be to initialize a variable, but it
934 * cannot be assigned to a variable. In that case you have to use
935 * g_static_rw_lock_init().
938 * GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
943 * g_static_rw_lock_init:
944 * @lock: a #GStaticRWLock to be initialized.
946 * A #GStaticRWLock must be initialized with this function before it
947 * can be used. Alternatively you can initialize it with
948 * #G_STATIC_RW_LOCK_INIT.
950 * Deprecated: 2.32: Use g_rw_lock_init() instead
953 g_static_rw_lock_init (GStaticRWLock* lock)
955 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
957 g_return_if_fail (lock);
963 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
966 *cond = g_cond_new ();
967 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
971 g_static_rw_lock_signal (GStaticRWLock* lock)
973 if (lock->want_to_write && lock->write_cond)
974 g_cond_signal (lock->write_cond);
975 else if (lock->want_to_read && lock->read_cond)
976 g_cond_broadcast (lock->read_cond);
980 * g_static_rw_lock_reader_lock:
981 * @lock: a #GStaticRWLock to lock for reading.
983 * Locks @lock for reading. There may be unlimited concurrent locks for
984 * reading of a #GStaticRWLock at the same time. If @lock is already
985 * locked for writing by another thread or if another thread is already
986 * waiting to lock @lock for writing, this function will block until
987 * @lock is unlocked by the other writing thread and no other writing
988 * threads want to lock @lock. This lock has to be unlocked by
989 * g_static_rw_lock_reader_unlock().
991 * #GStaticRWLock is not recursive. It might seem to be possible to
992 * recursively lock for reading, but that can result in a deadlock, due
993 * to writer preference.
995 * Deprecated: 2.32: Use g_rw_lock_reader_lock() instead
998 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
1000 g_return_if_fail (lock);
1002 if (!g_threads_got_initialized)
1005 g_static_mutex_lock (&lock->mutex);
1006 lock->want_to_read++;
1007 while (lock->have_writer || lock->want_to_write)
1008 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
1009 lock->want_to_read--;
1010 lock->read_counter++;
1011 g_static_mutex_unlock (&lock->mutex);
1015 * g_static_rw_lock_reader_trylock:
1016 * @lock: a #GStaticRWLock to lock for reading.
1017 * @Returns: %TRUE, if @lock could be locked for reading.
1019 * Tries to lock @lock for reading. If @lock is already locked for
1020 * writing by another thread or if another thread is already waiting to
1021 * lock @lock for writing, immediately returns %FALSE. Otherwise locks
1022 * @lock for reading and returns %TRUE. This lock has to be unlocked by
1023 * g_static_rw_lock_reader_unlock().
1025 * Deprectated: 2.32: Use g_rw_lock_reader_trylock() instead
1028 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
1030 gboolean ret_val = FALSE;
1032 g_return_val_if_fail (lock, FALSE);
1034 if (!g_threads_got_initialized)
1037 g_static_mutex_lock (&lock->mutex);
1038 if (!lock->have_writer && !lock->want_to_write)
1040 lock->read_counter++;
1043 g_static_mutex_unlock (&lock->mutex);
1048 * g_static_rw_lock_reader_unlock:
1049 * @lock: a #GStaticRWLock to unlock after reading.
1051 * Unlocks @lock. If a thread waits to lock @lock for writing and all
1052 * locks for reading have been unlocked, the waiting thread is woken up
1053 * and can lock @lock for writing.
1055 * Deprectated: 2.32: Use g_rw_lock_reader_unlock() instead
1058 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
1060 g_return_if_fail (lock);
1062 if (!g_threads_got_initialized)
1065 g_static_mutex_lock (&lock->mutex);
1066 lock->read_counter--;
1067 if (lock->read_counter == 0)
1068 g_static_rw_lock_signal (lock);
1069 g_static_mutex_unlock (&lock->mutex);
1073 * g_static_rw_lock_writer_lock:
1074 * @lock: a #GStaticRWLock to lock for writing.
1076 * Locks @lock for writing. If @lock is already locked for writing or
1077 * reading by other threads, this function will block until @lock is
1078 * completely unlocked and then lock @lock for writing. While this
1079 * functions waits to lock @lock, no other thread can lock @lock for
1080 * reading. When @lock is locked for writing, no other thread can lock
1081 * @lock (neither for reading nor writing). This lock has to be
1082 * unlocked by g_static_rw_lock_writer_unlock().
1084 * Deprectated: 2.32: Use g_rw_lock_writer_lock() instead
1087 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
1089 g_return_if_fail (lock);
1091 if (!g_threads_got_initialized)
1094 g_static_mutex_lock (&lock->mutex);
1095 lock->want_to_write++;
1096 while (lock->have_writer || lock->read_counter)
1097 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
1098 lock->want_to_write--;
1099 lock->have_writer = TRUE;
1100 g_static_mutex_unlock (&lock->mutex);
1104 * g_static_rw_lock_writer_trylock:
1105 * @lock: a #GStaticRWLock to lock for writing.
1106 * @Returns: %TRUE, if @lock could be locked for writing.
1108 * Tries to lock @lock for writing. If @lock is already locked (for
1109 * either reading or writing) by another thread, it immediately returns
1110 * %FALSE. Otherwise it locks @lock for writing and returns %TRUE. This
1111 * lock has to be unlocked by g_static_rw_lock_writer_unlock().
1113 * Deprectated: 2.32: Use g_rw_lock_writer_trylock() instead
1116 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
1118 gboolean ret_val = FALSE;
1120 g_return_val_if_fail (lock, FALSE);
1122 if (!g_threads_got_initialized)
1125 g_static_mutex_lock (&lock->mutex);
1126 if (!lock->have_writer && !lock->read_counter)
1128 lock->have_writer = TRUE;
1131 g_static_mutex_unlock (&lock->mutex);
1136 * g_static_rw_lock_writer_unlock:
1137 * @lock: a #GStaticRWLock to unlock after writing.
1139 * Unlocks @lock. If a thread is waiting to lock @lock for writing and
1140 * all locks for reading have been unlocked, the waiting thread is
1141 * woken up and can lock @lock for writing. If no thread is waiting to
1142 * lock @lock for writing, and some thread or threads are waiting to
1143 * lock @lock for reading, the waiting threads are woken up and can
1144 * lock @lock for reading.
1146 * Deprectated: 2.32: Use g_rw_lock_writer_unlock() instead
1149 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
1151 g_return_if_fail (lock);
1153 if (!g_threads_got_initialized)
1156 g_static_mutex_lock (&lock->mutex);
1157 lock->have_writer = FALSE;
1158 g_static_rw_lock_signal (lock);
1159 g_static_mutex_unlock (&lock->mutex);
1163 * g_static_rw_lock_free:
1164 * @lock: a #GStaticRWLock to be freed.
1166 * Releases all resources allocated to @lock.
1168 * You don't have to call this functions for a #GStaticRWLock with an
1169 * unbounded lifetime, i.e. objects declared 'static', but if you have
1170 * a #GStaticRWLock as a member of a structure, and the structure is
1171 * freed, you should also free the #GStaticRWLock.
1173 * Deprecated: 2.32: Use a #GRWLock instead
1176 g_static_rw_lock_free (GStaticRWLock* lock)
1178 g_return_if_fail (lock);
1180 if (lock->read_cond)
1182 g_cond_free (lock->read_cond);
1183 lock->read_cond = NULL;
1185 if (lock->write_cond)
1187 g_cond_free (lock->write_cond);
1188 lock->write_cond = NULL;
1190 g_static_mutex_free (&lock->mutex);
1193 /* GPrivate {{{1 ------------------------------------------------------ */
1197 * @notify: a #GDestroyNotify
1199 * Deprecated:2.32: dynamic allocation of #GPrivate is a bad idea. Use
1200 * static storage and G_PRIVATE_INIT() instead.
1202 * Returns: a newly allocated #GPrivate (which can never be destroyed)
1205 g_private_new (GDestroyNotify notify)
1207 GPrivate tmp = G_PRIVATE_INIT (notify);
1210 key = g_slice_new (GPrivate);
1216 /* {{{1 GStaticPrivate */
1218 typedef struct _GStaticPrivateNode GStaticPrivateNode;
1219 struct _GStaticPrivateNode
1222 GDestroyNotify destroy;
1223 GStaticPrivate *owner;
1229 * A #GStaticPrivate works almost like a #GPrivate, but it has one
1230 * significant advantage. It doesn't need to be created at run-time
1231 * like a #GPrivate, but can be defined at compile-time. This is
1232 * similar to the difference between #GMutex and #GStaticMutex. Now
1233 * look at our <function>give_me_next_number()</function> example with
1237 * <title>Using GStaticPrivate for per-thread data</title>
1240 * give_me_next_number (<!-- -->)
1242 * static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1243 * int *current_number = g_static_private_get (&current_number_key);
1245 * if (!current_number)
1247 * current_number = g_new (int,1);
1248 * *current_number = 0;
1249 * g_static_private_set (&current_number_key, current_number, g_free);
1252 * *current_number = calc_next_number (*current_number);
1254 * return *current_number;
1261 * G_STATIC_PRIVATE_INIT:
1263 * Every #GStaticPrivate must be initialized with this macro, before it
1267 * GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1272 * g_static_private_init:
1273 * @private_key: a #GStaticPrivate to be initialized
1275 * Initializes @private_key. Alternatively you can initialize it with
1276 * #G_STATIC_PRIVATE_INIT.
1279 g_static_private_init (GStaticPrivate *private_key)
1281 private_key->index = 0;
1285 * g_static_private_get:
1286 * @private_key: a #GStaticPrivate
1288 * Works like g_private_get() only for a #GStaticPrivate.
1290 * This function works even if g_thread_init() has not yet been called.
1292 * Returns: the corresponding pointer
1295 g_static_private_get (GStaticPrivate *private_key)
1297 GRealThread *self = (GRealThread*) g_thread_self ();
1299 gpointer ret = NULL;
1300 array = self->private_data;
1302 if (array && private_key->index != 0 && private_key->index <= array->len)
1304 GStaticPrivateNode *node;
1306 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1308 /* Deal with the possibility that the GStaticPrivate which used
1309 * to have this index got freed and the index got allocated to
1310 * a new one. In this case, the data in the node is stale, so
1311 * free it and return NULL.
1313 if (G_UNLIKELY (node->owner != private_key))
1316 node->destroy (node->data);
1317 node->destroy = NULL;
1328 * g_static_private_set:
1329 * @private_key: a #GStaticPrivate
1330 * @data: the new pointer
1331 * @notify: a function to be called with the pointer whenever the
1332 * current thread ends or sets this pointer again
1334 * Sets the pointer keyed to @private_key for the current thread and
1335 * the function @notify to be called with that pointer (%NULL or
1336 * non-%NULL), whenever the pointer is set again or whenever the
1337 * current thread ends.
1339 * This function works even if g_thread_init() has not yet been called.
1340 * If g_thread_init() is called later, the @data keyed to @private_key
1341 * will be inherited only by the main thread, i.e. the one that called
1344 * <note><para>@notify is used quite differently from @destructor in
1345 * g_private_new().</para></note>
1348 g_static_private_set (GStaticPrivate *private_key,
1350 GDestroyNotify notify)
1352 GRealThread *self = (GRealThread*) g_thread_self ();
1354 static guint next_index = 0;
1355 GStaticPrivateNode *node;
1357 if (!private_key->index)
1361 if (!private_key->index)
1363 if (g_thread_free_indices)
1365 private_key->index = GPOINTER_TO_UINT (g_thread_free_indices->data);
1366 g_thread_free_indices = g_slist_delete_link (g_thread_free_indices,
1367 g_thread_free_indices);
1370 private_key->index = ++next_index;
1373 G_UNLOCK (g_thread);
1376 array = self->private_data;
1379 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
1380 self->private_data = array;
1382 if (private_key->index > array->len)
1383 g_array_set_size (array, private_key->index);
1385 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1388 node->destroy (node->data);
1391 node->destroy = notify;
1392 node->owner = private_key;
1396 * g_static_private_free:
1397 * @private_key: a #GStaticPrivate to be freed
1399 * Releases all resources allocated to @private_key.
1401 * You don't have to call this functions for a #GStaticPrivate with an
1402 * unbounded lifetime, i.e. objects declared 'static', but if you have
1403 * a #GStaticPrivate as a member of a structure and the structure is
1404 * freed, you should also free the #GStaticPrivate.
1407 g_static_private_free (GStaticPrivate *private_key)
1409 guint idx = private_key->index;
1414 private_key->index = 0;
1416 /* Freeing the per-thread data is deferred to either the
1417 * thread end or the next g_static_private_get() call for
1421 g_thread_free_indices = g_slist_prepend (g_thread_free_indices,
1422 GUINT_TO_POINTER (idx));
1423 G_UNLOCK (g_thread);
1427 g_static_private_cleanup (GRealThread *thread)
1431 array = thread->private_data;
1432 thread->private_data = NULL;
1438 for (i = 0; i < array->len; i++ )
1440 GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
1442 node->destroy (node->data);
1444 g_array_free (array, TRUE);
1448 /* GMutex {{{1 ------------------------------------------------------ */
1453 * Allocates and initializes a new #GMutex.
1455 * Returns: a newly allocated #GMutex. Use g_mutex_free() to free
1457 * Deprecated:3.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:3.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:3.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:3.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 /* vim: set foldmethod=marker: */