1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gthread.c: MT safety related functions
5 * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
26 #include "gmessages.h"
30 #include "gthreadprivate.h"
31 #include "deprecated/gthread.h"
33 /* {{{1 Documentation */
37 * @G_THREAD_PRIORITY_LOW: a priority lower than normal
38 * @G_THREAD_PRIORITY_NORMAL: the default priority
39 * @G_THREAD_PRIORITY_HIGH: a priority higher than normal
40 * @G_THREAD_PRIORITY_URGENT: the highest priority
42 * Deprecated:2.32: thread priorities no longer have any effect.
47 * @mutex_new: virtual function pointer for g_mutex_new()
48 * @mutex_lock: virtual function pointer for g_mutex_lock()
49 * @mutex_trylock: virtual function pointer for g_mutex_trylock()
50 * @mutex_unlock: virtual function pointer for g_mutex_unlock()
51 * @mutex_free: virtual function pointer for g_mutex_free()
52 * @cond_new: virtual function pointer for g_cond_new()
53 * @cond_signal: virtual function pointer for g_cond_signal()
54 * @cond_broadcast: virtual function pointer for g_cond_broadcast()
55 * @cond_wait: virtual function pointer for g_cond_wait()
56 * @cond_timed_wait: virtual function pointer for g_cond_timed_wait()
57 * @cond_free: virtual function pointer for g_cond_free()
58 * @private_new: virtual function pointer for g_private_new()
59 * @private_get: virtual function pointer for g_private_get()
60 * @private_set: virtual function pointer for g_private_set()
61 * @thread_create: virtual function pointer for g_thread_create()
62 * @thread_yield: virtual function pointer for g_thread_yield()
63 * @thread_join: virtual function pointer for g_thread_join()
64 * @thread_exit: virtual function pointer for g_thread_exit()
65 * @thread_set_priority: virtual function pointer for
66 * g_thread_set_priority()
67 * @thread_self: virtual function pointer for g_thread_self()
68 * @thread_equal: used internally by recursive mutex locks and by some
71 * This function table is no longer used by g_thread_init()
72 * to initialize the thread system.
75 /* {{{1 Exported Variables */
77 gboolean g_thread_use_default_impl = TRUE;
79 GThreadFunctions g_thread_functions_for_glib_use =
107 return g_get_monotonic_time () * 1000;
110 guint64 (*g_thread_gettime) (void) = gettime;
112 /* Misc. GThread functions {{{1 */
115 * g_thread_set_priority:
116 * @thread: a #GThread.
119 * This function does nothing.
121 * Deprecated:2.32: Thread priorities no longer have any effect.
124 g_thread_set_priority (GThread *thread,
125 GThreadPriority priority)
130 * g_thread_create_full:
131 * @func: a function to execute in the new thread.
132 * @data: an argument to supply to the new thread.
133 * @stack_size: a stack size for the new thread.
134 * @joinable: should this thread be joinable?
137 * @error: return location for error.
138 * @Returns: the new #GThread on success.
140 * This function creates a new thread.
142 * Deprecated:2.32: The @bound and @priority arguments are now ignored.
143 * Use g_thread_create() or g_thread_create_with_stack_size() instead.
146 g_thread_create_full (GThreadFunc func,
151 GThreadPriority priority,
154 return g_thread_create_with_stack_size (func, data, joinable, stack_size, error);
157 /* GStaticMutex {{{1 ------------------------------------------------------ */
162 * A #GStaticMutex works like a #GMutex.
164 * Prior to GLib 2.32, GStaticMutex had the significant advantage
165 * that it doesn't need to be created at run-time, but can be defined
166 * at compile-time. Since 2.32, #GMutex can be statically allocated
167 * as well, and GStaticMutex has been deprecated.
169 * Here is a version of our give_me_next_number() example using
174 * Using <structname>GStaticMutex</structname>
175 * to simplify thread-safe programming
179 * give_me_next_number (void)
181 * static int current_number = 0;
183 * static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
185 * g_static_mutex_lock (&mutex);
186 * ret_val = current_number = calc_next_number (current_number);
187 * g_static_mutex_unlock (&mutex);
194 * Sometimes you would like to dynamically create a mutex. If you don't
195 * want to require prior calling to g_thread_init(), because your code
196 * should also be usable in non-threaded programs, you are not able to
197 * use g_mutex_new() and thus #GMutex, as that requires a prior call to
198 * g_thread_init(). In theses cases you can also use a #GStaticMutex.
199 * It must be initialized with g_static_mutex_init() before using it
200 * and freed with with g_static_mutex_free() when not needed anymore to
201 * free up any allocated resources.
203 * Even though #GStaticMutex is not opaque, it should only be used with
204 * the following functions, as it is defined differently on different
207 * All of the <function>g_static_mutex_*</function> functions apart
208 * from <function>g_static_mutex_get_mutex</function> can also be used
209 * even if g_thread_init() has not yet been called. Then they do
210 * nothing, apart from <function>g_static_mutex_trylock</function>,
211 * which does nothing but returning %TRUE.
213 * <note><para>All of the <function>g_static_mutex_*</function>
214 * functions are actually macros. Apart from taking their addresses, you
215 * can however use them as if they were functions.</para></note>
219 * G_STATIC_MUTEX_INIT:
221 * A #GStaticMutex must be initialized with this macro, before it can
222 * be used. This macro can used be to initialize a variable, but it
223 * cannot be assigned to a variable. In that case you have to use
224 * g_static_mutex_init().
227 * GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
232 * g_static_mutex_init:
233 * @mutex: a #GStaticMutex to be initialized.
235 * Initializes @mutex.
236 * Alternatively you can initialize it with #G_STATIC_MUTEX_INIT.
238 * Deprecated: 2.32: Use g_mutex_init()
241 g_static_mutex_init (GStaticMutex *mutex)
243 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
245 g_return_if_fail (mutex);
250 /* IMPLEMENTATION NOTE:
252 * On some platforms a GStaticMutex is actually a normal GMutex stored
253 * inside of a structure instead of being allocated dynamically. We can
254 * only do this for platforms on which we know, in advance, how to
255 * allocate (size) and initialise (value) that memory.
257 * On other platforms, a GStaticMutex is nothing more than a pointer to
258 * a GMutex. In that case, the first access we make to the static mutex
259 * must first allocate the normal GMutex and store it into the pointer.
261 * configure.ac writes macros into glibconfig.h to determine if
262 * g_static_mutex_get_mutex() accesses the structure in memory directly
263 * (on platforms where we are able to do that) or if it ends up here,
264 * where we may have to allocate the GMutex before returning it.
268 * g_static_mutex_get_mutex:
269 * @mutex: a #GStaticMutex.
270 * @Returns: the #GMutex corresponding to @mutex.
272 * For some operations (like g_cond_wait()) you must have a #GMutex
273 * instead of a #GStaticMutex. This function will return the
274 * corresponding #GMutex for @mutex.
276 * Deprecated: 2.32: Just use a #GMutex
279 g_static_mutex_get_mutex_impl (GMutex** mutex)
283 if (!g_thread_supported ())
286 result = g_atomic_pointer_get (mutex);
290 g_mutex_lock (&g_once_mutex);
295 result = g_mutex_new ();
296 g_atomic_pointer_set (mutex, result);
299 g_mutex_unlock (&g_once_mutex);
305 /* IMPLEMENTATION NOTE:
307 * g_static_mutex_lock(), g_static_mutex_trylock() and
308 * g_static_mutex_unlock() are all preprocessor macros that wrap the
309 * corresponding g_mutex_*() function around a call to
310 * g_static_mutex_get_mutex().
314 * g_static_mutex_lock:
315 * @mutex: a #GStaticMutex.
317 * Works like g_mutex_lock(), but for a #GStaticMutex.
319 * Deprecated: 2.32: Use g_mutex_lock()
323 * g_static_mutex_trylock:
324 * @mutex: a #GStaticMutex.
325 * @Returns: %TRUE, if the #GStaticMutex could be locked.
327 * Works like g_mutex_trylock(), but for a #GStaticMutex.
329 * Deprecated: 2.32: Use g_mutex_trylock()
333 * g_static_mutex_unlock:
334 * @mutex: a #GStaticMutex.
336 * Works like g_mutex_unlock(), but for a #GStaticMutex.
338 * Deprecated: 2.32: Use g_mutex_unlock()
342 * g_static_mutex_free:
343 * @mutex: a #GStaticMutex to be freed.
345 * Releases all resources allocated to @mutex.
347 * You don't have to call this functions for a #GStaticMutex with an
348 * unbounded lifetime, i.e. objects declared 'static', but if you have
349 * a #GStaticMutex as a member of a structure and the structure is
350 * freed, you should also free the #GStaticMutex.
352 * <note><para>Calling g_static_mutex_free() on a locked mutex may
353 * result in undefined behaviour.</para></note>
355 * Deprecated: 2.32: Use g_mutex_free()
358 g_static_mutex_free (GStaticMutex* mutex)
360 GMutex **runtime_mutex;
362 g_return_if_fail (mutex);
364 /* The runtime_mutex is the first (or only) member of GStaticMutex,
365 * see both versions (of glibconfig.h) in configure.ac. Note, that
366 * this variable is NULL, if g_thread_init() hasn't been called or
367 * if we're using the default thread implementation and it provides
369 runtime_mutex = ((GMutex**)mutex);
372 g_mutex_free (*runtime_mutex);
374 *runtime_mutex = NULL;
377 /* {{{1 GStaticRecMutex */
382 * A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
383 * multiple times by one thread. If you enter it n times, you have to
384 * unlock it n times again to let other threads lock it. An exception
385 * is the function g_static_rec_mutex_unlock_full(): that allows you to
386 * unlock a #GStaticRecMutex completely returning the depth, (i.e. the
387 * number of times this mutex was locked). The depth can later be used
388 * to restore the state of the #GStaticRecMutex by calling
389 * g_static_rec_mutex_lock_full(). In GLib 2.32, #GStaticRecMutex has
390 * been deprecated in favor of #GRecMutex.
392 * Even though #GStaticRecMutex is not opaque, it should only be used
393 * with the following functions.
395 * All of the <function>g_static_rec_mutex_*</function> functions can
396 * be used even if g_thread_init() has not been called. Then they do
397 * nothing, apart from <function>g_static_rec_mutex_trylock</function>,
398 * which does nothing but returning %TRUE.
402 * G_STATIC_REC_MUTEX_INIT:
404 * A #GStaticRecMutex must be initialized with this macro before it can
405 * be used. This macro can used be to initialize a variable, but it
406 * cannot be assigned to a variable. In that case you have to use
407 * g_static_rec_mutex_init().
410 * GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
415 * g_static_rec_mutex_init:
416 * @mutex: a #GStaticRecMutex to be initialized.
418 * A #GStaticRecMutex must be initialized with this function before it
419 * can be used. Alternatively you can initialize it with
420 * #G_STATIC_REC_MUTEX_INIT.
422 * Deprecated: 2.32: Use g_rec_mutex_init()
425 g_static_rec_mutex_init (GStaticRecMutex *mutex)
427 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
429 g_return_if_fail (mutex);
435 * g_static_rec_mutex_lock:
436 * @mutex: a #GStaticRecMutex to lock.
438 * Locks @mutex. If @mutex is already locked by another thread, the
439 * current thread will block until @mutex is unlocked by the other
440 * thread. If @mutex is already locked by the calling thread, this
441 * functions increases the depth of @mutex and returns immediately.
443 * Deprecated: 2.32: Use g_rec_mutex_lock()
446 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
450 g_return_if_fail (mutex);
452 if (!g_thread_supported ())
455 g_system_thread_self (&self);
457 if (g_system_thread_equal (&self, &mutex->owner))
462 g_static_mutex_lock (&mutex->mutex);
463 g_system_thread_assign (mutex->owner, self);
468 * g_static_rec_mutex_trylock:
469 * @mutex: a #GStaticRecMutex to lock.
470 * @Returns: %TRUE, if @mutex could be locked.
472 * Tries to lock @mutex. If @mutex is already locked by another thread,
473 * it immediately returns %FALSE. Otherwise it locks @mutex and returns
474 * %TRUE. If @mutex is already locked by the calling thread, this
475 * functions increases the depth of @mutex and immediately returns
478 * Deprecated: 2.32: Use g_rec_mutex_trylock()
481 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
485 g_return_val_if_fail (mutex, FALSE);
487 if (!g_thread_supported ())
490 g_system_thread_self (&self);
492 if (g_system_thread_equal (&self, &mutex->owner))
498 if (!g_static_mutex_trylock (&mutex->mutex))
501 g_system_thread_assign (mutex->owner, self);
507 * g_static_rec_mutex_unlock:
508 * @mutex: a #GStaticRecMutex to unlock.
510 * Unlocks @mutex. Another thread will be allowed to lock @mutex only
511 * when it has been unlocked as many times as it had been locked
512 * before. If @mutex is completely unlocked and another thread is
513 * blocked in a g_static_rec_mutex_lock() call for @mutex, it will be
514 * woken and can lock @mutex itself.
516 * Deprecated: 2.32: Use g_rec_mutex_unlock()
519 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
521 g_return_if_fail (mutex);
523 if (!g_thread_supported ())
526 if (mutex->depth > 1)
531 g_system_thread_assign (mutex->owner, zero_thread);
532 g_static_mutex_unlock (&mutex->mutex);
536 * g_static_rec_mutex_lock_full:
537 * @mutex: a #GStaticRecMutex to lock.
538 * @depth: number of times this mutex has to be unlocked to be
539 * completely unlocked.
541 * Works like calling g_static_rec_mutex_lock() for @mutex @depth times.
543 * Deprecated: 2.32: Use g_rec_mutex_lock()
546 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
550 g_return_if_fail (mutex);
552 if (!g_thread_supported ())
558 g_system_thread_self (&self);
560 if (g_system_thread_equal (&self, &mutex->owner))
562 mutex->depth += depth;
565 g_static_mutex_lock (&mutex->mutex);
566 g_system_thread_assign (mutex->owner, self);
567 mutex->depth = depth;
571 * g_static_rec_mutex_unlock_full:
572 * @mutex: a #GStaticRecMutex to completely unlock.
573 * @Returns: number of times @mutex has been locked by the current
576 * Completely unlocks @mutex. If another thread is blocked in a
577 * g_static_rec_mutex_lock() call for @mutex, it will be woken and can
578 * lock @mutex itself. This function returns the number of times that
579 * @mutex has been locked by the current thread. To restore the state
580 * before the call to g_static_rec_mutex_unlock_full() you can call
581 * g_static_rec_mutex_lock_full() with the depth returned by this
584 * Deprecated: 2.32: Use g_rec_mutex_unlock()
587 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
591 g_return_val_if_fail (mutex, 0);
593 if (!g_thread_supported ())
596 depth = mutex->depth;
598 g_system_thread_assign (mutex->owner, zero_thread);
600 g_static_mutex_unlock (&mutex->mutex);
606 * g_static_rec_mutex_free:
607 * @mutex: a #GStaticRecMutex to be freed.
609 * Releases all resources allocated to a #GStaticRecMutex.
611 * You don't have to call this functions for a #GStaticRecMutex with an
612 * unbounded lifetime, i.e. objects declared 'static', but if you have
613 * a #GStaticRecMutex as a member of a structure and the structure is
614 * freed, you should also free the #GStaticRecMutex.
616 * Deprecated: 2.32: Use g_rec_mutex_clear()
619 g_static_rec_mutex_free (GStaticRecMutex *mutex)
621 g_return_if_fail (mutex);
623 g_static_mutex_free (&mutex->mutex);
626 /* GStaticRWLock {{{1 ----------------------------------------------------- */
631 * The #GStaticRWLock struct represents a read-write lock. A read-write
632 * lock can be used for protecting data that some portions of code only
633 * read from, while others also write. In such situations it is
634 * desirable that several readers can read at once, whereas of course
635 * only one writer may write at a time. Take a look at the following
639 * <title>An array with access functions</title>
641 * GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
645 * my_array_get (guint index)
647 * gpointer retval = NULL;
652 * g_static_rw_lock_reader_lock (&rwlock);
653 * if (index < array->len)
654 * retval = g_ptr_array_index (array, index);
655 * g_static_rw_lock_reader_unlock (&rwlock);
661 * my_array_set (guint index, gpointer data)
663 * g_static_rw_lock_writer_lock (&rwlock);
666 * array = g_ptr_array_new (<!-- -->);
668 * if (index >= array->len)
669 * g_ptr_array_set_size (array, index+1);
670 * g_ptr_array_index (array, index) = data;
672 * g_static_rw_lock_writer_unlock (&rwlock);
677 * This example shows an array which can be accessed by many readers
678 * (the <function>my_array_get()</function> function) simultaneously,
679 * whereas the writers (the <function>my_array_set()</function>
680 * function) will only be allowed once at a time and only if no readers
681 * currently access the array. This is because of the potentially
682 * dangerous resizing of the array. Using these functions is fully
683 * multi-thread safe now.
685 * Most of the time, writers should have precedence over readers. That
686 * means, for this implementation, that as soon as a writer wants to
687 * lock the data, no other reader is allowed to lock the data, whereas,
688 * of course, the readers that already have locked the data are allowed
689 * to finish their operation. As soon as the last reader unlocks the
690 * data, the writer will lock it.
692 * Even though #GStaticRWLock is not opaque, it should only be used
693 * with the following functions.
695 * All of the <function>g_static_rw_lock_*</function> functions can be
696 * used even if g_thread_init() has not been called. Then they do
697 * nothing, apart from <function>g_static_rw_lock_*_trylock</function>,
698 * which does nothing but returning %TRUE.
700 * <note><para>A read-write lock has a higher overhead than a mutex. For
701 * example, both g_static_rw_lock_reader_lock() and
702 * g_static_rw_lock_reader_unlock() have to lock and unlock a
703 * #GStaticMutex, so it takes at least twice the time to lock and unlock
704 * a #GStaticRWLock that it does to lock and unlock a #GStaticMutex. So
705 * only data structures that are accessed by multiple readers, and which
706 * keep the lock for a considerable time justify a #GStaticRWLock. The
707 * above example most probably would fare better with a
708 * #GStaticMutex.</para></note>
710 * Deprecated: 2.32: Use a #GRWLock instead
714 * G_STATIC_RW_LOCK_INIT:
716 * A #GStaticRWLock must be initialized with this macro before it can
717 * be used. This macro can used be to initialize a variable, but it
718 * cannot be assigned to a variable. In that case you have to use
719 * g_static_rw_lock_init().
722 * GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
727 * g_static_rw_lock_init:
728 * @lock: a #GStaticRWLock to be initialized.
730 * A #GStaticRWLock must be initialized with this function before it
731 * can be used. Alternatively you can initialize it with
732 * #G_STATIC_RW_LOCK_INIT.
734 * Deprecated: 2.32: Use g_rw_lock_init() instead
737 g_static_rw_lock_init (GStaticRWLock* lock)
739 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
741 g_return_if_fail (lock);
747 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
750 *cond = g_cond_new ();
751 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
755 g_static_rw_lock_signal (GStaticRWLock* lock)
757 if (lock->want_to_write && lock->write_cond)
758 g_cond_signal (lock->write_cond);
759 else if (lock->want_to_read && lock->read_cond)
760 g_cond_broadcast (lock->read_cond);
764 * g_static_rw_lock_reader_lock:
765 * @lock: a #GStaticRWLock to lock for reading.
767 * Locks @lock for reading. There may be unlimited concurrent locks for
768 * reading of a #GStaticRWLock at the same time. If @lock is already
769 * locked for writing by another thread or if another thread is already
770 * waiting to lock @lock for writing, this function will block until
771 * @lock is unlocked by the other writing thread and no other writing
772 * threads want to lock @lock. This lock has to be unlocked by
773 * g_static_rw_lock_reader_unlock().
775 * #GStaticRWLock is not recursive. It might seem to be possible to
776 * recursively lock for reading, but that can result in a deadlock, due
777 * to writer preference.
779 * Deprecated: 2.32: Use g_rw_lock_reader_lock() instead
782 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
784 g_return_if_fail (lock);
786 if (!g_threads_got_initialized)
789 g_static_mutex_lock (&lock->mutex);
790 lock->want_to_read++;
791 while (lock->have_writer || lock->want_to_write)
792 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
793 lock->want_to_read--;
794 lock->read_counter++;
795 g_static_mutex_unlock (&lock->mutex);
799 * g_static_rw_lock_reader_trylock:
800 * @lock: a #GStaticRWLock to lock for reading.
801 * @Returns: %TRUE, if @lock could be locked for reading.
803 * Tries to lock @lock for reading. If @lock is already locked for
804 * writing by another thread or if another thread is already waiting to
805 * lock @lock for writing, immediately returns %FALSE. Otherwise locks
806 * @lock for reading and returns %TRUE. This lock has to be unlocked by
807 * g_static_rw_lock_reader_unlock().
809 * Deprectated: 2.32: Use g_rw_lock_reader_trylock() instead
812 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
814 gboolean ret_val = FALSE;
816 g_return_val_if_fail (lock, FALSE);
818 if (!g_threads_got_initialized)
821 g_static_mutex_lock (&lock->mutex);
822 if (!lock->have_writer && !lock->want_to_write)
824 lock->read_counter++;
827 g_static_mutex_unlock (&lock->mutex);
832 * g_static_rw_lock_reader_unlock:
833 * @lock: a #GStaticRWLock to unlock after reading.
835 * Unlocks @lock. If a thread waits to lock @lock for writing and all
836 * locks for reading have been unlocked, the waiting thread is woken up
837 * and can lock @lock for writing.
839 * Deprectated: 2.32: Use g_rw_lock_reader_unlock() instead
842 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
844 g_return_if_fail (lock);
846 if (!g_threads_got_initialized)
849 g_static_mutex_lock (&lock->mutex);
850 lock->read_counter--;
851 if (lock->read_counter == 0)
852 g_static_rw_lock_signal (lock);
853 g_static_mutex_unlock (&lock->mutex);
857 * g_static_rw_lock_writer_lock:
858 * @lock: a #GStaticRWLock to lock for writing.
860 * Locks @lock for writing. If @lock is already locked for writing or
861 * reading by other threads, this function will block until @lock is
862 * completely unlocked and then lock @lock for writing. While this
863 * functions waits to lock @lock, no other thread can lock @lock for
864 * reading. When @lock is locked for writing, no other thread can lock
865 * @lock (neither for reading nor writing). This lock has to be
866 * unlocked by g_static_rw_lock_writer_unlock().
868 * Deprectated: 2.32: Use g_rw_lock_writer_lock() instead
871 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
873 g_return_if_fail (lock);
875 if (!g_threads_got_initialized)
878 g_static_mutex_lock (&lock->mutex);
879 lock->want_to_write++;
880 while (lock->have_writer || lock->read_counter)
881 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
882 lock->want_to_write--;
883 lock->have_writer = TRUE;
884 g_static_mutex_unlock (&lock->mutex);
888 * g_static_rw_lock_writer_trylock:
889 * @lock: a #GStaticRWLock to lock for writing.
890 * @Returns: %TRUE, if @lock could be locked for writing.
892 * Tries to lock @lock for writing. If @lock is already locked (for
893 * either reading or writing) by another thread, it immediately returns
894 * %FALSE. Otherwise it locks @lock for writing and returns %TRUE. This
895 * lock has to be unlocked by g_static_rw_lock_writer_unlock().
897 * Deprectated: 2.32: Use g_rw_lock_writer_trylock() instead
900 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
902 gboolean ret_val = FALSE;
904 g_return_val_if_fail (lock, FALSE);
906 if (!g_threads_got_initialized)
909 g_static_mutex_lock (&lock->mutex);
910 if (!lock->have_writer && !lock->read_counter)
912 lock->have_writer = TRUE;
915 g_static_mutex_unlock (&lock->mutex);
920 * g_static_rw_lock_writer_unlock:
921 * @lock: a #GStaticRWLock to unlock after writing.
923 * Unlocks @lock. If a thread is waiting to lock @lock for writing and
924 * all locks for reading have been unlocked, the waiting thread is
925 * woken up and can lock @lock for writing. If no thread is waiting to
926 * lock @lock for writing, and some thread or threads are waiting to
927 * lock @lock for reading, the waiting threads are woken up and can
928 * lock @lock for reading.
930 * Deprectated: 2.32: Use g_rw_lock_writer_unlock() instead
933 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
935 g_return_if_fail (lock);
937 if (!g_threads_got_initialized)
940 g_static_mutex_lock (&lock->mutex);
941 lock->have_writer = FALSE;
942 g_static_rw_lock_signal (lock);
943 g_static_mutex_unlock (&lock->mutex);
947 * g_static_rw_lock_free:
948 * @lock: a #GStaticRWLock to be freed.
950 * Releases all resources allocated to @lock.
952 * You don't have to call this functions for a #GStaticRWLock with an
953 * unbounded lifetime, i.e. objects declared 'static', but if you have
954 * a #GStaticRWLock as a member of a structure, and the structure is
955 * freed, you should also free the #GStaticRWLock.
957 * Deprecated: 2.32: Use a #GRWLock instead
960 g_static_rw_lock_free (GStaticRWLock* lock)
962 g_return_if_fail (lock);
966 g_cond_free (lock->read_cond);
967 lock->read_cond = NULL;
969 if (lock->write_cond)
971 g_cond_free (lock->write_cond);
972 lock->write_cond = NULL;
974 g_static_mutex_free (&lock->mutex);
977 /* GPrivate {{{1 ------------------------------------------------------ */
981 * @notify: a #GDestroyNotify
983 * Deprecated:2.32: dynamic allocation of #GPrivate is a bad idea. Use
984 * static storage and G_PRIVATE_INIT() instead.
986 * Returns: a newly allocated #GPrivate (which can never be destroyed)
989 g_private_new (GDestroyNotify notify)
991 GPrivate tmp = G_PRIVATE_INIT (notify);
994 key = g_slice_new (GPrivate);
1001 /* vim: set foldmethod=marker: */