1
0
mirror of https://gitlab.gnome.org/GNOME/glib.git synced 2025-03-15 20:25:12 +01:00

GStaticRecMutex: implement via GRecMutex

Instead of doing our own hacked-up version.

This also reduces use of GSystemThread.
This commit is contained in:
Ryan Lortie 2011-10-11 11:08:48 -04:00
parent 96904b6790
commit 2b281e40f3

@ -639,6 +639,34 @@ g_static_rec_mutex_init (GStaticRecMutex *mutex)
*mutex = init_mutex; *mutex = init_mutex;
} }
GRecMutex *
g_static_rec_mutex_get_rec_mutex_impl (GStaticRecMutex* mutex)
{
GRecMutex *result;
if (!g_thread_supported ())
return NULL;
result = g_atomic_pointer_get (&mutex->mutex.mutex);
if (!result)
{
g_mutex_lock (&g_once_mutex);
result = (GRecMutex *) mutex->mutex.mutex;
if (!result)
{
result = g_slice_new (GRecMutex);
g_rec_mutex_init (result);
g_atomic_pointer_set (&mutex->mutex.mutex, result);
}
g_mutex_unlock (&g_once_mutex);
}
return result;
}
/** /**
* g_static_rec_mutex_lock: * g_static_rec_mutex_lock:
* @mutex: a #GStaticRecMutex to lock. * @mutex: a #GStaticRecMutex to lock.
@ -653,23 +681,10 @@ g_static_rec_mutex_init (GStaticRecMutex *mutex)
void void
g_static_rec_mutex_lock (GStaticRecMutex* mutex) g_static_rec_mutex_lock (GStaticRecMutex* mutex)
{ {
GSystemThread self; GRecMutex *rm;
rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
g_return_if_fail (mutex); g_rec_mutex_lock (rm);
if (!g_thread_supported ())
return;
g_system_thread_self (&self);
if (g_system_thread_equal (&self, &mutex->owner))
{
mutex->depth++; mutex->depth++;
return;
}
g_static_mutex_lock (&mutex->mutex);
g_system_thread_assign (mutex->owner, self);
mutex->depth = 1;
} }
/** /**
@ -688,27 +703,16 @@ g_static_rec_mutex_lock (GStaticRecMutex* mutex)
gboolean gboolean
g_static_rec_mutex_trylock (GStaticRecMutex* mutex) g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
{ {
GSystemThread self; GRecMutex *rm;
rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
g_return_val_if_fail (mutex, FALSE); if (g_rec_mutex_trylock (rm))
if (!g_thread_supported ())
return TRUE;
g_system_thread_self (&self);
if (g_system_thread_equal (&self, &mutex->owner))
{ {
mutex->depth++; mutex->depth++;
return TRUE; return TRUE;
} }
else
if (!g_static_mutex_trylock (&mutex->mutex))
return FALSE; return FALSE;
g_system_thread_assign (mutex->owner, self);
mutex->depth = 1;
return TRUE;
} }
/** /**
@ -726,18 +730,10 @@ g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
void void
g_static_rec_mutex_unlock (GStaticRecMutex* mutex) g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
{ {
g_return_if_fail (mutex); GRecMutex *rm;
rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
if (!g_thread_supported ())
return;
if (mutex->depth > 1)
{
mutex->depth--; mutex->depth--;
return; g_rec_mutex_unlock (rm);
}
g_system_thread_assign (mutex->owner, zero_thread);
g_static_mutex_unlock (&mutex->mutex);
} }
/** /**
@ -754,25 +750,14 @@ void
g_static_rec_mutex_lock_full (GStaticRecMutex *mutex, g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
guint depth) guint depth)
{ {
GSystemThread self; GRecMutex *rm;
g_return_if_fail (mutex);
if (!g_thread_supported ()) rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
return; while (depth--)
if (depth == 0)
return;
g_system_thread_self (&self);
if (g_system_thread_equal (&self, &mutex->owner))
{ {
mutex->depth += depth; g_rec_mutex_lock (rm);
return; mutex->depth++;
} }
g_static_mutex_lock (&mutex->mutex);
g_system_thread_assign (mutex->owner, self);
mutex->depth = depth;
} }
/** /**
@ -794,18 +779,13 @@ g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
guint guint
g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex) g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
{ {
guint depth; GRecMutex *rm;
gint depth;
g_return_val_if_fail (mutex, 0);
if (!g_thread_supported ())
return 1;
rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
depth = mutex->depth; depth = mutex->depth;
while (mutex->depth--)
g_system_thread_assign (mutex->owner, zero_thread); g_rec_mutex_unlock (rm);
mutex->depth = 0;
g_static_mutex_unlock (&mutex->mutex);
return depth; return depth;
} }
@ -828,7 +808,13 @@ g_static_rec_mutex_free (GStaticRecMutex *mutex)
{ {
g_return_if_fail (mutex); g_return_if_fail (mutex);
g_static_mutex_free (&mutex->mutex); if (mutex->mutex.mutex)
{
GRecMutex *rm = (GRecMutex *) mutex->mutex.mutex;
g_rec_mutex_clear (rm);
g_slice_free (GRecMutex, rm);
}
} }
/* GStaticRWLock {{{1 ----------------------------------------------------- */ /* GStaticRWLock {{{1 ----------------------------------------------------- */