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 /* Internal variables {{{1 */
114 static GRealThread *g_thread_all_threads = NULL;
115 static GSList *g_thread_free_indices = NULL;
117 /* Protects g_thread_all_threads and g_thread_free_indices */
118 G_LOCK_DEFINE_STATIC (g_thread);
120 /* Misc. GThread functions {{{1 */
123 * g_thread_set_priority:
124 * @thread: a #GThread.
127 * This function does nothing.
129 * Deprecated:2.32: Thread priorities no longer have any effect.
132 g_thread_set_priority (GThread *thread,
133 GThreadPriority priority)
139 * @func: a function to execute in the new thread
140 * @data: an argument to supply to the new thread
141 * @joinable: should this thread be joinable?
142 * @error: return location for error, or %NULL
144 * This function creates a new thread.
146 * If @joinable is %TRUE, you can wait for this threads termination
147 * calling g_thread_join(). Otherwise the thread will just disappear
148 * when it terminates.
150 * The new thread executes the function @func with the argument @data.
151 * If the thread was created successfully, it is returned.
153 * @error can be %NULL to ignore errors, or non-%NULL to report errors.
154 * The error is set, if and only if the function returns %NULL.
156 * Returns: the new #GThread on success
158 * Deprecated:2.32: Use g_thread_new() instead
161 g_thread_create (GThreadFunc func,
166 return g_thread_new_internal (NULL, func, data, joinable, 0, TRUE, error);
170 * g_thread_create_full:
171 * @func: a function to execute in the new thread.
172 * @data: an argument to supply to the new thread.
173 * @stack_size: a stack size for the new thread.
174 * @joinable: should this thread be joinable?
177 * @error: return location for error.
178 * @Returns: the new #GThread on success.
180 * This function creates a new thread.
182 * Deprecated:2.32: The @bound and @priority arguments are now ignored.
183 * Use g_thread_new() or g_thread_new_full() instead.
186 g_thread_create_full (GThreadFunc func,
191 GThreadPriority priority,
194 return g_thread_new_internal (NULL, func, data, joinable, stack_size, TRUE, error);
199 * @thread_func: function to call for all #GThread structures
200 * @user_data: second argument to @thread_func
202 * Call @thread_func on all #GThreads that have been
203 * created with g_thread_create().
205 * Note that threads may decide to exit while @thread_func is
206 * running, so without intimate knowledge about the lifetime of
207 * foreign threads, @thread_func shouldn't access the GThread*
208 * pointer passed in as first argument. However, @thread_func will
209 * not be called for threads which are known to have exited already.
211 * Due to thread lifetime checks, this function has an execution complexity
212 * which is quadratic in the number of existing threads.
216 * Deprecated:2.32: There aren't many things you can do with a #GThread,
217 * except comparing it with one that was returned from g_thread_create().
218 * There are better ways to find out if your thread is still alive.
221 g_thread_foreach (GFunc thread_func,
224 GSList *slist = NULL;
226 g_return_if_fail (thread_func != NULL);
227 /* snapshot the list of threads for iteration */
229 for (thread = g_thread_all_threads; thread; thread = thread->next)
230 slist = g_slist_prepend (slist, thread);
232 /* walk the list, skipping non-existent threads */
235 GSList *node = slist;
237 /* check whether the current thread still exists */
239 for (thread = g_thread_all_threads; thread; thread = thread->next)
240 if (thread == node->data)
244 thread_func (thread, user_data);
245 g_slist_free_1 (node);
250 g_enumerable_thread_add (GRealThread *thread)
253 thread->next = g_thread_all_threads;
254 g_thread_all_threads = thread;
259 g_enumerable_thread_remove (GRealThread *thread)
264 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
271 g_thread_all_threads = t->next;
278 /* GOnce {{{1 ------------------------------------------------------------- */
280 g_once_init_enter_impl (volatile gsize *location)
282 return (g_once_init_enter) (location);
285 /* GStaticMutex {{{1 ------------------------------------------------------ */
290 * A #GStaticMutex works like a #GMutex.
292 * Prior to GLib 2.32, GStaticMutex had the significant advantage
293 * that it doesn't need to be created at run-time, but can be defined
294 * at compile-time. Since 2.32, #GMutex can be statically allocated
295 * as well, and GStaticMutex has been deprecated.
297 * Here is a version of our give_me_next_number() example using
302 * Using <structname>GStaticMutex</structname>
303 * to simplify thread-safe programming
307 * give_me_next_number (void)
309 * static int current_number = 0;
311 * static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
313 * g_static_mutex_lock (&mutex);
314 * ret_val = current_number = calc_next_number (current_number);
315 * g_static_mutex_unlock (&mutex);
322 * Sometimes you would like to dynamically create a mutex. If you don't
323 * want to require prior calling to g_thread_init(), because your code
324 * should also be usable in non-threaded programs, you are not able to
325 * use g_mutex_new() and thus #GMutex, as that requires a prior call to
326 * g_thread_init(). In theses cases you can also use a #GStaticMutex.
327 * It must be initialized with g_static_mutex_init() before using it
328 * and freed with with g_static_mutex_free() when not needed anymore to
329 * free up any allocated resources.
331 * Even though #GStaticMutex is not opaque, it should only be used with
332 * the following functions, as it is defined differently on different
335 * All of the <function>g_static_mutex_*</function> functions apart
336 * from <function>g_static_mutex_get_mutex</function> can also be used
337 * even if g_thread_init() has not yet been called. Then they do
338 * nothing, apart from <function>g_static_mutex_trylock</function>,
339 * which does nothing but returning %TRUE.
341 * <note><para>All of the <function>g_static_mutex_*</function>
342 * functions are actually macros. Apart from taking their addresses, you
343 * can however use them as if they were functions.</para></note>
347 * G_STATIC_MUTEX_INIT:
349 * A #GStaticMutex must be initialized with this macro, before it can
350 * be used. This macro can used be to initialize a variable, but it
351 * cannot be assigned to a variable. In that case you have to use
352 * g_static_mutex_init().
355 * GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
360 * g_static_mutex_init:
361 * @mutex: a #GStaticMutex to be initialized.
363 * Initializes @mutex.
364 * Alternatively you can initialize it with #G_STATIC_MUTEX_INIT.
366 * Deprecated: 2.32: Use g_mutex_init()
369 g_static_mutex_init (GStaticMutex *mutex)
371 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
373 g_return_if_fail (mutex);
378 /* IMPLEMENTATION NOTE:
380 * On some platforms a GStaticMutex is actually a normal GMutex stored
381 * inside of a structure instead of being allocated dynamically. We can
382 * only do this for platforms on which we know, in advance, how to
383 * allocate (size) and initialise (value) that memory.
385 * On other platforms, a GStaticMutex is nothing more than a pointer to
386 * a GMutex. In that case, the first access we make to the static mutex
387 * must first allocate the normal GMutex and store it into the pointer.
389 * configure.ac writes macros into glibconfig.h to determine if
390 * g_static_mutex_get_mutex() accesses the structure in memory directly
391 * (on platforms where we are able to do that) or if it ends up here,
392 * where we may have to allocate the GMutex before returning it.
396 * g_static_mutex_get_mutex:
397 * @mutex: a #GStaticMutex.
398 * @Returns: the #GMutex corresponding to @mutex.
400 * For some operations (like g_cond_wait()) you must have a #GMutex
401 * instead of a #GStaticMutex. This function will return the
402 * corresponding #GMutex for @mutex.
404 * Deprecated: 2.32: Just use a #GMutex
407 g_static_mutex_get_mutex_impl (GMutex** mutex)
411 if (!g_thread_supported ())
414 result = g_atomic_pointer_get (mutex);
418 g_mutex_lock (&g_once_mutex);
423 result = g_mutex_new ();
424 g_atomic_pointer_set (mutex, result);
427 g_mutex_unlock (&g_once_mutex);
433 /* IMPLEMENTATION NOTE:
435 * g_static_mutex_lock(), g_static_mutex_trylock() and
436 * g_static_mutex_unlock() are all preprocessor macros that wrap the
437 * corresponding g_mutex_*() function around a call to
438 * g_static_mutex_get_mutex().
442 * g_static_mutex_lock:
443 * @mutex: a #GStaticMutex.
445 * Works like g_mutex_lock(), but for a #GStaticMutex.
447 * Deprecated: 2.32: Use g_mutex_lock()
451 * g_static_mutex_trylock:
452 * @mutex: a #GStaticMutex.
453 * @Returns: %TRUE, if the #GStaticMutex could be locked.
455 * Works like g_mutex_trylock(), but for a #GStaticMutex.
457 * Deprecated: 2.32: Use g_mutex_trylock()
461 * g_static_mutex_unlock:
462 * @mutex: a #GStaticMutex.
464 * Works like g_mutex_unlock(), but for a #GStaticMutex.
466 * Deprecated: 2.32: Use g_mutex_unlock()
470 * g_static_mutex_free:
471 * @mutex: a #GStaticMutex to be freed.
473 * Releases all resources allocated to @mutex.
475 * You don't have to call this functions for a #GStaticMutex with an
476 * unbounded lifetime, i.e. objects declared 'static', but if you have
477 * a #GStaticMutex as a member of a structure and the structure is
478 * freed, you should also free the #GStaticMutex.
480 * <note><para>Calling g_static_mutex_free() on a locked mutex may
481 * result in undefined behaviour.</para></note>
483 * Deprecated: 2.32: Use g_mutex_free()
486 g_static_mutex_free (GStaticMutex* mutex)
488 GMutex **runtime_mutex;
490 g_return_if_fail (mutex);
492 /* The runtime_mutex is the first (or only) member of GStaticMutex,
493 * see both versions (of glibconfig.h) in configure.ac. Note, that
494 * this variable is NULL, if g_thread_init() hasn't been called or
495 * if we're using the default thread implementation and it provides
497 runtime_mutex = ((GMutex**)mutex);
500 g_mutex_free (*runtime_mutex);
502 *runtime_mutex = NULL;
505 /* {{{1 GStaticRecMutex */
510 * A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
511 * multiple times by one thread. If you enter it n times, you have to
512 * unlock it n times again to let other threads lock it. An exception
513 * is the function g_static_rec_mutex_unlock_full(): that allows you to
514 * unlock a #GStaticRecMutex completely returning the depth, (i.e. the
515 * number of times this mutex was locked). The depth can later be used
516 * to restore the state of the #GStaticRecMutex by calling
517 * g_static_rec_mutex_lock_full(). In GLib 2.32, #GStaticRecMutex has
518 * been deprecated in favor of #GRecMutex.
520 * Even though #GStaticRecMutex is not opaque, it should only be used
521 * with the following functions.
523 * All of the <function>g_static_rec_mutex_*</function> functions can
524 * be used even if g_thread_init() has not been called. Then they do
525 * nothing, apart from <function>g_static_rec_mutex_trylock</function>,
526 * which does nothing but returning %TRUE.
530 * G_STATIC_REC_MUTEX_INIT:
532 * A #GStaticRecMutex must be initialized with this macro before it can
533 * be used. This macro can used be to initialize a variable, but it
534 * cannot be assigned to a variable. In that case you have to use
535 * g_static_rec_mutex_init().
538 * GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
543 * g_static_rec_mutex_init:
544 * @mutex: a #GStaticRecMutex to be initialized.
546 * A #GStaticRecMutex must be initialized with this function before it
547 * can be used. Alternatively you can initialize it with
548 * #G_STATIC_REC_MUTEX_INIT.
550 * Deprecated: 2.32: Use g_rec_mutex_init()
553 g_static_rec_mutex_init (GStaticRecMutex *mutex)
555 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
557 g_return_if_fail (mutex);
563 * g_static_rec_mutex_lock:
564 * @mutex: a #GStaticRecMutex to lock.
566 * Locks @mutex. If @mutex is already locked by another thread, the
567 * current thread will block until @mutex is unlocked by the other
568 * thread. If @mutex is already locked by the calling thread, this
569 * functions increases the depth of @mutex and returns immediately.
571 * Deprecated: 2.32: Use g_rec_mutex_lock()
574 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
578 g_return_if_fail (mutex);
580 if (!g_thread_supported ())
583 g_system_thread_self (&self);
585 if (g_system_thread_equal (&self, &mutex->owner))
590 g_static_mutex_lock (&mutex->mutex);
591 g_system_thread_assign (mutex->owner, self);
596 * g_static_rec_mutex_trylock:
597 * @mutex: a #GStaticRecMutex to lock.
598 * @Returns: %TRUE, if @mutex could be locked.
600 * Tries to lock @mutex. If @mutex is already locked by another thread,
601 * it immediately returns %FALSE. Otherwise it locks @mutex and returns
602 * %TRUE. If @mutex is already locked by the calling thread, this
603 * functions increases the depth of @mutex and immediately returns
606 * Deprecated: 2.32: Use g_rec_mutex_trylock()
609 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
613 g_return_val_if_fail (mutex, FALSE);
615 if (!g_thread_supported ())
618 g_system_thread_self (&self);
620 if (g_system_thread_equal (&self, &mutex->owner))
626 if (!g_static_mutex_trylock (&mutex->mutex))
629 g_system_thread_assign (mutex->owner, self);
635 * g_static_rec_mutex_unlock:
636 * @mutex: a #GStaticRecMutex to unlock.
638 * Unlocks @mutex. Another thread will be allowed to lock @mutex only
639 * when it has been unlocked as many times as it had been locked
640 * before. If @mutex is completely unlocked and another thread is
641 * blocked in a g_static_rec_mutex_lock() call for @mutex, it will be
642 * woken and can lock @mutex itself.
644 * Deprecated: 2.32: Use g_rec_mutex_unlock()
647 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
649 g_return_if_fail (mutex);
651 if (!g_thread_supported ())
654 if (mutex->depth > 1)
659 g_system_thread_assign (mutex->owner, zero_thread);
660 g_static_mutex_unlock (&mutex->mutex);
664 * g_static_rec_mutex_lock_full:
665 * @mutex: a #GStaticRecMutex to lock.
666 * @depth: number of times this mutex has to be unlocked to be
667 * completely unlocked.
669 * Works like calling g_static_rec_mutex_lock() for @mutex @depth times.
671 * Deprecated: 2.32: Use g_rec_mutex_lock()
674 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
678 g_return_if_fail (mutex);
680 if (!g_thread_supported ())
686 g_system_thread_self (&self);
688 if (g_system_thread_equal (&self, &mutex->owner))
690 mutex->depth += depth;
693 g_static_mutex_lock (&mutex->mutex);
694 g_system_thread_assign (mutex->owner, self);
695 mutex->depth = depth;
699 * g_static_rec_mutex_unlock_full:
700 * @mutex: a #GStaticRecMutex to completely unlock.
701 * @Returns: number of times @mutex has been locked by the current
704 * Completely unlocks @mutex. If another thread is blocked in a
705 * g_static_rec_mutex_lock() call for @mutex, it will be woken and can
706 * lock @mutex itself. This function returns the number of times that
707 * @mutex has been locked by the current thread. To restore the state
708 * before the call to g_static_rec_mutex_unlock_full() you can call
709 * g_static_rec_mutex_lock_full() with the depth returned by this
712 * Deprecated: 2.32: Use g_rec_mutex_unlock()
715 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
719 g_return_val_if_fail (mutex, 0);
721 if (!g_thread_supported ())
724 depth = mutex->depth;
726 g_system_thread_assign (mutex->owner, zero_thread);
728 g_static_mutex_unlock (&mutex->mutex);
734 * g_static_rec_mutex_free:
735 * @mutex: a #GStaticRecMutex to be freed.
737 * Releases all resources allocated to a #GStaticRecMutex.
739 * You don't have to call this functions for a #GStaticRecMutex with an
740 * unbounded lifetime, i.e. objects declared 'static', but if you have
741 * a #GStaticRecMutex as a member of a structure and the structure is
742 * freed, you should also free the #GStaticRecMutex.
744 * Deprecated: 2.32: Use g_rec_mutex_clear()
747 g_static_rec_mutex_free (GStaticRecMutex *mutex)
749 g_return_if_fail (mutex);
751 g_static_mutex_free (&mutex->mutex);
754 /* GStaticRWLock {{{1 ----------------------------------------------------- */
759 * The #GStaticRWLock struct represents a read-write lock. A read-write
760 * lock can be used for protecting data that some portions of code only
761 * read from, while others also write. In such situations it is
762 * desirable that several readers can read at once, whereas of course
763 * only one writer may write at a time. Take a look at the following
767 * <title>An array with access functions</title>
769 * GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
773 * my_array_get (guint index)
775 * gpointer retval = NULL;
780 * g_static_rw_lock_reader_lock (&rwlock);
781 * if (index < array->len)
782 * retval = g_ptr_array_index (array, index);
783 * g_static_rw_lock_reader_unlock (&rwlock);
789 * my_array_set (guint index, gpointer data)
791 * g_static_rw_lock_writer_lock (&rwlock);
794 * array = g_ptr_array_new (<!-- -->);
796 * if (index >= array->len)
797 * g_ptr_array_set_size (array, index+1);
798 * g_ptr_array_index (array, index) = data;
800 * g_static_rw_lock_writer_unlock (&rwlock);
805 * This example shows an array which can be accessed by many readers
806 * (the <function>my_array_get()</function> function) simultaneously,
807 * whereas the writers (the <function>my_array_set()</function>
808 * function) will only be allowed once at a time and only if no readers
809 * currently access the array. This is because of the potentially
810 * dangerous resizing of the array. Using these functions is fully
811 * multi-thread safe now.
813 * Most of the time, writers should have precedence over readers. That
814 * means, for this implementation, that as soon as a writer wants to
815 * lock the data, no other reader is allowed to lock the data, whereas,
816 * of course, the readers that already have locked the data are allowed
817 * to finish their operation. As soon as the last reader unlocks the
818 * data, the writer will lock it.
820 * Even though #GStaticRWLock is not opaque, it should only be used
821 * with the following functions.
823 * All of the <function>g_static_rw_lock_*</function> functions can be
824 * used even if g_thread_init() has not been called. Then they do
825 * nothing, apart from <function>g_static_rw_lock_*_trylock</function>,
826 * which does nothing but returning %TRUE.
828 * <note><para>A read-write lock has a higher overhead than a mutex. For
829 * example, both g_static_rw_lock_reader_lock() and
830 * g_static_rw_lock_reader_unlock() have to lock and unlock a
831 * #GStaticMutex, so it takes at least twice the time to lock and unlock
832 * a #GStaticRWLock that it does to lock and unlock a #GStaticMutex. So
833 * only data structures that are accessed by multiple readers, and which
834 * keep the lock for a considerable time justify a #GStaticRWLock. The
835 * above example most probably would fare better with a
836 * #GStaticMutex.</para></note>
838 * Deprecated: 2.32: Use a #GRWLock instead
842 * G_STATIC_RW_LOCK_INIT:
844 * A #GStaticRWLock must be initialized with this macro before it can
845 * be used. This macro can used be to initialize a variable, but it
846 * cannot be assigned to a variable. In that case you have to use
847 * g_static_rw_lock_init().
850 * GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
855 * g_static_rw_lock_init:
856 * @lock: a #GStaticRWLock to be initialized.
858 * A #GStaticRWLock must be initialized with this function before it
859 * can be used. Alternatively you can initialize it with
860 * #G_STATIC_RW_LOCK_INIT.
862 * Deprecated: 2.32: Use g_rw_lock_init() instead
865 g_static_rw_lock_init (GStaticRWLock* lock)
867 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
869 g_return_if_fail (lock);
875 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
878 *cond = g_cond_new ();
879 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
883 g_static_rw_lock_signal (GStaticRWLock* lock)
885 if (lock->want_to_write && lock->write_cond)
886 g_cond_signal (lock->write_cond);
887 else if (lock->want_to_read && lock->read_cond)
888 g_cond_broadcast (lock->read_cond);
892 * g_static_rw_lock_reader_lock:
893 * @lock: a #GStaticRWLock to lock for reading.
895 * Locks @lock for reading. There may be unlimited concurrent locks for
896 * reading of a #GStaticRWLock at the same time. If @lock is already
897 * locked for writing by another thread or if another thread is already
898 * waiting to lock @lock for writing, this function will block until
899 * @lock is unlocked by the other writing thread and no other writing
900 * threads want to lock @lock. This lock has to be unlocked by
901 * g_static_rw_lock_reader_unlock().
903 * #GStaticRWLock is not recursive. It might seem to be possible to
904 * recursively lock for reading, but that can result in a deadlock, due
905 * to writer preference.
907 * Deprecated: 2.32: Use g_rw_lock_reader_lock() instead
910 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
912 g_return_if_fail (lock);
914 if (!g_threads_got_initialized)
917 g_static_mutex_lock (&lock->mutex);
918 lock->want_to_read++;
919 while (lock->have_writer || lock->want_to_write)
920 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
921 lock->want_to_read--;
922 lock->read_counter++;
923 g_static_mutex_unlock (&lock->mutex);
927 * g_static_rw_lock_reader_trylock:
928 * @lock: a #GStaticRWLock to lock for reading.
929 * @Returns: %TRUE, if @lock could be locked for reading.
931 * Tries to lock @lock for reading. If @lock is already locked for
932 * writing by another thread or if another thread is already waiting to
933 * lock @lock for writing, immediately returns %FALSE. Otherwise locks
934 * @lock for reading and returns %TRUE. This lock has to be unlocked by
935 * g_static_rw_lock_reader_unlock().
937 * Deprectated: 2.32: Use g_rw_lock_reader_trylock() instead
940 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
942 gboolean ret_val = FALSE;
944 g_return_val_if_fail (lock, FALSE);
946 if (!g_threads_got_initialized)
949 g_static_mutex_lock (&lock->mutex);
950 if (!lock->have_writer && !lock->want_to_write)
952 lock->read_counter++;
955 g_static_mutex_unlock (&lock->mutex);
960 * g_static_rw_lock_reader_unlock:
961 * @lock: a #GStaticRWLock to unlock after reading.
963 * Unlocks @lock. If a thread waits to lock @lock for writing and all
964 * locks for reading have been unlocked, the waiting thread is woken up
965 * and can lock @lock for writing.
967 * Deprectated: 2.32: Use g_rw_lock_reader_unlock() instead
970 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
972 g_return_if_fail (lock);
974 if (!g_threads_got_initialized)
977 g_static_mutex_lock (&lock->mutex);
978 lock->read_counter--;
979 if (lock->read_counter == 0)
980 g_static_rw_lock_signal (lock);
981 g_static_mutex_unlock (&lock->mutex);
985 * g_static_rw_lock_writer_lock:
986 * @lock: a #GStaticRWLock to lock for writing.
988 * Locks @lock for writing. If @lock is already locked for writing or
989 * reading by other threads, this function will block until @lock is
990 * completely unlocked and then lock @lock for writing. While this
991 * functions waits to lock @lock, no other thread can lock @lock for
992 * reading. When @lock is locked for writing, no other thread can lock
993 * @lock (neither for reading nor writing). This lock has to be
994 * unlocked by g_static_rw_lock_writer_unlock().
996 * Deprectated: 2.32: Use g_rw_lock_writer_lock() instead
999 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
1001 g_return_if_fail (lock);
1003 if (!g_threads_got_initialized)
1006 g_static_mutex_lock (&lock->mutex);
1007 lock->want_to_write++;
1008 while (lock->have_writer || lock->read_counter)
1009 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
1010 lock->want_to_write--;
1011 lock->have_writer = TRUE;
1012 g_static_mutex_unlock (&lock->mutex);
1016 * g_static_rw_lock_writer_trylock:
1017 * @lock: a #GStaticRWLock to lock for writing.
1018 * @Returns: %TRUE, if @lock could be locked for writing.
1020 * Tries to lock @lock for writing. If @lock is already locked (for
1021 * either reading or writing) by another thread, it immediately returns
1022 * %FALSE. Otherwise it locks @lock for writing and returns %TRUE. This
1023 * lock has to be unlocked by g_static_rw_lock_writer_unlock().
1025 * Deprectated: 2.32: Use g_rw_lock_writer_trylock() instead
1028 g_static_rw_lock_writer_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->read_counter)
1040 lock->have_writer = TRUE;
1043 g_static_mutex_unlock (&lock->mutex);
1048 * g_static_rw_lock_writer_unlock:
1049 * @lock: a #GStaticRWLock to unlock after writing.
1051 * Unlocks @lock. If a thread is waiting to lock @lock for writing and
1052 * all locks for reading have been unlocked, the waiting thread is
1053 * woken up and can lock @lock for writing. If no thread is waiting to
1054 * lock @lock for writing, and some thread or threads are waiting to
1055 * lock @lock for reading, the waiting threads are woken up and can
1056 * lock @lock for reading.
1058 * Deprectated: 2.32: Use g_rw_lock_writer_unlock() instead
1061 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
1063 g_return_if_fail (lock);
1065 if (!g_threads_got_initialized)
1068 g_static_mutex_lock (&lock->mutex);
1069 lock->have_writer = FALSE;
1070 g_static_rw_lock_signal (lock);
1071 g_static_mutex_unlock (&lock->mutex);
1075 * g_static_rw_lock_free:
1076 * @lock: a #GStaticRWLock to be freed.
1078 * Releases all resources allocated to @lock.
1080 * You don't have to call this functions for a #GStaticRWLock with an
1081 * unbounded lifetime, i.e. objects declared 'static', but if you have
1082 * a #GStaticRWLock as a member of a structure, and the structure is
1083 * freed, you should also free the #GStaticRWLock.
1085 * Deprecated: 2.32: Use a #GRWLock instead
1088 g_static_rw_lock_free (GStaticRWLock* lock)
1090 g_return_if_fail (lock);
1092 if (lock->read_cond)
1094 g_cond_free (lock->read_cond);
1095 lock->read_cond = NULL;
1097 if (lock->write_cond)
1099 g_cond_free (lock->write_cond);
1100 lock->write_cond = NULL;
1102 g_static_mutex_free (&lock->mutex);
1105 /* GPrivate {{{1 ------------------------------------------------------ */
1109 * @notify: a #GDestroyNotify
1111 * Deprecated:2.32: dynamic allocation of #GPrivate is a bad idea. Use
1112 * static storage and G_PRIVATE_INIT() instead.
1114 * Returns: a newly allocated #GPrivate (which can never be destroyed)
1117 g_private_new (GDestroyNotify notify)
1119 GPrivate tmp = G_PRIVATE_INIT (notify);
1122 key = g_slice_new (GPrivate);
1128 /* {{{1 GStaticPrivate */
1130 typedef struct _GStaticPrivateNode GStaticPrivateNode;
1131 struct _GStaticPrivateNode
1134 GDestroyNotify destroy;
1135 GStaticPrivate *owner;
1141 * A #GStaticPrivate works almost like a #GPrivate, but it has one
1142 * significant advantage. It doesn't need to be created at run-time
1143 * like a #GPrivate, but can be defined at compile-time. This is
1144 * similar to the difference between #GMutex and #GStaticMutex. Now
1145 * look at our <function>give_me_next_number()</function> example with
1149 * <title>Using GStaticPrivate for per-thread data</title>
1152 * give_me_next_number (<!-- -->)
1154 * static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1155 * int *current_number = g_static_private_get (&current_number_key);
1157 * if (!current_number)
1159 * current_number = g_new (int,1);
1160 * *current_number = 0;
1161 * g_static_private_set (&current_number_key, current_number, g_free);
1164 * *current_number = calc_next_number (*current_number);
1166 * return *current_number;
1173 * G_STATIC_PRIVATE_INIT:
1175 * Every #GStaticPrivate must be initialized with this macro, before it
1179 * GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1184 * g_static_private_init:
1185 * @private_key: a #GStaticPrivate to be initialized
1187 * Initializes @private_key. Alternatively you can initialize it with
1188 * #G_STATIC_PRIVATE_INIT.
1191 g_static_private_init (GStaticPrivate *private_key)
1193 private_key->index = 0;
1197 * g_static_private_get:
1198 * @private_key: a #GStaticPrivate
1200 * Works like g_private_get() only for a #GStaticPrivate.
1202 * This function works even if g_thread_init() has not yet been called.
1204 * Returns: the corresponding pointer
1207 g_static_private_get (GStaticPrivate *private_key)
1209 GRealThread *self = (GRealThread*) g_thread_self ();
1211 gpointer ret = NULL;
1212 array = self->private_data;
1214 if (array && private_key->index != 0 && private_key->index <= array->len)
1216 GStaticPrivateNode *node;
1218 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1220 /* Deal with the possibility that the GStaticPrivate which used
1221 * to have this index got freed and the index got allocated to
1222 * a new one. In this case, the data in the node is stale, so
1223 * free it and return NULL.
1225 if (G_UNLIKELY (node->owner != private_key))
1228 node->destroy (node->data);
1229 node->destroy = NULL;
1240 * g_static_private_set:
1241 * @private_key: a #GStaticPrivate
1242 * @data: the new pointer
1243 * @notify: a function to be called with the pointer whenever the
1244 * current thread ends or sets this pointer again
1246 * Sets the pointer keyed to @private_key for the current thread and
1247 * the function @notify to be called with that pointer (%NULL or
1248 * non-%NULL), whenever the pointer is set again or whenever the
1249 * current thread ends.
1251 * This function works even if g_thread_init() has not yet been called.
1252 * If g_thread_init() is called later, the @data keyed to @private_key
1253 * will be inherited only by the main thread, i.e. the one that called
1256 * <note><para>@notify is used quite differently from @destructor in
1257 * g_private_new().</para></note>
1260 g_static_private_set (GStaticPrivate *private_key,
1262 GDestroyNotify notify)
1264 GRealThread *self = (GRealThread*) g_thread_self ();
1266 static guint next_index = 0;
1267 GStaticPrivateNode *node;
1269 if (!private_key->index)
1273 if (!private_key->index)
1275 if (g_thread_free_indices)
1277 private_key->index = GPOINTER_TO_UINT (g_thread_free_indices->data);
1278 g_thread_free_indices = g_slist_delete_link (g_thread_free_indices,
1279 g_thread_free_indices);
1282 private_key->index = ++next_index;
1285 G_UNLOCK (g_thread);
1288 array = self->private_data;
1291 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
1292 self->private_data = array;
1294 if (private_key->index > array->len)
1295 g_array_set_size (array, private_key->index);
1297 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1300 node->destroy (node->data);
1303 node->destroy = notify;
1304 node->owner = private_key;
1308 * g_static_private_free:
1309 * @private_key: a #GStaticPrivate to be freed
1311 * Releases all resources allocated to @private_key.
1313 * You don't have to call this functions for a #GStaticPrivate with an
1314 * unbounded lifetime, i.e. objects declared 'static', but if you have
1315 * a #GStaticPrivate as a member of a structure and the structure is
1316 * freed, you should also free the #GStaticPrivate.
1319 g_static_private_free (GStaticPrivate *private_key)
1321 guint idx = private_key->index;
1326 private_key->index = 0;
1328 /* Freeing the per-thread data is deferred to either the
1329 * thread end or the next g_static_private_get() call for
1333 g_thread_free_indices = g_slist_prepend (g_thread_free_indices,
1334 GUINT_TO_POINTER (idx));
1335 G_UNLOCK (g_thread);
1339 g_static_private_cleanup (GRealThread *thread)
1343 array = thread->private_data;
1344 thread->private_data = NULL;
1350 for (i = 0; i < array->len; i++ )
1352 GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
1354 node->destroy (node->data);
1356 g_array_free (array, TRUE);
1361 /* vim: set foldmethod=marker: */