Merge branch '896-variant-type-docs' into 'master'
[glib.git] / glib / deprecated / gthread-deprecated.c
blobc0ef4deff787f9167881ecede44f6036c2e9510d
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
6 * Owen Taylor
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/>.
22 #include "config.h"
24 /* we know we are deprecated here, no need for warnings */
25 #define GLIB_DISABLE_DEPRECATION_WARNINGS
27 #include "gmessages.h"
28 #include "gslice.h"
29 #include "gmain.h"
30 #include "gthread.h"
31 #include "gthreadprivate.h"
32 #include "deprecated/gthread.h"
33 #include "garray.h"
35 #include "gutils.h"
37 /* {{{1 Documentation */
39 /**
40 * SECTION:threads-deprecated
41 * @title: Deprecated thread API
42 * @short_description: old thread APIs (for reference only)
43 * @see_also: #GThread
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.
48 **/
50 /**
51 * GThreadPriority:
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
57 * Thread priorities.
59 * Deprecated:2.32: Thread priorities no longer have any effect.
62 /**
63 * GThreadFunctions:
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
86 * assertion checks
88 * This function table is no longer used by g_thread_init()
89 * to initialize the thread system.
92 /**
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 =
119 g_mutex_new,
120 g_mutex_lock,
121 g_mutex_trylock,
122 g_mutex_unlock,
123 g_mutex_free,
124 g_cond_new,
125 g_cond_signal,
126 g_cond_broadcast,
127 g_cond_wait,
128 g_cond_timed_wait,
129 g_cond_free,
130 g_private_new,
131 g_private_get,
132 g_private_set,
133 NULL,
134 g_thread_yield,
135 NULL,
136 NULL,
137 NULL,
138 NULL,
139 NULL,
142 static guint64
143 gettime (void)
145 return g_get_monotonic_time () * 1000;
148 guint64 (*g_thread_gettime) (void) = gettime;
150 /* Initialisation {{{1 ---------------------------------------------------- */
151 gboolean g_threads_got_initialized = TRUE;
154 * g_thread_init:
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
180 * of your program.
184 * g_thread_get_initialized:
186 * Indicates if g_thread_init() has been called.
188 * Returns: %TRUE if threads have been initialized.
190 * Since: 2.20
192 gboolean
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.
217 * @priority: ignored
219 * This function does nothing.
221 * Deprecated:2.32: Thread priorities no longer have any effect.
223 void
224 g_thread_set_priority (GThread *thread,
225 GThreadPriority priority)
230 * g_thread_foreach:
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.
246 * Since: 2.10
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.
252 void
253 g_thread_foreach (GFunc thread_func,
254 gpointer user_data)
256 GSList *slist = NULL;
257 GRealThread *thread;
258 g_return_if_fail (thread_func != NULL);
259 /* snapshot the list of threads for iteration */
260 G_LOCK (g_thread);
261 slist = g_slist_copy (g_thread_all_threads);
262 G_UNLOCK (g_thread);
263 /* walk the list, skipping non-existent threads */
264 while (slist)
266 GSList *node = slist;
267 slist = node->next;
268 /* check whether the current thread still exists */
269 G_LOCK (g_thread);
270 if (g_slist_find (g_thread_all_threads, node->data))
271 thread = node->data;
272 else
273 thread = NULL;
274 G_UNLOCK (g_thread);
275 if (thread)
276 thread_func (thread, user_data);
277 g_slist_free_1 (node);
281 static void
282 g_enumerable_thread_remove (gpointer data)
284 GRealThread *thread = data;
286 G_LOCK (g_thread);
287 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
288 G_UNLOCK (g_thread);
291 GPrivate enumerable_thread_private = G_PRIVATE_INIT (g_enumerable_thread_remove);
293 static void
294 g_enumerable_thread_add (GRealThread *thread)
296 G_LOCK (g_thread);
297 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
298 G_UNLOCK (g_thread);
300 g_private_set (&enumerable_thread_private, thread);
303 static gpointer
304 g_deprecated_thread_proxy (gpointer data)
306 GRealThread *real = data;
308 g_enumerable_thread_add (real);
310 return g_thread_proxy (data);
314 * g_thread_create:
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
337 GThread *
338 g_thread_create (GThreadFunc func,
339 gpointer data,
340 gboolean joinable,
341 GError **error)
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?
352 * @bound: ignored
353 * @priority: ignored
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().
363 GThread *
364 g_thread_create_full (GThreadFunc func,
365 gpointer data,
366 gulong stack_size,
367 gboolean joinable,
368 gboolean bound,
369 GThreadPriority priority,
370 GError **error)
372 GThread *thread;
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);
383 return thread;
386 /* GOnce {{{1 ------------------------------------------------------------- */
387 gboolean
388 g_once_init_enter_impl (volatile gsize *location)
390 return (g_once_init_enter) (location);
393 /* GStaticMutex {{{1 ------------------------------------------------------ */
396 * GStaticMutex:
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
406 * a GStaticMutex:
407 * |[
408 * int
409 * give_me_next_number (void)
411 * static int current_number = 0;
412 * int ret_val;
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);
419 * return ret_val;
421 * ]|
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
434 * platforms.
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
443 * functions.
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().
454 * |[
455 * GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
456 * ]|
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()
468 void
469 g_static_mutex_init (GStaticMutex *mutex)
471 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
473 g_return_if_fail (mutex);
475 *mutex = init_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
507 GMutex *
508 g_static_mutex_get_mutex_impl (GStaticMutex* mutex)
510 GMutex *result;
512 if (!g_thread_supported ())
513 return NULL;
515 result = g_atomic_pointer_get (&mutex->mutex);
517 if (!result)
519 G_LOCK (g_static_mutex);
521 result = mutex->mutex;
522 if (!result)
524 result = g_mutex_new ();
525 g_atomic_pointer_set (&mutex->mutex, result);
528 G_UNLOCK (g_static_mutex);
531 return result;
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()
587 void
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
598 * static mutexes. */
599 runtime_mutex = ((GMutex**)mutex);
601 if (*runtime_mutex)
602 g_mutex_free (*runtime_mutex);
604 *runtime_mutex = NULL;
607 /* {{{1 GStaticRecMutex */
610 * 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
628 * %TRUE.
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().
639 * |[
640 * GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
641 * ]|
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()
654 void
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);
661 *mutex = init_mutex;
664 static GRecMutex *
665 g_static_rec_mutex_get_rec_mutex_impl (GStaticRecMutex* mutex)
667 GRecMutex *result;
669 if (!g_thread_supported ())
670 return NULL;
672 result = g_atomic_pointer_get (&mutex->mutex.mutex);
674 if (!result)
676 G_LOCK (g_static_mutex);
678 result = (GRecMutex *) mutex->mutex.mutex;
679 if (!result)
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);
689 return result;
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()
703 void
704 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
706 GRecMutex *rm;
707 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
708 g_rec_mutex_lock (rm);
709 mutex->depth++;
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
720 * %TRUE.
722 * Returns: %TRUE, if @mutex could be locked.
724 * Deprecated: 2.32: Use g_rec_mutex_trylock()
726 gboolean
727 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
729 GRecMutex *rm;
730 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
732 if (g_rec_mutex_trylock (rm))
734 mutex->depth++;
735 return TRUE;
737 else
738 return FALSE;
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()
753 void
754 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
756 GRecMutex *rm;
757 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
758 mutex->depth--;
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()
772 void
773 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
774 guint depth)
776 GRecMutex *rm;
778 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
779 while (depth--)
781 g_rec_mutex_lock (rm);
782 mutex->depth++;
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
796 * function.
798 * Returns: number of times @mutex has been locked by the current
799 * thread.
801 * Deprecated: 2.32: Use g_rec_mutex_unlock()
803 guint
804 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
806 GRecMutex *rm;
807 gint depth;
808 gint i;
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;
814 i = mutex->depth;
815 mutex->depth = 0;
817 while (i--)
818 g_rec_mutex_unlock (rm);
820 return depth;
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()
836 void
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 ----------------------------------------------------- */
853 * GStaticRWLock:
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:
862 * |[
863 * GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
864 * GPtrArray *array;
866 * gpointer
867 * my_array_get (guint index)
869 * gpointer retval = NULL;
871 * if (!array)
872 * return 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);
879 * return retval;
882 * void
883 * my_array_set (guint index, gpointer data)
885 * g_static_rw_lock_writer_lock (&rwlock);
887 * if (!array)
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);
896 * ]|
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
926 * #GStaticMutex.
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().
939 * |[
940 * GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
941 * ]|
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
954 void
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);
961 *lock = init_lock;
964 inline static void
965 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
967 if (!*cond)
968 *cond = g_cond_new ();
969 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
972 inline static void
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
999 void
1000 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
1002 g_return_if_fail (lock);
1004 if (!g_threads_got_initialized)
1005 return;
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
1030 gboolean
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)
1038 return TRUE;
1040 g_static_mutex_lock (&lock->mutex);
1041 if (!lock->have_writer && !lock->want_to_write)
1043 lock->read_counter++;
1044 ret_val = TRUE;
1046 g_static_mutex_unlock (&lock->mutex);
1047 return ret_val;
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
1060 void
1061 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
1063 g_return_if_fail (lock);
1065 if (!g_threads_got_initialized)
1066 return;
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
1089 void
1090 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
1092 g_return_if_fail (lock);
1094 if (!g_threads_got_initialized)
1095 return;
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
1119 gboolean
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)
1127 return TRUE;
1129 g_static_mutex_lock (&lock->mutex);
1130 if (!lock->have_writer && !lock->read_counter)
1132 lock->have_writer = TRUE;
1133 ret_val = TRUE;
1135 g_static_mutex_unlock (&lock->mutex);
1136 return ret_val;
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
1152 void
1153 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
1155 g_return_if_fail (lock);
1157 if (!g_threads_got_initialized)
1158 return;
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
1179 void
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 ------------------------------------------------------ */
1200 * g_private_new:
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)
1210 GPrivate *
1211 g_private_new (GDestroyNotify notify)
1213 GPrivate tmp = G_PRIVATE_INIT (notify);
1214 GPrivate *key;
1216 key = g_slice_new (GPrivate);
1217 *key = tmp;
1219 return key;
1222 /* {{{1 GStaticPrivate */
1224 typedef struct _GStaticPrivateNode GStaticPrivateNode;
1225 struct _GStaticPrivateNode
1227 gpointer data;
1228 GDestroyNotify destroy;
1229 GStaticPrivate *owner;
1232 static void
1233 g_static_private_cleanup (gpointer data)
1235 GArray *array = data;
1236 guint i;
1238 for (i = 0; i < array->len; i++ )
1240 GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
1241 if (node->destroy)
1242 node->destroy (node->data);
1245 g_array_free (array, TRUE);
1248 GPrivate static_private_private = G_PRIVATE_INIT (g_static_private_cleanup);
1251 * GStaticPrivate:
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:
1259 * |[
1260 * int
1261 * give_me_next_number ()
1263 * static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1264 * int *current_number = g_static_private_get (&current_number_key);
1266 * if (!current_number)
1268 * current_number = g_new (int, 1);
1269 * *current_number = 0;
1270 * g_static_private_set (&current_number_key, current_number, g_free);
1273 * *current_number = calc_next_number (*current_number);
1275 * return *current_number;
1277 * ]|
1281 * G_STATIC_PRIVATE_INIT:
1283 * Every #GStaticPrivate must be initialized with this macro, before it
1284 * can be used.
1286 * |[
1287 * GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1288 * ]|
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.
1298 void
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
1314 gpointer
1315 g_static_private_get (GStaticPrivate *private_key)
1317 GArray *array;
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))
1335 if (node->destroy)
1336 node->destroy (node->data);
1337 node->destroy = NULL;
1338 node->data = NULL;
1339 node->owner = NULL;
1341 ret = node->data;
1344 return ret;
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
1362 * g_thread_init().
1364 * @notify is used quite differently from @destructor in g_private_new().
1366 void
1367 g_static_private_set (GStaticPrivate *private_key,
1368 gpointer data,
1369 GDestroyNotify notify)
1371 GArray *array;
1372 static guint next_index = 0;
1373 GStaticPrivateNode *node;
1375 if (!private_key->index)
1377 G_LOCK (g_thread);
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);
1387 else
1388 private_key->index = ++next_index;
1391 G_UNLOCK (g_thread);
1394 array = g_private_get (&static_private_private);
1395 if (!array)
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);
1405 if (node->destroy)
1406 node->destroy (node->data);
1408 node->data = 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.
1424 void
1425 g_static_private_free (GStaticPrivate *private_key)
1427 guint idx = private_key->index;
1429 if (!idx)
1430 return;
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
1436 * the same index.
1438 G_LOCK (g_thread);
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 ------------------------------------------------------ */
1447 * g_mutex_new:
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().
1456 GMutex *
1457 g_mutex_new (void)
1459 GMutex *mutex;
1461 mutex = g_slice_new (GMutex);
1462 g_mutex_init (mutex);
1464 return mutex;
1468 * g_mutex_free:
1469 * @mutex: a #GMutex
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().
1479 void
1480 g_mutex_free (GMutex *mutex)
1482 g_mutex_clear (mutex);
1483 g_slice_free (GMutex, mutex);
1486 /* GCond {{{1 ------------------------------------------------------ */
1489 * g_cond_new:
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().
1498 GCond *
1499 g_cond_new (void)
1501 GCond *cond;
1503 cond = g_slice_new (GCond);
1504 g_cond_init (cond);
1506 return cond;
1510 * g_cond_free:
1511 * @cond: a #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().
1521 void
1522 g_cond_free (GCond *cond)
1524 g_cond_clear (cond);
1525 g_slice_free (GCond, cond);
1529 * g_cond_timed_wait:
1530 * @cond: a #GCond
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.
1550 gboolean
1551 g_cond_timed_wait (GCond *cond,
1552 GMutex *mutex,
1553 GTimeVal *abs_time)
1555 gint64 end_time;
1557 if (abs_time == NULL)
1559 g_cond_wait (cond, mutex);
1560 return TRUE;
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);
1575 /* {{{1 Epilogue */
1576 /* vim: set foldmethod=marker: */