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"
33 /* {{{1 Documentation */
37 * @G_THREAD_PRIORITY_LOW: a priority lower than normal
38 * @G_THREAD_PRIORITY_NORMAL: the default priority
39 * @G_THREAD_PRIORITY_HIGH: a priority higher than normal
40 * @G_THREAD_PRIORITY_URGENT: the highest priority
42 * Deprecated:2.32: Thread priorities no longer have any effect.
47 * @mutex_new: virtual function pointer for g_mutex_new()
48 * @mutex_lock: virtual function pointer for g_mutex_lock()
49 * @mutex_trylock: virtual function pointer for g_mutex_trylock()
50 * @mutex_unlock: virtual function pointer for g_mutex_unlock()
51 * @mutex_free: virtual function pointer for g_mutex_free()
52 * @cond_new: virtual function pointer for g_cond_new()
53 * @cond_signal: virtual function pointer for g_cond_signal()
54 * @cond_broadcast: virtual function pointer for g_cond_broadcast()
55 * @cond_wait: virtual function pointer for g_cond_wait()
56 * @cond_timed_wait: virtual function pointer for g_cond_timed_wait()
57 * @cond_free: virtual function pointer for g_cond_free()
58 * @private_new: virtual function pointer for g_private_new()
59 * @private_get: virtual function pointer for g_private_get()
60 * @private_set: virtual function pointer for g_private_set()
61 * @thread_create: virtual function pointer for g_thread_create()
62 * @thread_yield: virtual function pointer for g_thread_yield()
63 * @thread_join: virtual function pointer for g_thread_join()
64 * @thread_exit: virtual function pointer for g_thread_exit()
65 * @thread_set_priority: virtual function pointer for
66 * g_thread_set_priority()
67 * @thread_self: virtual function pointer for g_thread_self()
68 * @thread_equal: used internally by recursive mutex locks and by some
71 * This function table is no longer used by g_thread_init()
72 * to initialize the thread system.
75 /* {{{1 Exported Variables */
77 gboolean g_thread_use_default_impl = TRUE;
79 GThreadFunctions g_thread_functions_for_glib_use =
107 return g_get_monotonic_time () * 1000;
110 guint64 (*g_thread_gettime) (void) = gettime;
112 /* Initialisation {{{1 ---------------------------------------------------- */
113 gboolean g_threads_got_initialized = TRUE;
114 GSystemThread zero_thread; /* This is initialized to all zero */
119 * @vtable: a function table of type #GThreadFunctions, that provides
120 * the entry points to the thread system to be used. Since 2.32,
121 * this parameter is ignored and should always be %NULL
123 * If you use GLib from more than one thread, you must initialize the
124 * thread system by calling g_thread_init().
126 * Since version 2.24, calling g_thread_init() multiple times is allowed,
127 * but nothing happens except for the first call.
129 * Since version 2.32, GLib does not support custom thread implementations
130 * anymore and the @vtable parameter is ignored and you should pass %NULL.
132 * <note><para>g_thread_init() must not be called directly or indirectly
133 * in a callback from GLib. Also no mutexes may be currently locked while
134 * calling g_thread_init().</para></note>
136 * <note><para>To use g_thread_init() in your program, you have to link
137 * with the libraries that the command <command>pkg-config --libs
138 * gthread-2.0</command> outputs. This is not the case for all the
139 * other thread-related functions of GLib. Those can be used without
140 * having to link with the thread libraries.</para></note>
144 * g_thread_get_initialized:
146 * Indicates if g_thread_init() has been called.
148 * Returns: %TRUE if threads have been initialized.
153 g_thread_get_initialized (void)
155 return g_thread_supported ();
158 /* We need this for ABI compatibility */
159 void g_thread_init_glib (void) { }
161 /* Internal variables {{{1 */
163 static GRealThread *g_thread_all_threads = NULL;
164 static GSList *g_thread_free_indices = NULL;
166 /* Protects g_thread_all_threads and g_thread_free_indices */
167 G_LOCK_DEFINE_STATIC (g_thread);
169 /* Misc. GThread functions {{{1 */
172 * g_thread_set_priority:
173 * @thread: a #GThread.
176 * This function does nothing.
178 * Deprecated:2.32: Thread priorities no longer have any effect.
181 g_thread_set_priority (GThread *thread,
182 GThreadPriority priority)
188 * @func: a function to execute in the new thread
189 * @data: an argument to supply to the new thread
190 * @joinable: should this thread be joinable?
191 * @error: return location for error, or %NULL
193 * This function creates a new thread.
195 * If @joinable is %TRUE, you can wait for this threads termination
196 * calling g_thread_join(). Otherwise the thread will just disappear
197 * when it terminates.
199 * The new thread executes the function @func with the argument @data.
200 * If the thread was created successfully, it is returned.
202 * @error can be %NULL to ignore errors, or non-%NULL to report errors.
203 * The error is set, if and only if the function returns %NULL.
205 * Returns: the new #GThread on success
207 * Deprecated:2.32: Use g_thread_new() instead
210 g_thread_create (GThreadFunc func,
215 return g_thread_new_internal (NULL, func, data, joinable, 0, TRUE, error);
219 * g_thread_create_full:
220 * @func: a function to execute in the new thread.
221 * @data: an argument to supply to the new thread.
222 * @stack_size: a stack size for the new thread.
223 * @joinable: should this thread be joinable?
226 * @error: return location for error.
227 * @Returns: the new #GThread on success.
229 * This function creates a new thread.
231 * Deprecated:2.32: The @bound and @priority arguments are now ignored.
232 * Use g_thread_new() or g_thread_new_full() instead.
235 g_thread_create_full (GThreadFunc func,
240 GThreadPriority priority,
243 return g_thread_new_internal (NULL, func, data, joinable, stack_size, TRUE, error);
248 * @thread_func: function to call for all #GThread structures
249 * @user_data: second argument to @thread_func
251 * Call @thread_func on all #GThreads that have been
252 * created with g_thread_create().
254 * Note that threads may decide to exit while @thread_func is
255 * running, so without intimate knowledge about the lifetime of
256 * foreign threads, @thread_func shouldn't access the GThread*
257 * pointer passed in as first argument. However, @thread_func will
258 * not be called for threads which are known to have exited already.
260 * Due to thread lifetime checks, this function has an execution complexity
261 * which is quadratic in the number of existing threads.
265 * Deprecated:2.32: There aren't many things you can do with a #GThread,
266 * except comparing it with one that was returned from g_thread_create().
267 * There are better ways to find out if your thread is still alive.
270 g_thread_foreach (GFunc thread_func,
273 GSList *slist = NULL;
275 g_return_if_fail (thread_func != NULL);
276 /* snapshot the list of threads for iteration */
278 for (thread = g_thread_all_threads; thread; thread = thread->next)
279 slist = g_slist_prepend (slist, thread);
281 /* walk the list, skipping non-existent threads */
284 GSList *node = slist;
286 /* check whether the current thread still exists */
288 for (thread = g_thread_all_threads; thread; thread = thread->next)
289 if (thread == node->data)
293 thread_func (thread, user_data);
294 g_slist_free_1 (node);
299 g_enumerable_thread_add (GRealThread *thread)
302 thread->next = g_thread_all_threads;
303 g_thread_all_threads = thread;
308 g_enumerable_thread_remove (GRealThread *thread)
313 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
320 g_thread_all_threads = t->next;
327 /* GOnce {{{1 ------------------------------------------------------------- */
329 g_once_init_enter_impl (volatile gsize *location)
331 return (g_once_init_enter) (location);
334 /* GStaticMutex {{{1 ------------------------------------------------------ */
339 * A #GStaticMutex works like a #GMutex.
341 * Prior to GLib 2.32, GStaticMutex had the significant advantage
342 * that it doesn't need to be created at run-time, but can be defined
343 * at compile-time. Since 2.32, #GMutex can be statically allocated
344 * as well, and GStaticMutex has been deprecated.
346 * Here is a version of our give_me_next_number() example using
351 * Using <structname>GStaticMutex</structname>
352 * to simplify thread-safe programming
356 * give_me_next_number (void)
358 * static int current_number = 0;
360 * static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
362 * g_static_mutex_lock (&mutex);
363 * ret_val = current_number = calc_next_number (current_number);
364 * g_static_mutex_unlock (&mutex);
371 * Sometimes you would like to dynamically create a mutex. If you don't
372 * want to require prior calling to g_thread_init(), because your code
373 * should also be usable in non-threaded programs, you are not able to
374 * use g_mutex_new() and thus #GMutex, as that requires a prior call to
375 * g_thread_init(). In theses cases you can also use a #GStaticMutex.
376 * It must be initialized with g_static_mutex_init() before using it
377 * and freed with with g_static_mutex_free() when not needed anymore to
378 * free up any allocated resources.
380 * Even though #GStaticMutex is not opaque, it should only be used with
381 * the following functions, as it is defined differently on different
384 * All of the <function>g_static_mutex_*</function> functions apart
385 * from <function>g_static_mutex_get_mutex</function> can also be used
386 * even if g_thread_init() has not yet been called. Then they do
387 * nothing, apart from <function>g_static_mutex_trylock</function>,
388 * which does nothing but returning %TRUE.
390 * <note><para>All of the <function>g_static_mutex_*</function>
391 * functions are actually macros. Apart from taking their addresses, you
392 * can however use them as if they were functions.</para></note>
396 * G_STATIC_MUTEX_INIT:
398 * A #GStaticMutex must be initialized with this macro, before it can
399 * be used. This macro can used be to initialize a variable, but it
400 * cannot be assigned to a variable. In that case you have to use
401 * g_static_mutex_init().
404 * GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
409 * g_static_mutex_init:
410 * @mutex: a #GStaticMutex to be initialized.
412 * Initializes @mutex.
413 * Alternatively you can initialize it with #G_STATIC_MUTEX_INIT.
415 * Deprecated: 2.32: Use g_mutex_init()
418 g_static_mutex_init (GStaticMutex *mutex)
420 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
422 g_return_if_fail (mutex);
427 /* IMPLEMENTATION NOTE:
429 * On some platforms a GStaticMutex is actually a normal GMutex stored
430 * inside of a structure instead of being allocated dynamically. We can
431 * only do this for platforms on which we know, in advance, how to
432 * allocate (size) and initialise (value) that memory.
434 * On other platforms, a GStaticMutex is nothing more than a pointer to
435 * a GMutex. In that case, the first access we make to the static mutex
436 * must first allocate the normal GMutex and store it into the pointer.
438 * configure.ac writes macros into glibconfig.h to determine if
439 * g_static_mutex_get_mutex() accesses the structure in memory directly
440 * (on platforms where we are able to do that) or if it ends up here,
441 * where we may have to allocate the GMutex before returning it.
445 * g_static_mutex_get_mutex:
446 * @mutex: a #GStaticMutex.
447 * @Returns: the #GMutex corresponding to @mutex.
449 * For some operations (like g_cond_wait()) you must have a #GMutex
450 * instead of a #GStaticMutex. This function will return the
451 * corresponding #GMutex for @mutex.
453 * Deprecated: 2.32: Just use a #GMutex
456 g_static_mutex_get_mutex_impl (GMutex** mutex)
460 if (!g_thread_supported ())
463 result = g_atomic_pointer_get (mutex);
467 g_mutex_lock (&g_once_mutex);
472 result = g_mutex_new ();
473 g_atomic_pointer_set (mutex, result);
476 g_mutex_unlock (&g_once_mutex);
482 /* IMPLEMENTATION NOTE:
484 * g_static_mutex_lock(), g_static_mutex_trylock() and
485 * g_static_mutex_unlock() are all preprocessor macros that wrap the
486 * corresponding g_mutex_*() function around a call to
487 * g_static_mutex_get_mutex().
491 * g_static_mutex_lock:
492 * @mutex: a #GStaticMutex.
494 * Works like g_mutex_lock(), but for a #GStaticMutex.
496 * Deprecated: 2.32: Use g_mutex_lock()
500 * g_static_mutex_trylock:
501 * @mutex: a #GStaticMutex.
502 * @Returns: %TRUE, if the #GStaticMutex could be locked.
504 * Works like g_mutex_trylock(), but for a #GStaticMutex.
506 * Deprecated: 2.32: Use g_mutex_trylock()
510 * g_static_mutex_unlock:
511 * @mutex: a #GStaticMutex.
513 * Works like g_mutex_unlock(), but for a #GStaticMutex.
515 * Deprecated: 2.32: Use g_mutex_unlock()
519 * g_static_mutex_free:
520 * @mutex: a #GStaticMutex to be freed.
522 * Releases all resources allocated to @mutex.
524 * You don't have to call this functions for a #GStaticMutex with an
525 * unbounded lifetime, i.e. objects declared 'static', but if you have
526 * a #GStaticMutex as a member of a structure and the structure is
527 * freed, you should also free the #GStaticMutex.
529 * <note><para>Calling g_static_mutex_free() on a locked mutex may
530 * result in undefined behaviour.</para></note>
532 * Deprecated: 2.32: Use g_mutex_free()
535 g_static_mutex_free (GStaticMutex* mutex)
537 GMutex **runtime_mutex;
539 g_return_if_fail (mutex);
541 /* The runtime_mutex is the first (or only) member of GStaticMutex,
542 * see both versions (of glibconfig.h) in configure.ac. Note, that
543 * this variable is NULL, if g_thread_init() hasn't been called or
544 * if we're using the default thread implementation and it provides
546 runtime_mutex = ((GMutex**)mutex);
549 g_mutex_free (*runtime_mutex);
551 *runtime_mutex = NULL;
554 /* {{{1 GStaticRecMutex */
559 * A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
560 * multiple times by one thread. If you enter it n times, you have to
561 * unlock it n times again to let other threads lock it. An exception
562 * is the function g_static_rec_mutex_unlock_full(): that allows you to
563 * unlock a #GStaticRecMutex completely returning the depth, (i.e. the
564 * number of times this mutex was locked). The depth can later be used
565 * to restore the state of the #GStaticRecMutex by calling
566 * g_static_rec_mutex_lock_full(). In GLib 2.32, #GStaticRecMutex has
567 * been deprecated in favor of #GRecMutex.
569 * Even though #GStaticRecMutex is not opaque, it should only be used
570 * with the following functions.
572 * All of the <function>g_static_rec_mutex_*</function> functions can
573 * be used even if g_thread_init() has not been called. Then they do
574 * nothing, apart from <function>g_static_rec_mutex_trylock</function>,
575 * which does nothing but returning %TRUE.
579 * G_STATIC_REC_MUTEX_INIT:
581 * A #GStaticRecMutex must be initialized with this macro before it can
582 * be used. This macro can used be to initialize a variable, but it
583 * cannot be assigned to a variable. In that case you have to use
584 * g_static_rec_mutex_init().
587 * GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
592 * g_static_rec_mutex_init:
593 * @mutex: a #GStaticRecMutex to be initialized.
595 * A #GStaticRecMutex must be initialized with this function before it
596 * can be used. Alternatively you can initialize it with
597 * #G_STATIC_REC_MUTEX_INIT.
599 * Deprecated: 2.32: Use g_rec_mutex_init()
602 g_static_rec_mutex_init (GStaticRecMutex *mutex)
604 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
606 g_return_if_fail (mutex);
612 * g_static_rec_mutex_lock:
613 * @mutex: a #GStaticRecMutex to lock.
615 * Locks @mutex. If @mutex is already locked by another thread, the
616 * current thread will block until @mutex is unlocked by the other
617 * thread. If @mutex is already locked by the calling thread, this
618 * functions increases the depth of @mutex and returns immediately.
620 * Deprecated: 2.32: Use g_rec_mutex_lock()
623 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
627 g_return_if_fail (mutex);
629 if (!g_thread_supported ())
632 g_system_thread_self (&self);
634 if (g_system_thread_equal (&self, &mutex->owner))
639 g_static_mutex_lock (&mutex->mutex);
640 g_system_thread_assign (mutex->owner, self);
645 * g_static_rec_mutex_trylock:
646 * @mutex: a #GStaticRecMutex to lock.
647 * @Returns: %TRUE, if @mutex could be locked.
649 * Tries to lock @mutex. If @mutex is already locked by another thread,
650 * it immediately returns %FALSE. Otherwise it locks @mutex and returns
651 * %TRUE. If @mutex is already locked by the calling thread, this
652 * functions increases the depth of @mutex and immediately returns
655 * Deprecated: 2.32: Use g_rec_mutex_trylock()
658 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
662 g_return_val_if_fail (mutex, FALSE);
664 if (!g_thread_supported ())
667 g_system_thread_self (&self);
669 if (g_system_thread_equal (&self, &mutex->owner))
675 if (!g_static_mutex_trylock (&mutex->mutex))
678 g_system_thread_assign (mutex->owner, self);
684 * g_static_rec_mutex_unlock:
685 * @mutex: a #GStaticRecMutex to unlock.
687 * Unlocks @mutex. Another thread will be allowed to lock @mutex only
688 * when it has been unlocked as many times as it had been locked
689 * before. If @mutex is completely unlocked and another thread is
690 * blocked in a g_static_rec_mutex_lock() call for @mutex, it will be
691 * woken and can lock @mutex itself.
693 * Deprecated: 2.32: Use g_rec_mutex_unlock()
696 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
698 g_return_if_fail (mutex);
700 if (!g_thread_supported ())
703 if (mutex->depth > 1)
708 g_system_thread_assign (mutex->owner, zero_thread);
709 g_static_mutex_unlock (&mutex->mutex);
713 * g_static_rec_mutex_lock_full:
714 * @mutex: a #GStaticRecMutex to lock.
715 * @depth: number of times this mutex has to be unlocked to be
716 * completely unlocked.
718 * Works like calling g_static_rec_mutex_lock() for @mutex @depth times.
720 * Deprecated: 2.32: Use g_rec_mutex_lock()
723 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
727 g_return_if_fail (mutex);
729 if (!g_thread_supported ())
735 g_system_thread_self (&self);
737 if (g_system_thread_equal (&self, &mutex->owner))
739 mutex->depth += depth;
742 g_static_mutex_lock (&mutex->mutex);
743 g_system_thread_assign (mutex->owner, self);
744 mutex->depth = depth;
748 * g_static_rec_mutex_unlock_full:
749 * @mutex: a #GStaticRecMutex to completely unlock.
750 * @Returns: number of times @mutex has been locked by the current
753 * Completely unlocks @mutex. If another thread is blocked in a
754 * g_static_rec_mutex_lock() call for @mutex, it will be woken and can
755 * lock @mutex itself. This function returns the number of times that
756 * @mutex has been locked by the current thread. To restore the state
757 * before the call to g_static_rec_mutex_unlock_full() you can call
758 * g_static_rec_mutex_lock_full() with the depth returned by this
761 * Deprecated: 2.32: Use g_rec_mutex_unlock()
764 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
768 g_return_val_if_fail (mutex, 0);
770 if (!g_thread_supported ())
773 depth = mutex->depth;
775 g_system_thread_assign (mutex->owner, zero_thread);
777 g_static_mutex_unlock (&mutex->mutex);
783 * g_static_rec_mutex_free:
784 * @mutex: a #GStaticRecMutex to be freed.
786 * Releases all resources allocated to a #GStaticRecMutex.
788 * You don't have to call this functions for a #GStaticRecMutex with an
789 * unbounded lifetime, i.e. objects declared 'static', but if you have
790 * a #GStaticRecMutex as a member of a structure and the structure is
791 * freed, you should also free the #GStaticRecMutex.
793 * Deprecated: 2.32: Use g_rec_mutex_clear()
796 g_static_rec_mutex_free (GStaticRecMutex *mutex)
798 g_return_if_fail (mutex);
800 g_static_mutex_free (&mutex->mutex);
803 /* GStaticRWLock {{{1 ----------------------------------------------------- */
808 * The #GStaticRWLock struct represents a read-write lock. A read-write
809 * lock can be used for protecting data that some portions of code only
810 * read from, while others also write. In such situations it is
811 * desirable that several readers can read at once, whereas of course
812 * only one writer may write at a time. Take a look at the following
816 * <title>An array with access functions</title>
818 * GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
822 * my_array_get (guint index)
824 * gpointer retval = NULL;
829 * g_static_rw_lock_reader_lock (&rwlock);
830 * if (index < array->len)
831 * retval = g_ptr_array_index (array, index);
832 * g_static_rw_lock_reader_unlock (&rwlock);
838 * my_array_set (guint index, gpointer data)
840 * g_static_rw_lock_writer_lock (&rwlock);
843 * array = g_ptr_array_new (<!-- -->);
845 * if (index >= array->len)
846 * g_ptr_array_set_size (array, index+1);
847 * g_ptr_array_index (array, index) = data;
849 * g_static_rw_lock_writer_unlock (&rwlock);
854 * This example shows an array which can be accessed by many readers
855 * (the <function>my_array_get()</function> function) simultaneously,
856 * whereas the writers (the <function>my_array_set()</function>
857 * function) will only be allowed once at a time and only if no readers
858 * currently access the array. This is because of the potentially
859 * dangerous resizing of the array. Using these functions is fully
860 * multi-thread safe now.
862 * Most of the time, writers should have precedence over readers. That
863 * means, for this implementation, that as soon as a writer wants to
864 * lock the data, no other reader is allowed to lock the data, whereas,
865 * of course, the readers that already have locked the data are allowed
866 * to finish their operation. As soon as the last reader unlocks the
867 * data, the writer will lock it.
869 * Even though #GStaticRWLock is not opaque, it should only be used
870 * with the following functions.
872 * All of the <function>g_static_rw_lock_*</function> functions can be
873 * used even if g_thread_init() has not been called. Then they do
874 * nothing, apart from <function>g_static_rw_lock_*_trylock</function>,
875 * which does nothing but returning %TRUE.
877 * <note><para>A read-write lock has a higher overhead than a mutex. For
878 * example, both g_static_rw_lock_reader_lock() and
879 * g_static_rw_lock_reader_unlock() have to lock and unlock a
880 * #GStaticMutex, so it takes at least twice the time to lock and unlock
881 * a #GStaticRWLock that it does to lock and unlock a #GStaticMutex. So
882 * only data structures that are accessed by multiple readers, and which
883 * keep the lock for a considerable time justify a #GStaticRWLock. The
884 * above example most probably would fare better with a
885 * #GStaticMutex.</para></note>
887 * Deprecated: 2.32: Use a #GRWLock instead
891 * G_STATIC_RW_LOCK_INIT:
893 * A #GStaticRWLock must be initialized with this macro before it can
894 * be used. This macro can used be to initialize a variable, but it
895 * cannot be assigned to a variable. In that case you have to use
896 * g_static_rw_lock_init().
899 * GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
904 * g_static_rw_lock_init:
905 * @lock: a #GStaticRWLock to be initialized.
907 * A #GStaticRWLock must be initialized with this function before it
908 * can be used. Alternatively you can initialize it with
909 * #G_STATIC_RW_LOCK_INIT.
911 * Deprecated: 2.32: Use g_rw_lock_init() instead
914 g_static_rw_lock_init (GStaticRWLock* lock)
916 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
918 g_return_if_fail (lock);
924 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
927 *cond = g_cond_new ();
928 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
932 g_static_rw_lock_signal (GStaticRWLock* lock)
934 if (lock->want_to_write && lock->write_cond)
935 g_cond_signal (lock->write_cond);
936 else if (lock->want_to_read && lock->read_cond)
937 g_cond_broadcast (lock->read_cond);
941 * g_static_rw_lock_reader_lock:
942 * @lock: a #GStaticRWLock to lock for reading.
944 * Locks @lock for reading. There may be unlimited concurrent locks for
945 * reading of a #GStaticRWLock at the same time. If @lock is already
946 * locked for writing by another thread or if another thread is already
947 * waiting to lock @lock for writing, this function will block until
948 * @lock is unlocked by the other writing thread and no other writing
949 * threads want to lock @lock. This lock has to be unlocked by
950 * g_static_rw_lock_reader_unlock().
952 * #GStaticRWLock is not recursive. It might seem to be possible to
953 * recursively lock for reading, but that can result in a deadlock, due
954 * to writer preference.
956 * Deprecated: 2.32: Use g_rw_lock_reader_lock() instead
959 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
961 g_return_if_fail (lock);
963 if (!g_threads_got_initialized)
966 g_static_mutex_lock (&lock->mutex);
967 lock->want_to_read++;
968 while (lock->have_writer || lock->want_to_write)
969 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
970 lock->want_to_read--;
971 lock->read_counter++;
972 g_static_mutex_unlock (&lock->mutex);
976 * g_static_rw_lock_reader_trylock:
977 * @lock: a #GStaticRWLock to lock for reading.
978 * @Returns: %TRUE, if @lock could be locked for reading.
980 * Tries to lock @lock for reading. If @lock is already locked for
981 * writing by another thread or if another thread is already waiting to
982 * lock @lock for writing, immediately returns %FALSE. Otherwise locks
983 * @lock for reading and returns %TRUE. This lock has to be unlocked by
984 * g_static_rw_lock_reader_unlock().
986 * Deprectated: 2.32: Use g_rw_lock_reader_trylock() instead
989 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
991 gboolean ret_val = FALSE;
993 g_return_val_if_fail (lock, FALSE);
995 if (!g_threads_got_initialized)
998 g_static_mutex_lock (&lock->mutex);
999 if (!lock->have_writer && !lock->want_to_write)
1001 lock->read_counter++;
1004 g_static_mutex_unlock (&lock->mutex);
1009 * g_static_rw_lock_reader_unlock:
1010 * @lock: a #GStaticRWLock to unlock after reading.
1012 * Unlocks @lock. If a thread waits to lock @lock for writing and all
1013 * locks for reading have been unlocked, the waiting thread is woken up
1014 * and can lock @lock for writing.
1016 * Deprectated: 2.32: Use g_rw_lock_reader_unlock() instead
1019 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
1021 g_return_if_fail (lock);
1023 if (!g_threads_got_initialized)
1026 g_static_mutex_lock (&lock->mutex);
1027 lock->read_counter--;
1028 if (lock->read_counter == 0)
1029 g_static_rw_lock_signal (lock);
1030 g_static_mutex_unlock (&lock->mutex);
1034 * g_static_rw_lock_writer_lock:
1035 * @lock: a #GStaticRWLock to lock for writing.
1037 * Locks @lock for writing. If @lock is already locked for writing or
1038 * reading by other threads, this function will block until @lock is
1039 * completely unlocked and then lock @lock for writing. While this
1040 * functions waits to lock @lock, no other thread can lock @lock for
1041 * reading. When @lock is locked for writing, no other thread can lock
1042 * @lock (neither for reading nor writing). This lock has to be
1043 * unlocked by g_static_rw_lock_writer_unlock().
1045 * Deprectated: 2.32: Use g_rw_lock_writer_lock() instead
1048 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
1050 g_return_if_fail (lock);
1052 if (!g_threads_got_initialized)
1055 g_static_mutex_lock (&lock->mutex);
1056 lock->want_to_write++;
1057 while (lock->have_writer || lock->read_counter)
1058 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
1059 lock->want_to_write--;
1060 lock->have_writer = TRUE;
1061 g_static_mutex_unlock (&lock->mutex);
1065 * g_static_rw_lock_writer_trylock:
1066 * @lock: a #GStaticRWLock to lock for writing.
1067 * @Returns: %TRUE, if @lock could be locked for writing.
1069 * Tries to lock @lock for writing. If @lock is already locked (for
1070 * either reading or writing) by another thread, it immediately returns
1071 * %FALSE. Otherwise it locks @lock for writing and returns %TRUE. This
1072 * lock has to be unlocked by g_static_rw_lock_writer_unlock().
1074 * Deprectated: 2.32: Use g_rw_lock_writer_trylock() instead
1077 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
1079 gboolean ret_val = FALSE;
1081 g_return_val_if_fail (lock, FALSE);
1083 if (!g_threads_got_initialized)
1086 g_static_mutex_lock (&lock->mutex);
1087 if (!lock->have_writer && !lock->read_counter)
1089 lock->have_writer = TRUE;
1092 g_static_mutex_unlock (&lock->mutex);
1097 * g_static_rw_lock_writer_unlock:
1098 * @lock: a #GStaticRWLock to unlock after writing.
1100 * Unlocks @lock. If a thread is waiting to lock @lock for writing and
1101 * all locks for reading have been unlocked, the waiting thread is
1102 * woken up and can lock @lock for writing. If no thread is waiting to
1103 * lock @lock for writing, and some thread or threads are waiting to
1104 * lock @lock for reading, the waiting threads are woken up and can
1105 * lock @lock for reading.
1107 * Deprectated: 2.32: Use g_rw_lock_writer_unlock() instead
1110 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
1112 g_return_if_fail (lock);
1114 if (!g_threads_got_initialized)
1117 g_static_mutex_lock (&lock->mutex);
1118 lock->have_writer = FALSE;
1119 g_static_rw_lock_signal (lock);
1120 g_static_mutex_unlock (&lock->mutex);
1124 * g_static_rw_lock_free:
1125 * @lock: a #GStaticRWLock to be freed.
1127 * Releases all resources allocated to @lock.
1129 * You don't have to call this functions for a #GStaticRWLock with an
1130 * unbounded lifetime, i.e. objects declared 'static', but if you have
1131 * a #GStaticRWLock as a member of a structure, and the structure is
1132 * freed, you should also free the #GStaticRWLock.
1134 * Deprecated: 2.32: Use a #GRWLock instead
1137 g_static_rw_lock_free (GStaticRWLock* lock)
1139 g_return_if_fail (lock);
1141 if (lock->read_cond)
1143 g_cond_free (lock->read_cond);
1144 lock->read_cond = NULL;
1146 if (lock->write_cond)
1148 g_cond_free (lock->write_cond);
1149 lock->write_cond = NULL;
1151 g_static_mutex_free (&lock->mutex);
1154 /* GPrivate {{{1 ------------------------------------------------------ */
1158 * @notify: a #GDestroyNotify
1160 * Deprecated:2.32: dynamic allocation of #GPrivate is a bad idea. Use
1161 * static storage and G_PRIVATE_INIT() instead.
1163 * Returns: a newly allocated #GPrivate (which can never be destroyed)
1166 g_private_new (GDestroyNotify notify)
1168 GPrivate tmp = G_PRIVATE_INIT (notify);
1171 key = g_slice_new (GPrivate);
1177 /* {{{1 GStaticPrivate */
1179 typedef struct _GStaticPrivateNode GStaticPrivateNode;
1180 struct _GStaticPrivateNode
1183 GDestroyNotify destroy;
1184 GStaticPrivate *owner;
1190 * A #GStaticPrivate works almost like a #GPrivate, but it has one
1191 * significant advantage. It doesn't need to be created at run-time
1192 * like a #GPrivate, but can be defined at compile-time. This is
1193 * similar to the difference between #GMutex and #GStaticMutex. Now
1194 * look at our <function>give_me_next_number()</function> example with
1198 * <title>Using GStaticPrivate for per-thread data</title>
1201 * give_me_next_number (<!-- -->)
1203 * static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1204 * int *current_number = g_static_private_get (&current_number_key);
1206 * if (!current_number)
1208 * current_number = g_new (int,1);
1209 * *current_number = 0;
1210 * g_static_private_set (&current_number_key, current_number, g_free);
1213 * *current_number = calc_next_number (*current_number);
1215 * return *current_number;
1222 * G_STATIC_PRIVATE_INIT:
1224 * Every #GStaticPrivate must be initialized with this macro, before it
1228 * GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1233 * g_static_private_init:
1234 * @private_key: a #GStaticPrivate to be initialized
1236 * Initializes @private_key. Alternatively you can initialize it with
1237 * #G_STATIC_PRIVATE_INIT.
1240 g_static_private_init (GStaticPrivate *private_key)
1242 private_key->index = 0;
1246 * g_static_private_get:
1247 * @private_key: a #GStaticPrivate
1249 * Works like g_private_get() only for a #GStaticPrivate.
1251 * This function works even if g_thread_init() has not yet been called.
1253 * Returns: the corresponding pointer
1256 g_static_private_get (GStaticPrivate *private_key)
1258 GRealThread *self = (GRealThread*) g_thread_self ();
1260 gpointer ret = NULL;
1261 array = self->private_data;
1263 if (array && private_key->index != 0 && private_key->index <= array->len)
1265 GStaticPrivateNode *node;
1267 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1269 /* Deal with the possibility that the GStaticPrivate which used
1270 * to have this index got freed and the index got allocated to
1271 * a new one. In this case, the data in the node is stale, so
1272 * free it and return NULL.
1274 if (G_UNLIKELY (node->owner != private_key))
1277 node->destroy (node->data);
1278 node->destroy = NULL;
1289 * g_static_private_set:
1290 * @private_key: a #GStaticPrivate
1291 * @data: the new pointer
1292 * @notify: a function to be called with the pointer whenever the
1293 * current thread ends or sets this pointer again
1295 * Sets the pointer keyed to @private_key for the current thread and
1296 * the function @notify to be called with that pointer (%NULL or
1297 * non-%NULL), whenever the pointer is set again or whenever the
1298 * current thread ends.
1300 * This function works even if g_thread_init() has not yet been called.
1301 * If g_thread_init() is called later, the @data keyed to @private_key
1302 * will be inherited only by the main thread, i.e. the one that called
1305 * <note><para>@notify is used quite differently from @destructor in
1306 * g_private_new().</para></note>
1309 g_static_private_set (GStaticPrivate *private_key,
1311 GDestroyNotify notify)
1313 GRealThread *self = (GRealThread*) g_thread_self ();
1315 static guint next_index = 0;
1316 GStaticPrivateNode *node;
1318 if (!private_key->index)
1322 if (!private_key->index)
1324 if (g_thread_free_indices)
1326 private_key->index = GPOINTER_TO_UINT (g_thread_free_indices->data);
1327 g_thread_free_indices = g_slist_delete_link (g_thread_free_indices,
1328 g_thread_free_indices);
1331 private_key->index = ++next_index;
1334 G_UNLOCK (g_thread);
1337 array = self->private_data;
1340 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
1341 self->private_data = array;
1343 if (private_key->index > array->len)
1344 g_array_set_size (array, private_key->index);
1346 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1349 node->destroy (node->data);
1352 node->destroy = notify;
1353 node->owner = private_key;
1357 * g_static_private_free:
1358 * @private_key: a #GStaticPrivate to be freed
1360 * Releases all resources allocated to @private_key.
1362 * You don't have to call this functions for a #GStaticPrivate with an
1363 * unbounded lifetime, i.e. objects declared 'static', but if you have
1364 * a #GStaticPrivate as a member of a structure and the structure is
1365 * freed, you should also free the #GStaticPrivate.
1368 g_static_private_free (GStaticPrivate *private_key)
1370 guint idx = private_key->index;
1375 private_key->index = 0;
1377 /* Freeing the per-thread data is deferred to either the
1378 * thread end or the next g_static_private_get() call for
1382 g_thread_free_indices = g_slist_prepend (g_thread_free_indices,
1383 GUINT_TO_POINTER (idx));
1384 G_UNLOCK (g_thread);
1388 g_static_private_cleanup (GRealThread *thread)
1392 array = thread->private_data;
1393 thread->private_data = NULL;
1399 for (i = 0; i < array->len; i++ )
1401 GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
1403 node->destroy (node->data);
1405 g_array_free (array, TRUE);
1410 /* vim: set foldmethod=marker: */