|
1 /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. */ |
|
2 #include <unistd.h> |
|
3 #include <glib.h> |
|
4 #include <glib-object.h> |
|
5 |
|
6 |
|
7 #ifdef SYMBIAN |
|
8 #include <glib_global.h> |
|
9 #include "mrt2_glib2_test.h" |
|
10 #endif /*SYMBIAN*/ |
|
11 |
|
12 |
|
13 |
|
14 #define G_TYPE_TEST (g_test_get_type ()) |
|
15 #define G_TEST(test) (G_TYPE_CHECK_INSTANCE_CAST ((test), G_TYPE_TEST, GTest)) |
|
16 #define G_IS_TEST(test) (G_TYPE_CHECK_INSTANCE_TYPE ((test), G_TYPE_TEST)) |
|
17 #define G_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_CAST ((tclass), G_TYPE_TEST, GTestClass)) |
|
18 #define G_IS_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_TYPE ((tclass), G_TYPE_TEST)) |
|
19 #define G_TEST_GET_CLASS(test) (G_TYPE_INSTANCE_GET_CLASS ((test), G_TYPE_TEST, GTestClass)) |
|
20 |
|
21 typedef struct _GTest GTest; |
|
22 typedef struct _GTestClass GTestClass; |
|
23 |
|
24 struct _GTest |
|
25 { |
|
26 GObject object; |
|
27 }; |
|
28 |
|
29 struct _GTestClass |
|
30 { |
|
31 GObjectClass parent_class; |
|
32 }; |
|
33 |
|
34 static GType g_test_get_type (void); |
|
35 static volatile gboolean stopping; |
|
36 |
|
37 static void g_test_class_init (GTestClass * klass); |
|
38 static void g_test_init (GTest * test); |
|
39 static void g_test_dispose (GObject * object); |
|
40 |
|
41 static GObjectClass *parent_class = NULL; |
|
42 |
|
43 static GType |
|
44 g_test_get_type (void) |
|
45 { |
|
46 static GType test_type = 0; |
|
47 |
|
48 if (!test_type) { |
|
49 static const GTypeInfo test_info = { |
|
50 sizeof (GTestClass), |
|
51 NULL, |
|
52 NULL, |
|
53 (GClassInitFunc) g_test_class_init, |
|
54 NULL, |
|
55 NULL, |
|
56 sizeof (GTest), |
|
57 0, |
|
58 (GInstanceInitFunc) g_test_init, |
|
59 NULL |
|
60 }; |
|
61 |
|
62 test_type = g_type_register_static (G_TYPE_OBJECT, "GTest", |
|
63 &test_info, 0); |
|
64 } |
|
65 return test_type; |
|
66 } |
|
67 |
|
68 static void |
|
69 g_test_class_init (GTestClass * klass) |
|
70 { |
|
71 GObjectClass *gobject_class; |
|
72 |
|
73 gobject_class = (GObjectClass *) klass; |
|
74 |
|
75 parent_class = g_type_class_ref (G_TYPE_OBJECT); |
|
76 |
|
77 gobject_class->dispose = g_test_dispose; |
|
78 } |
|
79 |
|
80 static void |
|
81 g_test_init (GTest * test) |
|
82 { |
|
83 //g_print ("init %p\n", test); |
|
84 } |
|
85 |
|
86 static void |
|
87 g_test_dispose (GObject * object) |
|
88 { |
|
89 GTest *test; |
|
90 |
|
91 test = G_TEST (object); |
|
92 |
|
93 //g_print ("dispose %p!\n", object); |
|
94 |
|
95 G_OBJECT_CLASS (parent_class)->dispose (object); |
|
96 } |
|
97 |
|
98 static void |
|
99 g_test_do_refcount (GTest * test) |
|
100 { |
|
101 g_object_ref (test); |
|
102 g_object_unref (test); |
|
103 } |
|
104 |
|
105 static gpointer |
|
106 run_thread (GTest * test) |
|
107 { |
|
108 gint i = 1; |
|
109 |
|
110 while (!stopping) { |
|
111 g_test_do_refcount (test); |
|
112 if ((i++ % 10000) == 0) { |
|
113 //g_print ("."); |
|
114 g_thread_yield(); /* force context switch */ |
|
115 } |
|
116 } |
|
117 |
|
118 return NULL; |
|
119 } |
|
120 |
|
121 int |
|
122 main (int argc, char **argv) |
|
123 { |
|
124 gint i; |
|
125 GTest *test1, *test2; |
|
126 GArray *test_threads; |
|
127 const guint n_threads = 5; |
|
128 |
|
129 #ifdef SYMBIAN |
|
130 |
|
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); |
|
133 #endif /*SYMBIAN*/ |
|
134 |
|
135 |
|
136 g_thread_init (NULL); |
|
137 //g_print ("START: %s\n", argv[0]); |
|
138 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | g_log_set_always_fatal (G_LOG_FATAL_MASK)); |
|
139 g_type_init (); |
|
140 |
|
141 test1 = g_object_new (G_TYPE_TEST, NULL); |
|
142 g_assert(test1 != NULL); |
|
143 |
|
144 test2 = g_object_new (G_TYPE_TEST, NULL); |
|
145 g_assert(test2 != NULL); |
|
146 |
|
147 test_threads = g_array_new (FALSE, FALSE, sizeof (GThread *)); |
|
148 |
|
149 stopping = FALSE; |
|
150 |
|
151 for (i = 0; i < n_threads; i++) { |
|
152 GThread *thread; |
|
153 |
|
154 thread = g_thread_create ((GThreadFunc) run_thread, test1, TRUE, NULL); |
|
155 g_array_append_val (test_threads, thread); |
|
156 |
|
157 thread = g_thread_create ((GThreadFunc) run_thread, test2, TRUE, NULL); |
|
158 g_array_append_val (test_threads, thread); |
|
159 } |
|
160 g_usleep (5000000); |
|
161 |
|
162 stopping = TRUE; |
|
163 |
|
164 //g_print ("\nstopping\n"); |
|
165 |
|
166 /* join all threads */ |
|
167 for (i = 0; i < 2 * n_threads; i++) { |
|
168 GThread *thread; |
|
169 |
|
170 thread = g_array_index (test_threads, GThread *, i); |
|
171 g_thread_join (thread); |
|
172 } |
|
173 |
|
174 //g_print ("stopped\n"); |
|
175 #ifdef SYMBIAN |
|
176 testResultXml("objects"); |
|
177 #endif /* EMULATOR */ |
|
178 |
|
179 return 0; |
|
180 } |