|
1 /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/ |
|
2 #undef G_DISABLE_ASSERT |
|
3 #undef G_LOG_DOMAIN |
|
4 |
|
5 |
|
6 #include <errno.h> |
|
7 #include <glib.h> |
|
8 |
|
9 #ifdef G_OS_UNIX |
|
10 #include <unistd.h> |
|
11 #endif |
|
12 #include <stdio.h> |
|
13 #include <stdlib.h> |
|
14 |
|
15 #ifdef G_OS_WIN32 |
|
16 #include <fcntl.h> /* For _O_BINARY used by pipe() macro */ |
|
17 #include <io.h> /* for _pipe() */ |
|
18 #endif |
|
19 |
|
20 |
|
21 |
|
22 #ifdef SYMBIAN |
|
23 #include <glib_global.h> |
|
24 #include "mrt2_glib2_test.h" |
|
25 #endif /*SYMBIAN*/ |
|
26 |
|
27 |
|
28 #define ITERS 10 |
|
29 #define INCREMENT 10 |
|
30 #define NTHREADS 4 |
|
31 #define NCRAWLERS 4 |
|
32 #define CRAWLER_TIMEOUT_RANGE 40 |
|
33 #define RECURSER_TIMEOUT 50 |
|
34 |
|
35 /* The partial ordering between the context array mutex and |
|
36 * crawler array mutex is that the crawler array mutex cannot |
|
37 * be locked while the context array mutex is locked |
|
38 */ |
|
39 GPtrArray *context_array; |
|
40 GMutex *context_array_mutex; |
|
41 GCond *context_array_cond; |
|
42 |
|
43 GMainLoop *main_loop; |
|
44 |
|
45 G_LOCK_DEFINE_STATIC (crawler_array_lock); |
|
46 GPtrArray *crawler_array; |
|
47 |
|
48 typedef struct _AddrData AddrData; |
|
49 typedef struct _TestData TestData; |
|
50 |
|
51 struct _AddrData |
|
52 { |
|
53 GMainLoop *loop; |
|
54 GIOChannel *dest; |
|
55 gint count; |
|
56 }; |
|
57 |
|
58 struct _TestData |
|
59 { |
|
60 gint current_val; |
|
61 gint iters; |
|
62 GIOChannel *in; |
|
63 }; |
|
64 |
|
65 static void cleanup_crawlers (GMainContext *context); |
|
66 |
|
67 gboolean |
|
68 read_all (GIOChannel *channel, char *buf, gsize len) |
|
69 { |
|
70 gsize bytes_read = 0; |
|
71 gsize count; |
|
72 GIOError err; |
|
73 |
|
74 while (bytes_read < len) |
|
75 { |
|
76 err = g_io_channel_read (channel, buf + bytes_read, len - bytes_read, &count); |
|
77 if (err) |
|
78 { |
|
79 if (err != G_IO_ERROR_AGAIN) |
|
80 g_assert(FALSE && "mainloop-test failed"); |
|
81 return FALSE; |
|
82 } |
|
83 else if (count == 0) |
|
84 return FALSE; |
|
85 |
|
86 bytes_read += count; |
|
87 } |
|
88 |
|
89 return TRUE; |
|
90 } |
|
91 |
|
92 gboolean |
|
93 write_all (GIOChannel *channel, char *buf, gsize len) |
|
94 { |
|
95 gsize bytes_written = 0; |
|
96 gsize count; |
|
97 GIOError err; |
|
98 |
|
99 while (bytes_written < len) |
|
100 { |
|
101 err = g_io_channel_write (channel, buf + bytes_written, len - bytes_written, &count); |
|
102 if (err && err != G_IO_ERROR_AGAIN) |
|
103 return FALSE; |
|
104 |
|
105 bytes_written += count; |
|
106 } |
|
107 |
|
108 return TRUE; |
|
109 } |
|
110 |
|
111 gboolean |
|
112 adder_callback (GIOChannel *source, |
|
113 GIOCondition condition, |
|
114 gpointer data) |
|
115 { |
|
116 char buf1[32]; |
|
117 char buf2[32]; |
|
118 |
|
119 char result[32]; |
|
120 |
|
121 AddrData *addr_data = data; |
|
122 |
|
123 if (!read_all (source, buf1, 32) || |
|
124 !read_all (source, buf2, 32)) |
|
125 { |
|
126 g_main_loop_quit (addr_data->loop); |
|
127 return FALSE; |
|
128 } |
|
129 |
|
130 sprintf (result, "%d", atoi(buf1) + atoi(buf2)); |
|
131 write_all (addr_data->dest, result, 32); |
|
132 |
|
133 return TRUE; |
|
134 } |
|
135 |
|
136 gboolean |
|
137 timeout_callback (gpointer data) |
|
138 { |
|
139 AddrData *addr_data = data; |
|
140 |
|
141 addr_data->count++; |
|
142 |
|
143 return TRUE; |
|
144 } |
|
145 |
|
146 gpointer |
|
147 adder_thread (gpointer data) |
|
148 { |
|
149 GMainContext *context; |
|
150 GSource *adder_source; |
|
151 GSource *timeout_source; |
|
152 |
|
153 GIOChannel **channels = data; |
|
154 AddrData addr_data; |
|
155 |
|
156 context = g_main_context_new (); |
|
157 |
|
158 g_assert(context != NULL); |
|
159 |
|
160 g_mutex_lock (context_array_mutex); |
|
161 |
|
162 g_ptr_array_add (context_array, context); |
|
163 |
|
164 if (context_array->len == NTHREADS) |
|
165 g_cond_broadcast (context_array_cond); |
|
166 |
|
167 g_mutex_unlock (context_array_mutex); |
|
168 |
|
169 addr_data.dest = channels[1]; |
|
170 addr_data.loop = g_main_loop_new (context, FALSE); |
|
171 addr_data.count = 0; |
|
172 |
|
173 adder_source = g_io_create_watch (channels[0], G_IO_IN | G_IO_HUP); |
|
174 |
|
175 g_assert(adder_source != NULL); |
|
176 |
|
177 g_source_set_callback (adder_source, (GSourceFunc)adder_callback, &addr_data, NULL); |
|
178 g_source_attach (adder_source, context); |
|
179 g_source_unref (adder_source); |
|
180 |
|
181 timeout_source = g_timeout_source_new (10); |
|
182 |
|
183 g_assert(timeout_source != NULL); |
|
184 |
|
185 g_source_set_callback (timeout_source, (GSourceFunc)timeout_callback, &addr_data, NULL); |
|
186 g_source_set_priority (timeout_source, G_PRIORITY_HIGH); |
|
187 g_source_attach (timeout_source, context); |
|
188 g_source_unref (timeout_source); |
|
189 |
|
190 g_main_loop_run (addr_data.loop); |
|
191 |
|
192 g_io_channel_unref (channels[0]); |
|
193 g_io_channel_unref (channels[1]); |
|
194 |
|
195 g_free (channels); |
|
196 |
|
197 g_main_loop_unref (addr_data.loop); |
|
198 |
|
199 #ifdef VERBOSE |
|
200 g_print ("Timeout run %d times\n", addr_data.count); |
|
201 #endif |
|
202 |
|
203 g_mutex_lock (context_array_mutex); |
|
204 g_ptr_array_remove (context_array, context); |
|
205 if (context_array->len == 0) |
|
206 g_main_loop_quit (main_loop); |
|
207 g_mutex_unlock (context_array_mutex); |
|
208 |
|
209 cleanup_crawlers (context); |
|
210 |
|
211 return NULL; |
|
212 } |
|
213 |
|
214 |
|
215 void |
|
216 io_pipe (GIOChannel **channels) |
|
217 { |
|
218 gint fds[2]; |
|
219 |
|
220 if (pipe(fds) < 0) |
|
221 { |
|
222 g_warning ("Cannot create pipe %s\n", g_strerror (errno)); |
|
223 |
|
224 g_assert(FALSE && "mainloop-test failed"); |
|
225 |
|
226 exit (1); |
|
227 } |
|
228 |
|
229 channels[0] = g_io_channel_unix_new (fds[0]); |
|
230 channels[1] = g_io_channel_unix_new (fds[1]); |
|
231 |
|
232 g_io_channel_set_close_on_unref (channels[0], TRUE); |
|
233 g_io_channel_set_close_on_unref (channels[1], TRUE); |
|
234 } |
|
235 |
|
236 void |
|
237 do_add (GIOChannel *in, gint a, gint b) |
|
238 { |
|
239 char buf1[32]; |
|
240 char buf2[32]; |
|
241 |
|
242 sprintf (buf1, "%d", a); |
|
243 sprintf (buf2, "%d", b); |
|
244 |
|
245 write_all (in, buf1, 32); |
|
246 write_all (in, buf2, 32); |
|
247 } |
|
248 |
|
249 gboolean |
|
250 adder_response (GIOChannel *source, |
|
251 GIOCondition condition, |
|
252 gpointer data) |
|
253 { |
|
254 char result[32]; |
|
255 TestData *test_data = data; |
|
256 |
|
257 if (!read_all (source, result, 32)) |
|
258 return FALSE; |
|
259 |
|
260 test_data->current_val = atoi (result); |
|
261 test_data->iters--; |
|
262 |
|
263 if (test_data->iters == 0) |
|
264 { |
|
265 if (test_data->current_val != ITERS * INCREMENT) |
|
266 { |
|
267 g_print ("Addition failed: %d != %d\n", |
|
268 test_data->current_val, ITERS * INCREMENT); |
|
269 |
|
270 g_assert(FALSE && "mainloop-test failed"); |
|
271 |
|
272 exit (1); |
|
273 } |
|
274 |
|
275 g_io_channel_unref (source); |
|
276 g_io_channel_unref (test_data->in); |
|
277 |
|
278 g_free (test_data); |
|
279 |
|
280 return FALSE; |
|
281 } |
|
282 |
|
283 do_add (test_data->in, test_data->current_val, INCREMENT); |
|
284 |
|
285 return TRUE; |
|
286 } |
|
287 |
|
288 void |
|
289 create_adder_thread (void) |
|
290 { |
|
291 GError *err = NULL; |
|
292 TestData *test_data; |
|
293 GThread *thread; |
|
294 |
|
295 GIOChannel *in_channels[2]; |
|
296 GIOChannel *out_channels[2]; |
|
297 |
|
298 GIOChannel **sub_channels; |
|
299 |
|
300 sub_channels = g_new (GIOChannel *, 2); |
|
301 |
|
302 io_pipe (in_channels); |
|
303 io_pipe (out_channels); |
|
304 |
|
305 sub_channels[0] = in_channels[0]; |
|
306 sub_channels[1] = out_channels[1]; |
|
307 |
|
308 g_thread_create (adder_thread, sub_channels, FALSE, &err); |
|
309 |
|
310 if (err) |
|
311 { |
|
312 g_warning ("Cannot create thread: %s", err->message); |
|
313 |
|
314 g_assert(FALSE && "mainloop-test failed"); |
|
315 |
|
316 exit (1); |
|
317 } |
|
318 |
|
319 test_data = g_new (TestData, 1); |
|
320 test_data->in = in_channels[1]; |
|
321 test_data->current_val = 0; |
|
322 test_data->iters = ITERS; |
|
323 |
|
324 g_io_add_watch (out_channels[0], G_IO_IN | G_IO_HUP, |
|
325 adder_response, test_data); |
|
326 |
|
327 do_add (test_data->in, test_data->current_val, INCREMENT); |
|
328 } |
|
329 |
|
330 static void create_crawler (void); |
|
331 |
|
332 static void |
|
333 remove_crawler (void) |
|
334 { |
|
335 GSource *other_source; |
|
336 |
|
337 if (crawler_array->len > 0) |
|
338 { |
|
339 other_source = crawler_array->pdata[g_random_int_range (0, crawler_array->len)]; |
|
340 g_source_destroy (other_source); |
|
341 g_assert (g_ptr_array_remove_fast (crawler_array, other_source)); |
|
342 } |
|
343 } |
|
344 |
|
345 static gint |
|
346 crawler_callback (gpointer data) |
|
347 { |
|
348 GSource *source = data; |
|
349 |
|
350 G_LOCK (crawler_array_lock); |
|
351 |
|
352 if (!g_ptr_array_remove_fast (crawler_array, source)) |
|
353 remove_crawler(); |
|
354 |
|
355 remove_crawler(); |
|
356 G_UNLOCK (crawler_array_lock); |
|
357 |
|
358 create_crawler(); |
|
359 //create_crawler(); |
|
360 |
|
361 return FALSE; |
|
362 } |
|
363 |
|
364 static void |
|
365 create_crawler (void) |
|
366 { |
|
367 GSource *source = g_timeout_source_new (g_random_int_range (0, CRAWLER_TIMEOUT_RANGE)); |
|
368 |
|
369 g_assert(source != NULL); |
|
370 |
|
371 g_source_set_callback (source, (GSourceFunc)crawler_callback, source, NULL); |
|
372 |
|
373 G_LOCK (crawler_array_lock); |
|
374 g_ptr_array_add (crawler_array, source); |
|
375 |
|
376 g_mutex_lock (context_array_mutex); |
|
377 if(context_array->len == 0) |
|
378 g_source_attach (source, context_array->pdata[0]); |
|
379 else |
|
380 g_source_attach (source, context_array->pdata[g_random_int_range (0, context_array->len)]); |
|
381 g_source_unref (source); |
|
382 g_mutex_unlock (context_array_mutex); |
|
383 |
|
384 G_UNLOCK (crawler_array_lock); |
|
385 } |
|
386 |
|
387 static void |
|
388 cleanup_crawlers (GMainContext *context) |
|
389 { |
|
390 gint i; |
|
391 |
|
392 G_LOCK (crawler_array_lock); |
|
393 for (i=0; i < crawler_array->len; i++) |
|
394 { |
|
395 if (g_source_get_context (crawler_array->pdata[i]) == context) |
|
396 { |
|
397 g_source_destroy (g_ptr_array_remove_index (crawler_array, i)); |
|
398 i--; |
|
399 } |
|
400 } |
|
401 G_UNLOCK (crawler_array_lock); |
|
402 } |
|
403 |
|
404 static gboolean |
|
405 recurser_idle (gpointer data) |
|
406 { |
|
407 GMainContext *context = data; |
|
408 gint i; |
|
409 |
|
410 for (i = 0; i < 10; i++) |
|
411 g_main_context_iteration (context, FALSE); |
|
412 |
|
413 return FALSE; |
|
414 } |
|
415 |
|
416 static gboolean |
|
417 recurser_start (gpointer data) |
|
418 { |
|
419 GMainContext *context; |
|
420 GSource *source; |
|
421 |
|
422 g_mutex_lock (context_array_mutex); |
|
423 |
|
424 if(context_array->len == 0) |
|
425 context = context_array->pdata[0]; |
|
426 else |
|
427 context = context_array->pdata[g_random_int_range (0, context_array->len)]; |
|
428 source = g_idle_source_new (); |
|
429 g_source_set_callback (source, recurser_idle, context, NULL); |
|
430 g_source_attach (source, context); |
|
431 g_source_unref (source); |
|
432 g_mutex_unlock (context_array_mutex); |
|
433 |
|
434 return TRUE; |
|
435 } |
|
436 |
|
437 int |
|
438 main (int argc, |
|
439 char *argv[]) |
|
440 { |
|
441 /* Only run the test, if threads are enabled and a default thread |
|
442 implementation is available */ |
|
443 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE) |
|
444 gint i; |
|
445 |
|
446 #ifdef SYMBIAN |
|
447 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); |
|
448 g_set_print_handler(mrtPrintHandler); |
|
449 #endif /*SYMBIAN*/ |
|
450 #if 0 |
|
451 g_thread_init (NULL); |
|
452 |
|
453 context_array = g_ptr_array_new (); |
|
454 |
|
455 g_assert(context_array != NULL); |
|
456 |
|
457 context_array_mutex = g_mutex_new (); |
|
458 |
|
459 g_assert(context_array_mutex != NULL); |
|
460 |
|
461 context_array_cond = g_cond_new (); |
|
462 |
|
463 g_assert(context_array_cond != NULL); |
|
464 |
|
465 crawler_array = g_ptr_array_new (); |
|
466 |
|
467 g_assert(crawler_array != NULL); |
|
468 |
|
469 main_loop = g_main_loop_new (NULL, FALSE); |
|
470 |
|
471 g_assert(main_loop != NULL); |
|
472 |
|
473 for (i = 0; i < NTHREADS; i++) |
|
474 create_adder_thread (); |
|
475 |
|
476 /* Wait for all threads to start |
|
477 */ |
|
478 g_mutex_lock (context_array_mutex); |
|
479 |
|
480 if (context_array->len < NTHREADS) |
|
481 g_cond_wait (context_array_cond, context_array_mutex); |
|
482 |
|
483 g_mutex_unlock (context_array_mutex); |
|
484 |
|
485 for (i = 0; i < NCRAWLERS; i++) |
|
486 create_crawler (); |
|
487 |
|
488 g_timeout_add (RECURSER_TIMEOUT, recurser_start, NULL); |
|
489 |
|
490 g_main_loop_run (main_loop); |
|
491 g_main_loop_unref (main_loop); |
|
492 #endif |
|
493 #endif |
|
494 |
|
495 #ifdef SYMBIAN |
|
496 testResultXml("mainloop-test"); |
|
497 #endif /* EMULATOR */ |
|
498 |
|
499 return 0; |
|
500 } |