+/* {{{1 GRecMutex */
+
+static pthread_mutex_t *
+g_rec_mutex_impl_new (void)
+{
+ pthread_mutexattr_t attr;
+ pthread_mutex_t *mutex;
+
+ mutex = malloc (sizeof (pthread_mutex_t));
+ if G_UNLIKELY (mutex == NULL)
+ g_thread_abort (errno, "malloc");
+
+ pthread_mutexattr_init (&attr);
+ pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
+ pthread_mutex_init (mutex, &attr);
+ pthread_mutexattr_destroy (&attr);
+
+ return mutex;
+}
+
+static void
+g_rec_mutex_impl_free (pthread_mutex_t *mutex)
+{
+ pthread_mutex_destroy (mutex);
+ free (mutex);
+}
+
+static inline pthread_mutex_t *
+g_rec_mutex_get_impl (GRecMutex *rec_mutex)
+{
+ pthread_mutex_t *impl = g_atomic_pointer_get (&rec_mutex->p);
+
+ if G_UNLIKELY (impl == NULL)
+ {
+ impl = g_rec_mutex_impl_new ();
+ if (!g_atomic_pointer_compare_and_exchange (&rec_mutex->p, NULL, impl))
+ g_rec_mutex_impl_free (impl);
+ impl = rec_mutex->p;
+ }
+
+ return impl;
+}
+
+/**
+ * g_rec_mutex_init:
+ * @rec_mutex: an uninitialized #GRecMutex
+ *
+ * Initializes a #GRecMutex so that it can be used.
+ *
+ * This function is useful to initialize a recursive mutex
+ * that has been allocated on the stack, or as part of a larger
+ * structure.
+ *
+ * It is not necessary to initialise a recursive mutex that has been
+ * statically allocated.
+ *
+ * |[<!-- language="C" -->
+ * typedef struct {
+ * GRecMutex m;
+ * ...
+ * } Blob;
+ *
+ * Blob *b;
+ *
+ * b = g_new (Blob, 1);
+ * g_rec_mutex_init (&b->m);
+ * ]|
+ *
+ * Calling g_rec_mutex_init() on an already initialized #GRecMutex
+ * leads to undefined behaviour.
+ *
+ * To undo the effect of g_rec_mutex_init() when a recursive mutex
+ * is no longer needed, use g_rec_mutex_clear().
+ *
+ * Since: 2.32
+ */
+void
+g_rec_mutex_init (GRecMutex *rec_mutex)
+{
+ rec_mutex->p = g_rec_mutex_impl_new ();
+}
+
+/**
+ * g_rec_mutex_clear:
+ * @rec_mutex: an initialized #GRecMutex
+ *
+ * Frees the resources allocated to a recursive mutex with
+ * g_rec_mutex_init().
+ *
+ * This function should not be used with a #GRecMutex that has been
+ * statically allocated.
+ *
+ * Calling g_rec_mutex_clear() on a locked recursive mutex leads
+ * to undefined behaviour.
+ *
+ * Sine: 2.32
+ */
+void
+g_rec_mutex_clear (GRecMutex *rec_mutex)
+{
+ g_rec_mutex_impl_free (rec_mutex->p);
+}
+
+/**
+ * g_rec_mutex_lock:
+ * @rec_mutex: a #GRecMutex
+ *
+ * Locks @rec_mutex. If @rec_mutex is already locked by another
+ * thread, the current thread will block until @rec_mutex is
+ * unlocked by the other thread. If @rec_mutex is already locked
+ * by the current thread, the 'lock count' of @rec_mutex is increased.
+ * The mutex will only become available again when it is unlocked
+ * as many times as it has been locked.
+ *
+ * Since: 2.32
+ */
+void
+g_rec_mutex_lock (GRecMutex *mutex)
+{
+ pthread_mutex_lock (g_rec_mutex_get_impl (mutex));
+}
+
+/**
+ * g_rec_mutex_unlock:
+ * @rec_mutex: a #GRecMutex
+ *
+ * Unlocks @rec_mutex. If another thread is blocked in a
+ * g_rec_mutex_lock() call for @rec_mutex, it will become unblocked
+ * and can lock @rec_mutex itself.
+ *
+ * Calling g_rec_mutex_unlock() on a recursive mutex that is not
+ * locked by the current thread leads to undefined behaviour.
+ *
+ * Since: 2.32
+ */
+void
+g_rec_mutex_unlock (GRecMutex *rec_mutex)
+{
+ pthread_mutex_unlock (rec_mutex->p);
+}
+
+/**
+ * g_rec_mutex_trylock:
+ * @rec_mutex: a #GRecMutex
+ *
+ * Tries to lock @rec_mutex. If @rec_mutex is already locked
+ * by another thread, it immediately returns %FALSE. Otherwise
+ * it locks @rec_mutex and returns %TRUE.
+ *
+ * Returns: %TRUE if @rec_mutex could be locked
+ *
+ * Since: 2.32
+ */
+gboolean
+g_rec_mutex_trylock (GRecMutex *rec_mutex)
+{
+ if (pthread_mutex_trylock (g_rec_mutex_get_impl (rec_mutex)) != 0)
+ return FALSE;
+
+ return TRUE;
+}
+