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.1 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, see <http://www.gnu.org/licenses/>.
24 /* we know we are deprecated here, no need for warnings */
25 #define GLIB_DISABLE_DEPRECATION_WARNINGS
27 #include "gmessages.h"
31 #include "gthreadprivate.h"
32 #include "deprecated/gthread.h"
37 /* {{{1 Documentation */
40 * SECTION:threads-deprecated
41 * @title: Deprecated thread API
42 * @short_description: old thread APIs (for reference only)
45 * These APIs are deprecated. You should not use them in new code.
46 * This section remains only to assist with understanding code that was
47 * written to use these APIs at some point in the past.
52 * @G_THREAD_PRIORITY_LOW: a priority lower than normal
53 * @G_THREAD_PRIORITY_NORMAL: the default priority
54 * @G_THREAD_PRIORITY_HIGH: a priority higher than normal
55 * @G_THREAD_PRIORITY_URGENT: the highest priority
59 * Deprecated:2.32: Thread priorities no longer have any effect.
64 * @mutex_new: virtual function pointer for g_mutex_new()
65 * @mutex_lock: virtual function pointer for g_mutex_lock()
66 * @mutex_trylock: virtual function pointer for g_mutex_trylock()
67 * @mutex_unlock: virtual function pointer for g_mutex_unlock()
68 * @mutex_free: virtual function pointer for g_mutex_free()
69 * @cond_new: virtual function pointer for g_cond_new()
70 * @cond_signal: virtual function pointer for g_cond_signal()
71 * @cond_broadcast: virtual function pointer for g_cond_broadcast()
72 * @cond_wait: virtual function pointer for g_cond_wait()
73 * @cond_timed_wait: virtual function pointer for g_cond_timed_wait()
74 * @cond_free: virtual function pointer for g_cond_free()
75 * @private_new: virtual function pointer for g_private_new()
76 * @private_get: virtual function pointer for g_private_get()
77 * @private_set: virtual function pointer for g_private_set()
78 * @thread_create: virtual function pointer for g_thread_create()
79 * @thread_yield: virtual function pointer for g_thread_yield()
80 * @thread_join: virtual function pointer for g_thread_join()
81 * @thread_exit: virtual function pointer for g_thread_exit()
82 * @thread_set_priority: virtual function pointer for
83 * g_thread_set_priority()
84 * @thread_self: virtual function pointer for g_thread_self()
85 * @thread_equal: used internally by recursive mutex locks and by some
88 * This function table is no longer used by g_thread_init()
89 * to initialize the thread system.
93 * G_THREADS_IMPL_POSIX:
95 * This macro is defined if POSIX style threads are used.
97 * Deprecated:2.32:POSIX threads are in use on all non-Windows systems.
98 * Use G_OS_WIN32 to detect Windows.
102 * G_THREADS_IMPL_WIN32:
104 * This macro is defined if Windows style threads are used.
106 * Deprecated:2.32:Use G_OS_WIN32 to detect Windows.
110 /* {{{1 Exported Variables */
112 /* Set this FALSE to have previously-compiled GStaticMutex code use the
113 * slow path (ie: call into us) to avoid compatibility problems.
115 gboolean g_thread_use_default_impl
= FALSE
;
117 GThreadFunctions g_thread_functions_for_glib_use
=
145 return g_get_monotonic_time () * 1000;
148 guint64 (*g_thread_gettime
) (void) = gettime
;
150 /* Initialisation {{{1 ---------------------------------------------------- */
151 gboolean g_threads_got_initialized
= TRUE
;
155 * @vtable: a function table of type #GThreadFunctions, that provides
156 * the entry points to the thread system to be used. Since 2.32,
157 * this parameter is ignored and should always be %NULL
159 * If you use GLib from more than one thread, you must initialize the
160 * thread system by calling g_thread_init().
162 * Since version 2.24, calling g_thread_init() multiple times is allowed,
163 * but nothing happens except for the first call.
165 * Since version 2.32, GLib does not support custom thread implementations
166 * anymore and the @vtable parameter is ignored and you should pass %NULL.
168 * <note><para>g_thread_init() must not be called directly or indirectly
169 * in a callback from GLib. Also no mutexes may be currently locked while
170 * calling g_thread_init().</para></note>
172 * <note><para>To use g_thread_init() in your program, you have to link
173 * with the libraries that the command <command>pkg-config --libs
174 * gthread-2.0</command> outputs. This is not the case for all the
175 * other thread-related functions of GLib. Those can be used without
176 * having to link with the thread libraries.</para></note>
178 * Deprecated:2.32: This function is no longer necessary. The GLib
179 * threading system is automatically initialized at the start
184 * g_thread_get_initialized:
186 * Indicates if g_thread_init() has been called.
188 * Returns: %TRUE if threads have been initialized.
193 g_thread_get_initialized (void)
195 return g_thread_supported ();
198 /* We need this for ABI compatibility */
199 GLIB_AVAILABLE_IN_ALL
200 void g_thread_init_glib (void);
201 void g_thread_init_glib (void) { }
203 /* Internal variables {{{1 */
205 static GSList
*g_thread_all_threads
= NULL
;
206 static GSList
*g_thread_free_indices
= NULL
;
208 /* Protects g_thread_all_threads and g_thread_free_indices */
209 G_LOCK_DEFINE_STATIC (g_static_mutex
);
210 G_LOCK_DEFINE_STATIC (g_thread
);
212 /* Misc. GThread functions {{{1 */
215 * g_thread_set_priority:
216 * @thread: a #GThread.
219 * This function does nothing.
221 * Deprecated:2.32: Thread priorities no longer have any effect.
224 g_thread_set_priority (GThread
*thread
,
225 GThreadPriority priority
)
231 * @thread_func: function to call for all #GThread structures
232 * @user_data: second argument to @thread_func
234 * Call @thread_func on all #GThreads that have been
235 * created with g_thread_create().
237 * Note that threads may decide to exit while @thread_func is
238 * running, so without intimate knowledge about the lifetime of
239 * foreign threads, @thread_func shouldn't access the GThread*
240 * pointer passed in as first argument. However, @thread_func will
241 * not be called for threads which are known to have exited already.
243 * Due to thread lifetime checks, this function has an execution complexity
244 * which is quadratic in the number of existing threads.
248 * Deprecated:2.32: There aren't many things you can do with a #GThread,
249 * except comparing it with one that was returned from g_thread_create().
250 * There are better ways to find out if your thread is still alive.
253 g_thread_foreach (GFunc thread_func
,
256 GSList
*slist
= NULL
;
258 g_return_if_fail (thread_func
!= NULL
);
259 /* snapshot the list of threads for iteration */
261 slist
= g_slist_copy (g_thread_all_threads
);
263 /* walk the list, skipping non-existent threads */
266 GSList
*node
= slist
;
268 /* check whether the current thread still exists */
270 if (g_slist_find (g_thread_all_threads
, node
->data
))
276 thread_func (thread
, user_data
);
277 g_slist_free_1 (node
);
282 g_enumerable_thread_remove (gpointer data
)
284 GRealThread
*thread
= data
;
287 g_thread_all_threads
= g_slist_remove (g_thread_all_threads
, thread
);
291 GPrivate enumerable_thread_private
= G_PRIVATE_INIT (g_enumerable_thread_remove
);
294 g_enumerable_thread_add (GRealThread
*thread
)
297 g_thread_all_threads
= g_slist_prepend (g_thread_all_threads
, thread
);
300 g_private_set (&enumerable_thread_private
, thread
);
304 g_deprecated_thread_proxy (gpointer data
)
306 GRealThread
*real
= data
;
308 g_enumerable_thread_add (real
);
310 return g_thread_proxy (data
);
315 * @func: a function to execute in the new thread
316 * @data: an argument to supply to the new thread
317 * @joinable: should this thread be joinable?
318 * @error: return location for error, or %NULL
320 * This function creates a new thread.
322 * The new thread executes the function @func with the argument @data.
323 * If the thread was created successfully, it is returned.
325 * @error can be %NULL to ignore errors, or non-%NULL to report errors.
326 * The error is set, if and only if the function returns %NULL.
328 * This function returns a reference to the created thread only if
329 * @joinable is %TRUE. In that case, you must free this reference by
330 * calling g_thread_unref() or g_thread_join(). If @joinable is %FALSE
331 * then you should probably not touch the return value.
333 * Returns: the new #GThread on success
335 * Deprecated:2.32: Use g_thread_new() instead
338 g_thread_create (GThreadFunc func
,
343 return g_thread_create_full (func
, data
, 0, joinable
, 0, 0, error
);
347 * g_thread_create_full:
348 * @func: a function to execute in the new thread.
349 * @data: an argument to supply to the new thread.
350 * @stack_size: a stack size for the new thread.
351 * @joinable: should this thread be joinable?
354 * @error: return location for error.
356 * This function creates a new thread.
358 * Returns: the new #GThread on success.
360 * Deprecated:2.32: The @bound and @priority arguments are now ignored.
361 * Use g_thread_new().
364 g_thread_create_full (GThreadFunc func
,
369 GThreadPriority priority
,
374 thread
= g_thread_new_internal (NULL
, g_deprecated_thread_proxy
,
375 func
, data
, stack_size
, error
);
377 if (thread
&& !joinable
)
379 thread
->joinable
= FALSE
;
380 g_thread_unref (thread
);
386 /* GOnce {{{1 ------------------------------------------------------------- */
388 g_once_init_enter_impl (volatile gsize
*location
)
390 return (g_once_init_enter
) (location
);
393 /* GStaticMutex {{{1 ------------------------------------------------------ */
398 * A #GStaticMutex works like a #GMutex.
400 * Prior to GLib 2.32, GStaticMutex had the significant advantage
401 * that it doesn't need to be created at run-time, but can be defined
402 * at compile-time. Since 2.32, #GMutex can be statically allocated
403 * as well, and GStaticMutex has been deprecated.
405 * Here is a version of our give_me_next_number() example using
409 * give_me_next_number (void)
411 * static int current_number = 0;
413 * static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
415 * g_static_mutex_lock (&mutex);
416 * ret_val = current_number = calc_next_number (current_number);
417 * g_static_mutex_unlock (&mutex);
423 * Sometimes you would like to dynamically create a mutex. If you don't
424 * want to require prior calling to g_thread_init(), because your code
425 * should also be usable in non-threaded programs, you are not able to
426 * use g_mutex_new() and thus #GMutex, as that requires a prior call to
427 * g_thread_init(). In theses cases you can also use a #GStaticMutex.
428 * It must be initialized with g_static_mutex_init() before using it
429 * and freed with with g_static_mutex_free() when not needed anymore to
430 * free up any allocated resources.
432 * Even though #GStaticMutex is not opaque, it should only be used with
433 * the following functions, as it is defined differently on different
436 * All of the g_static_mutex_* functions apart from
437 * g_static_mutex_get_mutex() can also be used even if g_thread_init()
438 * has not yet been called. Then they do nothing, apart from
439 * g_static_mutex_trylock() which does nothing but returning %TRUE.
441 * All of the g_static_mutex_* functions are actually macros. Apart from
442 * taking their addresses, you can however use them as if they were
447 * G_STATIC_MUTEX_INIT:
449 * A #GStaticMutex must be initialized with this macro, before it can
450 * be used. This macro can used be to initialize a variable, but it
451 * cannot be assigned to a variable. In that case you have to use
452 * g_static_mutex_init().
455 * GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
460 * g_static_mutex_init:
461 * @mutex: a #GStaticMutex to be initialized.
463 * Initializes @mutex.
464 * Alternatively you can initialize it with #G_STATIC_MUTEX_INIT.
466 * Deprecated: 2.32: Use g_mutex_init()
469 g_static_mutex_init (GStaticMutex
*mutex
)
471 static const GStaticMutex init_mutex
= G_STATIC_MUTEX_INIT
;
473 g_return_if_fail (mutex
);
478 /* IMPLEMENTATION NOTE:
480 * On some platforms a GStaticMutex is actually a normal GMutex stored
481 * inside of a structure instead of being allocated dynamically. We can
482 * only do this for platforms on which we know, in advance, how to
483 * allocate (size) and initialise (value) that memory.
485 * On other platforms, a GStaticMutex is nothing more than a pointer to
486 * a GMutex. In that case, the first access we make to the static mutex
487 * must first allocate the normal GMutex and store it into the pointer.
489 * configure.ac writes macros into glibconfig.h to determine if
490 * g_static_mutex_get_mutex() accesses the structure in memory directly
491 * (on platforms where we are able to do that) or if it ends up here,
492 * where we may have to allocate the GMutex before returning it.
496 * g_static_mutex_get_mutex:
497 * @mutex: a #GStaticMutex.
499 * For some operations (like g_cond_wait()) you must have a #GMutex
500 * instead of a #GStaticMutex. This function will return the
501 * corresponding #GMutex for @mutex.
503 * Returns: the #GMutex corresponding to @mutex.
505 * Deprecated: 2.32: Just use a #GMutex
508 g_static_mutex_get_mutex_impl (GStaticMutex
* mutex
)
512 if (!g_thread_supported ())
515 result
= g_atomic_pointer_get (&mutex
->mutex
);
519 G_LOCK (g_static_mutex
);
521 result
= mutex
->mutex
;
524 result
= g_mutex_new ();
525 g_atomic_pointer_set (&mutex
->mutex
, result
);
528 G_UNLOCK (g_static_mutex
);
534 /* IMPLEMENTATION NOTE:
536 * g_static_mutex_lock(), g_static_mutex_trylock() and
537 * g_static_mutex_unlock() are all preprocessor macros that wrap the
538 * corresponding g_mutex_*() function around a call to
539 * g_static_mutex_get_mutex().
543 * g_static_mutex_lock:
544 * @mutex: a #GStaticMutex.
546 * Works like g_mutex_lock(), but for a #GStaticMutex.
548 * Deprecated: 2.32: Use g_mutex_lock()
552 * g_static_mutex_trylock:
553 * @mutex: a #GStaticMutex.
555 * Works like g_mutex_trylock(), but for a #GStaticMutex.
557 * Returns: %TRUE, if the #GStaticMutex could be locked.
559 * Deprecated: 2.32: Use g_mutex_trylock()
563 * g_static_mutex_unlock:
564 * @mutex: a #GStaticMutex.
566 * Works like g_mutex_unlock(), but for a #GStaticMutex.
568 * Deprecated: 2.32: Use g_mutex_unlock()
572 * g_static_mutex_free:
573 * @mutex: a #GStaticMutex to be freed.
575 * Releases all resources allocated to @mutex.
577 * You don't have to call this functions for a #GStaticMutex with an
578 * unbounded lifetime, i.e. objects declared 'static', but if you have
579 * a #GStaticMutex as a member of a structure and the structure is
580 * freed, you should also free the #GStaticMutex.
582 * Calling g_static_mutex_free() on a locked mutex may result in
583 * undefined behaviour.
585 * Deprecated: 2.32: Use g_mutex_clear()
588 g_static_mutex_free (GStaticMutex
* mutex
)
590 GMutex
**runtime_mutex
;
592 g_return_if_fail (mutex
);
594 /* The runtime_mutex is the first (or only) member of GStaticMutex,
595 * see both versions (of glibconfig.h) in configure.ac. Note, that
596 * this variable is NULL, if g_thread_init() hasn't been called or
597 * if we're using the default thread implementation and it provides
599 runtime_mutex
= ((GMutex
**)mutex
);
602 g_mutex_free (*runtime_mutex
);
604 *runtime_mutex
= NULL
;
607 /* {{{1 GStaticRecMutex */
612 * A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
613 * multiple times by one thread. If you enter it n times, you have to
614 * unlock it n times again to let other threads lock it. An exception
615 * is the function g_static_rec_mutex_unlock_full(): that allows you to
616 * unlock a #GStaticRecMutex completely returning the depth, (i.e. the
617 * number of times this mutex was locked). The depth can later be used
618 * to restore the state of the #GStaticRecMutex by calling
619 * g_static_rec_mutex_lock_full(). In GLib 2.32, #GStaticRecMutex has
620 * been deprecated in favor of #GRecMutex.
622 * Even though #GStaticRecMutex is not opaque, it should only be used
623 * with the following functions.
625 * All of the g_static_rec_mutex_* functions can be used even if
626 * g_thread_init() has not been called. Then they do nothing, apart
627 * from g_static_rec_mutex_trylock(), which does nothing but returning
632 * G_STATIC_REC_MUTEX_INIT:
634 * A #GStaticRecMutex must be initialized with this macro before it can
635 * be used. This macro can used be to initialize a variable, but it
636 * cannot be assigned to a variable. In that case you have to use
637 * g_static_rec_mutex_init().
640 * GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
645 * g_static_rec_mutex_init:
646 * @mutex: a #GStaticRecMutex to be initialized.
648 * A #GStaticRecMutex must be initialized with this function before it
649 * can be used. Alternatively you can initialize it with
650 * #G_STATIC_REC_MUTEX_INIT.
652 * Deprecated: 2.32: Use g_rec_mutex_init()
655 g_static_rec_mutex_init (GStaticRecMutex
*mutex
)
657 static const GStaticRecMutex init_mutex
= G_STATIC_REC_MUTEX_INIT
;
659 g_return_if_fail (mutex
);
665 g_static_rec_mutex_get_rec_mutex_impl (GStaticRecMutex
* mutex
)
669 if (!g_thread_supported ())
672 result
= g_atomic_pointer_get (&mutex
->mutex
.mutex
);
676 G_LOCK (g_static_mutex
);
678 result
= (GRecMutex
*) mutex
->mutex
.mutex
;
681 result
= g_slice_new (GRecMutex
);
682 g_rec_mutex_init (result
);
683 g_atomic_pointer_set (&mutex
->mutex
.mutex
, result
);
686 G_UNLOCK (g_static_mutex
);
693 * g_static_rec_mutex_lock:
694 * @mutex: a #GStaticRecMutex to lock.
696 * Locks @mutex. If @mutex is already locked by another thread, the
697 * current thread will block until @mutex is unlocked by the other
698 * thread. If @mutex is already locked by the calling thread, this
699 * functions increases the depth of @mutex and returns immediately.
701 * Deprecated: 2.32: Use g_rec_mutex_lock()
704 g_static_rec_mutex_lock (GStaticRecMutex
* mutex
)
707 rm
= g_static_rec_mutex_get_rec_mutex_impl (mutex
);
708 g_rec_mutex_lock (rm
);
713 * g_static_rec_mutex_trylock:
714 * @mutex: a #GStaticRecMutex to lock.
716 * Tries to lock @mutex. If @mutex is already locked by another thread,
717 * it immediately returns %FALSE. Otherwise it locks @mutex and returns
718 * %TRUE. If @mutex is already locked by the calling thread, this
719 * functions increases the depth of @mutex and immediately returns
722 * Returns: %TRUE, if @mutex could be locked.
724 * Deprecated: 2.32: Use g_rec_mutex_trylock()
727 g_static_rec_mutex_trylock (GStaticRecMutex
* mutex
)
730 rm
= g_static_rec_mutex_get_rec_mutex_impl (mutex
);
732 if (g_rec_mutex_trylock (rm
))
742 * g_static_rec_mutex_unlock:
743 * @mutex: a #GStaticRecMutex to unlock.
745 * Unlocks @mutex. Another thread will be allowed to lock @mutex only
746 * when it has been unlocked as many times as it had been locked
747 * before. If @mutex is completely unlocked and another thread is
748 * blocked in a g_static_rec_mutex_lock() call for @mutex, it will be
749 * woken and can lock @mutex itself.
751 * Deprecated: 2.32: Use g_rec_mutex_unlock()
754 g_static_rec_mutex_unlock (GStaticRecMutex
* mutex
)
757 rm
= g_static_rec_mutex_get_rec_mutex_impl (mutex
);
759 g_rec_mutex_unlock (rm
);
763 * g_static_rec_mutex_lock_full:
764 * @mutex: a #GStaticRecMutex to lock.
765 * @depth: number of times this mutex has to be unlocked to be
766 * completely unlocked.
768 * Works like calling g_static_rec_mutex_lock() for @mutex @depth times.
770 * Deprecated: 2.32: Use g_rec_mutex_lock()
773 g_static_rec_mutex_lock_full (GStaticRecMutex
*mutex
,
778 rm
= g_static_rec_mutex_get_rec_mutex_impl (mutex
);
781 g_rec_mutex_lock (rm
);
787 * g_static_rec_mutex_unlock_full:
788 * @mutex: a #GStaticRecMutex to completely unlock.
790 * Completely unlocks @mutex. If another thread is blocked in a
791 * g_static_rec_mutex_lock() call for @mutex, it will be woken and can
792 * lock @mutex itself. This function returns the number of times that
793 * @mutex has been locked by the current thread. To restore the state
794 * before the call to g_static_rec_mutex_unlock_full() you can call
795 * g_static_rec_mutex_lock_full() with the depth returned by this
798 * Returns: number of times @mutex has been locked by the current
801 * Deprecated: 2.32: Use g_rec_mutex_unlock()
804 g_static_rec_mutex_unlock_full (GStaticRecMutex
*mutex
)
810 rm
= g_static_rec_mutex_get_rec_mutex_impl (mutex
);
812 /* all access to mutex->depth done while still holding the lock */
813 depth
= mutex
->depth
;
818 g_rec_mutex_unlock (rm
);
824 * g_static_rec_mutex_free:
825 * @mutex: a #GStaticRecMutex to be freed.
827 * Releases all resources allocated to a #GStaticRecMutex.
829 * You don't have to call this functions for a #GStaticRecMutex with an
830 * unbounded lifetime, i.e. objects declared 'static', but if you have
831 * a #GStaticRecMutex as a member of a structure and the structure is
832 * freed, you should also free the #GStaticRecMutex.
834 * Deprecated: 2.32: Use g_rec_mutex_clear()
837 g_static_rec_mutex_free (GStaticRecMutex
*mutex
)
839 g_return_if_fail (mutex
);
841 if (mutex
->mutex
.mutex
)
843 GRecMutex
*rm
= (GRecMutex
*) mutex
->mutex
.mutex
;
845 g_rec_mutex_clear (rm
);
846 g_slice_free (GRecMutex
, rm
);
850 /* GStaticRWLock {{{1 ----------------------------------------------------- */
855 * The #GStaticRWLock struct represents a read-write lock. A read-write
856 * lock can be used for protecting data that some portions of code only
857 * read from, while others also write. In such situations it is
858 * desirable that several readers can read at once, whereas of course
859 * only one writer may write at a time.
861 * Take a look at the following example:
863 * GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
867 * my_array_get (guint index)
869 * gpointer retval = NULL;
874 * g_static_rw_lock_reader_lock (&rwlock);
875 * if (index < array->len)
876 * retval = g_ptr_array_index (array, index);
877 * g_static_rw_lock_reader_unlock (&rwlock);
883 * my_array_set (guint index, gpointer data)
885 * g_static_rw_lock_writer_lock (&rwlock);
888 * array = g_ptr_array_new ();
890 * if (index >= array->len)
891 * g_ptr_array_set_size (array, index + 1);
892 * g_ptr_array_index (array, index) = data;
894 * g_static_rw_lock_writer_unlock (&rwlock);
898 * This example shows an array which can be accessed by many readers
899 * (the my_array_get() function) simultaneously, whereas the writers
900 * (the my_array_set() function) will only be allowed once at a time
901 * and only if no readers currently access the array. This is because
902 * of the potentially dangerous resizing of the array. Using these
903 * functions is fully multi-thread safe now.
905 * Most of the time, writers should have precedence over readers. That
906 * means, for this implementation, that as soon as a writer wants to
907 * lock the data, no other reader is allowed to lock the data, whereas,
908 * of course, the readers that already have locked the data are allowed
909 * to finish their operation. As soon as the last reader unlocks the
910 * data, the writer will lock it.
912 * Even though #GStaticRWLock is not opaque, it should only be used
913 * with the following functions.
915 * All of the g_static_rw_lock_* functions can be used even if
916 * g_thread_init() has not been called. Then they do nothing, apart
917 * from g_static_rw_lock_*_trylock, which does nothing but returning %TRUE.
919 * A read-write lock has a higher overhead than a mutex. For example, both
920 * g_static_rw_lock_reader_lock() and g_static_rw_lock_reader_unlock() have
921 * to lock and unlock a #GStaticMutex, so it takes at least twice the time
922 * to lock and unlock a #GStaticRWLock that it does to lock and unlock a
923 * #GStaticMutex. So only data structures that are accessed by multiple
924 * readers, and which keep the lock for a considerable time justify a
925 * #GStaticRWLock. The above example most probably would fare better with a
928 * Deprecated: 2.32: Use a #GRWLock instead
932 * G_STATIC_RW_LOCK_INIT:
934 * A #GStaticRWLock must be initialized with this macro before it can
935 * be used. This macro can used be to initialize a variable, but it
936 * cannot be assigned to a variable. In that case you have to use
937 * g_static_rw_lock_init().
940 * GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
945 * g_static_rw_lock_init:
946 * @lock: a #GStaticRWLock to be initialized.
948 * A #GStaticRWLock must be initialized with this function before it
949 * can be used. Alternatively you can initialize it with
950 * #G_STATIC_RW_LOCK_INIT.
952 * Deprecated: 2.32: Use g_rw_lock_init() instead
955 g_static_rw_lock_init (GStaticRWLock
* lock
)
957 static const GStaticRWLock init_lock
= G_STATIC_RW_LOCK_INIT
;
959 g_return_if_fail (lock
);
965 g_static_rw_lock_wait (GCond
** cond
, GStaticMutex
* mutex
)
968 *cond
= g_cond_new ();
969 g_cond_wait (*cond
, g_static_mutex_get_mutex (mutex
));
973 g_static_rw_lock_signal (GStaticRWLock
* lock
)
975 if (lock
->want_to_write
&& lock
->write_cond
)
976 g_cond_signal (lock
->write_cond
);
977 else if (lock
->want_to_read
&& lock
->read_cond
)
978 g_cond_broadcast (lock
->read_cond
);
982 * g_static_rw_lock_reader_lock:
983 * @lock: a #GStaticRWLock to lock for reading.
985 * Locks @lock for reading. There may be unlimited concurrent locks for
986 * reading of a #GStaticRWLock at the same time. If @lock is already
987 * locked for writing by another thread or if another thread is already
988 * waiting to lock @lock for writing, this function will block until
989 * @lock is unlocked by the other writing thread and no other writing
990 * threads want to lock @lock. This lock has to be unlocked by
991 * g_static_rw_lock_reader_unlock().
993 * #GStaticRWLock is not recursive. It might seem to be possible to
994 * recursively lock for reading, but that can result in a deadlock, due
995 * to writer preference.
997 * Deprecated: 2.32: Use g_rw_lock_reader_lock() instead
1000 g_static_rw_lock_reader_lock (GStaticRWLock
* lock
)
1002 g_return_if_fail (lock
);
1004 if (!g_threads_got_initialized
)
1007 g_static_mutex_lock (&lock
->mutex
);
1008 lock
->want_to_read
++;
1009 while (lock
->have_writer
|| lock
->want_to_write
)
1010 g_static_rw_lock_wait (&lock
->read_cond
, &lock
->mutex
);
1011 lock
->want_to_read
--;
1012 lock
->read_counter
++;
1013 g_static_mutex_unlock (&lock
->mutex
);
1017 * g_static_rw_lock_reader_trylock:
1018 * @lock: a #GStaticRWLock to lock for reading
1020 * Tries to lock @lock for reading. If @lock is already locked for
1021 * writing by another thread or if another thread is already waiting to
1022 * lock @lock for writing, immediately returns %FALSE. Otherwise locks
1023 * @lock for reading and returns %TRUE. This lock has to be unlocked by
1024 * g_static_rw_lock_reader_unlock().
1026 * Returns: %TRUE, if @lock could be locked for reading
1028 * Deprectated: 2.32: Use g_rw_lock_reader_trylock() instead
1031 g_static_rw_lock_reader_trylock (GStaticRWLock
* lock
)
1033 gboolean ret_val
= FALSE
;
1035 g_return_val_if_fail (lock
, FALSE
);
1037 if (!g_threads_got_initialized
)
1040 g_static_mutex_lock (&lock
->mutex
);
1041 if (!lock
->have_writer
&& !lock
->want_to_write
)
1043 lock
->read_counter
++;
1046 g_static_mutex_unlock (&lock
->mutex
);
1051 * g_static_rw_lock_reader_unlock:
1052 * @lock: a #GStaticRWLock to unlock after reading
1054 * Unlocks @lock. If a thread waits to lock @lock for writing and all
1055 * locks for reading have been unlocked, the waiting thread is woken up
1056 * and can lock @lock for writing.
1058 * Deprectated: 2.32: Use g_rw_lock_reader_unlock() instead
1061 g_static_rw_lock_reader_unlock (GStaticRWLock
* lock
)
1063 g_return_if_fail (lock
);
1065 if (!g_threads_got_initialized
)
1068 g_static_mutex_lock (&lock
->mutex
);
1069 lock
->read_counter
--;
1070 if (lock
->read_counter
== 0)
1071 g_static_rw_lock_signal (lock
);
1072 g_static_mutex_unlock (&lock
->mutex
);
1076 * g_static_rw_lock_writer_lock:
1077 * @lock: a #GStaticRWLock to lock for writing
1079 * Locks @lock for writing. If @lock is already locked for writing or
1080 * reading by other threads, this function will block until @lock is
1081 * completely unlocked and then lock @lock for writing. While this
1082 * functions waits to lock @lock, no other thread can lock @lock for
1083 * reading. When @lock is locked for writing, no other thread can lock
1084 * @lock (neither for reading nor writing). This lock has to be
1085 * unlocked by g_static_rw_lock_writer_unlock().
1087 * Deprectated: 2.32: Use g_rw_lock_writer_lock() instead
1090 g_static_rw_lock_writer_lock (GStaticRWLock
* lock
)
1092 g_return_if_fail (lock
);
1094 if (!g_threads_got_initialized
)
1097 g_static_mutex_lock (&lock
->mutex
);
1098 lock
->want_to_write
++;
1099 while (lock
->have_writer
|| lock
->read_counter
)
1100 g_static_rw_lock_wait (&lock
->write_cond
, &lock
->mutex
);
1101 lock
->want_to_write
--;
1102 lock
->have_writer
= TRUE
;
1103 g_static_mutex_unlock (&lock
->mutex
);
1107 * g_static_rw_lock_writer_trylock:
1108 * @lock: a #GStaticRWLock to lock for writing
1110 * Tries to lock @lock for writing. If @lock is already locked (for
1111 * either reading or writing) by another thread, it immediately returns
1112 * %FALSE. Otherwise it locks @lock for writing and returns %TRUE. This
1113 * lock has to be unlocked by g_static_rw_lock_writer_unlock().
1115 * Returns: %TRUE, if @lock could be locked for writing
1117 * Deprectated: 2.32: Use g_rw_lock_writer_trylock() instead
1120 g_static_rw_lock_writer_trylock (GStaticRWLock
* lock
)
1122 gboolean ret_val
= FALSE
;
1124 g_return_val_if_fail (lock
, FALSE
);
1126 if (!g_threads_got_initialized
)
1129 g_static_mutex_lock (&lock
->mutex
);
1130 if (!lock
->have_writer
&& !lock
->read_counter
)
1132 lock
->have_writer
= TRUE
;
1135 g_static_mutex_unlock (&lock
->mutex
);
1140 * g_static_rw_lock_writer_unlock:
1141 * @lock: a #GStaticRWLock to unlock after writing.
1143 * Unlocks @lock. If a thread is waiting to lock @lock for writing and
1144 * all locks for reading have been unlocked, the waiting thread is
1145 * woken up and can lock @lock for writing. If no thread is waiting to
1146 * lock @lock for writing, and some thread or threads are waiting to
1147 * lock @lock for reading, the waiting threads are woken up and can
1148 * lock @lock for reading.
1150 * Deprectated: 2.32: Use g_rw_lock_writer_unlock() instead
1153 g_static_rw_lock_writer_unlock (GStaticRWLock
* lock
)
1155 g_return_if_fail (lock
);
1157 if (!g_threads_got_initialized
)
1160 g_static_mutex_lock (&lock
->mutex
);
1161 lock
->have_writer
= FALSE
;
1162 g_static_rw_lock_signal (lock
);
1163 g_static_mutex_unlock (&lock
->mutex
);
1167 * g_static_rw_lock_free:
1168 * @lock: a #GStaticRWLock to be freed.
1170 * Releases all resources allocated to @lock.
1172 * You don't have to call this functions for a #GStaticRWLock with an
1173 * unbounded lifetime, i.e. objects declared 'static', but if you have
1174 * a #GStaticRWLock as a member of a structure, and the structure is
1175 * freed, you should also free the #GStaticRWLock.
1177 * Deprecated: 2.32: Use a #GRWLock instead
1180 g_static_rw_lock_free (GStaticRWLock
* lock
)
1182 g_return_if_fail (lock
);
1184 if (lock
->read_cond
)
1186 g_cond_free (lock
->read_cond
);
1187 lock
->read_cond
= NULL
;
1189 if (lock
->write_cond
)
1191 g_cond_free (lock
->write_cond
);
1192 lock
->write_cond
= NULL
;
1194 g_static_mutex_free (&lock
->mutex
);
1197 /* GPrivate {{{1 ------------------------------------------------------ */
1201 * @notify: a #GDestroyNotify
1203 * Creates a new #GPrivate.
1205 * Deprecated:2.32: dynamic allocation of #GPrivate is a bad idea. Use
1206 * static storage and G_PRIVATE_INIT() instead.
1208 * Returns: a newly allocated #GPrivate (which can never be destroyed)
1211 g_private_new (GDestroyNotify notify
)
1213 GPrivate tmp
= G_PRIVATE_INIT (notify
);
1216 key
= g_slice_new (GPrivate
);
1222 /* {{{1 GStaticPrivate */
1224 typedef struct _GStaticPrivateNode GStaticPrivateNode
;
1225 struct _GStaticPrivateNode
1228 GDestroyNotify destroy
;
1229 GStaticPrivate
*owner
;
1233 g_static_private_cleanup (gpointer data
)
1235 GArray
*array
= data
;
1238 for (i
= 0; i
< array
->len
; i
++ )
1240 GStaticPrivateNode
*node
= &g_array_index (array
, GStaticPrivateNode
, i
);
1242 node
->destroy (node
->data
);
1245 g_array_free (array
, TRUE
);
1248 GPrivate static_private_private
= G_PRIVATE_INIT (g_static_private_cleanup
);
1253 * A #GStaticPrivate works almost like a #GPrivate, but it has one
1254 * significant advantage. It doesn't need to be created at run-time
1255 * like a #GPrivate, but can be defined at compile-time. This is
1256 * similar to the difference between #GMutex and #GStaticMutex.
1258 * Now look at our give_me_next_number() example with #GStaticPrivate:
1261 * give_me_next_number ()
1263 * static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1264 * int *current_number = g_static_private_get (¤t_number_key);
1266 * if (!current_number)
1268 * current_number = g_new (int, 1);
1269 * *current_number = 0;
1270 * g_static_private_set (¤t_number_key, current_number, g_free);
1273 * *current_number = calc_next_number (*current_number);
1275 * return *current_number;
1281 * G_STATIC_PRIVATE_INIT:
1283 * Every #GStaticPrivate must be initialized with this macro, before it
1287 * GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1292 * g_static_private_init:
1293 * @private_key: a #GStaticPrivate to be initialized
1295 * Initializes @private_key. Alternatively you can initialize it with
1296 * #G_STATIC_PRIVATE_INIT.
1299 g_static_private_init (GStaticPrivate
*private_key
)
1301 private_key
->index
= 0;
1305 * g_static_private_get:
1306 * @private_key: a #GStaticPrivate
1308 * Works like g_private_get() only for a #GStaticPrivate.
1310 * This function works even if g_thread_init() has not yet been called.
1312 * Returns: the corresponding pointer
1315 g_static_private_get (GStaticPrivate
*private_key
)
1318 gpointer ret
= NULL
;
1320 array
= g_private_get (&static_private_private
);
1322 if (array
&& private_key
->index
!= 0 && private_key
->index
<= array
->len
)
1324 GStaticPrivateNode
*node
;
1326 node
= &g_array_index (array
, GStaticPrivateNode
, private_key
->index
- 1);
1328 /* Deal with the possibility that the GStaticPrivate which used
1329 * to have this index got freed and the index got allocated to
1330 * a new one. In this case, the data in the node is stale, so
1331 * free it and return NULL.
1333 if (G_UNLIKELY (node
->owner
!= private_key
))
1336 node
->destroy (node
->data
);
1337 node
->destroy
= NULL
;
1348 * g_static_private_set:
1349 * @private_key: a #GStaticPrivate
1350 * @data: the new pointer
1351 * @notify: a function to be called with the pointer whenever the
1352 * current thread ends or sets this pointer again
1354 * Sets the pointer keyed to @private_key for the current thread and
1355 * the function @notify to be called with that pointer (%NULL or
1356 * non-%NULL), whenever the pointer is set again or whenever the
1357 * current thread ends.
1359 * This function works even if g_thread_init() has not yet been called.
1360 * If g_thread_init() is called later, the @data keyed to @private_key
1361 * will be inherited only by the main thread, i.e. the one that called
1364 * @notify is used quite differently from @destructor in g_private_new().
1367 g_static_private_set (GStaticPrivate
*private_key
,
1369 GDestroyNotify notify
)
1372 static guint next_index
= 0;
1373 GStaticPrivateNode
*node
;
1375 if (!private_key
->index
)
1379 if (!private_key
->index
)
1381 if (g_thread_free_indices
)
1383 private_key
->index
= GPOINTER_TO_UINT (g_thread_free_indices
->data
);
1384 g_thread_free_indices
= g_slist_delete_link (g_thread_free_indices
,
1385 g_thread_free_indices
);
1388 private_key
->index
= ++next_index
;
1391 G_UNLOCK (g_thread
);
1394 array
= g_private_get (&static_private_private
);
1397 array
= g_array_new (FALSE
, TRUE
, sizeof (GStaticPrivateNode
));
1398 g_private_set (&static_private_private
, array
);
1400 if (private_key
->index
> array
->len
)
1401 g_array_set_size (array
, private_key
->index
);
1403 node
= &g_array_index (array
, GStaticPrivateNode
, private_key
->index
- 1);
1406 node
->destroy (node
->data
);
1409 node
->destroy
= notify
;
1410 node
->owner
= private_key
;
1414 * g_static_private_free:
1415 * @private_key: a #GStaticPrivate to be freed
1417 * Releases all resources allocated to @private_key.
1419 * You don't have to call this functions for a #GStaticPrivate with an
1420 * unbounded lifetime, i.e. objects declared 'static', but if you have
1421 * a #GStaticPrivate as a member of a structure and the structure is
1422 * freed, you should also free the #GStaticPrivate.
1425 g_static_private_free (GStaticPrivate
*private_key
)
1427 guint idx
= private_key
->index
;
1432 private_key
->index
= 0;
1434 /* Freeing the per-thread data is deferred to either the
1435 * thread end or the next g_static_private_get() call for
1439 g_thread_free_indices
= g_slist_prepend (g_thread_free_indices
,
1440 GUINT_TO_POINTER (idx
));
1441 G_UNLOCK (g_thread
);
1444 /* GMutex {{{1 ------------------------------------------------------ */
1449 * Allocates and initializes a new #GMutex.
1451 * Returns: a newly allocated #GMutex. Use g_mutex_free() to free
1453 * Deprecated: 2.32: GMutex can now be statically allocated, or embedded
1454 * in structures and initialised with g_mutex_init().
1461 mutex
= g_slice_new (GMutex
);
1462 g_mutex_init (mutex
);
1471 * Destroys a @mutex that has been created with g_mutex_new().
1473 * Calling g_mutex_free() on a locked mutex may result
1474 * in undefined behaviour.
1476 * Deprecated: 2.32: GMutex can now be statically allocated, or embedded
1477 * in structures and initialised with g_mutex_init().
1480 g_mutex_free (GMutex
*mutex
)
1482 g_mutex_clear (mutex
);
1483 g_slice_free (GMutex
, mutex
);
1486 /* GCond {{{1 ------------------------------------------------------ */
1491 * Allocates and initializes a new #GCond.
1493 * Returns: a newly allocated #GCond. Free with g_cond_free()
1495 * Deprecated: 2.32: GCond can now be statically allocated, or embedded
1496 * in structures and initialised with g_cond_init().
1503 cond
= g_slice_new (GCond
);
1513 * Destroys a #GCond that has been created with g_cond_new().
1515 * Calling g_cond_free() for a #GCond on which threads are
1516 * blocking leads to undefined behaviour.
1518 * Deprecated: 2.32: GCond can now be statically allocated, or embedded
1519 * in structures and initialised with g_cond_init().
1522 g_cond_free (GCond
*cond
)
1524 g_cond_clear (cond
);
1525 g_slice_free (GCond
, cond
);
1529 * g_cond_timed_wait:
1531 * @mutex: a #GMutex that is currently locked
1532 * @abs_time: a #GTimeVal, determining the final time
1534 * Waits until this thread is woken up on @cond, but not longer than
1535 * until the time specified by @abs_time. The @mutex is unlocked before
1536 * falling asleep and locked again before resuming.
1538 * If @abs_time is %NULL, g_cond_timed_wait() acts like g_cond_wait().
1540 * This function can be used even if g_thread_init() has not yet been
1541 * called, and, in that case, will immediately return %TRUE.
1543 * To easily calculate @abs_time a combination of g_get_current_time()
1544 * and g_time_val_add() can be used.
1546 * Returns: %TRUE if @cond was signalled, or %FALSE on timeout
1548 * Deprecated:2.32: Use g_cond_wait_until() instead.
1551 g_cond_timed_wait (GCond
*cond
,
1557 if (abs_time
== NULL
)
1559 g_cond_wait (cond
, mutex
);
1563 end_time
= abs_time
->tv_sec
;
1564 end_time
*= 1000000;
1565 end_time
+= abs_time
->tv_usec
;
1567 /* would be nice if we had clock_rtoffset, but that didn't seem to
1568 * make it into the kernel yet...
1570 end_time
+= g_get_monotonic_time () - g_get_real_time ();
1572 return g_cond_wait_until (cond
, mutex
, end_time
);
1576 /* vim: set foldmethod=marker: */