1 #include "gcontextspecificgroup.c"
8 static gchar
*test_file
;
10 char *test_file_buffer
;
12 static char async_read_buffer
[8192];
15 read_data (GObject
*source
, GAsyncResult
*result
, gpointer loop
)
17 GInputStream
*in
= G_INPUT_STREAM (source
);
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
);
32 opened_for_read (GObject
*source
, GAsyncResult
*result
, gpointer loop
)
34 GFile
*file
= G_FILE (source
);
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
,
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
;
64 test_thread_independence (void)
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
);
75 idle_start_test1_thread (gpointer loop
)
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
;
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
;
98 test1_thread (gpointer user_data
)
100 GMainContext
*context
;
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
);
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
);
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
137 static gboolean
test2_fail (gpointer user_data
);
140 test_context_independence (void)
142 GMainContext
*context
;
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
);
180 test2_fail (gpointer user_data
)
182 g_assert_not_reached ();
189 GObject parent_instance
;
191 GMainContext
*context
;
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
];
209 g_assert (!is_running
);
210 g_atomic_int_set (&is_running
, TRUE
);
216 g_assert (is_running
);
217 g_atomic_int_set (&is_running
, FALSE
);
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
);
231 per_thread_thing_init (PerThreadThing
*thing
)
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);
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
),
253 test_identity_thread (gpointer user_data
)
255 guint thread_nr
= GPOINTER_TO_UINT (user_data
);
256 GMainContext
*my_context
;
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
));
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
));
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
);
304 test_context_specific_identity (void)
306 GThread
*threads
[N_THREADS
];
307 gboolean exited
= FALSE
;
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
]));
316 g_assert (!group
.requested_state
);
320 changed_emitted (PerThreadThing
*thing
,
323 gint
*observed_value
= user_data
;
325 g_atomic_int_set (observed_value
, g_atomic_int_get (¤t_value
));
329 test_emit_thread (gpointer user_data
)
331 gint
*observed_value
= user_data
;
332 GMainContext
*my_context
;
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 (¤t_value
));
346 while (g_atomic_int_get (¤t_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
);
359 test_context_specific_emit (void)
361 GThread
*threads
[N_THREADS
];
362 gboolean exited
= FALSE
;
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
++)
373 /* don't burn CPU forever */
374 expiry
= g_get_monotonic_time () + 10 * G_TIME_SPAN_SECOND
;
376 g_atomic_int_set (¤t_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
)
387 if (g_get_monotonic_time () > expiry
)
388 g_error ("timed out");
392 /* tell them to quit */
393 g_atomic_int_set (¤t_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
]));
399 g_assert (!group
.requested_state
);
403 test_context_specific_emit_and_unref (void)
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
;
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
);
436 g_free (test_file_buffer
);