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)
131 * @func: a function to execute in the new thread
132 * @data: an argument to supply to the new thread
133 * @joinable: should this thread be joinable?
134 * @error: return location for error, or %NULL
136 * This function creates a new thread.
138 * If @joinable is %TRUE, you can wait for this threads termination
139 * calling g_thread_join(). Otherwise the thread will just disappear
140 * when it terminates.
142 * The new thread executes the function @func with the argument @data.
143 * If the thread was created successfully, it is returned.
145 * @error can be %NULL to ignore errors, or non-%NULL to report errors.
146 * The error is set, if and only if the function returns %NULL.
148 * Returns: the new #GThread on success
150 * Deprecated:2.32: Use g_thread_new() instead
153 g_thread_create (GThreadFunc func,
158 return g_thread_new_full (NULL, func, data, joinable, 0, error);
162 * g_thread_create_full:
163 * @func: a function to execute in the new thread.
164 * @data: an argument to supply to the new thread.
165 * @stack_size: a stack size for the new thread.
166 * @joinable: should this thread be joinable?
169 * @error: return location for error.
170 * @Returns: the new #GThread on success.
172 * This function creates a new thread.
174 * Deprecated:2.32: The @bound and @priority arguments are now ignored.
175 * Use g_thread_new() or g_thread_new_full() instead.
178 g_thread_create_full (GThreadFunc func,
183 GThreadPriority priority,
186 return g_thread_new_full (NULL, func, data, joinable, stack_size, error);
189 /* GStaticMutex {{{1 ------------------------------------------------------ */
194 * A #GStaticMutex works like a #GMutex.
196 * Prior to GLib 2.32, GStaticMutex had the significant advantage
197 * that it doesn't need to be created at run-time, but can be defined
198 * at compile-time. Since 2.32, #GMutex can be statically allocated
199 * as well, and GStaticMutex has been deprecated.
201 * Here is a version of our give_me_next_number() example using
206 * Using <structname>GStaticMutex</structname>
207 * to simplify thread-safe programming
211 * give_me_next_number (void)
213 * static int current_number = 0;
215 * static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
217 * g_static_mutex_lock (&mutex);
218 * ret_val = current_number = calc_next_number (current_number);
219 * g_static_mutex_unlock (&mutex);
226 * Sometimes you would like to dynamically create a mutex. If you don't
227 * want to require prior calling to g_thread_init(), because your code
228 * should also be usable in non-threaded programs, you are not able to
229 * use g_mutex_new() and thus #GMutex, as that requires a prior call to
230 * g_thread_init(). In theses cases you can also use a #GStaticMutex.
231 * It must be initialized with g_static_mutex_init() before using it
232 * and freed with with g_static_mutex_free() when not needed anymore to
233 * free up any allocated resources.
235 * Even though #GStaticMutex is not opaque, it should only be used with
236 * the following functions, as it is defined differently on different
239 * All of the <function>g_static_mutex_*</function> functions apart
240 * from <function>g_static_mutex_get_mutex</function> can also be used
241 * even if g_thread_init() has not yet been called. Then they do
242 * nothing, apart from <function>g_static_mutex_trylock</function>,
243 * which does nothing but returning %TRUE.
245 * <note><para>All of the <function>g_static_mutex_*</function>
246 * functions are actually macros. Apart from taking their addresses, you
247 * can however use them as if they were functions.</para></note>
251 * G_STATIC_MUTEX_INIT:
253 * A #GStaticMutex must be initialized with this macro, before it can
254 * be used. This macro can used be to initialize a variable, but it
255 * cannot be assigned to a variable. In that case you have to use
256 * g_static_mutex_init().
259 * GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
264 * g_static_mutex_init:
265 * @mutex: a #GStaticMutex to be initialized.
267 * Initializes @mutex.
268 * Alternatively you can initialize it with #G_STATIC_MUTEX_INIT.
270 * Deprecated: 2.32: Use g_mutex_init()
273 g_static_mutex_init (GStaticMutex *mutex)
275 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
277 g_return_if_fail (mutex);
282 /* IMPLEMENTATION NOTE:
284 * On some platforms a GStaticMutex is actually a normal GMutex stored
285 * inside of a structure instead of being allocated dynamically. We can
286 * only do this for platforms on which we know, in advance, how to
287 * allocate (size) and initialise (value) that memory.
289 * On other platforms, a GStaticMutex is nothing more than a pointer to
290 * a GMutex. In that case, the first access we make to the static mutex
291 * must first allocate the normal GMutex and store it into the pointer.
293 * configure.ac writes macros into glibconfig.h to determine if
294 * g_static_mutex_get_mutex() accesses the structure in memory directly
295 * (on platforms where we are able to do that) or if it ends up here,
296 * where we may have to allocate the GMutex before returning it.
300 * g_static_mutex_get_mutex:
301 * @mutex: a #GStaticMutex.
302 * @Returns: the #GMutex corresponding to @mutex.
304 * For some operations (like g_cond_wait()) you must have a #GMutex
305 * instead of a #GStaticMutex. This function will return the
306 * corresponding #GMutex for @mutex.
308 * Deprecated: 2.32: Just use a #GMutex
311 g_static_mutex_get_mutex_impl (GMutex** mutex)
315 if (!g_thread_supported ())
318 result = g_atomic_pointer_get (mutex);
322 g_mutex_lock (&g_once_mutex);
327 result = g_mutex_new ();
328 g_atomic_pointer_set (mutex, result);
331 g_mutex_unlock (&g_once_mutex);
337 /* IMPLEMENTATION NOTE:
339 * g_static_mutex_lock(), g_static_mutex_trylock() and
340 * g_static_mutex_unlock() are all preprocessor macros that wrap the
341 * corresponding g_mutex_*() function around a call to
342 * g_static_mutex_get_mutex().
346 * g_static_mutex_lock:
347 * @mutex: a #GStaticMutex.
349 * Works like g_mutex_lock(), but for a #GStaticMutex.
351 * Deprecated: 2.32: Use g_mutex_lock()
355 * g_static_mutex_trylock:
356 * @mutex: a #GStaticMutex.
357 * @Returns: %TRUE, if the #GStaticMutex could be locked.
359 * Works like g_mutex_trylock(), but for a #GStaticMutex.
361 * Deprecated: 2.32: Use g_mutex_trylock()
365 * g_static_mutex_unlock:
366 * @mutex: a #GStaticMutex.
368 * Works like g_mutex_unlock(), but for a #GStaticMutex.
370 * Deprecated: 2.32: Use g_mutex_unlock()
374 * g_static_mutex_free:
375 * @mutex: a #GStaticMutex to be freed.
377 * Releases all resources allocated to @mutex.
379 * You don't have to call this functions for a #GStaticMutex with an
380 * unbounded lifetime, i.e. objects declared 'static', but if you have
381 * a #GStaticMutex as a member of a structure and the structure is
382 * freed, you should also free the #GStaticMutex.
384 * <note><para>Calling g_static_mutex_free() on a locked mutex may
385 * result in undefined behaviour.</para></note>
387 * Deprecated: 2.32: Use g_mutex_free()
390 g_static_mutex_free (GStaticMutex* mutex)
392 GMutex **runtime_mutex;
394 g_return_if_fail (mutex);
396 /* The runtime_mutex is the first (or only) member of GStaticMutex,
397 * see both versions (of glibconfig.h) in configure.ac. Note, that
398 * this variable is NULL, if g_thread_init() hasn't been called or
399 * if we're using the default thread implementation and it provides
401 runtime_mutex = ((GMutex**)mutex);
404 g_mutex_free (*runtime_mutex);
406 *runtime_mutex = NULL;
409 /* {{{1 GStaticRecMutex */
414 * A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
415 * multiple times by one thread. If you enter it n times, you have to
416 * unlock it n times again to let other threads lock it. An exception
417 * is the function g_static_rec_mutex_unlock_full(): that allows you to
418 * unlock a #GStaticRecMutex completely returning the depth, (i.e. the
419 * number of times this mutex was locked). The depth can later be used
420 * to restore the state of the #GStaticRecMutex by calling
421 * g_static_rec_mutex_lock_full(). In GLib 2.32, #GStaticRecMutex has
422 * been deprecated in favor of #GRecMutex.
424 * Even though #GStaticRecMutex is not opaque, it should only be used
425 * with the following functions.
427 * All of the <function>g_static_rec_mutex_*</function> functions can
428 * be used even if g_thread_init() has not been called. Then they do
429 * nothing, apart from <function>g_static_rec_mutex_trylock</function>,
430 * which does nothing but returning %TRUE.
434 * G_STATIC_REC_MUTEX_INIT:
436 * A #GStaticRecMutex must be initialized with this macro before it can
437 * be used. This macro can used be to initialize a variable, but it
438 * cannot be assigned to a variable. In that case you have to use
439 * g_static_rec_mutex_init().
442 * GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
447 * g_static_rec_mutex_init:
448 * @mutex: a #GStaticRecMutex to be initialized.
450 * A #GStaticRecMutex must be initialized with this function before it
451 * can be used. Alternatively you can initialize it with
452 * #G_STATIC_REC_MUTEX_INIT.
454 * Deprecated: 2.32: Use g_rec_mutex_init()
457 g_static_rec_mutex_init (GStaticRecMutex *mutex)
459 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
461 g_return_if_fail (mutex);
467 * g_static_rec_mutex_lock:
468 * @mutex: a #GStaticRecMutex to lock.
470 * Locks @mutex. If @mutex is already locked by another thread, the
471 * current thread will block until @mutex is unlocked by the other
472 * thread. If @mutex is already locked by the calling thread, this
473 * functions increases the depth of @mutex and returns immediately.
475 * Deprecated: 2.32: Use g_rec_mutex_lock()
478 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
482 g_return_if_fail (mutex);
484 if (!g_thread_supported ())
487 g_system_thread_self (&self);
489 if (g_system_thread_equal (&self, &mutex->owner))
494 g_static_mutex_lock (&mutex->mutex);
495 g_system_thread_assign (mutex->owner, self);
500 * g_static_rec_mutex_trylock:
501 * @mutex: a #GStaticRecMutex to lock.
502 * @Returns: %TRUE, if @mutex could be locked.
504 * Tries to lock @mutex. If @mutex is already locked by another thread,
505 * it immediately returns %FALSE. Otherwise it locks @mutex and returns
506 * %TRUE. If @mutex is already locked by the calling thread, this
507 * functions increases the depth of @mutex and immediately returns
510 * Deprecated: 2.32: Use g_rec_mutex_trylock()
513 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
517 g_return_val_if_fail (mutex, FALSE);
519 if (!g_thread_supported ())
522 g_system_thread_self (&self);
524 if (g_system_thread_equal (&self, &mutex->owner))
530 if (!g_static_mutex_trylock (&mutex->mutex))
533 g_system_thread_assign (mutex->owner, self);
539 * g_static_rec_mutex_unlock:
540 * @mutex: a #GStaticRecMutex to unlock.
542 * Unlocks @mutex. Another thread will be allowed to lock @mutex only
543 * when it has been unlocked as many times as it had been locked
544 * before. If @mutex is completely unlocked and another thread is
545 * blocked in a g_static_rec_mutex_lock() call for @mutex, it will be
546 * woken and can lock @mutex itself.
548 * Deprecated: 2.32: Use g_rec_mutex_unlock()
551 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
553 g_return_if_fail (mutex);
555 if (!g_thread_supported ())
558 if (mutex->depth > 1)
563 g_system_thread_assign (mutex->owner, zero_thread);
564 g_static_mutex_unlock (&mutex->mutex);
568 * g_static_rec_mutex_lock_full:
569 * @mutex: a #GStaticRecMutex to lock.
570 * @depth: number of times this mutex has to be unlocked to be
571 * completely unlocked.
573 * Works like calling g_static_rec_mutex_lock() for @mutex @depth times.
575 * Deprecated: 2.32: Use g_rec_mutex_lock()
578 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
582 g_return_if_fail (mutex);
584 if (!g_thread_supported ())
590 g_system_thread_self (&self);
592 if (g_system_thread_equal (&self, &mutex->owner))
594 mutex->depth += depth;
597 g_static_mutex_lock (&mutex->mutex);
598 g_system_thread_assign (mutex->owner, self);
599 mutex->depth = depth;
603 * g_static_rec_mutex_unlock_full:
604 * @mutex: a #GStaticRecMutex to completely unlock.
605 * @Returns: number of times @mutex has been locked by the current
608 * Completely unlocks @mutex. If another thread is blocked in a
609 * g_static_rec_mutex_lock() call for @mutex, it will be woken and can
610 * lock @mutex itself. This function returns the number of times that
611 * @mutex has been locked by the current thread. To restore the state
612 * before the call to g_static_rec_mutex_unlock_full() you can call
613 * g_static_rec_mutex_lock_full() with the depth returned by this
616 * Deprecated: 2.32: Use g_rec_mutex_unlock()
619 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
623 g_return_val_if_fail (mutex, 0);
625 if (!g_thread_supported ())
628 depth = mutex->depth;
630 g_system_thread_assign (mutex->owner, zero_thread);
632 g_static_mutex_unlock (&mutex->mutex);
638 * g_static_rec_mutex_free:
639 * @mutex: a #GStaticRecMutex to be freed.
641 * Releases all resources allocated to a #GStaticRecMutex.
643 * You don't have to call this functions for a #GStaticRecMutex with an
644 * unbounded lifetime, i.e. objects declared 'static', but if you have
645 * a #GStaticRecMutex as a member of a structure and the structure is
646 * freed, you should also free the #GStaticRecMutex.
648 * Deprecated: 2.32: Use g_rec_mutex_clear()
651 g_static_rec_mutex_free (GStaticRecMutex *mutex)
653 g_return_if_fail (mutex);
655 g_static_mutex_free (&mutex->mutex);
658 /* GStaticRWLock {{{1 ----------------------------------------------------- */
663 * The #GStaticRWLock struct represents a read-write lock. A read-write
664 * lock can be used for protecting data that some portions of code only
665 * read from, while others also write. In such situations it is
666 * desirable that several readers can read at once, whereas of course
667 * only one writer may write at a time. Take a look at the following
671 * <title>An array with access functions</title>
673 * GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
677 * my_array_get (guint index)
679 * gpointer retval = NULL;
684 * g_static_rw_lock_reader_lock (&rwlock);
685 * if (index < array->len)
686 * retval = g_ptr_array_index (array, index);
687 * g_static_rw_lock_reader_unlock (&rwlock);
693 * my_array_set (guint index, gpointer data)
695 * g_static_rw_lock_writer_lock (&rwlock);
698 * array = g_ptr_array_new (<!-- -->);
700 * if (index >= array->len)
701 * g_ptr_array_set_size (array, index+1);
702 * g_ptr_array_index (array, index) = data;
704 * g_static_rw_lock_writer_unlock (&rwlock);
709 * This example shows an array which can be accessed by many readers
710 * (the <function>my_array_get()</function> function) simultaneously,
711 * whereas the writers (the <function>my_array_set()</function>
712 * function) will only be allowed once at a time and only if no readers
713 * currently access the array. This is because of the potentially
714 * dangerous resizing of the array. Using these functions is fully
715 * multi-thread safe now.
717 * Most of the time, writers should have precedence over readers. That
718 * means, for this implementation, that as soon as a writer wants to
719 * lock the data, no other reader is allowed to lock the data, whereas,
720 * of course, the readers that already have locked the data are allowed
721 * to finish their operation. As soon as the last reader unlocks the
722 * data, the writer will lock it.
724 * Even though #GStaticRWLock is not opaque, it should only be used
725 * with the following functions.
727 * All of the <function>g_static_rw_lock_*</function> functions can be
728 * used even if g_thread_init() has not been called. Then they do
729 * nothing, apart from <function>g_static_rw_lock_*_trylock</function>,
730 * which does nothing but returning %TRUE.
732 * <note><para>A read-write lock has a higher overhead than a mutex. For
733 * example, both g_static_rw_lock_reader_lock() and
734 * g_static_rw_lock_reader_unlock() have to lock and unlock a
735 * #GStaticMutex, so it takes at least twice the time to lock and unlock
736 * a #GStaticRWLock that it does to lock and unlock a #GStaticMutex. So
737 * only data structures that are accessed by multiple readers, and which
738 * keep the lock for a considerable time justify a #GStaticRWLock. The
739 * above example most probably would fare better with a
740 * #GStaticMutex.</para></note>
742 * Deprecated: 2.32: Use a #GRWLock instead
746 * G_STATIC_RW_LOCK_INIT:
748 * A #GStaticRWLock must be initialized with this macro before it can
749 * be used. This macro can used be to initialize a variable, but it
750 * cannot be assigned to a variable. In that case you have to use
751 * g_static_rw_lock_init().
754 * GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
759 * g_static_rw_lock_init:
760 * @lock: a #GStaticRWLock to be initialized.
762 * A #GStaticRWLock must be initialized with this function before it
763 * can be used. Alternatively you can initialize it with
764 * #G_STATIC_RW_LOCK_INIT.
766 * Deprecated: 2.32: Use g_rw_lock_init() instead
769 g_static_rw_lock_init (GStaticRWLock* lock)
771 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
773 g_return_if_fail (lock);
779 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
782 *cond = g_cond_new ();
783 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
787 g_static_rw_lock_signal (GStaticRWLock* lock)
789 if (lock->want_to_write && lock->write_cond)
790 g_cond_signal (lock->write_cond);
791 else if (lock->want_to_read && lock->read_cond)
792 g_cond_broadcast (lock->read_cond);
796 * g_static_rw_lock_reader_lock:
797 * @lock: a #GStaticRWLock to lock for reading.
799 * Locks @lock for reading. There may be unlimited concurrent locks for
800 * reading of a #GStaticRWLock at the same time. If @lock is already
801 * locked for writing by another thread or if another thread is already
802 * waiting to lock @lock for writing, this function will block until
803 * @lock is unlocked by the other writing thread and no other writing
804 * threads want to lock @lock. This lock has to be unlocked by
805 * g_static_rw_lock_reader_unlock().
807 * #GStaticRWLock is not recursive. It might seem to be possible to
808 * recursively lock for reading, but that can result in a deadlock, due
809 * to writer preference.
811 * Deprecated: 2.32: Use g_rw_lock_reader_lock() instead
814 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
816 g_return_if_fail (lock);
818 if (!g_threads_got_initialized)
821 g_static_mutex_lock (&lock->mutex);
822 lock->want_to_read++;
823 while (lock->have_writer || lock->want_to_write)
824 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
825 lock->want_to_read--;
826 lock->read_counter++;
827 g_static_mutex_unlock (&lock->mutex);
831 * g_static_rw_lock_reader_trylock:
832 * @lock: a #GStaticRWLock to lock for reading.
833 * @Returns: %TRUE, if @lock could be locked for reading.
835 * Tries to lock @lock for reading. If @lock is already locked for
836 * writing by another thread or if another thread is already waiting to
837 * lock @lock for writing, immediately returns %FALSE. Otherwise locks
838 * @lock for reading and returns %TRUE. This lock has to be unlocked by
839 * g_static_rw_lock_reader_unlock().
841 * Deprectated: 2.32: Use g_rw_lock_reader_trylock() instead
844 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
846 gboolean ret_val = FALSE;
848 g_return_val_if_fail (lock, FALSE);
850 if (!g_threads_got_initialized)
853 g_static_mutex_lock (&lock->mutex);
854 if (!lock->have_writer && !lock->want_to_write)
856 lock->read_counter++;
859 g_static_mutex_unlock (&lock->mutex);
864 * g_static_rw_lock_reader_unlock:
865 * @lock: a #GStaticRWLock to unlock after reading.
867 * Unlocks @lock. If a thread waits to lock @lock for writing and all
868 * locks for reading have been unlocked, the waiting thread is woken up
869 * and can lock @lock for writing.
871 * Deprectated: 2.32: Use g_rw_lock_reader_unlock() instead
874 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
876 g_return_if_fail (lock);
878 if (!g_threads_got_initialized)
881 g_static_mutex_lock (&lock->mutex);
882 lock->read_counter--;
883 if (lock->read_counter == 0)
884 g_static_rw_lock_signal (lock);
885 g_static_mutex_unlock (&lock->mutex);
889 * g_static_rw_lock_writer_lock:
890 * @lock: a #GStaticRWLock to lock for writing.
892 * Locks @lock for writing. If @lock is already locked for writing or
893 * reading by other threads, this function will block until @lock is
894 * completely unlocked and then lock @lock for writing. While this
895 * functions waits to lock @lock, no other thread can lock @lock for
896 * reading. When @lock is locked for writing, no other thread can lock
897 * @lock (neither for reading nor writing). This lock has to be
898 * unlocked by g_static_rw_lock_writer_unlock().
900 * Deprectated: 2.32: Use g_rw_lock_writer_lock() instead
903 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
905 g_return_if_fail (lock);
907 if (!g_threads_got_initialized)
910 g_static_mutex_lock (&lock->mutex);
911 lock->want_to_write++;
912 while (lock->have_writer || lock->read_counter)
913 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
914 lock->want_to_write--;
915 lock->have_writer = TRUE;
916 g_static_mutex_unlock (&lock->mutex);
920 * g_static_rw_lock_writer_trylock:
921 * @lock: a #GStaticRWLock to lock for writing.
922 * @Returns: %TRUE, if @lock could be locked for writing.
924 * Tries to lock @lock for writing. If @lock is already locked (for
925 * either reading or writing) by another thread, it immediately returns
926 * %FALSE. Otherwise it locks @lock for writing and returns %TRUE. This
927 * lock has to be unlocked by g_static_rw_lock_writer_unlock().
929 * Deprectated: 2.32: Use g_rw_lock_writer_trylock() instead
932 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
934 gboolean ret_val = FALSE;
936 g_return_val_if_fail (lock, FALSE);
938 if (!g_threads_got_initialized)
941 g_static_mutex_lock (&lock->mutex);
942 if (!lock->have_writer && !lock->read_counter)
944 lock->have_writer = TRUE;
947 g_static_mutex_unlock (&lock->mutex);
952 * g_static_rw_lock_writer_unlock:
953 * @lock: a #GStaticRWLock to unlock after writing.
955 * Unlocks @lock. If a thread is waiting to lock @lock for writing and
956 * all locks for reading have been unlocked, the waiting thread is
957 * woken up and can lock @lock for writing. If no thread is waiting to
958 * lock @lock for writing, and some thread or threads are waiting to
959 * lock @lock for reading, the waiting threads are woken up and can
960 * lock @lock for reading.
962 * Deprectated: 2.32: Use g_rw_lock_writer_unlock() instead
965 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
967 g_return_if_fail (lock);
969 if (!g_threads_got_initialized)
972 g_static_mutex_lock (&lock->mutex);
973 lock->have_writer = FALSE;
974 g_static_rw_lock_signal (lock);
975 g_static_mutex_unlock (&lock->mutex);
979 * g_static_rw_lock_free:
980 * @lock: a #GStaticRWLock to be freed.
982 * Releases all resources allocated to @lock.
984 * You don't have to call this functions for a #GStaticRWLock with an
985 * unbounded lifetime, i.e. objects declared 'static', but if you have
986 * a #GStaticRWLock as a member of a structure, and the structure is
987 * freed, you should also free the #GStaticRWLock.
989 * Deprecated: 2.32: Use a #GRWLock instead
992 g_static_rw_lock_free (GStaticRWLock* lock)
994 g_return_if_fail (lock);
998 g_cond_free (lock->read_cond);
999 lock->read_cond = NULL;
1001 if (lock->write_cond)
1003 g_cond_free (lock->write_cond);
1004 lock->write_cond = NULL;
1006 g_static_mutex_free (&lock->mutex);
1009 /* GPrivate {{{1 ------------------------------------------------------ */
1013 * @notify: a #GDestroyNotify
1015 * Deprecated:2.32: dynamic allocation of #GPrivate is a bad idea. Use
1016 * static storage and G_PRIVATE_INIT() instead.
1018 * Returns: a newly allocated #GPrivate (which can never be destroyed)
1021 g_private_new (GDestroyNotify notify)
1023 GPrivate tmp = G_PRIVATE_INIT (notify);
1026 key = g_slice_new (GPrivate);
1033 /* vim: set foldmethod=marker: */