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