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 /* GStaticMutex {{{1 ------------------------------------------------------ */
283 * A #GStaticMutex works like a #GMutex.
285 * Prior to GLib 2.32, GStaticMutex had the significant advantage
286 * that it doesn't need to be created at run-time, but can be defined
287 * at compile-time. Since 2.32, #GMutex can be statically allocated
288 * as well, and GStaticMutex has been deprecated.
290 * Here is a version of our give_me_next_number() example using
295 * Using <structname>GStaticMutex</structname>
296 * to simplify thread-safe programming
300 * give_me_next_number (void)
302 * static int current_number = 0;
304 * static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
306 * g_static_mutex_lock (&mutex);
307 * ret_val = current_number = calc_next_number (current_number);
308 * g_static_mutex_unlock (&mutex);
315 * Sometimes you would like to dynamically create a mutex. If you don't
316 * want to require prior calling to g_thread_init(), because your code
317 * should also be usable in non-threaded programs, you are not able to
318 * use g_mutex_new() and thus #GMutex, as that requires a prior call to
319 * g_thread_init(). In theses cases you can also use a #GStaticMutex.
320 * It must be initialized with g_static_mutex_init() before using it
321 * and freed with with g_static_mutex_free() when not needed anymore to
322 * free up any allocated resources.
324 * Even though #GStaticMutex is not opaque, it should only be used with
325 * the following functions, as it is defined differently on different
328 * All of the <function>g_static_mutex_*</function> functions apart
329 * from <function>g_static_mutex_get_mutex</function> can also be used
330 * even if g_thread_init() has not yet been called. Then they do
331 * nothing, apart from <function>g_static_mutex_trylock</function>,
332 * which does nothing but returning %TRUE.
334 * <note><para>All of the <function>g_static_mutex_*</function>
335 * functions are actually macros. Apart from taking their addresses, you
336 * can however use them as if they were functions.</para></note>
340 * G_STATIC_MUTEX_INIT:
342 * A #GStaticMutex must be initialized with this macro, before it can
343 * be used. This macro can used be to initialize a variable, but it
344 * cannot be assigned to a variable. In that case you have to use
345 * g_static_mutex_init().
348 * GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
353 * g_static_mutex_init:
354 * @mutex: a #GStaticMutex to be initialized.
356 * Initializes @mutex.
357 * Alternatively you can initialize it with #G_STATIC_MUTEX_INIT.
359 * Deprecated: 2.32: Use g_mutex_init()
362 g_static_mutex_init (GStaticMutex *mutex)
364 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
366 g_return_if_fail (mutex);
371 /* IMPLEMENTATION NOTE:
373 * On some platforms a GStaticMutex is actually a normal GMutex stored
374 * inside of a structure instead of being allocated dynamically. We can
375 * only do this for platforms on which we know, in advance, how to
376 * allocate (size) and initialise (value) that memory.
378 * On other platforms, a GStaticMutex is nothing more than a pointer to
379 * a GMutex. In that case, the first access we make to the static mutex
380 * must first allocate the normal GMutex and store it into the pointer.
382 * configure.ac writes macros into glibconfig.h to determine if
383 * g_static_mutex_get_mutex() accesses the structure in memory directly
384 * (on platforms where we are able to do that) or if it ends up here,
385 * where we may have to allocate the GMutex before returning it.
389 * g_static_mutex_get_mutex:
390 * @mutex: a #GStaticMutex.
391 * @Returns: the #GMutex corresponding to @mutex.
393 * For some operations (like g_cond_wait()) you must have a #GMutex
394 * instead of a #GStaticMutex. This function will return the
395 * corresponding #GMutex for @mutex.
397 * Deprecated: 2.32: Just use a #GMutex
400 g_static_mutex_get_mutex_impl (GMutex** mutex)
404 if (!g_thread_supported ())
407 result = g_atomic_pointer_get (mutex);
411 g_mutex_lock (&g_once_mutex);
416 result = g_mutex_new ();
417 g_atomic_pointer_set (mutex, result);
420 g_mutex_unlock (&g_once_mutex);
426 /* IMPLEMENTATION NOTE:
428 * g_static_mutex_lock(), g_static_mutex_trylock() and
429 * g_static_mutex_unlock() are all preprocessor macros that wrap the
430 * corresponding g_mutex_*() function around a call to
431 * g_static_mutex_get_mutex().
435 * g_static_mutex_lock:
436 * @mutex: a #GStaticMutex.
438 * Works like g_mutex_lock(), but for a #GStaticMutex.
440 * Deprecated: 2.32: Use g_mutex_lock()
444 * g_static_mutex_trylock:
445 * @mutex: a #GStaticMutex.
446 * @Returns: %TRUE, if the #GStaticMutex could be locked.
448 * Works like g_mutex_trylock(), but for a #GStaticMutex.
450 * Deprecated: 2.32: Use g_mutex_trylock()
454 * g_static_mutex_unlock:
455 * @mutex: a #GStaticMutex.
457 * Works like g_mutex_unlock(), but for a #GStaticMutex.
459 * Deprecated: 2.32: Use g_mutex_unlock()
463 * g_static_mutex_free:
464 * @mutex: a #GStaticMutex to be freed.
466 * Releases all resources allocated to @mutex.
468 * You don't have to call this functions for a #GStaticMutex with an
469 * unbounded lifetime, i.e. objects declared 'static', but if you have
470 * a #GStaticMutex as a member of a structure and the structure is
471 * freed, you should also free the #GStaticMutex.
473 * <note><para>Calling g_static_mutex_free() on a locked mutex may
474 * result in undefined behaviour.</para></note>
476 * Deprecated: 2.32: Use g_mutex_free()
479 g_static_mutex_free (GStaticMutex* mutex)
481 GMutex **runtime_mutex;
483 g_return_if_fail (mutex);
485 /* The runtime_mutex is the first (or only) member of GStaticMutex,
486 * see both versions (of glibconfig.h) in configure.ac. Note, that
487 * this variable is NULL, if g_thread_init() hasn't been called or
488 * if we're using the default thread implementation and it provides
490 runtime_mutex = ((GMutex**)mutex);
493 g_mutex_free (*runtime_mutex);
495 *runtime_mutex = NULL;
498 /* {{{1 GStaticRecMutex */
503 * A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
504 * multiple times by one thread. If you enter it n times, you have to
505 * unlock it n times again to let other threads lock it. An exception
506 * is the function g_static_rec_mutex_unlock_full(): that allows you to
507 * unlock a #GStaticRecMutex completely returning the depth, (i.e. the
508 * number of times this mutex was locked). The depth can later be used
509 * to restore the state of the #GStaticRecMutex by calling
510 * g_static_rec_mutex_lock_full(). In GLib 2.32, #GStaticRecMutex has
511 * been deprecated in favor of #GRecMutex.
513 * Even though #GStaticRecMutex is not opaque, it should only be used
514 * with the following functions.
516 * All of the <function>g_static_rec_mutex_*</function> functions can
517 * be used even if g_thread_init() has not been called. Then they do
518 * nothing, apart from <function>g_static_rec_mutex_trylock</function>,
519 * which does nothing but returning %TRUE.
523 * G_STATIC_REC_MUTEX_INIT:
525 * A #GStaticRecMutex must be initialized with this macro before it can
526 * be used. This macro can used be to initialize a variable, but it
527 * cannot be assigned to a variable. In that case you have to use
528 * g_static_rec_mutex_init().
531 * GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
536 * g_static_rec_mutex_init:
537 * @mutex: a #GStaticRecMutex to be initialized.
539 * A #GStaticRecMutex must be initialized with this function before it
540 * can be used. Alternatively you can initialize it with
541 * #G_STATIC_REC_MUTEX_INIT.
543 * Deprecated: 2.32: Use g_rec_mutex_init()
546 g_static_rec_mutex_init (GStaticRecMutex *mutex)
548 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
550 g_return_if_fail (mutex);
556 * g_static_rec_mutex_lock:
557 * @mutex: a #GStaticRecMutex to lock.
559 * Locks @mutex. If @mutex is already locked by another thread, the
560 * current thread will block until @mutex is unlocked by the other
561 * thread. If @mutex is already locked by the calling thread, this
562 * functions increases the depth of @mutex and returns immediately.
564 * Deprecated: 2.32: Use g_rec_mutex_lock()
567 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
571 g_return_if_fail (mutex);
573 if (!g_thread_supported ())
576 g_system_thread_self (&self);
578 if (g_system_thread_equal (&self, &mutex->owner))
583 g_static_mutex_lock (&mutex->mutex);
584 g_system_thread_assign (mutex->owner, self);
589 * g_static_rec_mutex_trylock:
590 * @mutex: a #GStaticRecMutex to lock.
591 * @Returns: %TRUE, if @mutex could be locked.
593 * Tries to lock @mutex. If @mutex is already locked by another thread,
594 * it immediately returns %FALSE. Otherwise it locks @mutex and returns
595 * %TRUE. If @mutex is already locked by the calling thread, this
596 * functions increases the depth of @mutex and immediately returns
599 * Deprecated: 2.32: Use g_rec_mutex_trylock()
602 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
606 g_return_val_if_fail (mutex, FALSE);
608 if (!g_thread_supported ())
611 g_system_thread_self (&self);
613 if (g_system_thread_equal (&self, &mutex->owner))
619 if (!g_static_mutex_trylock (&mutex->mutex))
622 g_system_thread_assign (mutex->owner, self);
628 * g_static_rec_mutex_unlock:
629 * @mutex: a #GStaticRecMutex to unlock.
631 * Unlocks @mutex. Another thread will be allowed to lock @mutex only
632 * when it has been unlocked as many times as it had been locked
633 * before. If @mutex is completely unlocked and another thread is
634 * blocked in a g_static_rec_mutex_lock() call for @mutex, it will be
635 * woken and can lock @mutex itself.
637 * Deprecated: 2.32: Use g_rec_mutex_unlock()
640 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
642 g_return_if_fail (mutex);
644 if (!g_thread_supported ())
647 if (mutex->depth > 1)
652 g_system_thread_assign (mutex->owner, zero_thread);
653 g_static_mutex_unlock (&mutex->mutex);
657 * g_static_rec_mutex_lock_full:
658 * @mutex: a #GStaticRecMutex to lock.
659 * @depth: number of times this mutex has to be unlocked to be
660 * completely unlocked.
662 * Works like calling g_static_rec_mutex_lock() for @mutex @depth times.
664 * Deprecated: 2.32: Use g_rec_mutex_lock()
667 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
671 g_return_if_fail (mutex);
673 if (!g_thread_supported ())
679 g_system_thread_self (&self);
681 if (g_system_thread_equal (&self, &mutex->owner))
683 mutex->depth += depth;
686 g_static_mutex_lock (&mutex->mutex);
687 g_system_thread_assign (mutex->owner, self);
688 mutex->depth = depth;
692 * g_static_rec_mutex_unlock_full:
693 * @mutex: a #GStaticRecMutex to completely unlock.
694 * @Returns: number of times @mutex has been locked by the current
697 * Completely unlocks @mutex. If another thread is blocked in a
698 * g_static_rec_mutex_lock() call for @mutex, it will be woken and can
699 * lock @mutex itself. This function returns the number of times that
700 * @mutex has been locked by the current thread. To restore the state
701 * before the call to g_static_rec_mutex_unlock_full() you can call
702 * g_static_rec_mutex_lock_full() with the depth returned by this
705 * Deprecated: 2.32: Use g_rec_mutex_unlock()
708 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
712 g_return_val_if_fail (mutex, 0);
714 if (!g_thread_supported ())
717 depth = mutex->depth;
719 g_system_thread_assign (mutex->owner, zero_thread);
721 g_static_mutex_unlock (&mutex->mutex);
727 * g_static_rec_mutex_free:
728 * @mutex: a #GStaticRecMutex to be freed.
730 * Releases all resources allocated to a #GStaticRecMutex.
732 * You don't have to call this functions for a #GStaticRecMutex with an
733 * unbounded lifetime, i.e. objects declared 'static', but if you have
734 * a #GStaticRecMutex as a member of a structure and the structure is
735 * freed, you should also free the #GStaticRecMutex.
737 * Deprecated: 2.32: Use g_rec_mutex_clear()
740 g_static_rec_mutex_free (GStaticRecMutex *mutex)
742 g_return_if_fail (mutex);
744 g_static_mutex_free (&mutex->mutex);
747 /* GStaticRWLock {{{1 ----------------------------------------------------- */
752 * The #GStaticRWLock struct represents a read-write lock. A read-write
753 * lock can be used for protecting data that some portions of code only
754 * read from, while others also write. In such situations it is
755 * desirable that several readers can read at once, whereas of course
756 * only one writer may write at a time. Take a look at the following
760 * <title>An array with access functions</title>
762 * GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
766 * my_array_get (guint index)
768 * gpointer retval = NULL;
773 * g_static_rw_lock_reader_lock (&rwlock);
774 * if (index < array->len)
775 * retval = g_ptr_array_index (array, index);
776 * g_static_rw_lock_reader_unlock (&rwlock);
782 * my_array_set (guint index, gpointer data)
784 * g_static_rw_lock_writer_lock (&rwlock);
787 * array = g_ptr_array_new (<!-- -->);
789 * if (index >= array->len)
790 * g_ptr_array_set_size (array, index+1);
791 * g_ptr_array_index (array, index) = data;
793 * g_static_rw_lock_writer_unlock (&rwlock);
798 * This example shows an array which can be accessed by many readers
799 * (the <function>my_array_get()</function> function) simultaneously,
800 * whereas the writers (the <function>my_array_set()</function>
801 * function) will only be allowed once at a time and only if no readers
802 * currently access the array. This is because of the potentially
803 * dangerous resizing of the array. Using these functions is fully
804 * multi-thread safe now.
806 * Most of the time, writers should have precedence over readers. That
807 * means, for this implementation, that as soon as a writer wants to
808 * lock the data, no other reader is allowed to lock the data, whereas,
809 * of course, the readers that already have locked the data are allowed
810 * to finish their operation. As soon as the last reader unlocks the
811 * data, the writer will lock it.
813 * Even though #GStaticRWLock is not opaque, it should only be used
814 * with the following functions.
816 * All of the <function>g_static_rw_lock_*</function> functions can be
817 * used even if g_thread_init() has not been called. Then they do
818 * nothing, apart from <function>g_static_rw_lock_*_trylock</function>,
819 * which does nothing but returning %TRUE.
821 * <note><para>A read-write lock has a higher overhead than a mutex. For
822 * example, both g_static_rw_lock_reader_lock() and
823 * g_static_rw_lock_reader_unlock() have to lock and unlock a
824 * #GStaticMutex, so it takes at least twice the time to lock and unlock
825 * a #GStaticRWLock that it does to lock and unlock a #GStaticMutex. So
826 * only data structures that are accessed by multiple readers, and which
827 * keep the lock for a considerable time justify a #GStaticRWLock. The
828 * above example most probably would fare better with a
829 * #GStaticMutex.</para></note>
831 * Deprecated: 2.32: Use a #GRWLock instead
835 * G_STATIC_RW_LOCK_INIT:
837 * A #GStaticRWLock must be initialized with this macro before it can
838 * be used. This macro can used be to initialize a variable, but it
839 * cannot be assigned to a variable. In that case you have to use
840 * g_static_rw_lock_init().
843 * GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
848 * g_static_rw_lock_init:
849 * @lock: a #GStaticRWLock to be initialized.
851 * A #GStaticRWLock must be initialized with this function before it
852 * can be used. Alternatively you can initialize it with
853 * #G_STATIC_RW_LOCK_INIT.
855 * Deprecated: 2.32: Use g_rw_lock_init() instead
858 g_static_rw_lock_init (GStaticRWLock* lock)
860 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
862 g_return_if_fail (lock);
868 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
871 *cond = g_cond_new ();
872 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
876 g_static_rw_lock_signal (GStaticRWLock* lock)
878 if (lock->want_to_write && lock->write_cond)
879 g_cond_signal (lock->write_cond);
880 else if (lock->want_to_read && lock->read_cond)
881 g_cond_broadcast (lock->read_cond);
885 * g_static_rw_lock_reader_lock:
886 * @lock: a #GStaticRWLock to lock for reading.
888 * Locks @lock for reading. There may be unlimited concurrent locks for
889 * reading of a #GStaticRWLock at the same time. If @lock is already
890 * locked for writing by another thread or if another thread is already
891 * waiting to lock @lock for writing, this function will block until
892 * @lock is unlocked by the other writing thread and no other writing
893 * threads want to lock @lock. This lock has to be unlocked by
894 * g_static_rw_lock_reader_unlock().
896 * #GStaticRWLock is not recursive. It might seem to be possible to
897 * recursively lock for reading, but that can result in a deadlock, due
898 * to writer preference.
900 * Deprecated: 2.32: Use g_rw_lock_reader_lock() instead
903 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
905 g_return_if_fail (lock);
907 if (!g_threads_got_initialized)
910 g_static_mutex_lock (&lock->mutex);
911 lock->want_to_read++;
912 while (lock->have_writer || lock->want_to_write)
913 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
914 lock->want_to_read--;
915 lock->read_counter++;
916 g_static_mutex_unlock (&lock->mutex);
920 * g_static_rw_lock_reader_trylock:
921 * @lock: a #GStaticRWLock to lock for reading.
922 * @Returns: %TRUE, if @lock could be locked for reading.
924 * Tries to lock @lock for reading. If @lock is already locked for
925 * writing by another thread or if another thread is already waiting to
926 * lock @lock for writing, immediately returns %FALSE. Otherwise locks
927 * @lock for reading and returns %TRUE. This lock has to be unlocked by
928 * g_static_rw_lock_reader_unlock().
930 * Deprectated: 2.32: Use g_rw_lock_reader_trylock() instead
933 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
935 gboolean ret_val = FALSE;
937 g_return_val_if_fail (lock, FALSE);
939 if (!g_threads_got_initialized)
942 g_static_mutex_lock (&lock->mutex);
943 if (!lock->have_writer && !lock->want_to_write)
945 lock->read_counter++;
948 g_static_mutex_unlock (&lock->mutex);
953 * g_static_rw_lock_reader_unlock:
954 * @lock: a #GStaticRWLock to unlock after reading.
956 * Unlocks @lock. If a thread waits to lock @lock for writing and all
957 * locks for reading have been unlocked, the waiting thread is woken up
958 * and can lock @lock for writing.
960 * Deprectated: 2.32: Use g_rw_lock_reader_unlock() instead
963 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
965 g_return_if_fail (lock);
967 if (!g_threads_got_initialized)
970 g_static_mutex_lock (&lock->mutex);
971 lock->read_counter--;
972 if (lock->read_counter == 0)
973 g_static_rw_lock_signal (lock);
974 g_static_mutex_unlock (&lock->mutex);
978 * g_static_rw_lock_writer_lock:
979 * @lock: a #GStaticRWLock to lock for writing.
981 * Locks @lock for writing. If @lock is already locked for writing or
982 * reading by other threads, this function will block until @lock is
983 * completely unlocked and then lock @lock for writing. While this
984 * functions waits to lock @lock, no other thread can lock @lock for
985 * reading. When @lock is locked for writing, no other thread can lock
986 * @lock (neither for reading nor writing). This lock has to be
987 * unlocked by g_static_rw_lock_writer_unlock().
989 * Deprectated: 2.32: Use g_rw_lock_writer_lock() instead
992 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
994 g_return_if_fail (lock);
996 if (!g_threads_got_initialized)
999 g_static_mutex_lock (&lock->mutex);
1000 lock->want_to_write++;
1001 while (lock->have_writer || lock->read_counter)
1002 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
1003 lock->want_to_write--;
1004 lock->have_writer = TRUE;
1005 g_static_mutex_unlock (&lock->mutex);
1009 * g_static_rw_lock_writer_trylock:
1010 * @lock: a #GStaticRWLock to lock for writing.
1011 * @Returns: %TRUE, if @lock could be locked for writing.
1013 * Tries to lock @lock for writing. If @lock is already locked (for
1014 * either reading or writing) by another thread, it immediately returns
1015 * %FALSE. Otherwise it locks @lock for writing and returns %TRUE. This
1016 * lock has to be unlocked by g_static_rw_lock_writer_unlock().
1018 * Deprectated: 2.32: Use g_rw_lock_writer_trylock() instead
1021 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
1023 gboolean ret_val = FALSE;
1025 g_return_val_if_fail (lock, FALSE);
1027 if (!g_threads_got_initialized)
1030 g_static_mutex_lock (&lock->mutex);
1031 if (!lock->have_writer && !lock->read_counter)
1033 lock->have_writer = TRUE;
1036 g_static_mutex_unlock (&lock->mutex);
1041 * g_static_rw_lock_writer_unlock:
1042 * @lock: a #GStaticRWLock to unlock after writing.
1044 * Unlocks @lock. If a thread is waiting to lock @lock for writing and
1045 * all locks for reading have been unlocked, the waiting thread is
1046 * woken up and can lock @lock for writing. If no thread is waiting to
1047 * lock @lock for writing, and some thread or threads are waiting to
1048 * lock @lock for reading, the waiting threads are woken up and can
1049 * lock @lock for reading.
1051 * Deprectated: 2.32: Use g_rw_lock_writer_unlock() instead
1054 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
1056 g_return_if_fail (lock);
1058 if (!g_threads_got_initialized)
1061 g_static_mutex_lock (&lock->mutex);
1062 lock->have_writer = FALSE;
1063 g_static_rw_lock_signal (lock);
1064 g_static_mutex_unlock (&lock->mutex);
1068 * g_static_rw_lock_free:
1069 * @lock: a #GStaticRWLock to be freed.
1071 * Releases all resources allocated to @lock.
1073 * You don't have to call this functions for a #GStaticRWLock with an
1074 * unbounded lifetime, i.e. objects declared 'static', but if you have
1075 * a #GStaticRWLock as a member of a structure, and the structure is
1076 * freed, you should also free the #GStaticRWLock.
1078 * Deprecated: 2.32: Use a #GRWLock instead
1081 g_static_rw_lock_free (GStaticRWLock* lock)
1083 g_return_if_fail (lock);
1085 if (lock->read_cond)
1087 g_cond_free (lock->read_cond);
1088 lock->read_cond = NULL;
1090 if (lock->write_cond)
1092 g_cond_free (lock->write_cond);
1093 lock->write_cond = NULL;
1095 g_static_mutex_free (&lock->mutex);
1098 /* GPrivate {{{1 ------------------------------------------------------ */
1102 * @notify: a #GDestroyNotify
1104 * Deprecated:2.32: dynamic allocation of #GPrivate is a bad idea. Use
1105 * static storage and G_PRIVATE_INIT() instead.
1107 * Returns: a newly allocated #GPrivate (which can never be destroyed)
1110 g_private_new (GDestroyNotify notify)
1112 GPrivate tmp = G_PRIVATE_INIT (notify);
1115 key = g_slice_new (GPrivate);
1121 /* {{{1 GStaticPrivate */
1123 typedef struct _GStaticPrivateNode GStaticPrivateNode;
1124 struct _GStaticPrivateNode
1127 GDestroyNotify destroy;
1128 GStaticPrivate *owner;
1134 * A #GStaticPrivate works almost like a #GPrivate, but it has one
1135 * significant advantage. It doesn't need to be created at run-time
1136 * like a #GPrivate, but can be defined at compile-time. This is
1137 * similar to the difference between #GMutex and #GStaticMutex. Now
1138 * look at our <function>give_me_next_number()</function> example with
1142 * <title>Using GStaticPrivate for per-thread data</title>
1145 * give_me_next_number (<!-- -->)
1147 * static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1148 * int *current_number = g_static_private_get (&current_number_key);
1150 * if (!current_number)
1152 * current_number = g_new (int,1);
1153 * *current_number = 0;
1154 * g_static_private_set (&current_number_key, current_number, g_free);
1157 * *current_number = calc_next_number (*current_number);
1159 * return *current_number;
1166 * G_STATIC_PRIVATE_INIT:
1168 * Every #GStaticPrivate must be initialized with this macro, before it
1172 * GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1177 * g_static_private_init:
1178 * @private_key: a #GStaticPrivate to be initialized
1180 * Initializes @private_key. Alternatively you can initialize it with
1181 * #G_STATIC_PRIVATE_INIT.
1184 g_static_private_init (GStaticPrivate *private_key)
1186 private_key->index = 0;
1190 * g_static_private_get:
1191 * @private_key: a #GStaticPrivate
1193 * Works like g_private_get() only for a #GStaticPrivate.
1195 * This function works even if g_thread_init() has not yet been called.
1197 * Returns: the corresponding pointer
1200 g_static_private_get (GStaticPrivate *private_key)
1202 GRealThread *self = (GRealThread*) g_thread_self ();
1204 gpointer ret = NULL;
1205 array = self->private_data;
1207 if (array && private_key->index != 0 && private_key->index <= array->len)
1209 GStaticPrivateNode *node;
1211 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1213 /* Deal with the possibility that the GStaticPrivate which used
1214 * to have this index got freed and the index got allocated to
1215 * a new one. In this case, the data in the node is stale, so
1216 * free it and return NULL.
1218 if (G_UNLIKELY (node->owner != private_key))
1221 node->destroy (node->data);
1222 node->destroy = NULL;
1233 * g_static_private_set:
1234 * @private_key: a #GStaticPrivate
1235 * @data: the new pointer
1236 * @notify: a function to be called with the pointer whenever the
1237 * current thread ends or sets this pointer again
1239 * Sets the pointer keyed to @private_key for the current thread and
1240 * the function @notify to be called with that pointer (%NULL or
1241 * non-%NULL), whenever the pointer is set again or whenever the
1242 * current thread ends.
1244 * This function works even if g_thread_init() has not yet been called.
1245 * If g_thread_init() is called later, the @data keyed to @private_key
1246 * will be inherited only by the main thread, i.e. the one that called
1249 * <note><para>@notify is used quite differently from @destructor in
1250 * g_private_new().</para></note>
1253 g_static_private_set (GStaticPrivate *private_key,
1255 GDestroyNotify notify)
1257 GRealThread *self = (GRealThread*) g_thread_self ();
1259 static guint next_index = 0;
1260 GStaticPrivateNode *node;
1262 if (!private_key->index)
1266 if (!private_key->index)
1268 if (g_thread_free_indices)
1270 private_key->index = GPOINTER_TO_UINT (g_thread_free_indices->data);
1271 g_thread_free_indices = g_slist_delete_link (g_thread_free_indices,
1272 g_thread_free_indices);
1275 private_key->index = ++next_index;
1278 G_UNLOCK (g_thread);
1281 array = self->private_data;
1284 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
1285 self->private_data = array;
1287 if (private_key->index > array->len)
1288 g_array_set_size (array, private_key->index);
1290 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1293 node->destroy (node->data);
1296 node->destroy = notify;
1297 node->owner = private_key;
1301 * g_static_private_free:
1302 * @private_key: a #GStaticPrivate to be freed
1304 * Releases all resources allocated to @private_key.
1306 * You don't have to call this functions for a #GStaticPrivate with an
1307 * unbounded lifetime, i.e. objects declared 'static', but if you have
1308 * a #GStaticPrivate as a member of a structure and the structure is
1309 * freed, you should also free the #GStaticPrivate.
1312 g_static_private_free (GStaticPrivate *private_key)
1314 guint idx = private_key->index;
1319 private_key->index = 0;
1321 /* Freeing the per-thread data is deferred to either the
1322 * thread end or the next g_static_private_get() call for
1326 g_thread_free_indices = g_slist_prepend (g_thread_free_indices,
1327 GUINT_TO_POINTER (idx));
1328 G_UNLOCK (g_thread);
1332 g_static_private_cleanup (GRealThread *thread)
1336 array = thread->private_data;
1337 thread->private_data = NULL;
1343 for (i = 0; i < array->len; i++ )
1345 GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
1347 node->destroy (node->data);
1349 g_array_free (array, TRUE);
1354 /* vim: set foldmethod=marker: */