1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gthread.c: MT safety related functions
5 * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
26 #include "gmessages.h"
30 #include "gthreadprivate.h"
31 #include "deprecated/gthread.h"
35 /* {{{1 Documentation */
39 * @G_THREAD_PRIORITY_LOW: a priority lower than normal
40 * @G_THREAD_PRIORITY_NORMAL: the default priority
41 * @G_THREAD_PRIORITY_HIGH: a priority higher than normal
42 * @G_THREAD_PRIORITY_URGENT: the highest priority
44 * Deprecated:2.32: Thread priorities no longer have any effect.
49 * @mutex_new: virtual function pointer for g_mutex_new()
50 * @mutex_lock: virtual function pointer for g_mutex_lock()
51 * @mutex_trylock: virtual function pointer for g_mutex_trylock()
52 * @mutex_unlock: virtual function pointer for g_mutex_unlock()
53 * @mutex_free: virtual function pointer for g_mutex_free()
54 * @cond_new: virtual function pointer for g_cond_new()
55 * @cond_signal: virtual function pointer for g_cond_signal()
56 * @cond_broadcast: virtual function pointer for g_cond_broadcast()
57 * @cond_wait: virtual function pointer for g_cond_wait()
58 * @cond_timed_wait: virtual function pointer for g_cond_timed_wait()
59 * @cond_free: virtual function pointer for g_cond_free()
60 * @private_new: virtual function pointer for g_private_new()
61 * @private_get: virtual function pointer for g_private_get()
62 * @private_set: virtual function pointer for g_private_set()
63 * @thread_create: virtual function pointer for g_thread_create()
64 * @thread_yield: virtual function pointer for g_thread_yield()
65 * @thread_join: virtual function pointer for g_thread_join()
66 * @thread_exit: virtual function pointer for g_thread_exit()
67 * @thread_set_priority: virtual function pointer for
68 * g_thread_set_priority()
69 * @thread_self: virtual function pointer for g_thread_self()
70 * @thread_equal: used internally by recursive mutex locks and by some
73 * This function table is no longer used by g_thread_init()
74 * to initialize the thread system.
77 /* {{{1 Exported Variables */
79 gboolean g_thread_use_default_impl = TRUE;
81 GThreadFunctions g_thread_functions_for_glib_use =
109 return g_get_monotonic_time () * 1000;
112 guint64 (*g_thread_gettime) (void) = gettime;
114 /* Initialisation {{{1 ---------------------------------------------------- */
115 gboolean g_threads_got_initialized = TRUE;
116 GSystemThread zero_thread; /* This is initialized to all zero */
121 * @vtable: a function table of type #GThreadFunctions, that provides
122 * the entry points to the thread system to be used. Since 2.32,
123 * this parameter is ignored and should always be %NULL
125 * If you use GLib from more than one thread, you must initialize the
126 * thread system by calling g_thread_init().
128 * Since version 2.24, calling g_thread_init() multiple times is allowed,
129 * but nothing happens except for the first call.
131 * Since version 2.32, GLib does not support custom thread implementations
132 * anymore and the @vtable parameter is ignored and you should pass %NULL.
134 * <note><para>g_thread_init() must not be called directly or indirectly
135 * in a callback from GLib. Also no mutexes may be currently locked while
136 * calling g_thread_init().</para></note>
138 * <note><para>To use g_thread_init() in your program, you have to link
139 * with the libraries that the command <command>pkg-config --libs
140 * gthread-2.0</command> outputs. This is not the case for all the
141 * other thread-related functions of GLib. Those can be used without
142 * having to link with the thread libraries.</para></note>
146 * g_thread_get_initialized:
148 * Indicates if g_thread_init() has been called.
150 * Returns: %TRUE if threads have been initialized.
155 g_thread_get_initialized (void)
157 return g_thread_supported ();
160 /* We need this for ABI compatibility */
161 void g_thread_init_glib (void) { }
163 /* Internal variables {{{1 */
165 static GRealThread *g_thread_all_threads = NULL;
166 static GSList *g_thread_free_indices = NULL;
168 /* Protects g_thread_all_threads and g_thread_free_indices */
169 G_LOCK_DEFINE_STATIC (g_thread);
171 /* Misc. GThread functions {{{1 */
174 * g_thread_set_priority:
175 * @thread: a #GThread.
178 * This function does nothing.
180 * Deprecated:2.32: Thread priorities no longer have any effect.
183 g_thread_set_priority (GThread *thread,
184 GThreadPriority priority)
190 * @func: a function to execute in the new thread
191 * @data: an argument to supply to the new thread
192 * @joinable: should this thread be joinable?
193 * @error: return location for error, or %NULL
195 * This function creates a new thread.
197 * If @joinable is %TRUE, you can wait for this threads termination
198 * calling g_thread_join(). Otherwise the thread will just disappear
199 * when it terminates.
201 * The new thread executes the function @func with the argument @data.
202 * If the thread was created successfully, it is returned.
204 * @error can be %NULL to ignore errors, or non-%NULL to report errors.
205 * The error is set, if and only if the function returns %NULL.
207 * Returns: the new #GThread on success
209 * Deprecated:2.32: Use g_thread_new() instead
212 g_thread_create (GThreadFunc func,
217 return g_thread_new_internal (NULL, func, data, joinable, 0, TRUE, error);
221 * g_thread_create_full:
222 * @func: a function to execute in the new thread.
223 * @data: an argument to supply to the new thread.
224 * @stack_size: a stack size for the new thread.
225 * @joinable: should this thread be joinable?
228 * @error: return location for error.
229 * @Returns: the new #GThread on success.
231 * This function creates a new thread.
233 * Deprecated:2.32: The @bound and @priority arguments are now ignored.
234 * Use g_thread_new() or g_thread_new_full() instead.
237 g_thread_create_full (GThreadFunc func,
242 GThreadPriority priority,
245 return g_thread_new_internal (NULL, func, data, joinable, stack_size, TRUE, error);
250 * @thread_func: function to call for all #GThread structures
251 * @user_data: second argument to @thread_func
253 * Call @thread_func on all #GThreads that have been
254 * created with g_thread_create().
256 * Note that threads may decide to exit while @thread_func is
257 * running, so without intimate knowledge about the lifetime of
258 * foreign threads, @thread_func shouldn't access the GThread*
259 * pointer passed in as first argument. However, @thread_func will
260 * not be called for threads which are known to have exited already.
262 * Due to thread lifetime checks, this function has an execution complexity
263 * which is quadratic in the number of existing threads.
267 * Deprecated:2.32: There aren't many things you can do with a #GThread,
268 * except comparing it with one that was returned from g_thread_create().
269 * There are better ways to find out if your thread is still alive.
272 g_thread_foreach (GFunc thread_func,
275 GSList *slist = NULL;
277 g_return_if_fail (thread_func != NULL);
278 /* snapshot the list of threads for iteration */
280 for (thread = g_thread_all_threads; thread; thread = thread->next)
281 slist = g_slist_prepend (slist, thread);
283 /* walk the list, skipping non-existent threads */
286 GSList *node = slist;
288 /* check whether the current thread still exists */
290 for (thread = g_thread_all_threads; thread; thread = thread->next)
291 if (thread == node->data)
295 thread_func (thread, user_data);
296 g_slist_free_1 (node);
301 g_enumerable_thread_add (GRealThread *thread)
304 thread->next = g_thread_all_threads;
305 g_thread_all_threads = thread;
310 g_enumerable_thread_remove (GRealThread *thread)
315 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
322 g_thread_all_threads = t->next;
329 /* GOnce {{{1 ------------------------------------------------------------- */
331 g_once_init_enter_impl (volatile gsize *location)
333 return (g_once_init_enter) (location);
336 /* GStaticMutex {{{1 ------------------------------------------------------ */
341 * A #GStaticMutex works like a #GMutex.
343 * Prior to GLib 2.32, GStaticMutex had the significant advantage
344 * that it doesn't need to be created at run-time, but can be defined
345 * at compile-time. Since 2.32, #GMutex can be statically allocated
346 * as well, and GStaticMutex has been deprecated.
348 * Here is a version of our give_me_next_number() example using
353 * Using <structname>GStaticMutex</structname>
354 * to simplify thread-safe programming
358 * give_me_next_number (void)
360 * static int current_number = 0;
362 * static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
364 * g_static_mutex_lock (&mutex);
365 * ret_val = current_number = calc_next_number (current_number);
366 * g_static_mutex_unlock (&mutex);
373 * Sometimes you would like to dynamically create a mutex. If you don't
374 * want to require prior calling to g_thread_init(), because your code
375 * should also be usable in non-threaded programs, you are not able to
376 * use g_mutex_new() and thus #GMutex, as that requires a prior call to
377 * g_thread_init(). In theses cases you can also use a #GStaticMutex.
378 * It must be initialized with g_static_mutex_init() before using it
379 * and freed with with g_static_mutex_free() when not needed anymore to
380 * free up any allocated resources.
382 * Even though #GStaticMutex is not opaque, it should only be used with
383 * the following functions, as it is defined differently on different
386 * All of the <function>g_static_mutex_*</function> functions apart
387 * from <function>g_static_mutex_get_mutex</function> can also be used
388 * even if g_thread_init() has not yet been called. Then they do
389 * nothing, apart from <function>g_static_mutex_trylock</function>,
390 * which does nothing but returning %TRUE.
392 * <note><para>All of the <function>g_static_mutex_*</function>
393 * functions are actually macros. Apart from taking their addresses, you
394 * can however use them as if they were functions.</para></note>
398 * G_STATIC_MUTEX_INIT:
400 * A #GStaticMutex must be initialized with this macro, before it can
401 * be used. This macro can used be to initialize a variable, but it
402 * cannot be assigned to a variable. In that case you have to use
403 * g_static_mutex_init().
406 * GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
411 * g_static_mutex_init:
412 * @mutex: a #GStaticMutex to be initialized.
414 * Initializes @mutex.
415 * Alternatively you can initialize it with #G_STATIC_MUTEX_INIT.
417 * Deprecated: 2.32: Use g_mutex_init()
420 g_static_mutex_init (GStaticMutex *mutex)
422 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
424 g_return_if_fail (mutex);
429 /* IMPLEMENTATION NOTE:
431 * On some platforms a GStaticMutex is actually a normal GMutex stored
432 * inside of a structure instead of being allocated dynamically. We can
433 * only do this for platforms on which we know, in advance, how to
434 * allocate (size) and initialise (value) that memory.
436 * On other platforms, a GStaticMutex is nothing more than a pointer to
437 * a GMutex. In that case, the first access we make to the static mutex
438 * must first allocate the normal GMutex and store it into the pointer.
440 * configure.ac writes macros into glibconfig.h to determine if
441 * g_static_mutex_get_mutex() accesses the structure in memory directly
442 * (on platforms where we are able to do that) or if it ends up here,
443 * where we may have to allocate the GMutex before returning it.
447 * g_static_mutex_get_mutex:
448 * @mutex: a #GStaticMutex.
449 * @Returns: the #GMutex corresponding to @mutex.
451 * For some operations (like g_cond_wait()) you must have a #GMutex
452 * instead of a #GStaticMutex. This function will return the
453 * corresponding #GMutex for @mutex.
455 * Deprecated: 2.32: Just use a #GMutex
458 g_static_mutex_get_mutex_impl (GMutex** mutex)
462 if (!g_thread_supported ())
465 result = g_atomic_pointer_get (mutex);
469 g_mutex_lock (&g_once_mutex);
474 result = g_mutex_new ();
475 g_atomic_pointer_set (mutex, result);
478 g_mutex_unlock (&g_once_mutex);
484 /* IMPLEMENTATION NOTE:
486 * g_static_mutex_lock(), g_static_mutex_trylock() and
487 * g_static_mutex_unlock() are all preprocessor macros that wrap the
488 * corresponding g_mutex_*() function around a call to
489 * g_static_mutex_get_mutex().
493 * g_static_mutex_lock:
494 * @mutex: a #GStaticMutex.
496 * Works like g_mutex_lock(), but for a #GStaticMutex.
498 * Deprecated: 2.32: Use g_mutex_lock()
502 * g_static_mutex_trylock:
503 * @mutex: a #GStaticMutex.
504 * @Returns: %TRUE, if the #GStaticMutex could be locked.
506 * Works like g_mutex_trylock(), but for a #GStaticMutex.
508 * Deprecated: 2.32: Use g_mutex_trylock()
512 * g_static_mutex_unlock:
513 * @mutex: a #GStaticMutex.
515 * Works like g_mutex_unlock(), but for a #GStaticMutex.
517 * Deprecated: 2.32: Use g_mutex_unlock()
521 * g_static_mutex_free:
522 * @mutex: a #GStaticMutex to be freed.
524 * Releases all resources allocated to @mutex.
526 * You don't have to call this functions for a #GStaticMutex with an
527 * unbounded lifetime, i.e. objects declared 'static', but if you have
528 * a #GStaticMutex as a member of a structure and the structure is
529 * freed, you should also free the #GStaticMutex.
531 * <note><para>Calling g_static_mutex_free() on a locked mutex may
532 * result in undefined behaviour.</para></note>
534 * Deprecated: 2.32: Use g_mutex_free()
537 g_static_mutex_free (GStaticMutex* mutex)
539 GMutex **runtime_mutex;
541 g_return_if_fail (mutex);
543 /* The runtime_mutex is the first (or only) member of GStaticMutex,
544 * see both versions (of glibconfig.h) in configure.ac. Note, that
545 * this variable is NULL, if g_thread_init() hasn't been called or
546 * if we're using the default thread implementation and it provides
548 runtime_mutex = ((GMutex**)mutex);
551 g_mutex_free (*runtime_mutex);
553 *runtime_mutex = NULL;
556 /* {{{1 GStaticRecMutex */
561 * A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
562 * multiple times by one thread. If you enter it n times, you have to
563 * unlock it n times again to let other threads lock it. An exception
564 * is the function g_static_rec_mutex_unlock_full(): that allows you to
565 * unlock a #GStaticRecMutex completely returning the depth, (i.e. the
566 * number of times this mutex was locked). The depth can later be used
567 * to restore the state of the #GStaticRecMutex by calling
568 * g_static_rec_mutex_lock_full(). In GLib 2.32, #GStaticRecMutex has
569 * been deprecated in favor of #GRecMutex.
571 * Even though #GStaticRecMutex is not opaque, it should only be used
572 * with the following functions.
574 * All of the <function>g_static_rec_mutex_*</function> functions can
575 * be used even if g_thread_init() has not been called. Then they do
576 * nothing, apart from <function>g_static_rec_mutex_trylock</function>,
577 * which does nothing but returning %TRUE.
581 * G_STATIC_REC_MUTEX_INIT:
583 * A #GStaticRecMutex must be initialized with this macro before it can
584 * be used. This macro can used be to initialize a variable, but it
585 * cannot be assigned to a variable. In that case you have to use
586 * g_static_rec_mutex_init().
589 * GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
594 * g_static_rec_mutex_init:
595 * @mutex: a #GStaticRecMutex to be initialized.
597 * A #GStaticRecMutex must be initialized with this function before it
598 * can be used. Alternatively you can initialize it with
599 * #G_STATIC_REC_MUTEX_INIT.
601 * Deprecated: 2.32: Use g_rec_mutex_init()
604 g_static_rec_mutex_init (GStaticRecMutex *mutex)
606 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
608 g_return_if_fail (mutex);
614 * g_static_rec_mutex_lock:
615 * @mutex: a #GStaticRecMutex to lock.
617 * Locks @mutex. If @mutex is already locked by another thread, the
618 * current thread will block until @mutex is unlocked by the other
619 * thread. If @mutex is already locked by the calling thread, this
620 * functions increases the depth of @mutex and returns immediately.
622 * Deprecated: 2.32: Use g_rec_mutex_lock()
625 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
629 g_return_if_fail (mutex);
631 if (!g_thread_supported ())
634 g_system_thread_self (&self);
636 if (g_system_thread_equal (&self, &mutex->owner))
641 g_static_mutex_lock (&mutex->mutex);
642 g_system_thread_assign (mutex->owner, self);
647 * g_static_rec_mutex_trylock:
648 * @mutex: a #GStaticRecMutex to lock.
649 * @Returns: %TRUE, if @mutex could be locked.
651 * Tries to lock @mutex. If @mutex is already locked by another thread,
652 * it immediately returns %FALSE. Otherwise it locks @mutex and returns
653 * %TRUE. If @mutex is already locked by the calling thread, this
654 * functions increases the depth of @mutex and immediately returns
657 * Deprecated: 2.32: Use g_rec_mutex_trylock()
660 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
664 g_return_val_if_fail (mutex, FALSE);
666 if (!g_thread_supported ())
669 g_system_thread_self (&self);
671 if (g_system_thread_equal (&self, &mutex->owner))
677 if (!g_static_mutex_trylock (&mutex->mutex))
680 g_system_thread_assign (mutex->owner, self);
686 * g_static_rec_mutex_unlock:
687 * @mutex: a #GStaticRecMutex to unlock.
689 * Unlocks @mutex. Another thread will be allowed to lock @mutex only
690 * when it has been unlocked as many times as it had been locked
691 * before. If @mutex is completely unlocked and another thread is
692 * blocked in a g_static_rec_mutex_lock() call for @mutex, it will be
693 * woken and can lock @mutex itself.
695 * Deprecated: 2.32: Use g_rec_mutex_unlock()
698 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
700 g_return_if_fail (mutex);
702 if (!g_thread_supported ())
705 if (mutex->depth > 1)
710 g_system_thread_assign (mutex->owner, zero_thread);
711 g_static_mutex_unlock (&mutex->mutex);
715 * g_static_rec_mutex_lock_full:
716 * @mutex: a #GStaticRecMutex to lock.
717 * @depth: number of times this mutex has to be unlocked to be
718 * completely unlocked.
720 * Works like calling g_static_rec_mutex_lock() for @mutex @depth times.
722 * Deprecated: 2.32: Use g_rec_mutex_lock()
725 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
729 g_return_if_fail (mutex);
731 if (!g_thread_supported ())
737 g_system_thread_self (&self);
739 if (g_system_thread_equal (&self, &mutex->owner))
741 mutex->depth += depth;
744 g_static_mutex_lock (&mutex->mutex);
745 g_system_thread_assign (mutex->owner, self);
746 mutex->depth = depth;
750 * g_static_rec_mutex_unlock_full:
751 * @mutex: a #GStaticRecMutex to completely unlock.
752 * @Returns: number of times @mutex has been locked by the current
755 * Completely unlocks @mutex. If another thread is blocked in a
756 * g_static_rec_mutex_lock() call for @mutex, it will be woken and can
757 * lock @mutex itself. This function returns the number of times that
758 * @mutex has been locked by the current thread. To restore the state
759 * before the call to g_static_rec_mutex_unlock_full() you can call
760 * g_static_rec_mutex_lock_full() with the depth returned by this
763 * Deprecated: 2.32: Use g_rec_mutex_unlock()
766 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
770 g_return_val_if_fail (mutex, 0);
772 if (!g_thread_supported ())
775 depth = mutex->depth;
777 g_system_thread_assign (mutex->owner, zero_thread);
779 g_static_mutex_unlock (&mutex->mutex);
785 * g_static_rec_mutex_free:
786 * @mutex: a #GStaticRecMutex to be freed.
788 * Releases all resources allocated to a #GStaticRecMutex.
790 * You don't have to call this functions for a #GStaticRecMutex with an
791 * unbounded lifetime, i.e. objects declared 'static', but if you have
792 * a #GStaticRecMutex as a member of a structure and the structure is
793 * freed, you should also free the #GStaticRecMutex.
795 * Deprecated: 2.32: Use g_rec_mutex_clear()
798 g_static_rec_mutex_free (GStaticRecMutex *mutex)
800 g_return_if_fail (mutex);
802 g_static_mutex_free (&mutex->mutex);
805 /* GStaticRWLock {{{1 ----------------------------------------------------- */
810 * The #GStaticRWLock struct represents a read-write lock. A read-write
811 * lock can be used for protecting data that some portions of code only
812 * read from, while others also write. In such situations it is
813 * desirable that several readers can read at once, whereas of course
814 * only one writer may write at a time. Take a look at the following
818 * <title>An array with access functions</title>
820 * GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
824 * my_array_get (guint index)
826 * gpointer retval = NULL;
831 * g_static_rw_lock_reader_lock (&rwlock);
832 * if (index < array->len)
833 * retval = g_ptr_array_index (array, index);
834 * g_static_rw_lock_reader_unlock (&rwlock);
840 * my_array_set (guint index, gpointer data)
842 * g_static_rw_lock_writer_lock (&rwlock);
845 * array = g_ptr_array_new (<!-- -->);
847 * if (index >= array->len)
848 * g_ptr_array_set_size (array, index+1);
849 * g_ptr_array_index (array, index) = data;
851 * g_static_rw_lock_writer_unlock (&rwlock);
856 * This example shows an array which can be accessed by many readers
857 * (the <function>my_array_get()</function> function) simultaneously,
858 * whereas the writers (the <function>my_array_set()</function>
859 * function) will only be allowed once at a time and only if no readers
860 * currently access the array. This is because of the potentially
861 * dangerous resizing of the array. Using these functions is fully
862 * multi-thread safe now.
864 * Most of the time, writers should have precedence over readers. That
865 * means, for this implementation, that as soon as a writer wants to
866 * lock the data, no other reader is allowed to lock the data, whereas,
867 * of course, the readers that already have locked the data are allowed
868 * to finish their operation. As soon as the last reader unlocks the
869 * data, the writer will lock it.
871 * Even though #GStaticRWLock is not opaque, it should only be used
872 * with the following functions.
874 * All of the <function>g_static_rw_lock_*</function> functions can be
875 * used even if g_thread_init() has not been called. Then they do
876 * nothing, apart from <function>g_static_rw_lock_*_trylock</function>,
877 * which does nothing but returning %TRUE.
879 * <note><para>A read-write lock has a higher overhead than a mutex. For
880 * example, both g_static_rw_lock_reader_lock() and
881 * g_static_rw_lock_reader_unlock() have to lock and unlock a
882 * #GStaticMutex, so it takes at least twice the time to lock and unlock
883 * a #GStaticRWLock that it does to lock and unlock a #GStaticMutex. So
884 * only data structures that are accessed by multiple readers, and which
885 * keep the lock for a considerable time justify a #GStaticRWLock. The
886 * above example most probably would fare better with a
887 * #GStaticMutex.</para></note>
889 * Deprecated: 2.32: Use a #GRWLock instead
893 * G_STATIC_RW_LOCK_INIT:
895 * A #GStaticRWLock must be initialized with this macro before it can
896 * be used. This macro can used be to initialize a variable, but it
897 * cannot be assigned to a variable. In that case you have to use
898 * g_static_rw_lock_init().
901 * GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
906 * g_static_rw_lock_init:
907 * @lock: a #GStaticRWLock to be initialized.
909 * A #GStaticRWLock must be initialized with this function before it
910 * can be used. Alternatively you can initialize it with
911 * #G_STATIC_RW_LOCK_INIT.
913 * Deprecated: 2.32: Use g_rw_lock_init() instead
916 g_static_rw_lock_init (GStaticRWLock* lock)
918 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
920 g_return_if_fail (lock);
926 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
929 *cond = g_cond_new ();
930 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
934 g_static_rw_lock_signal (GStaticRWLock* lock)
936 if (lock->want_to_write && lock->write_cond)
937 g_cond_signal (lock->write_cond);
938 else if (lock->want_to_read && lock->read_cond)
939 g_cond_broadcast (lock->read_cond);
943 * g_static_rw_lock_reader_lock:
944 * @lock: a #GStaticRWLock to lock for reading.
946 * Locks @lock for reading. There may be unlimited concurrent locks for
947 * reading of a #GStaticRWLock at the same time. If @lock is already
948 * locked for writing by another thread or if another thread is already
949 * waiting to lock @lock for writing, this function will block until
950 * @lock is unlocked by the other writing thread and no other writing
951 * threads want to lock @lock. This lock has to be unlocked by
952 * g_static_rw_lock_reader_unlock().
954 * #GStaticRWLock is not recursive. It might seem to be possible to
955 * recursively lock for reading, but that can result in a deadlock, due
956 * to writer preference.
958 * Deprecated: 2.32: Use g_rw_lock_reader_lock() instead
961 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
963 g_return_if_fail (lock);
965 if (!g_threads_got_initialized)
968 g_static_mutex_lock (&lock->mutex);
969 lock->want_to_read++;
970 while (lock->have_writer || lock->want_to_write)
971 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
972 lock->want_to_read--;
973 lock->read_counter++;
974 g_static_mutex_unlock (&lock->mutex);
978 * g_static_rw_lock_reader_trylock:
979 * @lock: a #GStaticRWLock to lock for reading.
980 * @Returns: %TRUE, if @lock could be locked for reading.
982 * Tries to lock @lock for reading. If @lock is already locked for
983 * writing by another thread or if another thread is already waiting to
984 * lock @lock for writing, immediately returns %FALSE. Otherwise locks
985 * @lock for reading and returns %TRUE. This lock has to be unlocked by
986 * g_static_rw_lock_reader_unlock().
988 * Deprectated: 2.32: Use g_rw_lock_reader_trylock() instead
991 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
993 gboolean ret_val = FALSE;
995 g_return_val_if_fail (lock, FALSE);
997 if (!g_threads_got_initialized)
1000 g_static_mutex_lock (&lock->mutex);
1001 if (!lock->have_writer && !lock->want_to_write)
1003 lock->read_counter++;
1006 g_static_mutex_unlock (&lock->mutex);
1011 * g_static_rw_lock_reader_unlock:
1012 * @lock: a #GStaticRWLock to unlock after reading.
1014 * Unlocks @lock. If a thread waits to lock @lock for writing and all
1015 * locks for reading have been unlocked, the waiting thread is woken up
1016 * and can lock @lock for writing.
1018 * Deprectated: 2.32: Use g_rw_lock_reader_unlock() instead
1021 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
1023 g_return_if_fail (lock);
1025 if (!g_threads_got_initialized)
1028 g_static_mutex_lock (&lock->mutex);
1029 lock->read_counter--;
1030 if (lock->read_counter == 0)
1031 g_static_rw_lock_signal (lock);
1032 g_static_mutex_unlock (&lock->mutex);
1036 * g_static_rw_lock_writer_lock:
1037 * @lock: a #GStaticRWLock to lock for writing.
1039 * Locks @lock for writing. If @lock is already locked for writing or
1040 * reading by other threads, this function will block until @lock is
1041 * completely unlocked and then lock @lock for writing. While this
1042 * functions waits to lock @lock, no other thread can lock @lock for
1043 * reading. When @lock is locked for writing, no other thread can lock
1044 * @lock (neither for reading nor writing). This lock has to be
1045 * unlocked by g_static_rw_lock_writer_unlock().
1047 * Deprectated: 2.32: Use g_rw_lock_writer_lock() instead
1050 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
1052 g_return_if_fail (lock);
1054 if (!g_threads_got_initialized)
1057 g_static_mutex_lock (&lock->mutex);
1058 lock->want_to_write++;
1059 while (lock->have_writer || lock->read_counter)
1060 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
1061 lock->want_to_write--;
1062 lock->have_writer = TRUE;
1063 g_static_mutex_unlock (&lock->mutex);
1067 * g_static_rw_lock_writer_trylock:
1068 * @lock: a #GStaticRWLock to lock for writing.
1069 * @Returns: %TRUE, if @lock could be locked for writing.
1071 * Tries to lock @lock for writing. If @lock is already locked (for
1072 * either reading or writing) by another thread, it immediately returns
1073 * %FALSE. Otherwise it locks @lock for writing and returns %TRUE. This
1074 * lock has to be unlocked by g_static_rw_lock_writer_unlock().
1076 * Deprectated: 2.32: Use g_rw_lock_writer_trylock() instead
1079 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
1081 gboolean ret_val = FALSE;
1083 g_return_val_if_fail (lock, FALSE);
1085 if (!g_threads_got_initialized)
1088 g_static_mutex_lock (&lock->mutex);
1089 if (!lock->have_writer && !lock->read_counter)
1091 lock->have_writer = TRUE;
1094 g_static_mutex_unlock (&lock->mutex);
1099 * g_static_rw_lock_writer_unlock:
1100 * @lock: a #GStaticRWLock to unlock after writing.
1102 * Unlocks @lock. If a thread is waiting to lock @lock for writing and
1103 * all locks for reading have been unlocked, the waiting thread is
1104 * woken up and can lock @lock for writing. If no thread is waiting to
1105 * lock @lock for writing, and some thread or threads are waiting to
1106 * lock @lock for reading, the waiting threads are woken up and can
1107 * lock @lock for reading.
1109 * Deprectated: 2.32: Use g_rw_lock_writer_unlock() instead
1112 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
1114 g_return_if_fail (lock);
1116 if (!g_threads_got_initialized)
1119 g_static_mutex_lock (&lock->mutex);
1120 lock->have_writer = FALSE;
1121 g_static_rw_lock_signal (lock);
1122 g_static_mutex_unlock (&lock->mutex);
1126 * g_static_rw_lock_free:
1127 * @lock: a #GStaticRWLock to be freed.
1129 * Releases all resources allocated to @lock.
1131 * You don't have to call this functions for a #GStaticRWLock with an
1132 * unbounded lifetime, i.e. objects declared 'static', but if you have
1133 * a #GStaticRWLock as a member of a structure, and the structure is
1134 * freed, you should also free the #GStaticRWLock.
1136 * Deprecated: 2.32: Use a #GRWLock instead
1139 g_static_rw_lock_free (GStaticRWLock* lock)
1141 g_return_if_fail (lock);
1143 if (lock->read_cond)
1145 g_cond_free (lock->read_cond);
1146 lock->read_cond = NULL;
1148 if (lock->write_cond)
1150 g_cond_free (lock->write_cond);
1151 lock->write_cond = NULL;
1153 g_static_mutex_free (&lock->mutex);
1156 /* GPrivate {{{1 ------------------------------------------------------ */
1160 * @notify: a #GDestroyNotify
1162 * Deprecated:2.32: dynamic allocation of #GPrivate is a bad idea. Use
1163 * static storage and G_PRIVATE_INIT() instead.
1165 * Returns: a newly allocated #GPrivate (which can never be destroyed)
1168 g_private_new (GDestroyNotify notify)
1170 GPrivate tmp = G_PRIVATE_INIT (notify);
1173 key = g_slice_new (GPrivate);
1179 /* {{{1 GStaticPrivate */
1181 typedef struct _GStaticPrivateNode GStaticPrivateNode;
1182 struct _GStaticPrivateNode
1185 GDestroyNotify destroy;
1186 GStaticPrivate *owner;
1192 * A #GStaticPrivate works almost like a #GPrivate, but it has one
1193 * significant advantage. It doesn't need to be created at run-time
1194 * like a #GPrivate, but can be defined at compile-time. This is
1195 * similar to the difference between #GMutex and #GStaticMutex. Now
1196 * look at our <function>give_me_next_number()</function> example with
1200 * <title>Using GStaticPrivate for per-thread data</title>
1203 * give_me_next_number (<!-- -->)
1205 * static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1206 * int *current_number = g_static_private_get (&current_number_key);
1208 * if (!current_number)
1210 * current_number = g_new (int,1);
1211 * *current_number = 0;
1212 * g_static_private_set (&current_number_key, current_number, g_free);
1215 * *current_number = calc_next_number (*current_number);
1217 * return *current_number;
1224 * G_STATIC_PRIVATE_INIT:
1226 * Every #GStaticPrivate must be initialized with this macro, before it
1230 * GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1235 * g_static_private_init:
1236 * @private_key: a #GStaticPrivate to be initialized
1238 * Initializes @private_key. Alternatively you can initialize it with
1239 * #G_STATIC_PRIVATE_INIT.
1242 g_static_private_init (GStaticPrivate *private_key)
1244 private_key->index = 0;
1248 * g_static_private_get:
1249 * @private_key: a #GStaticPrivate
1251 * Works like g_private_get() only for a #GStaticPrivate.
1253 * This function works even if g_thread_init() has not yet been called.
1255 * Returns: the corresponding pointer
1258 g_static_private_get (GStaticPrivate *private_key)
1260 GRealThread *self = (GRealThread*) g_thread_self ();
1262 gpointer ret = NULL;
1263 array = self->private_data;
1265 if (array && private_key->index != 0 && private_key->index <= array->len)
1267 GStaticPrivateNode *node;
1269 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1271 /* Deal with the possibility that the GStaticPrivate which used
1272 * to have this index got freed and the index got allocated to
1273 * a new one. In this case, the data in the node is stale, so
1274 * free it and return NULL.
1276 if (G_UNLIKELY (node->owner != private_key))
1279 node->destroy (node->data);
1280 node->destroy = NULL;
1291 * g_static_private_set:
1292 * @private_key: a #GStaticPrivate
1293 * @data: the new pointer
1294 * @notify: a function to be called with the pointer whenever the
1295 * current thread ends or sets this pointer again
1297 * Sets the pointer keyed to @private_key for the current thread and
1298 * the function @notify to be called with that pointer (%NULL or
1299 * non-%NULL), whenever the pointer is set again or whenever the
1300 * current thread ends.
1302 * This function works even if g_thread_init() has not yet been called.
1303 * If g_thread_init() is called later, the @data keyed to @private_key
1304 * will be inherited only by the main thread, i.e. the one that called
1307 * <note><para>@notify is used quite differently from @destructor in
1308 * g_private_new().</para></note>
1311 g_static_private_set (GStaticPrivate *private_key,
1313 GDestroyNotify notify)
1315 GRealThread *self = (GRealThread*) g_thread_self ();
1317 static guint next_index = 0;
1318 GStaticPrivateNode *node;
1320 if (!private_key->index)
1324 if (!private_key->index)
1326 if (g_thread_free_indices)
1328 private_key->index = GPOINTER_TO_UINT (g_thread_free_indices->data);
1329 g_thread_free_indices = g_slist_delete_link (g_thread_free_indices,
1330 g_thread_free_indices);
1333 private_key->index = ++next_index;
1336 G_UNLOCK (g_thread);
1339 array = self->private_data;
1342 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
1343 self->private_data = array;
1345 if (private_key->index > array->len)
1346 g_array_set_size (array, private_key->index);
1348 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1351 node->destroy (node->data);
1354 node->destroy = notify;
1355 node->owner = private_key;
1359 * g_static_private_free:
1360 * @private_key: a #GStaticPrivate to be freed
1362 * Releases all resources allocated to @private_key.
1364 * You don't have to call this functions for a #GStaticPrivate with an
1365 * unbounded lifetime, i.e. objects declared 'static', but if you have
1366 * a #GStaticPrivate as a member of a structure and the structure is
1367 * freed, you should also free the #GStaticPrivate.
1370 g_static_private_free (GStaticPrivate *private_key)
1372 guint idx = private_key->index;
1377 private_key->index = 0;
1379 /* Freeing the per-thread data is deferred to either the
1380 * thread end or the next g_static_private_get() call for
1384 g_thread_free_indices = g_slist_prepend (g_thread_free_indices,
1385 GUINT_TO_POINTER (idx));
1386 G_UNLOCK (g_thread);
1390 g_static_private_cleanup (GRealThread *thread)
1394 array = thread->private_data;
1395 thread->private_data = NULL;
1401 for (i = 0; i < array->len; i++ )
1403 GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
1405 node->destroy (node->data);
1407 g_array_free (array, TRUE);
1411 /* GMutex {{{1 ------------------------------------------------------ */
1416 * Allocates and initializes a new #GMutex.
1418 * Returns: a newly allocated #GMutex. Use g_mutex_free() to free
1420 * Deprecated:3.32:GMutex can now be statically allocated, or embedded
1421 * in structures and initialised with g_mutex_init().
1428 mutex = g_slice_new (GMutex);
1429 g_mutex_init (mutex);
1438 * Destroys a @mutex that has been created with g_mutex_new().
1440 * Calling g_mutex_free() on a locked mutex may result
1441 * in undefined behaviour.
1443 * Deprecated:3.32:GMutex can now be statically allocated, or embedded
1444 * in structures and initialised with g_mutex_init().
1447 g_mutex_free (GMutex *mutex)
1449 g_mutex_clear (mutex);
1450 g_slice_free (GMutex, mutex);
1453 /* GCond {{{1 ------------------------------------------------------ */
1458 * Allocates and initializes a new #GCond.
1460 * Returns: a newly allocated #GCond. Free with g_cond_free()
1462 * Deprecated:3.32:GCond can now be statically allocated, or embedded
1463 * in structures and initialised with g_cond_init().
1470 cond = g_slice_new (GCond);
1480 * Destroys a #GCond that has been created with g_cond_new().
1482 * Calling g_cond_free() for a #GCond on which threads are
1483 * blocking leads to undefined behaviour.
1485 * Deprecated:3.32:GCond can now be statically allocated, or embedded
1486 * in structures and initialised with g_cond_init().
1489 g_cond_free (GCond *cond)
1491 g_cond_clear (cond);
1492 g_slice_free (GCond, cond);
1496 /* vim: set foldmethod=marker: */