Add GRecMutexLocker

This is the same as GMutexLocker, just for recursive mutexes.
This commit is contained in:
Kalev Lember
2018-12-12 12:03:13 +01:00
parent 2a64176b83
commit 8c2e71bba0
4 changed files with 118 additions and 0 deletions

View File

@@ -344,6 +344,48 @@ test_g_mutex_locker (void)
}
}
/* Thread function to check that a recursive mutex given in @data is locked */
static gpointer
rec_mutex_locked_thread (gpointer data)
{
GRecMutex *rec_mutex = (GRecMutex *) data;
g_assert_false (g_rec_mutex_trylock (rec_mutex));
return NULL;
}
/* Thread function to check that a recursive mutex given in @data is unlocked */
static gpointer
rec_mutex_unlocked_thread (gpointer data)
{
GRecMutex *rec_mutex = (GRecMutex *) data;
g_assert_true (g_rec_mutex_trylock (rec_mutex));
return NULL;
}
static void
test_g_rec_mutex_locker (void)
{
GRecMutex rec_mutex;
GThread *thread;
g_rec_mutex_init (&rec_mutex);
if (TRUE)
{
g_autoptr(GRecMutexLocker) val = g_rec_mutex_locker_new (&rec_mutex);
g_assert_nonnull (val);
/* Verify that the mutex is actually locked */
thread = g_thread_new ("rec mutex locked", rec_mutex_locked_thread, &rec_mutex);
g_thread_join (thread);
}
/* Verify that the mutex is unlocked again */
thread = g_thread_new ("rec mutex unlocked", rec_mutex_unlocked_thread, &rec_mutex);
g_thread_join (thread);
}
static void
test_g_cond (void)
{
@@ -536,6 +578,7 @@ main (int argc, gchar *argv[])
g_test_add_func ("/autoptr/g_thread", test_g_thread);
g_test_add_func ("/autoptr/g_mutex", test_g_mutex);
g_test_add_func ("/autoptr/g_mutex_locker", test_g_mutex_locker);
g_test_add_func ("/autoptr/g_rec_mutex_locker", test_g_rec_mutex_locker);
g_test_add_func ("/autoptr/g_cond", test_g_cond);
g_test_add_func ("/autoptr/g_timer", test_g_timer);
g_test_add_func ("/autoptr/g_time_zone", test_g_time_zone);