static void test_g_static_rec_mutex (void) { GThread *thread; g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex)); test_g_static_rec_mutex_thread_ready = FALSE; thread = g_thread_create (test_g_static_rec_mutex_thread, GINT_TO_POINTER (42), TRUE, NULL); /* This busy wait is only for testing purposes and not an example of * good code!*/ while (!test_g_static_rec_mutex_thread_ready) g_usleep (G_USEC_PER_SEC / 5); g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex)); test_g_static_rec_mutex_int = 41; g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex); test_g_static_rec_mutex_int = 42; g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex); /* This busy wait is only for testing purposes and not an example of * good code!*/ while (test_g_static_rec_mutex_thread_ready) g_usleep (G_USEC_PER_SEC / 5); g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex); test_g_static_rec_mutex_int = 0; g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex); g_assert (GPOINTER_TO_INT (g_thread_join (thread)) == 43); }
/** * Aquire the IO lock */ void rs_io_lock_real(const gchar *source_file, gint line, const gchar *caller) { RS_DEBUG(LOCKING, "[%s:%d %s()] \033[33mrequesting\033[0m IO lock (thread %p)", source_file, line, caller, (g_timer_start(io_lock_timer), g_thread_self())); /* Each loop tries approx every millisecond, so we wait 10 secs */ int tries_left = 10*1000; while (FALSE == g_static_rec_mutex_trylock(&io_lock)); { g_usleep(1000); if (--tries_left <= 0) { RS_DEBUG(LOCKING, "[%s:%d %s()] \033[31mIO Lock was not released after \033[36m%.2f\033[0mms\033[0m, ignoring IO lock (thread %p)", source_file, line, caller, g_timer_elapsed(io_lock_timer, NULL)*1000.0, (g_timer_start(io_lock_timer), g_thread_self())); return; } } RS_DEBUG(LOCKING, "[%s:%d %s()] \033[32mgot\033[0m IO lock after \033[36m%.2f\033[0mms (thread %p)", source_file, line, caller, g_timer_elapsed(io_lock_timer, NULL)*1000.0, (g_timer_start(io_lock_timer), g_thread_self())); }
static gpointer test_g_static_rec_mutex_thread (gpointer data) { g_assert (GPOINTER_TO_INT (data) == 42); g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex) == FALSE); test_g_static_rec_mutex_thread_ready = TRUE; g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex); g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex); g_assert (test_g_static_rec_mutex_int == 42); test_g_static_rec_mutex_thread_ready = FALSE; g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex); g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex); g_thread_exit (GINT_TO_POINTER (43)); g_assert_not_reached (); return NULL; }