sl@0: #undef G_DISABLE_ASSERT sl@0: #undef G_LOG_DOMAIN sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #ifdef SYMBIAN sl@0: #include sl@0: #include "mrt2_glib2_test.h" sl@0: #endif /*SYMBIAN*/ sl@0: sl@0: sl@0: static GAsyncQueue *queue; sl@0: G_LOCK_DEFINE_STATIC (queue_lock); sl@0: static guint queue_ready; sl@0: #define THREADS 10 sl@0: sl@0: static gpointer thread_push (gpointer data) sl@0: { sl@0: g_async_queue_push (queue, data); sl@0: return NULL; sl@0: } sl@0: sl@0: static gpointer thread_pop (gpointer data) sl@0: { sl@0: gint flag = *(gint*)&data; sl@0: gpointer res = g_async_queue_pop (queue); sl@0: gint g_async_queue_pop_op = *(gint*)&res; sl@0: sl@0: G_LOCK (queue_lock); sl@0: queue_ready++; sl@0: G_UNLOCK (queue_lock); sl@0: g_assert (g_async_queue_pop_op == (34 + flag)); sl@0: return NULL; sl@0: } sl@0: sl@0: void basic_test() sl@0: { sl@0: GThread *thread1[THREADS], *thread2[THREADS]; sl@0: int i; sl@0: sl@0: queue = g_async_queue_new (); sl@0: sl@0: for(i = 0; i < THREADS; i++) sl@0: { sl@0: thread1[i] = g_thread_create (thread_push, GINT_TO_POINTER(34+i), TRUE, NULL); sl@0: thread2[i] = g_thread_create (thread_pop, GINT_TO_POINTER(i), TRUE, NULL); sl@0: } sl@0: sl@0: /*while(queue_ready != 9) sl@0: g_usleep (G_USEC_PER_SEC / 5);*/ sl@0: sl@0: for(i = 0; i < THREADS; i++) sl@0: { sl@0: g_thread_join (thread1[i]); sl@0: g_thread_join (thread2[i]); sl@0: } sl@0: sl@0: } sl@0: sl@0: sl@0: static gpointer thread_ref_push (gpointer data) sl@0: { sl@0: g_async_queue_lock (queue); //Lock the queue sl@0: g_async_queue_ref (queue); //Increase ref by 1 sl@0: g_async_queue_unref_and_unlock (queue); //Decrease ref by 1 and unlock sl@0: g_async_queue_push (queue, data); sl@0: sleep(2); sl@0: g_async_queue_push (queue, data); sl@0: return NULL; sl@0: } sl@0: sl@0: static gpointer thread_ref_pop (gpointer data) sl@0: { sl@0: gpointer res; sl@0: gint g_async_queue_try_pop_unlocked_op; sl@0: GTimeVal time_val = sl@0: { sl@0: 0,100 sl@0: }; sl@0: sl@0: g_async_queue_ref_unlocked (queue); //Increase ref by 1 and unlock sl@0: sl@0: g_async_queue_lock (queue); //Lock the queue sl@0: res = g_async_queue_try_pop_unlocked (queue); sl@0: g_async_queue_try_pop_unlocked_op = *(gint*)&res; sl@0: g_async_queue_unlock (queue); //unlock the queue while the other thread sleeps sl@0: g_assert(g_async_queue_timed_pop (queue, &time_val) == NULL); sl@0: sl@0: G_LOCK (queue_lock); sl@0: queue_ready++; sl@0: G_UNLOCK (queue_lock); sl@0: g_assert (g_async_queue_try_pop_unlocked_op == 55); sl@0: return NULL; sl@0: } sl@0: sl@0: void ref_test() sl@0: { sl@0: GThread *thread1, *thread2; sl@0: sl@0: g_assert (g_async_queue_try_pop (queue) == NULL); sl@0: sl@0: thread1 = g_thread_create (thread_ref_push, GINT_TO_POINTER(55), TRUE, NULL); sl@0: thread2 = g_thread_create (thread_ref_pop, NULL, TRUE, NULL); sl@0: sl@0: /* while(!queue_ready) sl@0: g_usleep (G_USEC_PER_SEC / 5);*/ sl@0: sl@0: g_thread_join (thread1); sl@0: g_thread_join (thread2); sl@0: sl@0: } sl@0: sl@0: sl@0: /* run all the tests */ sl@0: void sl@0: run_all_tests() sl@0: { sl@0: queue_ready = 0; sl@0: basic_test(); sl@0: queue_ready = 0; sl@0: ref_test() ; sl@0: } sl@0: sl@0: int sl@0: main (int argc, sl@0: char *argv[]) sl@0: { sl@0: #ifdef SYMBIAN sl@0: g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL); sl@0: g_set_print_handler(mrtPrintHandler); sl@0: #endif /*SYMBIAN*/ sl@0: sl@0: sl@0: /* Only run the test, if threads are enabled and a default thread sl@0: implementation is available */ sl@0: #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE) sl@0: g_thread_init (NULL); sl@0: run_all_tests (); sl@0: #endif sl@0: #ifdef SYMBIAN sl@0: testResultXml("tasyncqueue"); sl@0: #endif /* EMULATOR */ sl@0: return 0; sl@0: }