From e7e40ba1cff2b2ac2f33f056baffb4e46437f440 Mon Sep 17 00:00:00 2001 From: Kalev Lember Date: Fri, 14 Dec 2018 17:30:58 +0100 Subject: [PATCH] tests: Update GMutexLocker tests Spawn a thread and assert that the mutex actually got locked and then unlocked again, same as we do in GRecMutexLocker tests. --- glib/tests/autoptr.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/glib/tests/autoptr.c b/glib/tests/autoptr.c index 076acced7..92d4bbaca 100644 --- a/glib/tests/autoptr.c +++ b/glib/tests/autoptr.c @@ -329,10 +329,30 @@ test_g_mutex (void) g_mutex_init (&val); } +/* Thread function to check that a mutex given in @data is locked */ +static gpointer +mutex_locked_thread (gpointer data) +{ + GMutex *mutex = (GMutex *) data; + g_assert_false (g_mutex_trylock (mutex)); + return NULL; +} + +/* Thread function to check that a mutex given in @data is unlocked */ +static gpointer +mutex_unlocked_thread (gpointer data) +{ + GMutex *mutex = (GMutex *) data; + g_assert_true (g_mutex_trylock (mutex)); + g_mutex_unlock (mutex); + return NULL; +} + static void test_g_mutex_locker (void) { GMutex mutex; + GThread *thread; g_mutex_init (&mutex); @@ -340,8 +360,16 @@ test_g_mutex_locker (void) { g_autoptr(GMutexLocker) val = g_mutex_locker_new (&mutex); - g_assert (val != NULL); + g_assert_nonnull (val); + + /* Verify that the mutex is actually locked */ + thread = g_thread_new ("mutex locked", mutex_locked_thread, &mutex); + g_thread_join (thread); } + + /* Verify that the mutex is unlocked again */ + thread = g_thread_new ("mutex unlocked", mutex_unlocked_thread, &mutex); + g_thread_join (thread); } /* Thread function to check that a recursive mutex given in @data is locked */