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"
29 #include "gthreadprivate.h"
30 #include "deprecated/gthread.h"
32 /* {{{1 Documentation */
36 * @G_THREAD_PRIORITY_LOW: a priority lower than normal
37 * @G_THREAD_PRIORITY_NORMAL: the default priority
38 * @G_THREAD_PRIORITY_HIGH: a priority higher than normal
39 * @G_THREAD_PRIORITY_URGENT: the highest priority
41 * Deprecated:2.32: thread priorities no longer have any effect.
46 * @mutex_new: virtual function pointer for g_mutex_new()
47 * @mutex_lock: virtual function pointer for g_mutex_lock()
48 * @mutex_trylock: virtual function pointer for g_mutex_trylock()
49 * @mutex_unlock: virtual function pointer for g_mutex_unlock()
50 * @mutex_free: virtual function pointer for g_mutex_free()
51 * @cond_new: virtual function pointer for g_cond_new()
52 * @cond_signal: virtual function pointer for g_cond_signal()
53 * @cond_broadcast: virtual function pointer for g_cond_broadcast()
54 * @cond_wait: virtual function pointer for g_cond_wait()
55 * @cond_timed_wait: virtual function pointer for g_cond_timed_wait()
56 * @cond_free: virtual function pointer for g_cond_free()
57 * @private_new: virtual function pointer for g_private_new()
58 * @private_get: virtual function pointer for g_private_get()
59 * @private_set: virtual function pointer for g_private_set()
60 * @thread_create: virtual function pointer for g_thread_create()
61 * @thread_yield: virtual function pointer for g_thread_yield()
62 * @thread_join: virtual function pointer for g_thread_join()
63 * @thread_exit: virtual function pointer for g_thread_exit()
64 * @thread_set_priority: virtual function pointer for
65 * g_thread_set_priority()
66 * @thread_self: virtual function pointer for g_thread_self()
67 * @thread_equal: used internally by recursive mutex locks and by some
70 * This function table is no longer used by g_thread_init()
71 * to initialize the thread system.
74 /* {{{1 Exported Variables */
76 gboolean g_thread_use_default_impl = TRUE;
78 GThreadFunctions g_thread_functions_for_glib_use =
106 return g_get_monotonic_time () * 1000;
109 guint64 (*g_thread_gettime) (void) = gettime;
111 /* Misc. GThread functions {{{1 */
114 * g_thread_set_priority:
115 * @thread: a #GThread.
118 * This function does nothing.
120 * Deprecated:2.32: Thread priorities no longer have any effect.
123 g_thread_set_priority (GThread *thread,
124 GThreadPriority priority)
129 * g_thread_create_full:
130 * @func: a function to execute in the new thread.
131 * @data: an argument to supply to the new thread.
132 * @stack_size: a stack size for the new thread.
133 * @joinable: should this thread be joinable?
136 * @error: return location for error.
137 * @Returns: the new #GThread on success.
139 * This function creates a new thread.
141 * Deprecated:2.32: The @bound and @priority arguments are now ignored.
142 * Use g_thread_create() or g_thread_create_with_stack_size() instead.
145 g_thread_create_full (GThreadFunc func,
150 GThreadPriority priority,
153 return g_thread_create_with_stack_size (func, data, joinable, stack_size, error);
156 /* GStaticMutex {{{1 ------------------------------------------------------ */
161 * A #GStaticMutex works like a #GMutex.
163 * Prior to GLib 2.32, GStaticMutex had the significant advantage
164 * that it doesn't need to be created at run-time, but can be defined
165 * at compile-time. Since 2.32, #GMutex can be statically allocated
166 * as well, and GStaticMutex has been deprecated.
168 * Here is a version of our give_me_next_number() example using
173 * Using <structname>GStaticMutex</structname>
174 * to simplify thread-safe programming
178 * give_me_next_number (void)
180 * static int current_number = 0;
182 * static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
184 * g_static_mutex_lock (&mutex);
185 * ret_val = current_number = calc_next_number (current_number);
186 * g_static_mutex_unlock (&mutex);
193 * Sometimes you would like to dynamically create a mutex. If you don't
194 * want to require prior calling to g_thread_init(), because your code
195 * should also be usable in non-threaded programs, you are not able to
196 * use g_mutex_new() and thus #GMutex, as that requires a prior call to
197 * g_thread_init(). In theses cases you can also use a #GStaticMutex.
198 * It must be initialized with g_static_mutex_init() before using it
199 * and freed with with g_static_mutex_free() when not needed anymore to
200 * free up any allocated resources.
202 * Even though #GStaticMutex is not opaque, it should only be used with
203 * the following functions, as it is defined differently on different
206 * All of the <function>g_static_mutex_*</function> functions apart
207 * from <function>g_static_mutex_get_mutex</function> can also be used
208 * even if g_thread_init() has not yet been called. Then they do
209 * nothing, apart from <function>g_static_mutex_trylock</function>,
210 * which does nothing but returning %TRUE.
212 * <note><para>All of the <function>g_static_mutex_*</function>
213 * functions are actually macros. Apart from taking their addresses, you
214 * can however use them as if they were functions.</para></note>
218 * G_STATIC_MUTEX_INIT:
220 * A #GStaticMutex must be initialized with this macro, before it can
221 * be used. This macro can used be to initialize a variable, but it
222 * cannot be assigned to a variable. In that case you have to use
223 * g_static_mutex_init().
226 * GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
231 * g_static_mutex_init:
232 * @mutex: a #GStaticMutex to be initialized.
234 * Initializes @mutex.
235 * Alternatively you can initialize it with #G_STATIC_MUTEX_INIT.
237 * Deprecated: 2.32: Use g_mutex_init()
240 g_static_mutex_init (GStaticMutex *mutex)
242 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
244 g_return_if_fail (mutex);
249 /* IMPLEMENTATION NOTE:
251 * On some platforms a GStaticMutex is actually a normal GMutex stored
252 * inside of a structure instead of being allocated dynamically. We can
253 * only do this for platforms on which we know, in advance, how to
254 * allocate (size) and initialise (value) that memory.
256 * On other platforms, a GStaticMutex is nothing more than a pointer to
257 * a GMutex. In that case, the first access we make to the static mutex
258 * must first allocate the normal GMutex and store it into the pointer.
260 * configure.ac writes macros into glibconfig.h to determine if
261 * g_static_mutex_get_mutex() accesses the structure in memory directly
262 * (on platforms where we are able to do that) or if it ends up here,
263 * where we may have to allocate the GMutex before returning it.
267 * g_static_mutex_get_mutex:
268 * @mutex: a #GStaticMutex.
269 * @Returns: the #GMutex corresponding to @mutex.
271 * For some operations (like g_cond_wait()) you must have a #GMutex
272 * instead of a #GStaticMutex. This function will return the
273 * corresponding #GMutex for @mutex.
275 * Deprecated: 2.32: Just use a #GMutex
278 g_static_mutex_get_mutex_impl (GMutex** mutex)
282 if (!g_thread_supported ())
285 result = g_atomic_pointer_get (mutex);
289 g_mutex_lock (&g_once_mutex);
294 result = g_mutex_new ();
295 g_atomic_pointer_set (mutex, result);
298 g_mutex_unlock (&g_once_mutex);
304 /* IMPLEMENTATION NOTE:
306 * g_static_mutex_lock(), g_static_mutex_trylock() and
307 * g_static_mutex_unlock() are all preprocessor macros that wrap the
308 * corresponding g_mutex_*() function around a call to
309 * g_static_mutex_get_mutex().
313 * g_static_mutex_lock:
314 * @mutex: a #GStaticMutex.
316 * Works like g_mutex_lock(), but for a #GStaticMutex.
318 * Deprecated: 2.32: Use g_mutex_lock()
322 * g_static_mutex_trylock:
323 * @mutex: a #GStaticMutex.
324 * @Returns: %TRUE, if the #GStaticMutex could be locked.
326 * Works like g_mutex_trylock(), but for a #GStaticMutex.
328 * Deprecated: 2.32: Use g_mutex_trylock()
332 * g_static_mutex_unlock:
333 * @mutex: a #GStaticMutex.
335 * Works like g_mutex_unlock(), but for a #GStaticMutex.
337 * Deprecated: 2.32: Use g_mutex_unlock()
341 * g_static_mutex_free:
342 * @mutex: a #GStaticMutex to be freed.
344 * Releases all resources allocated to @mutex.
346 * You don't have to call this functions for a #GStaticMutex with an
347 * unbounded lifetime, i.e. objects declared 'static', but if you have
348 * a #GStaticMutex as a member of a structure and the structure is
349 * freed, you should also free the #GStaticMutex.
351 * <note><para>Calling g_static_mutex_free() on a locked mutex may
352 * result in undefined behaviour.</para></note>
354 * Deprecated: 2.32: Use g_mutex_free()
357 g_static_mutex_free (GStaticMutex* mutex)
359 GMutex **runtime_mutex;
361 g_return_if_fail (mutex);
363 /* The runtime_mutex is the first (or only) member of GStaticMutex,
364 * see both versions (of glibconfig.h) in configure.ac. Note, that
365 * this variable is NULL, if g_thread_init() hasn't been called or
366 * if we're using the default thread implementation and it provides
368 runtime_mutex = ((GMutex**)mutex);
371 g_mutex_free (*runtime_mutex);
373 *runtime_mutex = NULL;
376 /* {{{1 GStaticRecMutex */
381 * A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
382 * multiple times by one thread. If you enter it n times, you have to
383 * unlock it n times again to let other threads lock it. An exception
384 * is the function g_static_rec_mutex_unlock_full(): that allows you to
385 * unlock a #GStaticRecMutex completely returning the depth, (i.e. the
386 * number of times this mutex was locked). The depth can later be used
387 * to restore the state of the #GStaticRecMutex by calling
388 * g_static_rec_mutex_lock_full(). In GLib 2.32, #GStaticRecMutex has
389 * been deprecated in favor of #GRecMutex.
391 * Even though #GStaticRecMutex is not opaque, it should only be used
392 * with the following functions.
394 * All of the <function>g_static_rec_mutex_*</function> functions can
395 * be used even if g_thread_init() has not been called. Then they do
396 * nothing, apart from <function>g_static_rec_mutex_trylock</function>,
397 * which does nothing but returning %TRUE.
401 * G_STATIC_REC_MUTEX_INIT:
403 * A #GStaticRecMutex must be initialized with this macro before it can
404 * be used. This macro can used be to initialize a variable, but it
405 * cannot be assigned to a variable. In that case you have to use
406 * g_static_rec_mutex_init().
409 * GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
414 * g_static_rec_mutex_init:
415 * @mutex: a #GStaticRecMutex to be initialized.
417 * A #GStaticRecMutex must be initialized with this function before it
418 * can be used. Alternatively you can initialize it with
419 * #G_STATIC_REC_MUTEX_INIT.
421 * Deprecated: 2.32: Use g_rec_mutex_init()
424 g_static_rec_mutex_init (GStaticRecMutex *mutex)
426 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
428 g_return_if_fail (mutex);
434 * g_static_rec_mutex_lock:
435 * @mutex: a #GStaticRecMutex to lock.
437 * Locks @mutex. If @mutex is already locked by another thread, the
438 * current thread will block until @mutex is unlocked by the other
439 * thread. If @mutex is already locked by the calling thread, this
440 * functions increases the depth of @mutex and returns immediately.
442 * Deprecated: 2.32: Use g_rec_mutex_lock()
445 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
449 g_return_if_fail (mutex);
451 if (!g_thread_supported ())
454 g_system_thread_self (&self);
456 if (g_system_thread_equal (&self, &mutex->owner))
461 g_static_mutex_lock (&mutex->mutex);
462 g_system_thread_assign (mutex->owner, self);
467 * g_static_rec_mutex_trylock:
468 * @mutex: a #GStaticRecMutex to lock.
469 * @Returns: %TRUE, if @mutex could be locked.
471 * Tries to lock @mutex. If @mutex is already locked by another thread,
472 * it immediately returns %FALSE. Otherwise it locks @mutex and returns
473 * %TRUE. If @mutex is already locked by the calling thread, this
474 * functions increases the depth of @mutex and immediately returns
477 * Deprecated: 2.32: Use g_rec_mutex_trylock()
480 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
484 g_return_val_if_fail (mutex, FALSE);
486 if (!g_thread_supported ())
489 g_system_thread_self (&self);
491 if (g_system_thread_equal (&self, &mutex->owner))
497 if (!g_static_mutex_trylock (&mutex->mutex))
500 g_system_thread_assign (mutex->owner, self);
506 * g_static_rec_mutex_unlock:
507 * @mutex: a #GStaticRecMutex to unlock.
509 * Unlocks @mutex. Another thread will be allowed to lock @mutex only
510 * when it has been unlocked as many times as it had been locked
511 * before. If @mutex is completely unlocked and another thread is
512 * blocked in a g_static_rec_mutex_lock() call for @mutex, it will be
513 * woken and can lock @mutex itself.
515 * Deprecated: 2.32: Use g_rec_mutex_unlock()
518 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
520 g_return_if_fail (mutex);
522 if (!g_thread_supported ())
525 if (mutex->depth > 1)
530 g_system_thread_assign (mutex->owner, zero_thread);
531 g_static_mutex_unlock (&mutex->mutex);
535 * g_static_rec_mutex_lock_full:
536 * @mutex: a #GStaticRecMutex to lock.
537 * @depth: number of times this mutex has to be unlocked to be
538 * completely unlocked.
540 * Works like calling g_static_rec_mutex_lock() for @mutex @depth times.
542 * Deprecated: 2.32: Use g_rec_mutex_lock()
545 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
549 g_return_if_fail (mutex);
551 if (!g_thread_supported ())
557 g_system_thread_self (&self);
559 if (g_system_thread_equal (&self, &mutex->owner))
561 mutex->depth += depth;
564 g_static_mutex_lock (&mutex->mutex);
565 g_system_thread_assign (mutex->owner, self);
566 mutex->depth = depth;
570 * g_static_rec_mutex_unlock_full:
571 * @mutex: a #GStaticRecMutex to completely unlock.
572 * @Returns: number of times @mutex has been locked by the current
575 * Completely unlocks @mutex. If another thread is blocked in a
576 * g_static_rec_mutex_lock() call for @mutex, it will be woken and can
577 * lock @mutex itself. This function returns the number of times that
578 * @mutex has been locked by the current thread. To restore the state
579 * before the call to g_static_rec_mutex_unlock_full() you can call
580 * g_static_rec_mutex_lock_full() with the depth returned by this
583 * Deprecated: 2.32: Use g_rec_mutex_unlock()
586 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
590 g_return_val_if_fail (mutex, 0);
592 if (!g_thread_supported ())
595 depth = mutex->depth;
597 g_system_thread_assign (mutex->owner, zero_thread);
599 g_static_mutex_unlock (&mutex->mutex);
605 * g_static_rec_mutex_free:
606 * @mutex: a #GStaticRecMutex to be freed.
608 * Releases all resources allocated to a #GStaticRecMutex.
610 * You don't have to call this functions for a #GStaticRecMutex with an
611 * unbounded lifetime, i.e. objects declared 'static', but if you have
612 * a #GStaticRecMutex as a member of a structure and the structure is
613 * freed, you should also free the #GStaticRecMutex.
615 * Deprecated: 2.32: Use g_rec_mutex_clear()
618 g_static_rec_mutex_free (GStaticRecMutex *mutex)
620 g_return_if_fail (mutex);
622 g_static_mutex_free (&mutex->mutex);
625 /* GStaticRWLock {{{1 ----------------------------------------------------- */
630 * The #GStaticRWLock struct represents a read-write lock. A read-write
631 * lock can be used for protecting data that some portions of code only
632 * read from, while others also write. In such situations it is
633 * desirable that several readers can read at once, whereas of course
634 * only one writer may write at a time. Take a look at the following
638 * <title>An array with access functions</title>
640 * GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
644 * my_array_get (guint index)
646 * gpointer retval = NULL;
651 * g_static_rw_lock_reader_lock (&rwlock);
652 * if (index < array->len)
653 * retval = g_ptr_array_index (array, index);
654 * g_static_rw_lock_reader_unlock (&rwlock);
660 * my_array_set (guint index, gpointer data)
662 * g_static_rw_lock_writer_lock (&rwlock);
665 * array = g_ptr_array_new (<!-- -->);
667 * if (index >= array->len)
668 * g_ptr_array_set_size (array, index+1);
669 * g_ptr_array_index (array, index) = data;
671 * g_static_rw_lock_writer_unlock (&rwlock);
676 * This example shows an array which can be accessed by many readers
677 * (the <function>my_array_get()</function> function) simultaneously,
678 * whereas the writers (the <function>my_array_set()</function>
679 * function) will only be allowed once at a time and only if no readers
680 * currently access the array. This is because of the potentially
681 * dangerous resizing of the array. Using these functions is fully
682 * multi-thread safe now.
684 * Most of the time, writers should have precedence over readers. That
685 * means, for this implementation, that as soon as a writer wants to
686 * lock the data, no other reader is allowed to lock the data, whereas,
687 * of course, the readers that already have locked the data are allowed
688 * to finish their operation. As soon as the last reader unlocks the
689 * data, the writer will lock it.
691 * Even though #GStaticRWLock is not opaque, it should only be used
692 * with the following functions.
694 * All of the <function>g_static_rw_lock_*</function> functions can be
695 * used even if g_thread_init() has not been called. Then they do
696 * nothing, apart from <function>g_static_rw_lock_*_trylock</function>,
697 * which does nothing but returning %TRUE.
699 * <note><para>A read-write lock has a higher overhead than a mutex. For
700 * example, both g_static_rw_lock_reader_lock() and
701 * g_static_rw_lock_reader_unlock() have to lock and unlock a
702 * #GStaticMutex, so it takes at least twice the time to lock and unlock
703 * a #GStaticRWLock that it does to lock and unlock a #GStaticMutex. So
704 * only data structures that are accessed by multiple readers, and which
705 * keep the lock for a considerable time justify a #GStaticRWLock. The
706 * above example most probably would fare better with a
707 * #GStaticMutex.</para></note>
709 * Deprecated: 2.32: Use a #GRWLock instead
713 * G_STATIC_RW_LOCK_INIT:
715 * A #GStaticRWLock must be initialized with this macro before it can
716 * be used. This macro can used be to initialize a variable, but it
717 * cannot be assigned to a variable. In that case you have to use
718 * g_static_rw_lock_init().
721 * GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
726 * g_static_rw_lock_init:
727 * @lock: a #GStaticRWLock to be initialized.
729 * A #GStaticRWLock must be initialized with this function before it
730 * can be used. Alternatively you can initialize it with
731 * #G_STATIC_RW_LOCK_INIT.
733 * Deprecated: 2.32: Use g_rw_lock_init() instead
736 g_static_rw_lock_init (GStaticRWLock* lock)
738 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
740 g_return_if_fail (lock);
746 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
749 *cond = g_cond_new ();
750 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
754 g_static_rw_lock_signal (GStaticRWLock* lock)
756 if (lock->want_to_write && lock->write_cond)
757 g_cond_signal (lock->write_cond);
758 else if (lock->want_to_read && lock->read_cond)
759 g_cond_broadcast (lock->read_cond);
763 * g_static_rw_lock_reader_lock:
764 * @lock: a #GStaticRWLock to lock for reading.
766 * Locks @lock for reading. There may be unlimited concurrent locks for
767 * reading of a #GStaticRWLock at the same time. If @lock is already
768 * locked for writing by another thread or if another thread is already
769 * waiting to lock @lock for writing, this function will block until
770 * @lock is unlocked by the other writing thread and no other writing
771 * threads want to lock @lock. This lock has to be unlocked by
772 * g_static_rw_lock_reader_unlock().
774 * #GStaticRWLock is not recursive. It might seem to be possible to
775 * recursively lock for reading, but that can result in a deadlock, due
776 * to writer preference.
778 * Deprecated: 2.32: Use g_rw_lock_reader_lock() instead
781 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
783 g_return_if_fail (lock);
785 if (!g_threads_got_initialized)
788 g_static_mutex_lock (&lock->mutex);
789 lock->want_to_read++;
790 while (lock->have_writer || lock->want_to_write)
791 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
792 lock->want_to_read--;
793 lock->read_counter++;
794 g_static_mutex_unlock (&lock->mutex);
798 * g_static_rw_lock_reader_trylock:
799 * @lock: a #GStaticRWLock to lock for reading.
800 * @Returns: %TRUE, if @lock could be locked for reading.
802 * Tries to lock @lock for reading. If @lock is already locked for
803 * writing by another thread or if another thread is already waiting to
804 * lock @lock for writing, immediately returns %FALSE. Otherwise locks
805 * @lock for reading and returns %TRUE. This lock has to be unlocked by
806 * g_static_rw_lock_reader_unlock().
808 * Deprectated: 2.32: Use g_rw_lock_reader_trylock() instead
811 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
813 gboolean ret_val = FALSE;
815 g_return_val_if_fail (lock, FALSE);
817 if (!g_threads_got_initialized)
820 g_static_mutex_lock (&lock->mutex);
821 if (!lock->have_writer && !lock->want_to_write)
823 lock->read_counter++;
826 g_static_mutex_unlock (&lock->mutex);
831 * g_static_rw_lock_reader_unlock:
832 * @lock: a #GStaticRWLock to unlock after reading.
834 * Unlocks @lock. If a thread waits to lock @lock for writing and all
835 * locks for reading have been unlocked, the waiting thread is woken up
836 * and can lock @lock for writing.
838 * Deprectated: 2.32: Use g_rw_lock_reader_unlock() instead
841 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
843 g_return_if_fail (lock);
845 if (!g_threads_got_initialized)
848 g_static_mutex_lock (&lock->mutex);
849 lock->read_counter--;
850 if (lock->read_counter == 0)
851 g_static_rw_lock_signal (lock);
852 g_static_mutex_unlock (&lock->mutex);
856 * g_static_rw_lock_writer_lock:
857 * @lock: a #GStaticRWLock to lock for writing.
859 * Locks @lock for writing. If @lock is already locked for writing or
860 * reading by other threads, this function will block until @lock is
861 * completely unlocked and then lock @lock for writing. While this
862 * functions waits to lock @lock, no other thread can lock @lock for
863 * reading. When @lock is locked for writing, no other thread can lock
864 * @lock (neither for reading nor writing). This lock has to be
865 * unlocked by g_static_rw_lock_writer_unlock().
867 * Deprectated: 2.32: Use g_rw_lock_writer_lock() instead
870 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
872 g_return_if_fail (lock);
874 if (!g_threads_got_initialized)
877 g_static_mutex_lock (&lock->mutex);
878 lock->want_to_write++;
879 while (lock->have_writer || lock->read_counter)
880 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
881 lock->want_to_write--;
882 lock->have_writer = TRUE;
883 g_static_mutex_unlock (&lock->mutex);
887 * g_static_rw_lock_writer_trylock:
888 * @lock: a #GStaticRWLock to lock for writing.
889 * @Returns: %TRUE, if @lock could be locked for writing.
891 * Tries to lock @lock for writing. If @lock is already locked (for
892 * either reading or writing) by another thread, it immediately returns
893 * %FALSE. Otherwise it locks @lock for writing and returns %TRUE. This
894 * lock has to be unlocked by g_static_rw_lock_writer_unlock().
896 * Deprectated: 2.32: Use g_rw_lock_writer_trylock() instead
899 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
901 gboolean ret_val = FALSE;
903 g_return_val_if_fail (lock, FALSE);
905 if (!g_threads_got_initialized)
908 g_static_mutex_lock (&lock->mutex);
909 if (!lock->have_writer && !lock->read_counter)
911 lock->have_writer = TRUE;
914 g_static_mutex_unlock (&lock->mutex);
919 * g_static_rw_lock_writer_unlock:
920 * @lock: a #GStaticRWLock to unlock after writing.
922 * Unlocks @lock. If a thread is waiting to lock @lock for writing and
923 * all locks for reading have been unlocked, the waiting thread is
924 * woken up and can lock @lock for writing. If no thread is waiting to
925 * lock @lock for writing, and some thread or threads are waiting to
926 * lock @lock for reading, the waiting threads are woken up and can
927 * lock @lock for reading.
929 * Deprectated: 2.32: Use g_rw_lock_writer_unlock() instead
932 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
934 g_return_if_fail (lock);
936 if (!g_threads_got_initialized)
939 g_static_mutex_lock (&lock->mutex);
940 lock->have_writer = FALSE;
941 g_static_rw_lock_signal (lock);
942 g_static_mutex_unlock (&lock->mutex);
946 * g_static_rw_lock_free:
947 * @lock: a #GStaticRWLock to be freed.
949 * Releases all resources allocated to @lock.
951 * You don't have to call this functions for a #GStaticRWLock with an
952 * unbounded lifetime, i.e. objects declared 'static', but if you have
953 * a #GStaticRWLock as a member of a structure, and the structure is
954 * freed, you should also free the #GStaticRWLock.
956 * Deprecated: 2.32: Use a #GRWLock instead
959 g_static_rw_lock_free (GStaticRWLock* lock)
961 g_return_if_fail (lock);
965 g_cond_free (lock->read_cond);
966 lock->read_cond = NULL;
968 if (lock->write_cond)
970 g_cond_free (lock->write_cond);
971 lock->write_cond = NULL;
973 g_static_mutex_free (&lock->mutex);
976 /* vim: set foldmethod=marker: */