Remove developer script not needed in git repository
[glib.git] / gio / tests / contexts.c
blobc6dc2611106a851fc83e470241f8d62513fec130
1 #include "gcontextspecificgroup.c"
2 #include <gio/gio.h>
3 #include <stdlib.h>
4 #include <string.h>
6 #define N_THREADS 10
8 static gchar *test_file;
10 char *test_file_buffer;
11 gsize test_file_size;
12 static char async_read_buffer[8192];
14 static void
15 read_data (GObject *source, GAsyncResult *result, gpointer loop)
17 GInputStream *in = G_INPUT_STREAM (source);
18 GError *error = NULL;
19 gssize nread;
21 nread = g_input_stream_read_finish (in, result, &error);
22 g_assert_no_error (error);
24 g_assert_cmpint (nread, >, 0);
25 g_assert_cmpint (nread, <=, MIN(sizeof (async_read_buffer), test_file_size));
26 g_assert (memcmp (async_read_buffer, test_file_buffer, nread) == 0);
28 g_main_loop_quit (loop);
31 static void
32 opened_for_read (GObject *source, GAsyncResult *result, gpointer loop)
34 GFile *file = G_FILE (source);
35 GFileInputStream *in;
36 GError *error = NULL;
38 in = g_file_read_finish (file, result, &error);
39 g_assert_no_error (error);
41 memset (async_read_buffer, 0, sizeof (async_read_buffer));
42 g_input_stream_read_async (G_INPUT_STREAM (in),
43 async_read_buffer, sizeof (async_read_buffer),
44 G_PRIORITY_DEFAULT, NULL,
45 read_data, loop);
47 g_object_unref (in);
50 /* Test 1: Async I/O started in a thread with a thread-default context
51 * will stick to that thread, and will complete even if the default
52 * main loop is blocked. (NB: the last part would not be true if we
53 * were testing GFileMonitor!)
56 static gboolean idle_start_test1_thread (gpointer loop);
57 static gpointer test1_thread (gpointer user_data);
59 static gboolean test1_done;
60 static GCond test1_cond;
61 static GMutex test1_mutex;
63 static void
64 test_thread_independence (void)
66 GMainLoop *loop;
68 loop = g_main_loop_new (NULL, FALSE);
69 g_idle_add (idle_start_test1_thread, loop);
70 g_main_loop_run (loop);
71 g_main_loop_unref (loop);
74 static gboolean
75 idle_start_test1_thread (gpointer loop)
77 gint64 time;
78 GThread *thread;
79 gboolean io_completed;
81 g_mutex_lock (&test1_mutex);
82 thread = g_thread_new ("test1", test1_thread, NULL);
84 time = g_get_monotonic_time () + 2 * G_TIME_SPAN_SECOND;
85 while (!test1_done)
87 io_completed = g_cond_wait_until (&test1_cond, &test1_mutex, time);
88 g_assert (io_completed);
90 g_thread_join (thread);
92 g_mutex_unlock (&test1_mutex);
93 g_main_loop_quit (loop);
94 return G_SOURCE_REMOVE;
97 static gpointer
98 test1_thread (gpointer user_data)
100 GMainContext *context;
101 GMainLoop *loop;
102 GFile *file;
104 /* Wait for main thread to be waiting on test1_cond */
105 g_mutex_lock (&test1_mutex);
107 context = g_main_context_new ();
108 g_assert (g_main_context_get_thread_default () == NULL);
109 g_main_context_push_thread_default (context);
110 g_assert (g_main_context_get_thread_default () == context);
112 file = g_file_new_for_path (test_file);
113 g_assert (g_file_supports_thread_contexts (file));
115 loop = g_main_loop_new (context, FALSE);
116 g_file_read_async (file, G_PRIORITY_DEFAULT, NULL,
117 opened_for_read, loop);
118 g_object_unref (file);
119 g_main_loop_run (loop);
120 g_main_loop_unref (loop);
122 test1_done = TRUE;
123 g_cond_signal (&test1_cond);
124 g_mutex_unlock (&test1_mutex);
126 g_main_context_pop_thread_default (context);
127 g_main_context_unref (context);
129 return NULL;
132 /* Test 2: If we push a thread-default context in the main thread, we
133 * can run async ops in that context without running the default
134 * context.
137 static gboolean test2_fail (gpointer user_data);
139 static void
140 test_context_independence (void)
142 GMainContext *context;
143 GMainLoop *loop;
144 GFile *file;
145 guint default_timeout;
146 GSource *thread_default_timeout;
148 context = g_main_context_new ();
149 g_assert (g_main_context_get_thread_default () == NULL);
150 g_main_context_push_thread_default (context);
151 g_assert (g_main_context_get_thread_default () == context);
153 file = g_file_new_for_path (test_file);
154 g_assert (g_file_supports_thread_contexts (file));
156 /* Add a timeout to the main loop, to fail immediately if it gets run */
157 default_timeout = g_timeout_add_full (G_PRIORITY_HIGH, 0,
158 test2_fail, NULL, NULL);
159 /* Add a timeout to the alternate loop, to fail if the I/O *doesn't* run */
160 thread_default_timeout = g_timeout_source_new_seconds (2);
161 g_source_set_callback (thread_default_timeout, test2_fail, NULL, NULL);
162 g_source_attach (thread_default_timeout, context);
164 loop = g_main_loop_new (context, FALSE);
165 g_file_read_async (file, G_PRIORITY_DEFAULT, NULL,
166 opened_for_read, loop);
167 g_object_unref (file);
168 g_main_loop_run (loop);
169 g_main_loop_unref (loop);
171 g_source_remove (default_timeout);
172 g_source_destroy (thread_default_timeout);
173 g_source_unref (thread_default_timeout);
175 g_main_context_pop_thread_default (context);
176 g_main_context_unref (context);
179 static gboolean
180 test2_fail (gpointer user_data)
182 g_assert_not_reached ();
183 return FALSE;
187 typedef struct
189 GObject parent_instance;
191 GMainContext *context;
192 } PerThreadThing;
194 typedef GObjectClass PerThreadThingClass;
196 static GType per_thread_thing_get_type (void);
198 G_DEFINE_TYPE (PerThreadThing, per_thread_thing, G_TYPE_OBJECT)
200 static GContextSpecificGroup group;
201 static gpointer instances[N_THREADS];
202 static gint is_running;
203 static gint current_value;
204 static gint observed_values[N_THREADS];
206 static void
207 start_func (void)
209 g_assert (!is_running);
210 g_atomic_int_set (&is_running, TRUE);
213 static void
214 stop_func (void)
216 g_assert (is_running);
217 g_atomic_int_set (&is_running, FALSE);
220 static void
221 per_thread_thing_finalize (GObject *object)
223 PerThreadThing *thing = (PerThreadThing *) object;
225 g_context_specific_group_remove (&group, thing->context, thing, stop_func);
227 G_OBJECT_CLASS (per_thread_thing_parent_class)->finalize (object);
230 static void
231 per_thread_thing_init (PerThreadThing *thing)
235 static void
236 per_thread_thing_class_init (PerThreadThingClass *class)
238 class->finalize = per_thread_thing_finalize;
240 g_signal_new ("changed", per_thread_thing_get_type (), G_SIGNAL_RUN_FIRST, 0,
241 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
244 static gpointer
245 per_thread_thing_get (void)
247 return g_context_specific_group_get (&group, per_thread_thing_get_type (),
248 G_STRUCT_OFFSET (PerThreadThing, context),
249 start_func);
252 static gpointer
253 test_identity_thread (gpointer user_data)
255 guint thread_nr = GPOINTER_TO_UINT (user_data);
256 GMainContext *my_context;
257 guint i, j;
259 my_context = g_main_context_new ();
260 g_main_context_push_thread_default (my_context);
262 g_assert (!instances[thread_nr]);
263 instances[thread_nr] = per_thread_thing_get ();
264 g_assert (g_atomic_int_get (&is_running));
266 for (i = 0; i < 100; i++)
268 gpointer instance = per_thread_thing_get ();
270 for (j = 0; j < N_THREADS; j++)
271 g_assert ((instance == instances[j]) == (thread_nr == j));
273 g_assert (g_atomic_int_get (&is_running));
275 g_thread_yield ();
277 g_assert (g_atomic_int_get (&is_running));
280 for (i = 0; i < 100; i++)
282 g_object_unref (instances[thread_nr]);
284 for (j = 0; j < N_THREADS; j++)
285 g_assert ((instances[thread_nr] == instances[j]) == (thread_nr == j));
287 g_assert (g_atomic_int_get (&is_running));
289 g_thread_yield ();
292 /* drop the last ref */
293 g_object_unref (instances[thread_nr]);
294 instances[thread_nr] = NULL;
296 g_main_context_pop_thread_default (my_context);
297 g_main_context_unref (my_context);
299 /* at least one thread should see this cleared on exit */
300 return GUINT_TO_POINTER (!group.requested_state);
303 static void
304 test_context_specific_identity (void)
306 GThread *threads[N_THREADS];
307 gboolean exited = FALSE;
308 guint i;
310 g_assert (!g_atomic_int_get (&is_running));
311 for (i = 0; i < N_THREADS; i++)
312 threads[i] = g_thread_new ("test", test_identity_thread, GUINT_TO_POINTER (i));
313 for (i = 0; i < N_THREADS; i++)
314 exited |= GPOINTER_TO_UINT (g_thread_join (threads[i]));
315 g_assert (exited);
316 g_assert (!group.requested_state);
319 static void
320 changed_emitted (PerThreadThing *thing,
321 gpointer user_data)
323 gint *observed_value = user_data;
325 g_atomic_int_set (observed_value, g_atomic_int_get (&current_value));
328 static gpointer
329 test_emit_thread (gpointer user_data)
331 gint *observed_value = user_data;
332 GMainContext *my_context;
333 gpointer instance;
335 my_context = g_main_context_new ();
336 g_main_context_push_thread_default (my_context);
338 instance = per_thread_thing_get ();
339 g_assert (g_atomic_int_get (&is_running));
341 g_signal_connect (instance, "changed", G_CALLBACK (changed_emitted), observed_value);
343 /* observe after connection */
344 g_atomic_int_set (observed_value, g_atomic_int_get (&current_value));
346 while (g_atomic_int_get (&current_value) != -1)
347 g_main_context_iteration (my_context, TRUE);
349 g_object_unref (instance);
351 g_main_context_pop_thread_default (my_context);
352 g_main_context_unref (my_context);
354 /* at least one thread should see this cleared on exit */
355 return GUINT_TO_POINTER (!group.requested_state);
358 static void
359 test_context_specific_emit (void)
361 GThread *threads[N_THREADS];
362 gboolean exited = FALSE;
363 guint i, n;
365 for (i = 0; i < N_THREADS; i++)
366 threads[i] = g_thread_new ("test", test_emit_thread, &observed_values[i]);
368 /* make changes and ensure that they are observed */
369 for (n = 0; n < 1000; n++)
371 guint64 expiry;
373 /* don't burn CPU forever */
374 expiry = g_get_monotonic_time () + 10 * G_TIME_SPAN_SECOND;
376 g_atomic_int_set (&current_value, n);
378 /* wake them to notice */
379 for (i = 0; i < g_test_rand_int_range (1, 5); i++)
380 g_context_specific_group_emit (&group, g_signal_lookup ("changed", per_thread_thing_get_type ()));
382 for (i = 0; i < N_THREADS; i++)
383 while (g_atomic_int_get (&observed_values[i]) != n)
385 g_thread_yield ();
387 if (g_get_monotonic_time () > expiry)
388 g_error ("timed out");
392 /* tell them to quit */
393 g_atomic_int_set (&current_value, -1);
394 g_context_specific_group_emit (&group, g_signal_lookup ("notify", G_TYPE_OBJECT));
396 for (i = 0; i < N_THREADS; i++)
397 exited |= GPOINTER_TO_UINT (g_thread_join (threads[i]));
398 g_assert (exited);
399 g_assert (!group.requested_state);
402 static void
403 test_context_specific_emit_and_unref (void)
405 gpointer obj;
407 obj = per_thread_thing_get ();
408 g_context_specific_group_emit (&group, g_signal_lookup ("changed", per_thread_thing_get_type ()));
409 g_object_unref (obj);
411 while (g_main_context_iteration (NULL, 0))
416 main (int argc, char **argv)
418 GError *error = NULL;
419 int ret;
421 g_test_init (&argc, &argv, NULL);
423 test_file = g_test_build_filename (G_TEST_DIST, "contexts.c", NULL);
424 g_file_get_contents (test_file, &test_file_buffer,
425 &test_file_size, &error);
426 g_assert_no_error (error);
428 g_test_add_func ("/gio/contexts/thread-independence", test_thread_independence);
429 g_test_add_func ("/gio/contexts/context-independence", test_context_independence);
430 g_test_add_func ("/gio/contexts/context-specific/identity", test_context_specific_identity);
431 g_test_add_func ("/gio/contexts/context-specific/emit", test_context_specific_emit);
432 g_test_add_func ("/gio/contexts/context-specific/emit-and-unref", test_context_specific_emit_and_unref);
434 ret = g_test_run();
436 g_free (test_file_buffer);
437 g_free (test_file);
439 return ret;