|
1 #undef G_DISABLE_ASSERT |
|
2 #undef G_LOG_DOMAIN |
|
3 |
|
4 #include <glib.h> |
|
5 #include <stdio.h> |
|
6 |
|
7 #ifdef SYMBIAN |
|
8 #include <glib_global.h> |
|
9 #include "mrt2_glib2_test.h" |
|
10 #endif /*SYMBIAN*/ |
|
11 |
|
12 |
|
13 static GAsyncQueue *queue; |
|
14 G_LOCK_DEFINE_STATIC (queue_lock); |
|
15 static guint queue_ready; |
|
16 #define THREADS 10 |
|
17 |
|
18 static gpointer thread_push (gpointer data) |
|
19 { |
|
20 g_async_queue_push (queue, data); |
|
21 return NULL; |
|
22 } |
|
23 |
|
24 static gpointer thread_pop (gpointer data) |
|
25 { |
|
26 gint flag = *(gint*)&data; |
|
27 gpointer res = g_async_queue_pop (queue); |
|
28 gint g_async_queue_pop_op = *(gint*)&res; |
|
29 |
|
30 G_LOCK (queue_lock); |
|
31 queue_ready++; |
|
32 G_UNLOCK (queue_lock); |
|
33 g_assert (g_async_queue_pop_op == (34 + flag)); |
|
34 return NULL; |
|
35 } |
|
36 |
|
37 void basic_test() |
|
38 { |
|
39 GThread *thread1[THREADS], *thread2[THREADS]; |
|
40 int i; |
|
41 |
|
42 queue = g_async_queue_new (); |
|
43 |
|
44 for(i = 0; i < THREADS; i++) |
|
45 { |
|
46 thread1[i] = g_thread_create (thread_push, GINT_TO_POINTER(34+i), TRUE, NULL); |
|
47 thread2[i] = g_thread_create (thread_pop, GINT_TO_POINTER(i), TRUE, NULL); |
|
48 } |
|
49 |
|
50 /*while(queue_ready != 9) |
|
51 g_usleep (G_USEC_PER_SEC / 5);*/ |
|
52 |
|
53 for(i = 0; i < THREADS; i++) |
|
54 { |
|
55 g_thread_join (thread1[i]); |
|
56 g_thread_join (thread2[i]); |
|
57 } |
|
58 |
|
59 } |
|
60 |
|
61 |
|
62 static gpointer thread_ref_push (gpointer data) |
|
63 { |
|
64 g_async_queue_lock (queue); //Lock the queue |
|
65 g_async_queue_ref (queue); //Increase ref by 1 |
|
66 g_async_queue_unref_and_unlock (queue); //Decrease ref by 1 and unlock |
|
67 g_async_queue_push (queue, data); |
|
68 sleep(2); |
|
69 g_async_queue_push (queue, data); |
|
70 return NULL; |
|
71 } |
|
72 |
|
73 static gpointer thread_ref_pop (gpointer data) |
|
74 { |
|
75 gpointer res; |
|
76 gint g_async_queue_try_pop_unlocked_op; |
|
77 GTimeVal time_val = |
|
78 { |
|
79 0,100 |
|
80 }; |
|
81 |
|
82 g_async_queue_ref_unlocked (queue); //Increase ref by 1 and unlock |
|
83 |
|
84 g_async_queue_lock (queue); //Lock the queue |
|
85 res = g_async_queue_try_pop_unlocked (queue); |
|
86 g_async_queue_try_pop_unlocked_op = *(gint*)&res; |
|
87 g_async_queue_unlock (queue); //unlock the queue while the other thread sleeps |
|
88 g_assert(g_async_queue_timed_pop (queue, &time_val) == NULL); |
|
89 |
|
90 G_LOCK (queue_lock); |
|
91 queue_ready++; |
|
92 G_UNLOCK (queue_lock); |
|
93 g_assert (g_async_queue_try_pop_unlocked_op == 55); |
|
94 return NULL; |
|
95 } |
|
96 |
|
97 void ref_test() |
|
98 { |
|
99 GThread *thread1, *thread2; |
|
100 |
|
101 g_assert (g_async_queue_try_pop (queue) == NULL); |
|
102 |
|
103 thread1 = g_thread_create (thread_ref_push, GINT_TO_POINTER(55), TRUE, NULL); |
|
104 thread2 = g_thread_create (thread_ref_pop, NULL, TRUE, NULL); |
|
105 |
|
106 /* while(!queue_ready) |
|
107 g_usleep (G_USEC_PER_SEC / 5);*/ |
|
108 |
|
109 g_thread_join (thread1); |
|
110 g_thread_join (thread2); |
|
111 |
|
112 } |
|
113 |
|
114 |
|
115 /* run all the tests */ |
|
116 void |
|
117 run_all_tests() |
|
118 { |
|
119 queue_ready = 0; |
|
120 basic_test(); |
|
121 queue_ready = 0; |
|
122 ref_test() ; |
|
123 } |
|
124 |
|
125 int |
|
126 main (int argc, |
|
127 char *argv[]) |
|
128 { |
|
129 #ifdef SYMBIAN |
|
130 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); |
|
131 g_set_print_handler(mrtPrintHandler); |
|
132 #endif /*SYMBIAN*/ |
|
133 |
|
134 |
|
135 /* Only run the test, if threads are enabled and a default thread |
|
136 implementation is available */ |
|
137 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE) |
|
138 g_thread_init (NULL); |
|
139 run_all_tests (); |
|
140 #endif |
|
141 #ifdef SYMBIAN |
|
142 testResultXml("tasyncqueue"); |
|
143 #endif /* EMULATOR */ |
|
144 return 0; |
|
145 } |