1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 *
4 * gthread.c: MT safety related functions
5 * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
6 * Owen Taylor
7 *
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.
12 *
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.
17 *
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/>.
20 */
21
22 #include "config.h"
23
24 /* we know we are deprecated here, no need for warnings */
25 #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
26 #define GLIB_DISABLE_DEPRECATION_WARNINGS
27 #endif
28
29 #include "gmessages.h"
30 #include "gslice.h"
31 #include "gmain.h"
32 #include "gthread.h"
33 #include "gthreadprivate.h"
34 #include "deprecated/gthread.h"
35 #include "garray.h"
36
37 #include "gutils.h"
38
39 /* {{{1 Documentation */
40
41 /**
42 * SECTION:threads-deprecated
43 * @title: Deprecated thread API
44 * @short_description: old thread APIs (for reference only)
45 * @see_also: #GThread
46 *
47 * These APIs are deprecated. You should not use them in new code.
48 * This section remains only to assist with understanding code that was
49 * written to use these APIs at some point in the past.
50 **/
51
52 /**
53 * GThreadPriority:
54 * @G_THREAD_PRIORITY_LOW: a priority lower than normal
55 * @G_THREAD_PRIORITY_NORMAL: the default priority
56 * @G_THREAD_PRIORITY_HIGH: a priority higher than normal
57 * @G_THREAD_PRIORITY_URGENT: the highest priority
58 *
59 * Thread priorities.
60 *
61 * Deprecated:2.32: Thread priorities no longer have any effect.
62 */
63
64 /**
65 * GThreadFunctions:
66 * @mutex_new: virtual function pointer for g_mutex_new()
67 * @mutex_lock: virtual function pointer for g_mutex_lock()
68 * @mutex_trylock: virtual function pointer for g_mutex_trylock()
69 * @mutex_unlock: virtual function pointer for g_mutex_unlock()
70 * @mutex_free: virtual function pointer for g_mutex_free()
71 * @cond_new: virtual function pointer for g_cond_new()
72 * @cond_signal: virtual function pointer for g_cond_signal()
73 * @cond_broadcast: virtual function pointer for g_cond_broadcast()
74 * @cond_wait: virtual function pointer for g_cond_wait()
75 * @cond_timed_wait: virtual function pointer for g_cond_timed_wait()
76 * @cond_free: virtual function pointer for g_cond_free()
77 * @private_new: virtual function pointer for g_private_new()
78 * @private_get: virtual function pointer for g_private_get()
79 * @private_set: virtual function pointer for g_private_set()
80 * @thread_create: virtual function pointer for g_thread_create()
81 * @thread_yield: virtual function pointer for g_thread_yield()
82 * @thread_join: virtual function pointer for g_thread_join()
83 * @thread_exit: virtual function pointer for g_thread_exit()
84 * @thread_set_priority: virtual function pointer for
85 * g_thread_set_priority()
86 * @thread_self: virtual function pointer for g_thread_self()
87 * @thread_equal: used internally by recursive mutex locks and by some
88 * assertion checks
89 *
90 * This function table is no longer used by g_thread_init()
91 * to initialize the thread system.
92 */
93
94 /**
95 * G_THREADS_IMPL_POSIX:
96 *
97 * This macro is defined if POSIX style threads are used.
98 *
99 * Deprecated:2.32:POSIX threads are in use on all non-Windows systems.
100 * Use G_OS_WIN32 to detect Windows.
101 */
102
103 /**
104 * G_THREADS_IMPL_WIN32:
105 *
106 * This macro is defined if Windows style threads are used.
107 *
108 * Deprecated:2.32:Use G_OS_WIN32 to detect Windows.
109 */
110
111
112 /* {{{1 Exported Variables */
113
114 /* Set this FALSE to have previously-compiled GStaticMutex code use the
115 * slow path (ie: call into us) to avoid compatibility problems.
116 */
117 gboolean g_thread_use_default_impl = FALSE;
118
119 GThreadFunctions g_thread_functions_for_glib_use =
120 {
121 g_mutex_new,
122 g_mutex_lock,
123 g_mutex_trylock,
124 g_mutex_unlock,
125 g_mutex_free,
126 g_cond_new,
127 g_cond_signal,
128 g_cond_broadcast,
129 g_cond_wait,
130 g_cond_timed_wait,
131 g_cond_free,
132 g_private_new,
133 g_private_get,
134 g_private_set,
135 NULL,
136 g_thread_yield,
137 NULL,
138 NULL,
139 NULL,
140 NULL,
141 NULL,
142 };
143
144 static guint64
gettime(void)145 gettime (void)
146 {
147 return g_get_monotonic_time () * 1000;
148 }
149
150 guint64 (*g_thread_gettime) (void) = gettime;
151
152 /* Initialisation {{{1 ---------------------------------------------------- */
153 gboolean g_threads_got_initialized = TRUE;
154
155 /**
156 * g_thread_init:
157 * @vtable: a function table of type #GThreadFunctions, that provides
158 * the entry points to the thread system to be used. Since 2.32,
159 * this parameter is ignored and should always be %NULL
160 *
161 * If you use GLib from more than one thread, you must initialize the
162 * thread system by calling g_thread_init().
163 *
164 * Since version 2.24, calling g_thread_init() multiple times is allowed,
165 * but nothing happens except for the first call.
166 *
167 * Since version 2.32, GLib does not support custom thread implementations
168 * anymore and the @vtable parameter is ignored and you should pass %NULL.
169 *
170 * <note><para>g_thread_init() must not be called directly or indirectly
171 * in a callback from GLib. Also no mutexes may be currently locked while
172 * calling g_thread_init().</para></note>
173 *
174 * <note><para>To use g_thread_init() in your program, you have to link
175 * with the libraries that the command <command>pkg-config --libs
176 * gthread-2.0</command> outputs. This is not the case for all the
177 * other thread-related functions of GLib. Those can be used without
178 * having to link with the thread libraries.</para></note>
179 *
180 * Deprecated:2.32: This function is no longer necessary. The GLib
181 * threading system is automatically initialized at the start
182 * of your program.
183 */
184
185 /**
186 * g_thread_get_initialized:
187 *
188 * Indicates if g_thread_init() has been called.
189 *
190 * Returns: %TRUE if threads have been initialized.
191 *
192 * Since: 2.20
193 */
194 gboolean
g_thread_get_initialized(void)195 g_thread_get_initialized (void)
196 {
197 return g_thread_supported ();
198 }
199
200 /* We need this for ABI compatibility */
201 GLIB_AVAILABLE_IN_ALL
202 void g_thread_init_glib (void);
g_thread_init_glib(void)203 void g_thread_init_glib (void) { }
204
205 /* Internal variables {{{1 */
206
207 static GSList *g_thread_all_threads = NULL;
208 static GSList *g_thread_free_indices = NULL;
209
210 /* Protects g_thread_all_threads and g_thread_free_indices */
211 G_LOCK_DEFINE_STATIC (g_static_mutex);
212 G_LOCK_DEFINE_STATIC (g_thread);
213
214 /* Misc. GThread functions {{{1 */
215
216 /**
217 * g_thread_set_priority:
218 * @thread: a #GThread.
219 * @priority: ignored
220 *
221 * This function does nothing.
222 *
223 * Deprecated:2.32: Thread priorities no longer have any effect.
224 */
225 void
g_thread_set_priority(GThread * thread,GThreadPriority priority)226 g_thread_set_priority (GThread *thread,
227 GThreadPriority priority)
228 {
229 }
230
231 /**
232 * g_thread_foreach:
233 * @thread_func: function to call for all #GThread structures
234 * @user_data: second argument to @thread_func
235 *
236 * Call @thread_func on all #GThreads that have been
237 * created with g_thread_create().
238 *
239 * Note that threads may decide to exit while @thread_func is
240 * running, so without intimate knowledge about the lifetime of
241 * foreign threads, @thread_func shouldn't access the GThread*
242 * pointer passed in as first argument. However, @thread_func will
243 * not be called for threads which are known to have exited already.
244 *
245 * Due to thread lifetime checks, this function has an execution complexity
246 * which is quadratic in the number of existing threads.
247 *
248 * Since: 2.10
249 *
250 * Deprecated:2.32: There aren't many things you can do with a #GThread,
251 * except comparing it with one that was returned from g_thread_create().
252 * There are better ways to find out if your thread is still alive.
253 */
254 void
g_thread_foreach(GFunc thread_func,gpointer user_data)255 g_thread_foreach (GFunc thread_func,
256 gpointer user_data)
257 {
258 GSList *slist = NULL;
259 GRealThread *thread;
260 g_return_if_fail (thread_func != NULL);
261 /* snapshot the list of threads for iteration */
262 G_LOCK (g_thread);
263 slist = g_slist_copy (g_thread_all_threads);
264 G_UNLOCK (g_thread);
265 /* walk the list, skipping non-existent threads */
266 while (slist)
267 {
268 GSList *node = slist;
269 slist = node->next;
270 /* check whether the current thread still exists */
271 G_LOCK (g_thread);
272 if (g_slist_find (g_thread_all_threads, node->data))
273 thread = node->data;
274 else
275 thread = NULL;
276 G_UNLOCK (g_thread);
277 if (thread)
278 thread_func (thread, user_data);
279 g_slist_free_1 (node);
280 }
281 }
282
283 static void
g_enumerable_thread_remove(gpointer data)284 g_enumerable_thread_remove (gpointer data)
285 {
286 GRealThread *thread = data;
287
288 G_LOCK (g_thread);
289 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
290 G_UNLOCK (g_thread);
291 }
292
293 GPrivate enumerable_thread_private = G_PRIVATE_INIT (g_enumerable_thread_remove);
294
295 static void
g_enumerable_thread_add(GRealThread * thread)296 g_enumerable_thread_add (GRealThread *thread)
297 {
298 G_LOCK (g_thread);
299 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
300 G_UNLOCK (g_thread);
301
302 g_private_set (&enumerable_thread_private, thread);
303 }
304
305 static gpointer
g_deprecated_thread_proxy(gpointer data)306 g_deprecated_thread_proxy (gpointer data)
307 {
308 GRealThread *real = data;
309
310 g_enumerable_thread_add (real);
311
312 return g_thread_proxy (data);
313 }
314
315 /**
316 * g_thread_create:
317 * @func: a function to execute in the new thread
318 * @data: an argument to supply to the new thread
319 * @joinable: should this thread be joinable?
320 * @error: return location for error, or %NULL
321 *
322 * This function creates a new thread.
323 *
324 * The new thread executes the function @func with the argument @data.
325 * If the thread was created successfully, it is returned.
326 *
327 * @error can be %NULL to ignore errors, or non-%NULL to report errors.
328 * The error is set, if and only if the function returns %NULL.
329 *
330 * This function returns a reference to the created thread only if
331 * @joinable is %TRUE. In that case, you must free this reference by
332 * calling g_thread_unref() or g_thread_join(). If @joinable is %FALSE
333 * then you should probably not touch the return value.
334 *
335 * Returns: the new #GThread on success
336 *
337 * Deprecated:2.32: Use g_thread_new() instead
338 */
339 GThread *
g_thread_create(GThreadFunc func,gpointer data,gboolean joinable,GError ** error)340 g_thread_create (GThreadFunc func,
341 gpointer data,
342 gboolean joinable,
343 GError **error)
344 {
345 return g_thread_create_full (func, data, 0, joinable, 0, 0, error);
346 }
347
348 /**
349 * g_thread_create_full:
350 * @func: a function to execute in the new thread.
351 * @data: an argument to supply to the new thread.
352 * @stack_size: a stack size for the new thread.
353 * @joinable: should this thread be joinable?
354 * @bound: ignored
355 * @priority: ignored
356 * @error: return location for error.
357 *
358 * This function creates a new thread.
359 *
360 * Returns: the new #GThread on success.
361 *
362 * Deprecated:2.32: The @bound and @priority arguments are now ignored.
363 * Use g_thread_new().
364 */
365 GThread *
g_thread_create_full(GThreadFunc func,gpointer data,gulong stack_size,gboolean joinable,gboolean bound,GThreadPriority priority,GError ** error)366 g_thread_create_full (GThreadFunc func,
367 gpointer data,
368 gulong stack_size,
369 gboolean joinable,
370 gboolean bound,
371 GThreadPriority priority,
372 GError **error)
373 {
374 GThread *thread;
375
376 thread = g_thread_new_internal (NULL, g_deprecated_thread_proxy,
377 func, data, stack_size, NULL, error);
378
379 if (thread && !joinable)
380 {
381 thread->joinable = FALSE;
382 g_thread_unref (thread);
383 }
384
385 return thread;
386 }
387
388 /* GOnce {{{1 ------------------------------------------------------------- */
389 gboolean
g_once_init_enter_impl(volatile gsize * location)390 g_once_init_enter_impl (volatile gsize *location)
391 {
392 return (g_once_init_enter) (location);
393 }
394
395 /* GStaticMutex {{{1 ------------------------------------------------------ */
396
397 /**
398 * GStaticMutex:
399 *
400 * A #GStaticMutex works like a #GMutex.
401 *
402 * Prior to GLib 2.32, GStaticMutex had the significant advantage
403 * that it doesn't need to be created at run-time, but can be defined
404 * at compile-time. Since 2.32, #GMutex can be statically allocated
405 * as well, and GStaticMutex has been deprecated.
406 *
407 * Here is a version of our give_me_next_number() example using
408 * a GStaticMutex:
409 * |[
410 * int
411 * give_me_next_number (void)
412 * {
413 * static int current_number = 0;
414 * int ret_val;
415 * static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
416 *
417 * g_static_mutex_lock (&mutex);
418 * ret_val = current_number = calc_next_number (current_number);
419 * g_static_mutex_unlock (&mutex);
420 *
421 * return ret_val;
422 * }
423 * ]|
424 *
425 * Sometimes you would like to dynamically create a mutex. If you don't
426 * want to require prior calling to g_thread_init(), because your code
427 * should also be usable in non-threaded programs, you are not able to
428 * use g_mutex_new() and thus #GMutex, as that requires a prior call to
429 * g_thread_init(). In these cases you can also use a #GStaticMutex.
430 * It must be initialized with g_static_mutex_init() before using it
431 * and freed with with g_static_mutex_free() when not needed anymore to
432 * free up any allocated resources.
433 *
434 * Even though #GStaticMutex is not opaque, it should only be used with
435 * the following functions, as it is defined differently on different
436 * platforms.
437 *
438 * All of the g_static_mutex_* functions apart from
439 * g_static_mutex_get_mutex() can also be used even if g_thread_init()
440 * has not yet been called. Then they do nothing, apart from
441 * g_static_mutex_trylock() which does nothing but returning %TRUE.
442 *
443 * All of the g_static_mutex_* functions are actually macros. Apart from
444 * taking their addresses, you can however use them as if they were
445 * functions.
446 */
447
448 /**
449 * G_STATIC_MUTEX_INIT:
450 *
451 * A #GStaticMutex must be initialized with this macro, before it can
452 * be used. This macro can used be to initialize a variable, but it
453 * cannot be assigned to a variable. In that case you have to use
454 * g_static_mutex_init().
455 *
456 * |[
457 * GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
458 * ]|
459 **/
460
461 /**
462 * g_static_mutex_init:
463 * @mutex: a #GStaticMutex to be initialized.
464 *
465 * Initializes @mutex.
466 * Alternatively you can initialize it with #G_STATIC_MUTEX_INIT.
467 *
468 * Deprecated: 2.32: Use g_mutex_init()
469 */
470 void
g_static_mutex_init(GStaticMutex * mutex)471 g_static_mutex_init (GStaticMutex *mutex)
472 {
473 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
474
475 g_return_if_fail (mutex);
476
477 *mutex = init_mutex;
478 }
479
480 /* IMPLEMENTATION NOTE:
481 *
482 * On some platforms a GStaticMutex is actually a normal GMutex stored
483 * inside of a structure instead of being allocated dynamically. We can
484 * only do this for platforms on which we know, in advance, how to
485 * allocate (size) and initialise (value) that memory.
486 *
487 * On other platforms, a GStaticMutex is nothing more than a pointer to
488 * a GMutex. In that case, the first access we make to the static mutex
489 * must first allocate the normal GMutex and store it into the pointer.
490 *
491 * configure.ac writes macros into glibconfig.h to determine if
492 * g_static_mutex_get_mutex() accesses the structure in memory directly
493 * (on platforms where we are able to do that) or if it ends up here,
494 * where we may have to allocate the GMutex before returning it.
495 */
496
497 /**
498 * g_static_mutex_get_mutex:
499 * @mutex: a #GStaticMutex.
500 *
501 * For some operations (like g_cond_wait()) you must have a #GMutex
502 * instead of a #GStaticMutex. This function will return the
503 * corresponding #GMutex for @mutex.
504 *
505 * Returns: the #GMutex corresponding to @mutex.
506 *
507 * Deprecated: 2.32: Just use a #GMutex
508 */
509 GMutex *
g_static_mutex_get_mutex_impl(GStaticMutex * mutex)510 g_static_mutex_get_mutex_impl (GStaticMutex* mutex)
511 {
512 GMutex *result;
513
514 if (!g_thread_supported ())
515 return NULL;
516
517 result = g_atomic_pointer_get (&mutex->mutex);
518
519 if (!result)
520 {
521 G_LOCK (g_static_mutex);
522
523 result = mutex->mutex;
524 if (!result)
525 {
526 result = g_mutex_new ();
527 g_atomic_pointer_set (&mutex->mutex, result);
528 }
529
530 G_UNLOCK (g_static_mutex);
531 }
532
533 return result;
534 }
535
536 /* IMPLEMENTATION NOTE:
537 *
538 * g_static_mutex_lock(), g_static_mutex_trylock() and
539 * g_static_mutex_unlock() are all preprocessor macros that wrap the
540 * corresponding g_mutex_*() function around a call to
541 * g_static_mutex_get_mutex().
542 */
543
544 /**
545 * g_static_mutex_lock:
546 * @mutex: a #GStaticMutex.
547 *
548 * Works like g_mutex_lock(), but for a #GStaticMutex.
549 *
550 * Deprecated: 2.32: Use g_mutex_lock()
551 */
552
553 /**
554 * g_static_mutex_trylock:
555 * @mutex: a #GStaticMutex.
556 *
557 * Works like g_mutex_trylock(), but for a #GStaticMutex.
558 *
559 * Returns: %TRUE, if the #GStaticMutex could be locked.
560 *
561 * Deprecated: 2.32: Use g_mutex_trylock()
562 */
563
564 /**
565 * g_static_mutex_unlock:
566 * @mutex: a #GStaticMutex.
567 *
568 * Works like g_mutex_unlock(), but for a #GStaticMutex.
569 *
570 * Deprecated: 2.32: Use g_mutex_unlock()
571 */
572
573 /**
574 * g_static_mutex_free:
575 * @mutex: a #GStaticMutex to be freed.
576 *
577 * Releases all resources allocated to @mutex.
578 *
579 * You don't have to call this functions for a #GStaticMutex with an
580 * unbounded lifetime, i.e. objects declared 'static', but if you have
581 * a #GStaticMutex as a member of a structure and the structure is
582 * freed, you should also free the #GStaticMutex.
583 *
584 * Calling g_static_mutex_free() on a locked mutex may result in
585 * undefined behaviour.
586 *
587 * Deprecated: 2.32: Use g_mutex_clear()
588 */
589 void
g_static_mutex_free(GStaticMutex * mutex)590 g_static_mutex_free (GStaticMutex* mutex)
591 {
592 GMutex **runtime_mutex;
593
594 g_return_if_fail (mutex);
595
596 /* The runtime_mutex is the first (or only) member of GStaticMutex,
597 * see both versions (of glibconfig.h) in configure.ac. Note, that
598 * this variable is NULL, if g_thread_init() hasn't been called or
599 * if we're using the default thread implementation and it provides
600 * static mutexes. */
601 runtime_mutex = ((GMutex**)mutex);
602
603 if (*runtime_mutex)
604 g_mutex_free (*runtime_mutex);
605
606 *runtime_mutex = NULL;
607 }
608
609 /* {{{1 GStaticRecMutex */
610
611 /**
612 * GStaticRecMutex:
613 *
614 * A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
615 * multiple times by one thread. If you enter it n times, you have to
616 * unlock it n times again to let other threads lock it. An exception
617 * is the function g_static_rec_mutex_unlock_full(): that allows you to
618 * unlock a #GStaticRecMutex completely returning the depth, (i.e. the
619 * number of times this mutex was locked). The depth can later be used
620 * to restore the state of the #GStaticRecMutex by calling
621 * g_static_rec_mutex_lock_full(). In GLib 2.32, #GStaticRecMutex has
622 * been deprecated in favor of #GRecMutex.
623 *
624 * Even though #GStaticRecMutex is not opaque, it should only be used
625 * with the following functions.
626 *
627 * All of the g_static_rec_mutex_* functions can be used even if
628 * g_thread_init() has not been called. Then they do nothing, apart
629 * from g_static_rec_mutex_trylock(), which does nothing but returning
630 * %TRUE.
631 */
632
633 /**
634 * G_STATIC_REC_MUTEX_INIT:
635 *
636 * A #GStaticRecMutex must be initialized with this macro before it can
637 * be used. This macro can used be to initialize a variable, but it
638 * cannot be assigned to a variable. In that case you have to use
639 * g_static_rec_mutex_init().
640 *
641 * |[
642 * GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
643 * ]|
644 */
645
646 /**
647 * g_static_rec_mutex_init:
648 * @mutex: a #GStaticRecMutex to be initialized.
649 *
650 * A #GStaticRecMutex must be initialized with this function before it
651 * can be used. Alternatively you can initialize it with
652 * #G_STATIC_REC_MUTEX_INIT.
653 *
654 * Deprecated: 2.32: Use g_rec_mutex_init()
655 */
656 void
g_static_rec_mutex_init(GStaticRecMutex * mutex)657 g_static_rec_mutex_init (GStaticRecMutex *mutex)
658 {
659 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
660
661 g_return_if_fail (mutex);
662
663 *mutex = init_mutex;
664 }
665
666 static GRecMutex *
g_static_rec_mutex_get_rec_mutex_impl(GStaticRecMutex * mutex)667 g_static_rec_mutex_get_rec_mutex_impl (GStaticRecMutex* mutex)
668 {
669 GRecMutex *result;
670
671 if (!g_thread_supported ())
672 return NULL;
673
674 result = (GRecMutex *) g_atomic_pointer_get (&mutex->mutex.mutex);
675
676 if (!result)
677 {
678 G_LOCK (g_static_mutex);
679
680 result = (GRecMutex *) mutex->mutex.mutex;
681 if (!result)
682 {
683 result = g_slice_new (GRecMutex);
684 g_rec_mutex_init (result);
685 g_atomic_pointer_set (&mutex->mutex.mutex, (GMutex *) result);
686 }
687
688 G_UNLOCK (g_static_mutex);
689 }
690
691 return result;
692 }
693
694 /**
695 * g_static_rec_mutex_lock:
696 * @mutex: a #GStaticRecMutex to lock.
697 *
698 * Locks @mutex. If @mutex is already locked by another thread, the
699 * current thread will block until @mutex is unlocked by the other
700 * thread. If @mutex is already locked by the calling thread, this
701 * functions increases the depth of @mutex and returns immediately.
702 *
703 * Deprecated: 2.32: Use g_rec_mutex_lock()
704 */
705 void
g_static_rec_mutex_lock(GStaticRecMutex * mutex)706 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
707 {
708 GRecMutex *rm;
709 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
710 g_rec_mutex_lock (rm);
711 mutex->depth++;
712 }
713
714 /**
715 * g_static_rec_mutex_trylock:
716 * @mutex: a #GStaticRecMutex to lock.
717 *
718 * Tries to lock @mutex. If @mutex is already locked by another thread,
719 * it immediately returns %FALSE. Otherwise it locks @mutex and returns
720 * %TRUE. If @mutex is already locked by the calling thread, this
721 * functions increases the depth of @mutex and immediately returns
722 * %TRUE.
723 *
724 * Returns: %TRUE, if @mutex could be locked.
725 *
726 * Deprecated: 2.32: Use g_rec_mutex_trylock()
727 */
728 gboolean
g_static_rec_mutex_trylock(GStaticRecMutex * mutex)729 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
730 {
731 GRecMutex *rm;
732 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
733
734 if (g_rec_mutex_trylock (rm))
735 {
736 mutex->depth++;
737 return TRUE;
738 }
739 else
740 return FALSE;
741 }
742
743 /**
744 * g_static_rec_mutex_unlock:
745 * @mutex: a #GStaticRecMutex to unlock.
746 *
747 * Unlocks @mutex. Another thread will be allowed to lock @mutex only
748 * when it has been unlocked as many times as it had been locked
749 * before. If @mutex is completely unlocked and another thread is
750 * blocked in a g_static_rec_mutex_lock() call for @mutex, it will be
751 * woken and can lock @mutex itself.
752 *
753 * Deprecated: 2.32: Use g_rec_mutex_unlock()
754 */
755 void
g_static_rec_mutex_unlock(GStaticRecMutex * mutex)756 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
757 {
758 GRecMutex *rm;
759 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
760 mutex->depth--;
761 g_rec_mutex_unlock (rm);
762 }
763
764 /**
765 * g_static_rec_mutex_lock_full:
766 * @mutex: a #GStaticRecMutex to lock.
767 * @depth: number of times this mutex has to be unlocked to be
768 * completely unlocked.
769 *
770 * Works like calling g_static_rec_mutex_lock() for @mutex @depth times.
771 *
772 * Deprecated: 2.32: Use g_rec_mutex_lock()
773 */
774 void
g_static_rec_mutex_lock_full(GStaticRecMutex * mutex,guint depth)775 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
776 guint depth)
777 {
778 GRecMutex *rm;
779
780 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
781 while (depth--)
782 {
783 g_rec_mutex_lock (rm);
784 mutex->depth++;
785 }
786 }
787
788 /**
789 * g_static_rec_mutex_unlock_full:
790 * @mutex: a #GStaticRecMutex to completely unlock.
791 *
792 * Completely unlocks @mutex. If another thread is blocked in a
793 * g_static_rec_mutex_lock() call for @mutex, it will be woken and can
794 * lock @mutex itself. This function returns the number of times that
795 * @mutex has been locked by the current thread. To restore the state
796 * before the call to g_static_rec_mutex_unlock_full() you can call
797 * g_static_rec_mutex_lock_full() with the depth returned by this
798 * function.
799 *
800 * Returns: number of times @mutex has been locked by the current
801 * thread.
802 *
803 * Deprecated: 2.32: Use g_rec_mutex_unlock()
804 */
805 guint
g_static_rec_mutex_unlock_full(GStaticRecMutex * mutex)806 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
807 {
808 GRecMutex *rm;
809 gint depth;
810 gint i;
811
812 rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
813
814 /* all access to mutex->depth done while still holding the lock */
815 depth = mutex->depth;
816 i = mutex->depth;
817 mutex->depth = 0;
818
819 while (i--)
820 g_rec_mutex_unlock (rm);
821
822 return depth;
823 }
824
825 /**
826 * g_static_rec_mutex_free:
827 * @mutex: a #GStaticRecMutex to be freed.
828 *
829 * Releases all resources allocated to a #GStaticRecMutex.
830 *
831 * You don't have to call this functions for a #GStaticRecMutex with an
832 * unbounded lifetime, i.e. objects declared 'static', but if you have
833 * a #GStaticRecMutex as a member of a structure and the structure is
834 * freed, you should also free the #GStaticRecMutex.
835 *
836 * Deprecated: 2.32: Use g_rec_mutex_clear()
837 */
838 void
g_static_rec_mutex_free(GStaticRecMutex * mutex)839 g_static_rec_mutex_free (GStaticRecMutex *mutex)
840 {
841 g_return_if_fail (mutex);
842
843 if (mutex->mutex.mutex)
844 {
845 GRecMutex *rm = (GRecMutex *) mutex->mutex.mutex;
846
847 g_rec_mutex_clear (rm);
848 g_slice_free (GRecMutex, rm);
849 }
850 }
851
852 /* GStaticRWLock {{{1 ----------------------------------------------------- */
853
854 /**
855 * GStaticRWLock:
856 *
857 * The #GStaticRWLock struct represents a read-write lock. A read-write
858 * lock can be used for protecting data that some portions of code only
859 * read from, while others also write. In such situations it is
860 * desirable that several readers can read at once, whereas of course
861 * only one writer may write at a time.
862 *
863 * Take a look at the following example:
864 * |[
865 * GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
866 * GPtrArray *array;
867 *
868 * gpointer
869 * my_array_get (guint index)
870 * {
871 * gpointer retval = NULL;
872 *
873 * if (!array)
874 * return NULL;
875 *
876 * g_static_rw_lock_reader_lock (&rwlock);
877 * if (index < array->len)
878 * retval = g_ptr_array_index (array, index);
879 * g_static_rw_lock_reader_unlock (&rwlock);
880 *
881 * return retval;
882 * }
883 *
884 * void
885 * my_array_set (guint index, gpointer data)
886 * {
887 * g_static_rw_lock_writer_lock (&rwlock);
888 *
889 * if (!array)
890 * array = g_ptr_array_new ();
891 *
892 * if (index >= array->len)
893 * g_ptr_array_set_size (array, index + 1);
894 * g_ptr_array_index (array, index) = data;
895 *
896 * g_static_rw_lock_writer_unlock (&rwlock);
897 * }
898 * ]|
899 *
900 * This example shows an array which can be accessed by many readers
901 * (the my_array_get() function) simultaneously, whereas the writers
902 * (the my_array_set() function) will only be allowed once at a time
903 * and only if no readers currently access the array. This is because
904 * of the potentially dangerous resizing of the array. Using these
905 * functions is fully multi-thread safe now.
906 *
907 * Most of the time, writers should have precedence over readers. That
908 * means, for this implementation, that as soon as a writer wants to
909 * lock the data, no other reader is allowed to lock the data, whereas,
910 * of course, the readers that already have locked the data are allowed
911 * to finish their operation. As soon as the last reader unlocks the
912 * data, the writer will lock it.
913 *
914 * Even though #GStaticRWLock is not opaque, it should only be used
915 * with the following functions.
916 *
917 * All of the g_static_rw_lock_* functions can be used even if
918 * g_thread_init() has not been called. Then they do nothing, apart
919 * from g_static_rw_lock_*_trylock, which does nothing but returning %TRUE.
920 *
921 * A read-write lock has a higher overhead than a mutex. For example, both
922 * g_static_rw_lock_reader_lock() and g_static_rw_lock_reader_unlock() have
923 * to lock and unlock a #GStaticMutex, so it takes at least twice the time
924 * to lock and unlock a #GStaticRWLock that it does to lock and unlock a
925 * #GStaticMutex. So only data structures that are accessed by multiple
926 * readers, and which keep the lock for a considerable time justify a
927 * #GStaticRWLock. The above example most probably would fare better with a
928 * #GStaticMutex.
929 *
930 * Deprecated: 2.32: Use a #GRWLock instead
931 **/
932
933 /**
934 * G_STATIC_RW_LOCK_INIT:
935 *
936 * A #GStaticRWLock must be initialized with this macro before it can
937 * be used. This macro can used be to initialize a variable, but it
938 * cannot be assigned to a variable. In that case you have to use
939 * g_static_rw_lock_init().
940 *
941 * |[
942 * GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
943 * ]|
944 */
945
946 /**
947 * g_static_rw_lock_init:
948 * @lock: a #GStaticRWLock to be initialized.
949 *
950 * A #GStaticRWLock must be initialized with this function before it
951 * can be used. Alternatively you can initialize it with
952 * #G_STATIC_RW_LOCK_INIT.
953 *
954 * Deprecated: 2.32: Use g_rw_lock_init() instead
955 */
956 void
g_static_rw_lock_init(GStaticRWLock * lock)957 g_static_rw_lock_init (GStaticRWLock* lock)
958 {
959 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
960
961 g_return_if_fail (lock);
962
963 *lock = init_lock;
964 }
965
966 inline static void
g_static_rw_lock_wait(GCond ** cond,GStaticMutex * mutex)967 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
968 {
969 if (!*cond)
970 *cond = g_cond_new ();
971 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
972 }
973
974 inline static void
g_static_rw_lock_signal(GStaticRWLock * lock)975 g_static_rw_lock_signal (GStaticRWLock* lock)
976 {
977 if (lock->want_to_write && lock->write_cond)
978 g_cond_signal (lock->write_cond);
979 else if (lock->want_to_read && lock->read_cond)
980 g_cond_broadcast (lock->read_cond);
981 }
982
983 /**
984 * g_static_rw_lock_reader_lock:
985 * @lock: a #GStaticRWLock to lock for reading.
986 *
987 * Locks @lock for reading. There may be unlimited concurrent locks for
988 * reading of a #GStaticRWLock at the same time. If @lock is already
989 * locked for writing by another thread or if another thread is already
990 * waiting to lock @lock for writing, this function will block until
991 * @lock is unlocked by the other writing thread and no other writing
992 * threads want to lock @lock. This lock has to be unlocked by
993 * g_static_rw_lock_reader_unlock().
994 *
995 * #GStaticRWLock is not recursive. It might seem to be possible to
996 * recursively lock for reading, but that can result in a deadlock, due
997 * to writer preference.
998 *
999 * Deprecated: 2.32: Use g_rw_lock_reader_lock() instead
1000 */
1001 void
g_static_rw_lock_reader_lock(GStaticRWLock * lock)1002 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
1003 {
1004 g_return_if_fail (lock);
1005
1006 if (!g_threads_got_initialized)
1007 return;
1008
1009 g_static_mutex_lock (&lock->mutex);
1010 lock->want_to_read++;
1011 while (lock->have_writer || lock->want_to_write)
1012 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
1013 lock->want_to_read--;
1014 lock->read_counter++;
1015 g_static_mutex_unlock (&lock->mutex);
1016 }
1017
1018 /**
1019 * g_static_rw_lock_reader_trylock:
1020 * @lock: a #GStaticRWLock to lock for reading
1021 *
1022 * Tries to lock @lock for reading. If @lock is already locked for
1023 * writing by another thread or if another thread is already waiting to
1024 * lock @lock for writing, immediately returns %FALSE. Otherwise locks
1025 * @lock for reading and returns %TRUE. This lock has to be unlocked by
1026 * g_static_rw_lock_reader_unlock().
1027 *
1028 * Returns: %TRUE, if @lock could be locked for reading
1029 *
1030 * Deprecated: 2.32: Use g_rw_lock_reader_trylock() instead
1031 */
1032 gboolean
g_static_rw_lock_reader_trylock(GStaticRWLock * lock)1033 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
1034 {
1035 gboolean ret_val = FALSE;
1036
1037 g_return_val_if_fail (lock, FALSE);
1038
1039 if (!g_threads_got_initialized)
1040 return TRUE;
1041
1042 g_static_mutex_lock (&lock->mutex);
1043 if (!lock->have_writer && !lock->want_to_write)
1044 {
1045 lock->read_counter++;
1046 ret_val = TRUE;
1047 }
1048 g_static_mutex_unlock (&lock->mutex);
1049 return ret_val;
1050 }
1051
1052 /**
1053 * g_static_rw_lock_reader_unlock:
1054 * @lock: a #GStaticRWLock to unlock after reading
1055 *
1056 * Unlocks @lock. If a thread waits to lock @lock for writing and all
1057 * locks for reading have been unlocked, the waiting thread is woken up
1058 * and can lock @lock for writing.
1059 *
1060 * Deprecated: 2.32: Use g_rw_lock_reader_unlock() instead
1061 */
1062 void
g_static_rw_lock_reader_unlock(GStaticRWLock * lock)1063 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
1064 {
1065 g_return_if_fail (lock);
1066
1067 if (!g_threads_got_initialized)
1068 return;
1069
1070 g_static_mutex_lock (&lock->mutex);
1071 lock->read_counter--;
1072 if (lock->read_counter == 0)
1073 g_static_rw_lock_signal (lock);
1074 g_static_mutex_unlock (&lock->mutex);
1075 }
1076
1077 /**
1078 * g_static_rw_lock_writer_lock:
1079 * @lock: a #GStaticRWLock to lock for writing
1080 *
1081 * Locks @lock for writing. If @lock is already locked for writing or
1082 * reading by other threads, this function will block until @lock is
1083 * completely unlocked and then lock @lock for writing. While this
1084 * functions waits to lock @lock, no other thread can lock @lock for
1085 * reading. When @lock is locked for writing, no other thread can lock
1086 * @lock (neither for reading nor writing). This lock has to be
1087 * unlocked by g_static_rw_lock_writer_unlock().
1088 *
1089 * Deprecated: 2.32: Use g_rw_lock_writer_lock() instead
1090 */
1091 void
g_static_rw_lock_writer_lock(GStaticRWLock * lock)1092 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
1093 {
1094 g_return_if_fail (lock);
1095
1096 if (!g_threads_got_initialized)
1097 return;
1098
1099 g_static_mutex_lock (&lock->mutex);
1100 lock->want_to_write++;
1101 while (lock->have_writer || lock->read_counter)
1102 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
1103 lock->want_to_write--;
1104 lock->have_writer = TRUE;
1105 g_static_mutex_unlock (&lock->mutex);
1106 }
1107
1108 /**
1109 * g_static_rw_lock_writer_trylock:
1110 * @lock: a #GStaticRWLock to lock for writing
1111 *
1112 * Tries to lock @lock for writing. If @lock is already locked (for
1113 * either reading or writing) by another thread, it immediately returns
1114 * %FALSE. Otherwise it locks @lock for writing and returns %TRUE. This
1115 * lock has to be unlocked by g_static_rw_lock_writer_unlock().
1116 *
1117 * Returns: %TRUE, if @lock could be locked for writing
1118 *
1119 * Deprecated: 2.32: Use g_rw_lock_writer_trylock() instead
1120 */
1121 gboolean
g_static_rw_lock_writer_trylock(GStaticRWLock * lock)1122 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
1123 {
1124 gboolean ret_val = FALSE;
1125
1126 g_return_val_if_fail (lock, FALSE);
1127
1128 if (!g_threads_got_initialized)
1129 return TRUE;
1130
1131 g_static_mutex_lock (&lock->mutex);
1132 if (!lock->have_writer && !lock->read_counter)
1133 {
1134 lock->have_writer = TRUE;
1135 ret_val = TRUE;
1136 }
1137 g_static_mutex_unlock (&lock->mutex);
1138 return ret_val;
1139 }
1140
1141 /**
1142 * g_static_rw_lock_writer_unlock:
1143 * @lock: a #GStaticRWLock to unlock after writing.
1144 *
1145 * Unlocks @lock. If a thread is waiting to lock @lock for writing and
1146 * all locks for reading have been unlocked, the waiting thread is
1147 * woken up and can lock @lock for writing. If no thread is waiting to
1148 * lock @lock for writing, and some thread or threads are waiting to
1149 * lock @lock for reading, the waiting threads are woken up and can
1150 * lock @lock for reading.
1151 *
1152 * Deprecated: 2.32: Use g_rw_lock_writer_unlock() instead
1153 */
1154 void
g_static_rw_lock_writer_unlock(GStaticRWLock * lock)1155 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
1156 {
1157 g_return_if_fail (lock);
1158
1159 if (!g_threads_got_initialized)
1160 return;
1161
1162 g_static_mutex_lock (&lock->mutex);
1163 lock->have_writer = FALSE;
1164 g_static_rw_lock_signal (lock);
1165 g_static_mutex_unlock (&lock->mutex);
1166 }
1167
1168 /**
1169 * g_static_rw_lock_free:
1170 * @lock: a #GStaticRWLock to be freed.
1171 *
1172 * Releases all resources allocated to @lock.
1173 *
1174 * You don't have to call this functions for a #GStaticRWLock with an
1175 * unbounded lifetime, i.e. objects declared 'static', but if you have
1176 * a #GStaticRWLock as a member of a structure, and the structure is
1177 * freed, you should also free the #GStaticRWLock.
1178 *
1179 * Deprecated: 2.32: Use a #GRWLock instead
1180 */
1181 void
g_static_rw_lock_free(GStaticRWLock * lock)1182 g_static_rw_lock_free (GStaticRWLock* lock)
1183 {
1184 g_return_if_fail (lock);
1185
1186 if (lock->read_cond)
1187 {
1188 g_cond_free (lock->read_cond);
1189 lock->read_cond = NULL;
1190 }
1191 if (lock->write_cond)
1192 {
1193 g_cond_free (lock->write_cond);
1194 lock->write_cond = NULL;
1195 }
1196 g_static_mutex_free (&lock->mutex);
1197 }
1198
1199 /* GPrivate {{{1 ------------------------------------------------------ */
1200
1201 /**
1202 * g_private_new:
1203 * @notify: a #GDestroyNotify
1204 *
1205 * Creates a new #GPrivate.
1206 *
1207 * Deprecated:2.32: dynamic allocation of #GPrivate is a bad idea. Use
1208 * static storage and G_PRIVATE_INIT() instead.
1209 *
1210 * Returns: a newly allocated #GPrivate (which can never be destroyed)
1211 */
1212 GPrivate *
g_private_new(GDestroyNotify notify)1213 g_private_new (GDestroyNotify notify)
1214 {
1215 GPrivate tmp = G_PRIVATE_INIT (notify);
1216 GPrivate *key;
1217
1218 key = g_slice_new (GPrivate);
1219 *key = tmp;
1220
1221 return key;
1222 }
1223
1224 /* {{{1 GStaticPrivate */
1225
1226 typedef struct _GStaticPrivateNode GStaticPrivateNode;
1227 struct _GStaticPrivateNode
1228 {
1229 gpointer data;
1230 GDestroyNotify destroy;
1231 GStaticPrivate *owner;
1232 };
1233
1234 static void
g_static_private_cleanup(gpointer data)1235 g_static_private_cleanup (gpointer data)
1236 {
1237 GArray *array = data;
1238 guint i;
1239
1240 for (i = 0; i < array->len; i++ )
1241 {
1242 GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
1243 if (node->destroy)
1244 node->destroy (node->data);
1245 }
1246
1247 g_array_free (array, TRUE);
1248 }
1249
1250 GPrivate static_private_private = G_PRIVATE_INIT (g_static_private_cleanup);
1251
1252 /**
1253 * GStaticPrivate:
1254 *
1255 * A #GStaticPrivate works almost like a #GPrivate, but it has one
1256 * significant advantage. It doesn't need to be created at run-time
1257 * like a #GPrivate, but can be defined at compile-time. This is
1258 * similar to the difference between #GMutex and #GStaticMutex.
1259 *
1260 * Now look at our give_me_next_number() example with #GStaticPrivate:
1261 * |[
1262 * int
1263 * give_me_next_number ()
1264 * {
1265 * static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1266 * int *current_number = g_static_private_get (¤t_number_key);
1267 *
1268 * if (!current_number)
1269 * {
1270 * current_number = g_new (int, 1);
1271 * *current_number = 0;
1272 * g_static_private_set (¤t_number_key, current_number, g_free);
1273 * }
1274 *
1275 * *current_number = calc_next_number (*current_number);
1276 *
1277 * return *current_number;
1278 * }
1279 * ]|
1280 */
1281
1282 /**
1283 * G_STATIC_PRIVATE_INIT:
1284 *
1285 * Every #GStaticPrivate must be initialized with this macro, before it
1286 * can be used.
1287 *
1288 * |[
1289 * GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1290 * ]|
1291 */
1292
1293 /**
1294 * g_static_private_init:
1295 * @private_key: a #GStaticPrivate to be initialized
1296 *
1297 * Initializes @private_key. Alternatively you can initialize it with
1298 * #G_STATIC_PRIVATE_INIT.
1299 */
1300 void
g_static_private_init(GStaticPrivate * private_key)1301 g_static_private_init (GStaticPrivate *private_key)
1302 {
1303 private_key->index = 0;
1304 }
1305
1306 /**
1307 * g_static_private_get:
1308 * @private_key: a #GStaticPrivate
1309 *
1310 * Works like g_private_get() only for a #GStaticPrivate.
1311 *
1312 * This function works even if g_thread_init() has not yet been called.
1313 *
1314 * Returns: the corresponding pointer
1315 */
1316 gpointer
g_static_private_get(GStaticPrivate * private_key)1317 g_static_private_get (GStaticPrivate *private_key)
1318 {
1319 GArray *array;
1320 gpointer ret = NULL;
1321
1322 array = g_private_get (&static_private_private);
1323
1324 if (array && private_key->index != 0 && private_key->index <= array->len)
1325 {
1326 GStaticPrivateNode *node;
1327
1328 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1329
1330 /* Deal with the possibility that the GStaticPrivate which used
1331 * to have this index got freed and the index got allocated to
1332 * a new one. In this case, the data in the node is stale, so
1333 * free it and return NULL.
1334 */
1335 if (G_UNLIKELY (node->owner != private_key))
1336 {
1337 if (node->destroy)
1338 node->destroy (node->data);
1339 node->destroy = NULL;
1340 node->data = NULL;
1341 node->owner = NULL;
1342 }
1343 ret = node->data;
1344 }
1345
1346 return ret;
1347 }
1348
1349 /**
1350 * g_static_private_set:
1351 * @private_key: a #GStaticPrivate
1352 * @data: the new pointer
1353 * @notify: a function to be called with the pointer whenever the
1354 * current thread ends or sets this pointer again
1355 *
1356 * Sets the pointer keyed to @private_key for the current thread and
1357 * the function @notify to be called with that pointer (%NULL or
1358 * non-%NULL), whenever the pointer is set again or whenever the
1359 * current thread ends.
1360 *
1361 * This function works even if g_thread_init() has not yet been called.
1362 * If g_thread_init() is called later, the @data keyed to @private_key
1363 * will be inherited only by the main thread, i.e. the one that called
1364 * g_thread_init().
1365 *
1366 * @notify is used quite differently from @destructor in g_private_new().
1367 */
1368 void
g_static_private_set(GStaticPrivate * private_key,gpointer data,GDestroyNotify notify)1369 g_static_private_set (GStaticPrivate *private_key,
1370 gpointer data,
1371 GDestroyNotify notify)
1372 {
1373 GArray *array;
1374 static guint next_index = 0;
1375 GStaticPrivateNode *node;
1376
1377 if (!private_key->index)
1378 {
1379 G_LOCK (g_thread);
1380
1381 if (!private_key->index)
1382 {
1383 if (g_thread_free_indices)
1384 {
1385 private_key->index = GPOINTER_TO_UINT (g_thread_free_indices->data);
1386 g_thread_free_indices = g_slist_delete_link (g_thread_free_indices,
1387 g_thread_free_indices);
1388 }
1389 else
1390 private_key->index = ++next_index;
1391 }
1392
1393 G_UNLOCK (g_thread);
1394 }
1395
1396 array = g_private_get (&static_private_private);
1397 if (!array)
1398 {
1399 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
1400 g_private_set (&static_private_private, array);
1401 }
1402 if (private_key->index > array->len)
1403 g_array_set_size (array, private_key->index);
1404
1405 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1406
1407 if (node->destroy)
1408 node->destroy (node->data);
1409
1410 node->data = data;
1411 node->destroy = notify;
1412 node->owner = private_key;
1413 }
1414
1415 /**
1416 * g_static_private_free:
1417 * @private_key: a #GStaticPrivate to be freed
1418 *
1419 * Releases all resources allocated to @private_key.
1420 *
1421 * You don't have to call this functions for a #GStaticPrivate with an
1422 * unbounded lifetime, i.e. objects declared 'static', but if you have
1423 * a #GStaticPrivate as a member of a structure and the structure is
1424 * freed, you should also free the #GStaticPrivate.
1425 */
1426 void
g_static_private_free(GStaticPrivate * private_key)1427 g_static_private_free (GStaticPrivate *private_key)
1428 {
1429 guint idx = private_key->index;
1430
1431 if (!idx)
1432 return;
1433
1434 private_key->index = 0;
1435
1436 /* Freeing the per-thread data is deferred to either the
1437 * thread end or the next g_static_private_get() call for
1438 * the same index.
1439 */
1440 G_LOCK (g_thread);
1441 g_thread_free_indices = g_slist_prepend (g_thread_free_indices,
1442 GUINT_TO_POINTER (idx));
1443 G_UNLOCK (g_thread);
1444 }
1445
1446 /* GMutex {{{1 ------------------------------------------------------ */
1447
1448 /**
1449 * g_mutex_new:
1450 *
1451 * Allocates and initializes a new #GMutex.
1452 *
1453 * Returns: a newly allocated #GMutex. Use g_mutex_free() to free
1454 *
1455 * Deprecated: 2.32: GMutex can now be statically allocated, or embedded
1456 * in structures and initialised with g_mutex_init().
1457 */
1458 GMutex *
g_mutex_new(void)1459 g_mutex_new (void)
1460 {
1461 GMutex *mutex;
1462
1463 mutex = g_slice_new (GMutex);
1464 g_mutex_init (mutex);
1465
1466 return mutex;
1467 }
1468
1469 /**
1470 * g_mutex_free:
1471 * @mutex: a #GMutex
1472 *
1473 * Destroys a @mutex that has been created with g_mutex_new().
1474 *
1475 * Calling g_mutex_free() on a locked mutex may result
1476 * in undefined behaviour.
1477 *
1478 * Deprecated: 2.32: GMutex can now be statically allocated, or embedded
1479 * in structures and initialised with g_mutex_init().
1480 */
1481 void
g_mutex_free(GMutex * mutex)1482 g_mutex_free (GMutex *mutex)
1483 {
1484 g_mutex_clear (mutex);
1485 g_slice_free (GMutex, mutex);
1486 }
1487
1488 /* GCond {{{1 ------------------------------------------------------ */
1489
1490 /**
1491 * g_cond_new:
1492 *
1493 * Allocates and initializes a new #GCond.
1494 *
1495 * Returns: a newly allocated #GCond. Free with g_cond_free()
1496 *
1497 * Deprecated: 2.32: GCond can now be statically allocated, or embedded
1498 * in structures and initialised with g_cond_init().
1499 */
1500 GCond *
g_cond_new(void)1501 g_cond_new (void)
1502 {
1503 GCond *cond;
1504
1505 cond = g_slice_new (GCond);
1506 g_cond_init (cond);
1507
1508 return cond;
1509 }
1510
1511 /**
1512 * g_cond_free:
1513 * @cond: a #GCond
1514 *
1515 * Destroys a #GCond that has been created with g_cond_new().
1516 *
1517 * Calling g_cond_free() for a #GCond on which threads are
1518 * blocking leads to undefined behaviour.
1519 *
1520 * Deprecated: 2.32: GCond can now be statically allocated, or embedded
1521 * in structures and initialised with g_cond_init().
1522 */
1523 void
g_cond_free(GCond * cond)1524 g_cond_free (GCond *cond)
1525 {
1526 g_cond_clear (cond);
1527 g_slice_free (GCond, cond);
1528 }
1529
1530 /**
1531 * g_cond_timed_wait:
1532 * @cond: a #GCond
1533 * @mutex: a #GMutex that is currently locked
1534 * @abs_time: a #GTimeVal, determining the final time
1535 *
1536 * Waits until this thread is woken up on @cond, but not longer than
1537 * until the time specified by @abs_time. The @mutex is unlocked before
1538 * falling asleep and locked again before resuming.
1539 *
1540 * If @abs_time is %NULL, g_cond_timed_wait() acts like g_cond_wait().
1541 *
1542 * This function can be used even if g_thread_init() has not yet been
1543 * called, and, in that case, will immediately return %TRUE.
1544 *
1545 * To easily calculate @abs_time a combination of g_get_real_time()
1546 * and g_time_val_add() can be used.
1547 *
1548 * Returns: %TRUE if @cond was signalled, or %FALSE on timeout
1549 *
1550 * Deprecated:2.32: Use g_cond_wait_until() instead.
1551 */
1552 gboolean
g_cond_timed_wait(GCond * cond,GMutex * mutex,GTimeVal * abs_time)1553 g_cond_timed_wait (GCond *cond,
1554 GMutex *mutex,
1555 GTimeVal *abs_time)
1556 {
1557 gint64 end_time;
1558
1559 if (abs_time == NULL)
1560 {
1561 g_cond_wait (cond, mutex);
1562 return TRUE;
1563 }
1564
1565 end_time = abs_time->tv_sec;
1566 end_time *= 1000000;
1567 end_time += abs_time->tv_usec;
1568
1569 /* would be nice if we had clock_rtoffset, but that didn't seem to
1570 * make it into the kernel yet...
1571 */
1572 end_time += g_get_monotonic_time () - g_get_real_time ();
1573
1574 return g_cond_wait_until (cond, mutex, end_time);
1575 }
1576
1577 /* {{{1 Epilogue */
1578 /* vim: set foldmethod=marker: */
1579