Update contrib.
1 #undef G_DISABLE_ASSERT
9 #include <glib_global.h>
10 #include "mrt2_glib2_test.h"
14 static GAsyncQueue *queue;
15 G_LOCK_DEFINE_STATIC (queue_lock);
16 static guint queue_ready;
19 static gpointer thread_push (gpointer data)
21 g_async_queue_push (queue, data);
25 static gpointer thread_pop (gpointer data)
27 gint flag = *(gint*)&data;
28 gpointer res = g_async_queue_pop (queue);
29 gint g_async_queue_pop_op = *(gint*)&res;
33 G_UNLOCK (queue_lock);
34 g_assert (g_async_queue_pop_op == (34 + flag));
40 GThread *thread1[THREADS], *thread2[THREADS];
43 queue = g_async_queue_new ();
45 for(i = 0; i < THREADS; i++)
47 thread1[i] = g_thread_create (thread_push, GINT_TO_POINTER(34+i), TRUE, NULL);
48 thread2[i] = g_thread_create (thread_pop, GINT_TO_POINTER(i), TRUE, NULL);
51 /*while(queue_ready != 9)
52 g_usleep (G_USEC_PER_SEC / 5);*/
54 for(i = 0; i < THREADS; i++)
56 g_thread_join (thread1[i]);
57 g_thread_join (thread2[i]);
63 static gpointer thread_ref_push (gpointer data)
65 g_async_queue_lock (queue); //Lock the queue
66 g_async_queue_ref (queue); //Increase ref by 1
67 g_async_queue_unref_and_unlock (queue); //Decrease ref by 1 and unlock
68 g_async_queue_push (queue, data);
70 g_async_queue_push (queue, data);
74 static gpointer thread_ref_pop (gpointer data)
77 gint g_async_queue_try_pop_unlocked_op;
83 g_async_queue_ref_unlocked (queue); //Increase ref by 1 and unlock
85 g_async_queue_lock (queue); //Lock the queue
86 res = g_async_queue_try_pop_unlocked (queue);
87 g_async_queue_try_pop_unlocked_op = *(gint*)&res;
88 g_async_queue_unlock (queue); //unlock the queue while the other thread sleeps
89 g_assert(g_async_queue_timed_pop (queue, &time_val) == NULL);
93 G_UNLOCK (queue_lock);
94 g_assert (g_async_queue_try_pop_unlocked_op == 55);
100 GThread *thread1, *thread2;
102 g_assert (g_async_queue_try_pop (queue) == NULL);
104 thread1 = g_thread_create (thread_ref_push, GINT_TO_POINTER(55), TRUE, NULL);
105 thread2 = g_thread_create (thread_ref_pop, NULL, TRUE, NULL);
107 /* while(!queue_ready)
108 g_usleep (G_USEC_PER_SEC / 5);*/
110 g_thread_join (thread1);
111 g_thread_join (thread2);
116 /* run all the tests */
131 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);
132 g_set_print_handler(mrtPrintHandler);
136 /* Only run the test, if threads are enabled and a default thread
137 implementation is available */
138 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
139 g_thread_init (NULL);
143 testResultXml("tasyncqueue");
144 #endif /* EMULATOR */