Debian Codegraph @

glib/gio/tests/task.c

raw
1 /*
2 * Copyright 2012-2019 Red Hat, Inc.
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * See the included COPYING file for more information.
12 */
13
14 #include <gio/gio.h>
15 #include <string.h>
16
17 static GMainLoop *loop;
18 static GThread *main_thread;
19 static gssize magic;
20
21 /* We need objects for a few tests where we don't care what type
22 * they are, just that they're GObjects.
23 */
24 #define g_dummy_object_new g_socket_client_new
25
26 static gboolean
27 idle_quit_loop (gpointer user_data)
28 {
29 g_main_loop_quit (loop);
30 return FALSE;
31 }
32
33 static void
34 completed_cb (GObject *gobject,
35 GParamSpec *pspec,
36 gpointer user_data)
37 {
38 gboolean *notification_emitted = user_data;
39 *notification_emitted = TRUE;
40 }
41
42 static void
43 wait_for_completed_notification (GTask *task)
44 {
45 gboolean notification_emitted = FALSE;
46 gboolean is_completed = FALSE;
47
48 /* Hold a ref. so we can check the :completed property afterwards. */
49 g_object_ref (task);
50
51 g_signal_connect (task, "notify::completed",
52 (GCallback) completed_cb, &notification_emitted);
53 g_idle_add (idle_quit_loop, NULL);
54 g_main_loop_run (loop);
55 g_assert_true (notification_emitted);
56
57 g_assert_true (g_task_get_completed (task));
58 g_object_get (G_OBJECT (task), "completed", &is_completed, NULL);
59 g_assert_true (is_completed);
60
61 g_object_unref (task);
62 }
63
64 /* test_basic */
65
66 static void
67 basic_callback (GObject *object,
68 GAsyncResult *result,
69 gpointer user_data)
70 {
71 gssize *result_out = user_data;
72 GError *error = NULL;
73
74 g_assert (object == NULL);
75 g_assert (g_task_is_valid (result, object));
76 g_assert (g_async_result_get_user_data (result) == user_data);
77 g_assert (!g_task_had_error (G_TASK (result)));
78 g_assert_false (g_task_get_completed (G_TASK (result)));
79
80 *result_out = g_task_propagate_int (G_TASK (result), &error);
81 g_assert_no_error (error);
82
83 g_assert (!g_task_had_error (G_TASK (result)));
84
85 g_main_loop_quit (loop);
86 }
87
88 static gboolean
89 basic_return (gpointer user_data)
90 {
91 GTask *task = user_data;
92
93 g_task_return_int (task, magic);
94 g_object_unref (task);
95
96 return FALSE;
97 }
98
99 static void
100 basic_destroy_notify (gpointer user_data)
101 {
102 gboolean *destroyed = user_data;
103
104 *destroyed = TRUE;
105 }
106
107 static void
108 test_basic (void)
109 {
110 GTask *task;
111 gssize result;
112 gboolean task_data_destroyed = FALSE;
113 gboolean notification_emitted = FALSE;
114
115 task = g_task_new (NULL, NULL, basic_callback, &result);
116 g_task_set_task_data (task, &task_data_destroyed, basic_destroy_notify);
117 g_object_add_weak_pointer (G_OBJECT (task), (gpointer *)&task);
118 g_signal_connect (task, "notify::completed",
119 (GCallback) completed_cb, &notification_emitted);
120
121 g_idle_add (basic_return, task);
122 g_main_loop_run (loop);
123
124 g_assert_cmpint (result, ==, magic);
125 g_assert (task_data_destroyed == TRUE);
126 g_assert_true (notification_emitted);
127 g_assert (task == NULL);
128 }
129
130 /* test_error */
131
132 typedef struct {
133 GQuark expected_domain;
134 int expected_code;
135 char *expected_message;
136 gssize int_result;
137 } TaskErrorResult;
138
139 static void
140 error_callback (GObject *object,
141 GAsyncResult *result,
142 gpointer user_data)
143 {
144 TaskErrorResult *result_inout = user_data;
145 GError *error = NULL;
146
147 g_assert (object == NULL);
148 g_assert (g_task_is_valid (result, object));
149 g_assert (g_async_result_get_user_data (result) == user_data);
150 g_assert (g_task_had_error (G_TASK (result)));
151 g_assert_false (g_task_get_completed (G_TASK (result)));
152
153 result_inout->int_result = g_task_propagate_int (G_TASK (result), &error);
154 g_assert_error (error, result_inout->expected_domain, result_inout->expected_code);
155 g_assert_cmpstr (error->message, ==, result_inout->expected_message);
156 g_error_free (error);
157
158 g_assert (g_task_had_error (G_TASK (result)));
159 }
160
161 static gboolean
162 error_return (gpointer user_data)
163 {
164 GTask *task = user_data;
165
166 g_task_return_new_error (task,
167 G_IO_ERROR, G_IO_ERROR_FAILED,
168 "Failed %p", task);
169 g_object_unref (task);
170
171 return FALSE;
172 }
173
174 static void
175 error_destroy_notify (gpointer user_data)
176 {
177 gboolean *destroyed = user_data;
178
179 *destroyed = TRUE;
180 }
181
182 static void
183 test_error (void)
184 {
185 GTask *task;
186 TaskErrorResult result;
187 gboolean first_task_data_destroyed = FALSE;
188 gboolean second_task_data_destroyed = FALSE;
189
190 task = g_task_new (NULL, NULL, error_callback, &result);
191 result = (TaskErrorResult){
192 .expected_domain = G_IO_ERROR,
193 .expected_code = G_IO_ERROR_FAILED,
194 .expected_message = g_strdup_printf ("Failed %p", task),
195 };
196 g_object_add_weak_pointer (G_OBJECT (task), (gpointer *)&task);
197
198 g_assert (first_task_data_destroyed == FALSE);
199 g_task_set_task_data (task, &first_task_data_destroyed, error_destroy_notify);
200 g_assert (first_task_data_destroyed == FALSE);
201
202 /* Calling g_task_set_task_data() again will destroy the first data */
203 g_task_set_task_data (task, &second_task_data_destroyed, error_destroy_notify);
204 g_assert (first_task_data_destroyed == TRUE);
205 g_assert (second_task_data_destroyed == FALSE);
206
207 g_idle_add (error_return, task);
208 wait_for_completed_notification (task);
209
210 g_assert_cmpint (result.int_result, ==, -1);
211 g_assert (second_task_data_destroyed == TRUE);
212 g_assert (task == NULL);
213 g_free (result.expected_message);
214 }
215
216 static void
217 test_error_literal (void)
218 {
219 GTask *task;
220 TaskErrorResult result;
221
222 task = g_task_new (NULL, NULL, error_callback, &result);
223 result = (TaskErrorResult){
224 .expected_domain = G_IO_ERROR,
225 .expected_code = G_IO_ERROR_FAILED,
226 .expected_message = "Literal Failure",
227 };
228
229 g_task_return_new_error_literal (task,
230 result.expected_domain,
231 result.expected_code,
232 "Literal Failure");
233
234 wait_for_completed_notification (task);
235 g_assert_cmpint (result.int_result, ==, -1);
236
237 g_assert_finalize_object (task);
238 }
239
240 static void
241 test_error_literal_from_variable (void)
242 {
243 GTask *task;
244 TaskErrorResult result;
245
246 task = g_task_new (NULL, NULL, error_callback, &result);
247 result = (TaskErrorResult){
248 .expected_domain = G_IO_ERROR,
249 .expected_code = G_IO_ERROR_FAILED,
250 .expected_message = "Literal Failure",
251 };
252
253 g_task_return_new_error_literal (task,
254 result.expected_domain,
255 result.expected_code,
256 result.expected_message);
257
258 wait_for_completed_notification (task);
259 g_assert_cmpint (result.int_result, ==, -1);
260 g_assert_finalize_object (task);
261 }
262
263 /* test_return_from_same_iteration: calling g_task_return_* from the
264 * loop iteration the task was created in defers completion until the
265 * next iteration.
266 */
267 gboolean same_result = FALSE;
268 gboolean same_notification_emitted = FALSE;
269
270 static void
271 same_callback (GObject *object,
272 GAsyncResult *result,
273 gpointer user_data)
274 {
275 gboolean *result_out = user_data;
276 GError *error = NULL;
277
278 g_assert (object == NULL);
279 g_assert (g_task_is_valid (result, object));
280 g_assert (g_async_result_get_user_data (result) == user_data);
281 g_assert (!g_task_had_error (G_TASK (result)));
282 g_assert_false (g_task_get_completed (G_TASK (result)));
283
284 *result_out = g_task_propagate_boolean (G_TASK (result), &error);
285 g_assert_no_error (error);
286
287 g_assert (!g_task_had_error (G_TASK (result)));
288
289 g_main_loop_quit (loop);
290 }
291
292 static gboolean
293 same_start (gpointer user_data)
294 {
295 gpointer *weak_pointer = user_data;
296 GTask *task;
297
298 task = g_task_new (NULL, NULL, same_callback, &same_result);
299 *weak_pointer = task;
300 g_object_add_weak_pointer (G_OBJECT (task), weak_pointer);
301 g_signal_connect (task, "notify::completed",
302 (GCallback) completed_cb, &same_notification_emitted);
303
304 g_task_return_boolean (task, TRUE);
305 g_object_unref (task);
306
307 /* same_callback should not have been invoked yet */
308 g_assert (same_result == FALSE);
309 g_assert (*weak_pointer == task);
310 g_assert_false (same_notification_emitted);
311
312 return FALSE;
313 }
314
315 static void
316 test_return_from_same_iteration (void)
317 {
318 gpointer weak_pointer;
319
320 g_idle_add (same_start, &weak_pointer);
321 g_main_loop_run (loop);
322
323 g_assert (same_result == TRUE);
324 g_assert (weak_pointer == NULL);
325 g_assert_true (same_notification_emitted);
326 }
327
328 /* test_return_from_toplevel: calling g_task_return_* from outside any
329 * main loop completes the task inside the main loop.
330 */
331 gboolean toplevel_notification_emitted = FALSE;
332
333 static void
334 toplevel_callback (GObject *object,
335 GAsyncResult *result,
336 gpointer user_data)
337 {
338 gboolean *result_out = user_data;
339 GError *error = NULL;
340
341 g_assert (object == NULL);
342 g_assert (g_task_is_valid (result, object));
343 g_assert (g_async_result_get_user_data (result) == user_data);
344 g_assert (!g_task_had_error (G_TASK (result)));
345 g_assert_false (g_task_get_completed (G_TASK (result)));
346
347 *result_out = g_task_propagate_boolean (G_TASK (result), &error);
348 g_assert_no_error (error);
349
350 g_assert (!g_task_had_error (G_TASK (result)));
351
352 g_main_loop_quit (loop);
353 }
354
355 static void
356 test_return_from_toplevel (void)
357 {
358 GTask *task;
359 gboolean result = FALSE;
360
361 task = g_task_new (NULL, NULL, toplevel_callback, &result);
362 g_object_add_weak_pointer (G_OBJECT (task), (gpointer *)&task);
363 g_signal_connect (task, "notify::completed",
364 (GCallback) completed_cb, &toplevel_notification_emitted);
365
366 g_task_return_boolean (task, TRUE);
367 g_object_unref (task);
368
369 /* toplevel_callback should not have been invoked yet */
370 g_assert (result == FALSE);
371 g_assert (task != NULL);
372 g_assert_false (toplevel_notification_emitted);
373
374 g_main_loop_run (loop);
375
376 g_assert (result == TRUE);
377 g_assert (task == NULL);
378 g_assert_true (toplevel_notification_emitted);
379 }
380
381 /* test_return_from_anon_thread: calling g_task_return_* from a
382 * thread with no thread-default main context will complete the
383 * task in the task's context/thread.
384 */
385
386 gboolean anon_thread_notification_emitted = FALSE;
387 GThread *anon_thread;
388
389 static void
390 anon_callback (GObject *object,
391 GAsyncResult *result,
392 gpointer user_data)
393 {
394 gssize *result_out = user_data;
395 GError *error = NULL;
396
397 g_assert (object == NULL);
398 g_assert (g_task_is_valid (result, object));
399 g_assert (g_async_result_get_user_data (result) == user_data);
400 g_assert (!g_task_had_error (G_TASK (result)));
401 g_assert_false (g_task_get_completed (G_TASK (result)));
402
403 g_assert (g_thread_self () == main_thread);
404
405 *result_out = g_task_propagate_int (G_TASK (result), &error);
406 g_assert_no_error (error);
407
408 g_assert (!g_task_had_error (G_TASK (result)));
409
410 g_main_loop_quit (loop);
411 }
412
413 static gpointer
414 anon_thread_func (gpointer user_data)
415 {
416 GTask *task = user_data;
417
418 g_task_return_int (task, magic);
419 g_object_unref (task);
420
421 return NULL;
422 }
423
424 static gboolean
425 anon_start (gpointer user_data)
426 {
427 GTask *task = user_data;
428
429 anon_thread = g_thread_new ("test_return_from_anon_thread",
430 anon_thread_func, task);
431 return FALSE;
432 }
433
434 static void
435 test_return_from_anon_thread (void)
436 {
437 GTask *task;
438 gssize result = 0;
439
440 task = g_task_new (NULL, NULL, anon_callback, &result);
441 g_object_add_weak_pointer (G_OBJECT (task), (gpointer *)&task);
442 g_signal_connect (task, "notify::completed",
443 (GCallback) completed_cb,
444 &anon_thread_notification_emitted);
445
446 g_idle_add (anon_start, task);
447 g_main_loop_run (loop);
448
449 g_thread_join (anon_thread);
450
451 g_assert_cmpint (result, ==, magic);
452 g_assert (task == NULL);
453 g_assert_true (anon_thread_notification_emitted);
454 }
455
456 /* test_return_from_wrong_thread: calling g_task_return_* from a
457 * thread with its own thread-default main context will complete the
458 * task in the task's context/thread.
459 */
460
461 gboolean wrong_thread_notification_emitted = FALSE;
462 GThread *wrong_thread;
463
464 static void
465 wrong_callback (GObject *object,
466 GAsyncResult *result,
467 gpointer user_data)
468 {
469 gssize *result_out = user_data;
470 GError *error = NULL;
471
472 g_assert (object == NULL);
473 g_assert (g_task_is_valid (result, object));
474 g_assert (g_async_result_get_user_data (result) == user_data);
475 g_assert (!g_task_had_error (G_TASK (result)));
476 g_assert_false (g_task_get_completed (G_TASK (result)));
477
478 g_assert (g_thread_self () == main_thread);
479
480 *result_out = g_task_propagate_int (G_TASK (result), &error);
481 g_assert_no_error (error);
482
483 g_assert (!g_task_had_error (G_TASK (result)));
484
485 g_main_loop_quit (loop);
486 }
487
488 static gpointer
489 wrong_thread_func (gpointer user_data)
490 {
491 GTask *task = user_data;
492 GMainContext *context;
493
494 context = g_main_context_new ();
495 g_main_context_push_thread_default (context);
496
497 g_assert (g_task_get_context (task) != context);
498
499 g_task_return_int (task, magic);
500 g_object_unref (task);
501
502 g_main_context_pop_thread_default (context);
503 g_main_context_unref (context);
504
505 return NULL;
506 }
507
508 static gboolean
509 wrong_start (gpointer user_data)
510 {
511 GTask *task = user_data;
512
513 wrong_thread = g_thread_new ("test_return_from_anon_thread",
514 wrong_thread_func, task);
515 return FALSE;
516 }
517
518 static void
519 test_return_from_wrong_thread (void)
520 {
521 GTask *task;
522 gssize result = 0;
523
524 task = g_task_new (NULL, NULL, wrong_callback, &result);
525 g_object_add_weak_pointer (G_OBJECT (task), (gpointer *)&task);
526 g_signal_connect (task, "notify::completed",
527 (GCallback) completed_cb,
528 &wrong_thread_notification_emitted);
529
530 g_idle_add (wrong_start, task);
531 g_main_loop_run (loop);
532
533 g_thread_join (wrong_thread);
534
535 g_assert_cmpint (result, ==, magic);
536 g_assert (task == NULL);
537 g_assert_true (wrong_thread_notification_emitted);
538 }
539
540 /* test_no_callback */
541
542 static void
543 test_no_callback (void)
544 {
545 GTask *task;
546
547 task = g_task_new (NULL, NULL, NULL, NULL);
548 g_object_add_weak_pointer (G_OBJECT (task), (gpointer *)&task);
549
550 g_task_return_boolean (task, TRUE);
551 g_object_unref (task);
552
553 /* Even though there’s no callback, the :completed notification has to
554 * happen in an idle handler. */
555 g_assert_nonnull (task);
556 wait_for_completed_notification (task);
557 g_assert_null (task);
558 }
559
560 /* test_report_error */
561
562 static void test_report_error (void);
563 gboolean error_notification_emitted = FALSE;
564
565 static void
566 report_callback (GObject *object,
567 GAsyncResult *result,
568 gpointer user_data)
569 {
570 gpointer *weak_pointer = user_data;
571 GError *error = NULL;
572 gssize ret;
573
574 g_assert (object == NULL);
575 g_assert (g_task_is_valid (result, object));
576 g_assert (g_async_result_get_user_data (result) == user_data);
577 g_assert (g_async_result_is_tagged (result, test_report_error));
578 g_assert (g_task_get_source_tag (G_TASK (result)) == test_report_error);
579 g_assert (g_task_had_error (G_TASK (result)));
580 g_assert_false (g_task_get_completed (G_TASK (result)));
581
582 ret = g_task_propagate_int (G_TASK (result), &error);
583 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
584 g_assert_cmpint (ret, ==, -1);
585 g_error_free (error);
586
587 g_assert (g_task_had_error (G_TASK (result)));
588
589 *weak_pointer = result;
590 g_object_add_weak_pointer (G_OBJECT (result), weak_pointer);
591 g_signal_connect (result, "notify::completed",
592 (GCallback) completed_cb, &error_notification_emitted);
593
594 g_main_loop_quit (loop);
595 }
596
597 static void
598 test_report_error (void)
599 {
600 gpointer weak_pointer = (gpointer)-1;
601
602 g_task_report_new_error (NULL, report_callback, &weak_pointer,
603 test_report_error,
604 G_IO_ERROR, G_IO_ERROR_FAILED,
605 "Failed");
606 g_main_loop_run (loop);
607
608 g_assert (weak_pointer == NULL);
609 g_assert_true (error_notification_emitted);
610 }
611
612 /* test_priority: tasks complete in priority order */
613
614 static int counter = 0;
615
616 static void
617 priority_callback (GObject *object,
618 GAsyncResult *result,
619 gpointer user_data)
620 {
621 gssize *ret_out = user_data;
622 GError *error = NULL;
623
624 g_assert (object == NULL);
625 g_assert (g_task_is_valid (result, object));
626 g_assert (g_async_result_get_user_data (result) == user_data);
627 g_assert (!g_task_had_error (G_TASK (result)));
628 g_assert_false (g_task_get_completed (G_TASK (result)));
629
630 g_task_propagate_boolean (G_TASK (result), &error);
631 g_assert_no_error (error);
632
633 g_assert (!g_task_had_error (G_TASK (result)));
634
635 *ret_out = ++counter;
636
637 if (counter == 3)
638 g_main_loop_quit (loop);
639 }
640
641 static void
642 test_priority (void)
643 {
644 GTask *t1, *t2, *t3;
645 gssize ret1, ret2, ret3;
646
647 /* t2 has higher priority than either t1 or t3, so we can't
648 * accidentally pass the test just by completing the tasks in the
649 * order they were created (or in reverse order).
650 */
651
652 t1 = g_task_new (NULL, NULL, priority_callback, &ret1);
653 g_task_set_priority (t1, G_PRIORITY_DEFAULT);
654 g_task_return_boolean (t1, TRUE);
655 g_object_unref (t1);
656
657 t2 = g_task_new (NULL, NULL, priority_callback, &ret2);
658 g_task_set_priority (t2, G_PRIORITY_HIGH);
659 g_task_return_boolean (t2, TRUE);
660 g_object_unref (t2);
661
662 t3 = g_task_new (NULL, NULL, priority_callback, &ret3);
663 g_task_set_priority (t3, G_PRIORITY_LOW);
664 g_task_return_boolean (t3, TRUE);
665 g_object_unref (t3);
666
667 g_main_loop_run (loop);
668
669 g_assert_cmpint (ret2, ==, 1);
670 g_assert_cmpint (ret1, ==, 2);
671 g_assert_cmpint (ret3, ==, 3);
672 }
673
674 /* Test that getting and setting the task name works. */
675 static void name_callback (GObject *object,
676 GAsyncResult *result,
677 gpointer user_data);
678
679 static void
680 test_name (void)
681 {
682 GTask *t1 = NULL;
683 char *orig = g_strdup ("some task");
684 gchar *name1 = NULL;
685
686 t1 = g_task_new (NULL, NULL, name_callback, &name1);
687 (g_task_set_name) (t1, orig);
688 g_task_return_boolean (t1, TRUE);
689 g_object_unref (t1);
690
691 g_main_loop_run (loop);
692
693 g_assert_cmpstr (name1, ==, orig);
694
695 g_free (name1);
696 g_free (orig);
697 }
698
699 static void
700 test_name_macro_wrapper (void)
701 {
702 GTask *t1 = NULL;
703 char *orig = g_strdup ("some task");
704 gchar *name1 = NULL;
705
706 t1 = g_task_new (NULL, NULL, name_callback, &name1);
707 g_task_set_name (t1, orig);
708 g_task_return_boolean (t1, TRUE);
709 g_object_unref (t1);
710
711 g_main_loop_run (loop);
712
713 g_assert_cmpstr (name1, ==, orig);
714
715 g_free (name1);
716 g_free (orig);
717 }
718
719 static void
720 name_callback (GObject *object,
721 GAsyncResult *result,
722 gpointer user_data)
723 {
724 gchar **name_out = user_data;
725 GError *local_error = NULL;
726
727 g_assert_null (*name_out);
728 *name_out = g_strdup (g_task_get_name (G_TASK (result)));
729
730 g_task_propagate_boolean (G_TASK (result), &local_error);
731 g_assert_no_error (local_error);
732
733 g_main_loop_quit (loop);
734 }
735
736 static void static_name_callback (GObject *object,
737 GAsyncResult *result,
738 gpointer user_data);
739
740 static void
741 test_static_name (void)
742 {
743 GTask *t1 = NULL;
744 char *orig = "some task";
745 char *name1 = NULL;
746
747 t1 = g_task_new (NULL, NULL, static_name_callback, &name1);
748 g_task_set_static_name (t1, orig);
749 g_task_return_boolean (t1, TRUE);
750 g_object_unref (t1);
751
752 g_main_loop_run (loop);
753
754 g_assert_true (name1 == orig);
755 }
756
757 static void
758 static_name_callback (GObject *object,
759 GAsyncResult *result,
760 gpointer user_data)
761 {
762 const char **name_out = user_data;
763 GError *local_error = NULL;
764
765 g_assert_null (*name_out);
766 *name_out = g_task_get_name (G_TASK (result));
767
768 g_task_propagate_boolean (G_TASK (result), &local_error);
769 g_assert_no_error (local_error);
770
771 g_main_loop_quit (loop);
772 }
773
774 /* test_asynchronous_cancellation: cancelled tasks are returned
775 * asynchronously, i.e. not from inside the GCancellable::cancelled
776 * handler.
777 *
778 * The test is set up further below in test_asynchronous_cancellation.
779 */
780
781 /* asynchronous_cancellation_callback represents the callback that the
782 * caller of a typical asynchronous API would have passed. See
783 * test_asynchronous_cancellation.
784 */
785 static void
786 asynchronous_cancellation_callback (GObject *object,
787 GAsyncResult *result,
788 gpointer user_data)
789 {
790 GError *error = NULL;
791 guint run_task_id;
792
793 g_assert_null (object);
794 g_assert_true (g_task_is_valid (result, object));
795 g_assert_true (g_async_result_get_user_data (result) == user_data);
796 g_assert_true (g_task_had_error (G_TASK (result)));
797 g_assert_false (g_task_get_completed (G_TASK (result)));
798
799 run_task_id = GPOINTER_TO_UINT (g_task_get_task_data (G_TASK (result)));
800 g_assert_cmpuint (run_task_id, ==, 0);
801
802 g_task_propagate_boolean (G_TASK (result), &error);
803 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
804 g_clear_error (&error);
805
806 g_assert_true (g_task_had_error (G_TASK (result)));
807
808 g_main_loop_quit (loop);
809 }
810
811 /* asynchronous_cancellation_cancel_task represents a user cancelling
812 * the ongoing operation. To make it somewhat realistic it is delayed
813 * by 50ms via a timeout GSource. See test_asynchronous_cancellation.
814 */
815 static gboolean
816 asynchronous_cancellation_cancel_task (gpointer user_data)
817 {
818 GCancellable *cancellable;
819 GTask *task = G_TASK (user_data);
820
821 cancellable = g_task_get_cancellable (task);
822 g_assert_true (G_IS_CANCELLABLE (cancellable));
823
824 g_cancellable_cancel (cancellable);
825 g_assert_false (g_task_get_completed (task));
826
827 return G_SOURCE_REMOVE;
828 }
829
830 /* asynchronous_cancellation_cancelled is the GCancellable::cancelled
831 * handler that's used by the asynchronous implementation for
832 * cancelling itself.
833 */
834 static void
835 asynchronous_cancellation_cancelled (GCancellable *cancellable,
836 gpointer user_data)
837 {
838 GTask *task = G_TASK (user_data);
839 guint run_task_id;
840
841 g_assert_true (cancellable == g_task_get_cancellable (task));
842
843 run_task_id = GPOINTER_TO_UINT (g_task_get_task_data (task));
844 g_assert_cmpuint (run_task_id, !=, 0);
845
846 g_source_remove (run_task_id);
847 g_task_set_task_data (task, GUINT_TO_POINTER (0), NULL);
848
849 g_task_return_boolean (task, FALSE);
850 g_assert_false (g_task_get_completed (task));
851 }
852
853 /* asynchronous_cancellation_run_task represents the actual
854 * asynchronous work being done in an idle GSource as was mentioned
855 * above. This is effectively meant to be an infinite loop so that
856 * the only way to break out of it is via cancellation.
857 */
858 static gboolean
859 asynchronous_cancellation_run_task (gpointer user_data)
860 {
861 GCancellable *cancellable;
862 GTask *task = G_TASK (user_data);
863
864 cancellable = g_task_get_cancellable (task);
865 g_assert_true (G_IS_CANCELLABLE (cancellable));
866 g_assert_false (g_cancellable_is_cancelled (cancellable));
867
868 return G_SOURCE_CONTINUE;
869 }
870
871 /* Test that cancellation is always asynchronous. The completion callback for
872 * a #GTask must not be called from inside the cancellation handler.
873 *
874 * The body of the loop inside test_asynchronous_cancellation
875 * represents what would have been a typical asynchronous API call,
876 * and its implementation. They are fused together without an API
877 * boundary. The actual work done by this asynchronous API is
878 * represented by an idle GSource.
879 */
880 static void
881 test_asynchronous_cancellation (void)
882 {
883 guint i;
884
885 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/issues/1608");
886
887 /* Run a few times to shake out any timing issues between the
888 * cancellation and task sources.
889 */
890 for (i = 0; i < 5; i++)
891 {
892 GCancellable *cancellable;
893 GTask *task;
894 gboolean notification_emitted = FALSE;
895 guint run_task_id;
896
897 cancellable = g_cancellable_new ();
898
899 task = g_task_new (NULL, cancellable, asynchronous_cancellation_callback, NULL);
900 g_cancellable_connect (cancellable, (GCallback) asynchronous_cancellation_cancelled, task, NULL);
901 g_signal_connect (task, "notify::completed", (GCallback) completed_cb, &notification_emitted);
902
903 run_task_id = g_idle_add (asynchronous_cancellation_run_task, task);
904 g_source_set_name_by_id (run_task_id, "[test_asynchronous_cancellation] run_task");
905 g_task_set_task_data (task, GUINT_TO_POINTER (run_task_id), NULL);
906
907 g_timeout_add (50, asynchronous_cancellation_cancel_task, task);
908
909 g_main_loop_run (loop);
910
911 g_assert_true (g_task_get_completed (task));
912 g_assert_true (notification_emitted);
913
914 g_object_unref (cancellable);
915 g_object_unref (task);
916 }
917 }
918
919 /* test_check_cancellable: cancellation overrides return value */
920
921 enum {
922 CANCEL_BEFORE = (1 << 1),
923 CANCEL_AFTER = (1 << 2),
924 CHECK_CANCELLABLE = (1 << 3)
925 };
926 #define NUM_CANCEL_TESTS (CANCEL_BEFORE | CANCEL_AFTER | CHECK_CANCELLABLE)
927
928 static void
929 cancel_callback (GObject *object,
930 GAsyncResult *result,
931 gpointer user_data)
932 {
933 int state = GPOINTER_TO_INT (user_data);
934 GTask *task;
935 GCancellable *cancellable;
936 GError *error = NULL;
937
938 g_assert (object == NULL);
939 g_assert (g_task_is_valid (result, object));
940 g_assert (g_async_result_get_user_data (result) == user_data);
941
942 task = G_TASK (result);
943 cancellable = g_task_get_cancellable (task);
944 g_assert (G_IS_CANCELLABLE (cancellable));
945
946 if (state & (CANCEL_BEFORE | CANCEL_AFTER))
947 g_assert (g_cancellable_is_cancelled (cancellable));
948 else
949 g_assert (!g_cancellable_is_cancelled (cancellable));
950
951 if (state & CHECK_CANCELLABLE)
952 g_assert (g_task_get_check_cancellable (task));
953 else
954 g_assert (!g_task_get_check_cancellable (task));
955
956 if (g_task_propagate_boolean (task, &error))
957 {
958 g_assert (!g_cancellable_is_cancelled (cancellable) ||
959 !g_task_get_check_cancellable (task));
960 }
961 else
962 {
963 g_assert (g_cancellable_is_cancelled (cancellable) &&
964 g_task_get_check_cancellable (task));
965 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
966 g_error_free (error);
967 }
968
969 g_main_loop_quit (loop);
970 }
971
972 static void
973 test_check_cancellable (void)
974 {
975 GTask *task;
976 GCancellable *cancellable;
977 int state;
978
979 cancellable = g_cancellable_new ();
980
981 for (state = 0; state <= NUM_CANCEL_TESTS; state++)
982 {
983 task = g_task_new (NULL, cancellable, cancel_callback,
984 GINT_TO_POINTER (state));
985 g_task_set_check_cancellable (task, (state & CHECK_CANCELLABLE) != 0);
986
987 if (state & CANCEL_BEFORE)
988 g_cancellable_cancel (cancellable);
989 g_task_return_boolean (task, TRUE);
990 if (state & CANCEL_AFTER)
991 g_cancellable_cancel (cancellable);
992
993 g_main_loop_run (loop);
994 g_object_unref (task);
995 g_cancellable_reset (cancellable);
996 }
997
998 g_object_unref (cancellable);
999 }
1000
1001 /* test_return_if_cancelled */
1002
1003 static void
1004 return_if_cancelled_callback (GObject *object,
1005 GAsyncResult *result,
1006 gpointer user_data)
1007 {
1008 GError *error = NULL;
1009
1010 g_assert (object == NULL);
1011 g_assert (g_task_is_valid (result, object));
1012 g_assert (g_async_result_get_user_data (result) == user_data);
1013 g_assert (g_task_had_error (G_TASK (result)));
1014 g_assert_false (g_task_get_completed (G_TASK (result)));
1015
1016 g_task_propagate_boolean (G_TASK (result), &error);
1017 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
1018 g_clear_error (&error);
1019
1020 g_assert (g_task_had_error (G_TASK (result)));
1021
1022 g_main_loop_quit (loop);
1023 }
1024
1025 static void
1026 test_return_if_cancelled (void)
1027 {
1028 GTask *task;
1029 GCancellable *cancellable;
1030 gboolean cancelled;
1031 gboolean notification_emitted = FALSE;
1032
1033 cancellable = g_cancellable_new ();
1034
1035 task = g_task_new (NULL, cancellable, return_if_cancelled_callback, NULL);
1036 g_signal_connect (task, "notify::completed",
1037 (GCallback) completed_cb, &notification_emitted);
1038
1039 g_cancellable_cancel (cancellable);
1040 cancelled = g_task_return_error_if_cancelled (task);
1041 g_assert (cancelled);
1042 g_assert_false (notification_emitted);
1043 g_main_loop_run (loop);
1044 g_object_unref (task);
1045 g_assert_true (notification_emitted);
1046 g_cancellable_reset (cancellable);
1047
1048 notification_emitted = FALSE;
1049
1050 task = g_task_new (NULL, cancellable, return_if_cancelled_callback, NULL);
1051 g_signal_connect (task, "notify::completed",
1052 (GCallback) completed_cb, &notification_emitted);
1053
1054 g_task_set_check_cancellable (task, FALSE);
1055 g_cancellable_cancel (cancellable);
1056 cancelled = g_task_return_error_if_cancelled (task);
1057 g_assert (cancelled);
1058 g_assert_false (notification_emitted);
1059 g_main_loop_run (loop);
1060 g_object_unref (task);
1061 g_assert_true (notification_emitted);
1062 g_object_unref (cancellable);
1063 }
1064
1065 /* test_run_in_thread */
1066
1067 static GMutex run_in_thread_mutex;
1068 static GCond run_in_thread_cond;
1069
1070 static void
1071 task_weak_notify (gpointer user_data,
1072 GObject *ex_task)
1073 {
1074 gboolean *weak_notify_ran = user_data;
1075
1076 g_mutex_lock (&run_in_thread_mutex);
1077 g_atomic_int_set (weak_notify_ran, TRUE);
1078 g_cond_signal (&run_in_thread_cond);
1079 g_mutex_unlock (&run_in_thread_mutex);
1080 }
1081
1082 static void
1083 run_in_thread_callback (GObject *object,
1084 GAsyncResult *result,
1085 gpointer user_data)
1086 {
1087 gboolean *done = user_data;
1088 GError *error = NULL;
1089 gssize ret;
1090
1091 g_assert (g_thread_self () == main_thread);
1092
1093 g_assert (object == NULL);
1094 g_assert (g_task_is_valid (result, object));
1095 g_assert (g_async_result_get_user_data (result) == user_data);
1096 g_assert (!g_task_had_error (G_TASK (result)));
1097 g_assert_false (g_task_get_completed (G_TASK (result)));
1098 g_assert_cmpstr (g_task_get_name (G_TASK (result)), ==, "test_run_in_thread name");
1099
1100 ret = g_task_propagate_int (G_TASK (result), &error);
1101 g_assert_no_error (error);
1102 g_assert_cmpint (ret, ==, magic);
1103
1104 g_assert (!g_task_had_error (G_TASK (result)));
1105
1106 *done = TRUE;
1107 g_main_loop_quit (loop);
1108 }
1109
1110 static void
1111 run_in_thread_thread (GTask *task,
1112 gpointer source_object,
1113 gpointer task_data,
1114 GCancellable *cancellable)
1115 {
1116 gboolean *thread_ran = task_data;
1117
1118 g_assert (source_object == g_task_get_source_object (task));
1119 g_assert (task_data == g_task_get_task_data (task));
1120 g_assert (cancellable == g_task_get_cancellable (task));
1121 g_assert_false (g_task_get_completed (task));
1122 g_assert_cmpstr (g_task_get_name (task), ==, "test_run_in_thread name");
1123
1124 g_assert (g_thread_self () != main_thread);
1125
1126 g_mutex_lock (&run_in_thread_mutex);
1127 g_atomic_int_set (thread_ran, TRUE);
1128 g_cond_signal (&run_in_thread_cond);
1129 g_mutex_unlock (&run_in_thread_mutex);
1130
1131 g_task_return_int (task, magic);
1132 }
1133
1134 static void
1135 test_run_in_thread (void)
1136 {
1137 GTask *task;
1138 gboolean thread_ran = FALSE; /* (atomic) */
1139 gboolean weak_notify_ran = FALSE; /* (atomic) */
1140 gboolean notification_emitted = FALSE;
1141 gboolean done = FALSE;
1142
1143 task = g_task_new (NULL, NULL, run_in_thread_callback, &done);
1144 g_task_set_name (task, "test_run_in_thread name");
1145 g_object_weak_ref (G_OBJECT (task), task_weak_notify, (gpointer)&weak_notify_ran);
1146 g_signal_connect (task, "notify::completed",
1147 (GCallback) completed_cb, &notification_emitted);
1148
1149 g_task_set_task_data (task, (gpointer)&thread_ran, NULL);
1150 g_task_run_in_thread (task, run_in_thread_thread);
1151
1152 g_mutex_lock (&run_in_thread_mutex);
1153 while (!g_atomic_int_get (&thread_ran))
1154 g_cond_wait (&run_in_thread_cond, &run_in_thread_mutex);
1155 g_mutex_unlock (&run_in_thread_mutex);
1156
1157 g_assert (done == FALSE);
1158 g_assert_false (g_atomic_int_get (&weak_notify_ran));
1159
1160 g_main_loop_run (loop);
1161
1162 g_assert (done == TRUE);
1163 g_assert_true (notification_emitted);
1164
1165 g_assert_cmpstr (g_task_get_name (task), ==, "test_run_in_thread name");
1166
1167 g_object_unref (task);
1168
1169 g_mutex_lock (&run_in_thread_mutex);
1170 while (!g_atomic_int_get (&weak_notify_ran))
1171 g_cond_wait (&run_in_thread_cond, &run_in_thread_mutex);
1172 g_mutex_unlock (&run_in_thread_mutex);
1173 }
1174
1175 /* test_run_in_thread_sync */
1176
1177 static void
1178 run_in_thread_sync_callback (GObject *object,
1179 GAsyncResult *result,
1180 gpointer user_data)
1181 {
1182 /* g_task_run_in_thread_sync() does not invoke the task's callback */
1183 g_assert_not_reached ();
1184 }
1185
1186 static void
1187 run_in_thread_sync_thread (GTask *task,
1188 gpointer source_object,
1189 gpointer task_data,
1190 GCancellable *cancellable)
1191 {
1192 gboolean *thread_ran = task_data;
1193
1194 g_assert (source_object == g_task_get_source_object (task));
1195 g_assert (task_data == g_task_get_task_data (task));
1196 g_assert (cancellable == g_task_get_cancellable (task));
1197 g_assert_false (g_task_get_completed (task));
1198
1199 g_assert (g_thread_self () != main_thread);
1200
1201 g_atomic_int_set (thread_ran, TRUE);
1202 g_task_return_int (task, magic);
1203 }
1204
1205 static void
1206 test_run_in_thread_sync (void)
1207 {
1208 GTask *task;
1209 gboolean thread_ran = FALSE;
1210 gssize ret;
1211 gboolean notification_emitted = FALSE;
1212 GError *error = NULL;
1213
1214 task = g_task_new (NULL, NULL, run_in_thread_sync_callback, NULL);
1215 g_signal_connect (task, "notify::completed",
1216 (GCallback) completed_cb,
1217 &notification_emitted);
1218
1219 g_task_set_task_data (task, &thread_ran, NULL);
1220 g_task_run_in_thread_sync (task, run_in_thread_sync_thread);
1221
1222 g_assert_true (g_atomic_int_get (&thread_ran));
1223 g_assert (task != NULL);
1224 g_assert (!g_task_had_error (task));
1225 g_assert_true (g_task_get_completed (task));
1226 g_assert_true (notification_emitted);
1227
1228 ret = g_task_propagate_int (task, &error);
1229 g_assert_no_error (error);
1230 g_assert_cmpint (ret, ==, magic);
1231
1232 g_assert (!g_task_had_error (task));
1233
1234 g_object_unref (task);
1235 }
1236
1237 /* test_run_in_thread_priority */
1238
1239 static GMutex fake_task_mutex, last_fake_task_mutex;
1240 static gint sequence_number = 0;
1241
1242 static void
1243 quit_main_loop_callback (GObject *object,
1244 GAsyncResult *result,
1245 gpointer user_data)
1246 {
1247 GError *error = NULL;
1248 gboolean ret;
1249
1250 g_assert (g_thread_self () == main_thread);
1251
1252 g_assert (object == NULL);
1253 g_assert (g_task_is_valid (result, object));
1254 g_assert (g_async_result_get_user_data (result) == user_data);
1255 g_assert (!g_task_had_error (G_TASK (result)));
1256 g_assert_false (g_task_get_completed (G_TASK (result)));
1257
1258 ret = g_task_propagate_boolean (G_TASK (result), &error);
1259 g_assert_no_error (error);
1260 g_assert_cmpint (ret, ==, TRUE);
1261
1262 g_assert (!g_task_had_error (G_TASK (result)));
1263
1264 g_main_loop_quit (loop);
1265 }
1266
1267 static void
1268 set_sequence_number_thread (GTask *task,
1269 gpointer source_object,
1270 gpointer task_data,
1271 GCancellable *cancellable)
1272 {
1273 gint *seq_no_p = task_data;
1274
1275 *seq_no_p = ++sequence_number;
1276 g_task_return_boolean (task, TRUE);
1277 }
1278
1279 static void
1280 fake_task_thread (GTask *task,
1281 gpointer source_object,
1282 gpointer task_data,
1283 GCancellable *cancellable)
1284 {
1285 GMutex *mutex = task_data;
1286
1287 g_mutex_lock (mutex);
1288 g_mutex_unlock (mutex);
1289 g_task_return_boolean (task, TRUE);
1290 }
1291
1292 #define G_TASK_THREAD_POOL_SIZE 10
1293 static int fake_tasks_running;
1294
1295 static void
1296 fake_task_callback (GObject *source,
1297 GAsyncResult *result,
1298 gpointer user_data)
1299 {
1300 if (--fake_tasks_running == 0)
1301 g_main_loop_quit (loop);
1302 }
1303
1304 static void
1305 clog_up_thread_pool (void)
1306 {
1307 GTask *task;
1308 int i;
1309
1310 g_thread_pool_stop_unused_threads ();
1311
1312 g_mutex_lock (&fake_task_mutex);
1313 for (i = 0; i < G_TASK_THREAD_POOL_SIZE - 1; i++)
1314 {
1315 task = g_task_new (NULL, NULL, fake_task_callback, NULL);
1316 g_task_set_task_data (task, &fake_task_mutex, NULL);
1317 g_assert_cmpint (g_task_get_priority (task), ==, G_PRIORITY_DEFAULT);
1318 g_task_set_priority (task, G_PRIORITY_HIGH * 2);
1319 g_assert_cmpint (g_task_get_priority (task), ==, G_PRIORITY_HIGH * 2);
1320 g_task_run_in_thread (task, fake_task_thread);
1321 g_object_unref (task);
1322 fake_tasks_running++;
1323 }
1324
1325 g_mutex_lock (&last_fake_task_mutex);
1326 task = g_task_new (NULL, NULL, NULL, NULL);
1327 g_task_set_task_data (task, &last_fake_task_mutex, NULL);
1328 g_task_set_priority (task, G_PRIORITY_HIGH * 2);
1329 g_task_run_in_thread (task, fake_task_thread);
1330 g_object_unref (task);
1331 }
1332
1333 static void
1334 unclog_thread_pool (void)
1335 {
1336 g_mutex_unlock (&fake_task_mutex);
1337 g_main_loop_run (loop);
1338 }
1339
1340 static void
1341 test_run_in_thread_priority (void)
1342 {
1343 GTask *task;
1344 GCancellable *cancellable;
1345 int seq_a, seq_b, seq_c, seq_d;
1346
1347 clog_up_thread_pool ();
1348
1349 /* Queue three more tasks that we'll arrange to have run serially */
1350 task = g_task_new (NULL, NULL, NULL, NULL);
1351 g_task_set_task_data (task, &seq_a, NULL);
1352 g_task_run_in_thread (task, set_sequence_number_thread);
1353 g_object_unref (task);
1354
1355 task = g_task_new (NULL, NULL, quit_main_loop_callback, NULL);
1356 g_task_set_task_data (task, &seq_b, NULL);
1357 g_task_set_priority (task, G_PRIORITY_LOW);
1358 g_task_run_in_thread (task, set_sequence_number_thread);
1359 g_object_unref (task);
1360
1361 task = g_task_new (NULL, NULL, NULL, NULL);
1362 g_task_set_task_data (task, &seq_c, NULL);
1363 g_task_set_priority (task, G_PRIORITY_HIGH);
1364 g_task_run_in_thread (task, set_sequence_number_thread);
1365 g_object_unref (task);
1366
1367 cancellable = g_cancellable_new ();
1368 task = g_task_new (NULL, cancellable, NULL, NULL);
1369 g_task_set_task_data (task, &seq_d, NULL);
1370 g_task_run_in_thread (task, set_sequence_number_thread);
1371 g_cancellable_cancel (cancellable);
1372 g_object_unref (cancellable);
1373 g_object_unref (task);
1374
1375 /* Let the last fake task complete; the four other tasks will then
1376 * complete serially, in the order D, C, A, B, and B will quit the
1377 * main loop.
1378 */
1379 g_mutex_unlock (&last_fake_task_mutex);
1380 g_main_loop_run (loop);
1381
1382 g_assert_cmpint (seq_d, ==, 1);
1383 g_assert_cmpint (seq_c, ==, 2);
1384 g_assert_cmpint (seq_a, ==, 3);
1385 g_assert_cmpint (seq_b, ==, 4);
1386
1387 unclog_thread_pool ();
1388 }
1389
1390 /* test_run_in_thread_nested: task threads that block waiting on
1391 * other task threads will not cause the thread pool to starve.
1392 */
1393
1394 static void
1395 run_nested_task_thread (GTask *task,
1396 gpointer source_object,
1397 gpointer task_data,
1398 GCancellable *cancellable)
1399 {
1400 GTask *nested;
1401 int *nested_tasks_left = task_data;
1402
1403 if ((*nested_tasks_left)--)
1404 {
1405 nested = g_task_new (NULL, NULL, NULL, NULL);
1406 g_task_set_task_data (nested, nested_tasks_left, NULL);
1407 g_task_run_in_thread_sync (nested, run_nested_task_thread);
1408 g_object_unref (nested);
1409 }
1410
1411 g_task_return_boolean (task, TRUE);
1412 }
1413
1414 static void
1415 test_run_in_thread_nested (void)
1416 {
1417 GTask *task;
1418 int nested_tasks_left = 2;
1419
1420 clog_up_thread_pool ();
1421
1422 task = g_task_new (NULL, NULL, quit_main_loop_callback, NULL);
1423 g_task_set_task_data (task, &nested_tasks_left, NULL);
1424 g_task_run_in_thread (task, run_nested_task_thread);
1425 g_object_unref (task);
1426
1427 g_mutex_unlock (&last_fake_task_mutex);
1428 g_main_loop_run (loop);
1429
1430 unclog_thread_pool ();
1431 }
1432
1433 /* test_run_in_thread_overflow: if you queue lots and lots and lots of
1434 * tasks, they won't all run at once.
1435 */
1436 static GMutex overflow_mutex;
1437 static guint overflow_completed;
1438
1439 static void
1440 run_overflow_task_thread (GTask *task,
1441 gpointer source_object,
1442 gpointer task_data,
1443 GCancellable *cancellable)
1444 {
1445 gchar *result = task_data;
1446
1447 if (g_task_return_error_if_cancelled (task))
1448 {
1449 *result = 'X';
1450 }
1451 else
1452 {
1453 /* Block until the main thread is ready. */
1454 g_mutex_lock (&overflow_mutex);
1455 g_mutex_unlock (&overflow_mutex);
1456
1457 *result = '.';
1458
1459 g_task_return_boolean (task, TRUE);
1460 }
1461
1462 g_atomic_int_inc (&overflow_completed);
1463 }
1464
1465 #define NUM_OVERFLOW_TASKS 1024
1466
1467 static void
1468 test_run_in_thread_overflow (void)
1469 {
1470 GCancellable *cancellable;
1471 GTask *task;
1472 gchar buf[NUM_OVERFLOW_TASKS + 1];
1473 gint i;
1474
1475 /* Queue way too many tasks and then sleep for a bit. The first 10
1476 * tasks will be dispatched to threads and will then block on
1477 * overflow_mutex, so more threads will be created while this thread
1478 * is sleeping. Then we cancel the cancellable, unlock the mutex,
1479 * wait for all of the tasks to complete, and make sure that we got
1480 * the behavior we expected.
1481 */
1482
1483 memset (buf, 0, sizeof (buf));
1484 cancellable = g_cancellable_new ();
1485
1486 g_mutex_lock (&overflow_mutex);
1487
1488 for (i = 0; i < NUM_OVERFLOW_TASKS; i++)
1489 {
1490 task = g_task_new (NULL, cancellable, NULL, NULL);
1491 g_task_set_task_data (task, buf + i, NULL);
1492 g_task_run_in_thread (task, run_overflow_task_thread);
1493 g_object_unref (task);
1494 }
1495
1496 if (g_test_slow ())
1497 g_usleep (5000000); /* 5 s */
1498 else
1499 g_usleep (500000); /* 0.5 s */
1500 g_cancellable_cancel (cancellable);
1501 g_object_unref (cancellable);
1502
1503 g_mutex_unlock (&overflow_mutex);
1504
1505 /* Wait for all tasks to complete. */
1506 while (g_atomic_int_get (&overflow_completed) != NUM_OVERFLOW_TASKS)
1507 g_usleep (1000);
1508
1509 g_assert_cmpint (strlen (buf), ==, NUM_OVERFLOW_TASKS);
1510
1511 i = strspn (buf, ".");
1512 /* Given the sleep times above, i should be 14 for normal, 40 for
1513 * slow. But if the machine is too slow/busy then the scheduling
1514 * might get messed up and we'll get more or fewer threads than
1515 * expected. But there are limits to how messed up it could
1516 * plausibly get (and we hope that if gtask is actually broken then
1517 * it will exceed those limits).
1518 */
1519 g_assert_cmpint (i, >=, 10);
1520 if (g_test_slow ())
1521 g_assert_cmpint (i, <, 50);
1522 else
1523 g_assert_cmpint (i, <, 20);
1524
1525 g_assert_cmpint (i + strspn (buf + i, "X"), ==, NUM_OVERFLOW_TASKS);
1526 }
1527
1528 /* test_return_on_cancel */
1529
1530 GMutex roc_init_mutex, roc_finish_mutex;
1531 GCond roc_init_cond, roc_finish_cond;
1532
1533 typedef enum {
1534 THREAD_STARTING,
1535 THREAD_RUNNING,
1536 THREAD_CANCELLED,
1537 THREAD_COMPLETED
1538 } ThreadState;
1539
1540 static void
1541 return_on_cancel_callback (GObject *object,
1542 GAsyncResult *result,
1543 gpointer user_data)
1544 {
1545 gboolean *callback_ran = user_data;
1546 GError *error = NULL;
1547 gssize ret;
1548
1549 g_assert (g_thread_self () == main_thread);
1550
1551 g_assert (object == NULL);
1552 g_assert (g_task_is_valid (result, object));
1553 g_assert (g_async_result_get_user_data (result) == user_data);
1554 g_assert (g_task_had_error (G_TASK (result)));
1555 g_assert_false (g_task_get_completed (G_TASK (result)));
1556
1557 ret = g_task_propagate_int (G_TASK (result), &error);
1558 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
1559 g_clear_error (&error);
1560 g_assert_cmpint (ret, ==, -1);
1561
1562 g_assert (g_task_had_error (G_TASK (result)));
1563
1564 *callback_ran = TRUE;
1565 g_main_loop_quit (loop);
1566 }
1567
1568 static void
1569 return_on_cancel_thread (GTask *task,
1570 gpointer source_object,
1571 gpointer task_data,
1572 GCancellable *cancellable)
1573 {
1574 ThreadState *state = task_data;
1575
1576 g_assert (source_object == g_task_get_source_object (task));
1577 g_assert (task_data == g_task_get_task_data (task));
1578 g_assert (cancellable == g_task_get_cancellable (task));
1579
1580 g_assert (g_thread_self () != main_thread);
1581
1582 g_mutex_lock (&roc_init_mutex);
1583 *state = THREAD_RUNNING;
1584 g_cond_signal (&roc_init_cond);
1585 g_mutex_unlock (&roc_init_mutex);
1586
1587 g_mutex_lock (&roc_finish_mutex);
1588
1589 if (!g_task_get_return_on_cancel (task) ||
1590 g_task_set_return_on_cancel (task, FALSE))
1591 {
1592 *state = THREAD_COMPLETED;
1593 g_task_return_int (task, magic);
1594 }
1595 else
1596 *state = THREAD_CANCELLED;
1597
1598 g_cond_signal (&roc_finish_cond);
1599 g_mutex_unlock (&roc_finish_mutex);
1600 }
1601
1602 static void
1603 test_return_on_cancel (void)
1604 {
1605 GTask *task;
1606 GCancellable *cancellable;
1607 ThreadState thread_state; /* (atomic) */
1608 gboolean weak_notify_ran = FALSE; /* (atomic) */
1609 gboolean callback_ran;
1610 gboolean notification_emitted = FALSE;
1611
1612 cancellable = g_cancellable_new ();
1613
1614 /* If return-on-cancel is FALSE (default), the task does not return
1615 * early.
1616 */
1617 callback_ran = FALSE;
1618 g_atomic_int_set (&thread_state, THREAD_STARTING);
1619 task = g_task_new (NULL, cancellable, return_on_cancel_callback, &callback_ran);
1620 g_signal_connect (task, "notify::completed",
1621 (GCallback) completed_cb, &notification_emitted);
1622
1623 g_task_set_task_data (task, (gpointer)&thread_state, NULL);
1624 g_mutex_lock (&roc_init_mutex);
1625 g_mutex_lock (&roc_finish_mutex);
1626 g_task_run_in_thread (task, return_on_cancel_thread);
1627 g_object_unref (task);
1628
1629 while (g_atomic_int_get (&thread_state) == THREAD_STARTING)
1630 g_cond_wait (&roc_init_cond, &roc_init_mutex);
1631 g_mutex_unlock (&roc_init_mutex);
1632
1633 g_assert_cmpint (g_atomic_int_get (&thread_state), ==, THREAD_RUNNING);
1634 g_assert (callback_ran == FALSE);
1635
1636 g_cancellable_cancel (cancellable);
1637 g_mutex_unlock (&roc_finish_mutex);
1638 g_main_loop_run (loop);
1639
1640 g_assert_cmpint (g_atomic_int_get (&thread_state), ==, THREAD_COMPLETED);
1641 g_assert (callback_ran == TRUE);
1642 g_assert_true (notification_emitted);
1643
1644 g_cancellable_reset (cancellable);
1645
1646 /* If return-on-cancel is TRUE, it does return early */
1647 callback_ran = FALSE;
1648 notification_emitted = FALSE;
1649 g_atomic_int_set (&thread_state, THREAD_STARTING);
1650 task = g_task_new (NULL, cancellable, return_on_cancel_callback, &callback_ran);
1651 g_object_weak_ref (G_OBJECT (task), task_weak_notify, (gpointer)&weak_notify_ran);
1652 g_signal_connect (task, "notify::completed",
1653 (GCallback) completed_cb, &notification_emitted);
1654 g_task_set_return_on_cancel (task, TRUE);
1655
1656 g_task_set_task_data (task, (gpointer)&thread_state, NULL);
1657 g_mutex_lock (&roc_init_mutex);
1658 g_mutex_lock (&roc_finish_mutex);
1659 g_task_run_in_thread (task, return_on_cancel_thread);
1660 g_object_unref (task);
1661
1662 while (g_atomic_int_get (&thread_state) == THREAD_STARTING)
1663 g_cond_wait (&roc_init_cond, &roc_init_mutex);
1664 g_mutex_unlock (&roc_init_mutex);
1665
1666 g_assert_cmpint (g_atomic_int_get (&thread_state), ==, THREAD_RUNNING);
1667 g_assert (callback_ran == FALSE);
1668
1669 g_cancellable_cancel (cancellable);
1670 g_main_loop_run (loop);
1671 g_assert_cmpint (g_atomic_int_get (&thread_state), ==, THREAD_RUNNING);
1672 g_assert (callback_ran == TRUE);
1673
1674 g_assert_false (g_atomic_int_get (&weak_notify_ran));
1675
1676 while (g_atomic_int_get (&thread_state) == THREAD_RUNNING)
1677 g_cond_wait (&roc_finish_cond, &roc_finish_mutex);
1678 g_mutex_unlock (&roc_finish_mutex);
1679
1680 g_assert_cmpint (g_atomic_int_get (&thread_state), ==, THREAD_CANCELLED);
1681 g_mutex_lock (&run_in_thread_mutex);
1682 while (!g_atomic_int_get (&weak_notify_ran))
1683 g_cond_wait (&run_in_thread_cond, &run_in_thread_mutex);
1684 g_mutex_unlock (&run_in_thread_mutex);
1685
1686 g_assert_true (notification_emitted);
1687 g_cancellable_reset (cancellable);
1688
1689 /* If the task is already cancelled before it starts, it returns
1690 * immediately, but the thread func still runs.
1691 */
1692 callback_ran = FALSE;
1693 notification_emitted = FALSE;
1694 g_atomic_int_set (&thread_state, THREAD_STARTING);
1695 task = g_task_new (NULL, cancellable, return_on_cancel_callback, &callback_ran);
1696 g_signal_connect (task, "notify::completed",
1697 (GCallback) completed_cb, &notification_emitted);
1698 g_task_set_return_on_cancel (task, TRUE);
1699
1700 g_cancellable_cancel (cancellable);
1701
1702 g_task_set_task_data (task, (gpointer)&thread_state, NULL);
1703 g_mutex_lock (&roc_init_mutex);
1704 g_mutex_lock (&roc_finish_mutex);
1705 g_task_run_in_thread (task, return_on_cancel_thread);
1706 g_object_unref (task);
1707
1708 g_main_loop_run (loop);
1709 g_assert (callback_ran == TRUE);
1710
1711 while (g_atomic_int_get (&thread_state) == THREAD_STARTING)
1712 g_cond_wait (&roc_init_cond, &roc_init_mutex);
1713 g_mutex_unlock (&roc_init_mutex);
1714
1715 g_assert_cmpint (g_atomic_int_get (&thread_state), ==, THREAD_RUNNING);
1716
1717 while (g_atomic_int_get (&thread_state) == THREAD_RUNNING)
1718 g_cond_wait (&roc_finish_cond, &roc_finish_mutex);
1719 g_mutex_unlock (&roc_finish_mutex);
1720
1721 g_assert_cmpint (g_atomic_int_get (&thread_state), ==, THREAD_CANCELLED);
1722 g_assert_true (notification_emitted);
1723
1724 g_object_unref (cancellable);
1725 }
1726
1727 /* test_return_on_cancel_sync */
1728
1729 static gpointer
1730 cancel_sync_runner_thread (gpointer task)
1731 {
1732 g_task_run_in_thread_sync (task, return_on_cancel_thread);
1733 return NULL;
1734 }
1735
1736 static void
1737 test_return_on_cancel_sync (void)
1738 {
1739 GTask *task;
1740 GCancellable *cancellable;
1741 ThreadState thread_state; /* (atomic) */
1742 GThread *runner_thread;
1743 gssize ret;
1744 GError *error = NULL;
1745
1746 cancellable = g_cancellable_new ();
1747
1748 /* If return-on-cancel is FALSE, the task does not return early.
1749 */
1750 g_atomic_int_set (&thread_state, THREAD_STARTING);
1751 task = g_task_new (NULL, cancellable, run_in_thread_sync_callback, NULL);
1752
1753 g_task_set_task_data (task, (gpointer)&thread_state, NULL);
1754 g_mutex_lock (&roc_init_mutex);
1755 g_mutex_lock (&roc_finish_mutex);
1756 runner_thread = g_thread_new ("return-on-cancel-sync runner thread",
1757 cancel_sync_runner_thread, task);
1758
1759 while (g_atomic_int_get (&thread_state) == THREAD_STARTING)
1760 g_cond_wait (&roc_init_cond, &roc_init_mutex);
1761 g_mutex_unlock (&roc_init_mutex);
1762
1763 g_assert_cmpint (g_atomic_int_get (&thread_state), ==, THREAD_RUNNING);
1764
1765 g_cancellable_cancel (cancellable);
1766 g_mutex_unlock (&roc_finish_mutex);
1767 g_thread_join (runner_thread);
1768 g_assert_cmpint (g_atomic_int_get (&thread_state), ==, THREAD_COMPLETED);
1769
1770 ret = g_task_propagate_int (task, &error);
1771 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
1772 g_clear_error (&error);
1773 g_assert_cmpint (ret, ==, -1);
1774
1775 g_object_unref (task);
1776
1777 g_cancellable_reset (cancellable);
1778
1779 /* If return-on-cancel is TRUE, it does return early */
1780 g_atomic_int_set (&thread_state, THREAD_STARTING);
1781 task = g_task_new (NULL, cancellable, run_in_thread_sync_callback, NULL);
1782 g_task_set_return_on_cancel (task, TRUE);
1783
1784 g_task_set_task_data (task, (gpointer)&thread_state, NULL);
1785 g_mutex_lock (&roc_init_mutex);
1786 g_mutex_lock (&roc_finish_mutex);
1787 runner_thread = g_thread_new ("return-on-cancel-sync runner thread",
1788 cancel_sync_runner_thread, task);
1789
1790 while (g_atomic_int_get (&thread_state) == THREAD_STARTING)
1791 g_cond_wait (&roc_init_cond, &roc_init_mutex);
1792 g_mutex_unlock (&roc_init_mutex);
1793
1794 g_assert_cmpint (g_atomic_int_get (&thread_state), ==, THREAD_RUNNING);
1795
1796 g_cancellable_cancel (cancellable);
1797 g_thread_join (runner_thread);
1798 g_assert_cmpint (g_atomic_int_get (&thread_state), ==, THREAD_RUNNING);
1799
1800 ret = g_task_propagate_int (task, &error);
1801 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
1802 g_clear_error (&error);
1803 g_assert_cmpint (ret, ==, -1);
1804
1805 g_object_unref (task);
1806
1807 while (g_atomic_int_get (&thread_state) == THREAD_RUNNING)
1808 g_cond_wait (&roc_finish_cond, &roc_finish_mutex);
1809 g_mutex_unlock (&roc_finish_mutex);
1810
1811 g_assert_cmpint (g_atomic_int_get (&thread_state), ==, THREAD_CANCELLED);
1812
1813 g_cancellable_reset (cancellable);
1814
1815 /* If the task is already cancelled before it starts, it returns
1816 * immediately, but the thread func still runs.
1817 */
1818 g_atomic_int_set (&thread_state, THREAD_STARTING);
1819 task = g_task_new (NULL, cancellable, run_in_thread_sync_callback, NULL);
1820 g_task_set_return_on_cancel (task, TRUE);
1821
1822 g_cancellable_cancel (cancellable);
1823
1824 g_task_set_task_data (task, (gpointer)&thread_state, NULL);
1825 g_mutex_lock (&roc_init_mutex);
1826 g_mutex_lock (&roc_finish_mutex);
1827 runner_thread = g_thread_new ("return-on-cancel-sync runner thread",
1828 cancel_sync_runner_thread, task);
1829
1830 g_thread_join (runner_thread);
1831 g_assert_cmpint (g_atomic_int_get (&thread_state), ==, THREAD_STARTING);
1832
1833 ret = g_task_propagate_int (task, &error);
1834 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
1835 g_clear_error (&error);
1836 g_assert_cmpint (ret, ==, -1);
1837
1838 g_object_unref (task);
1839
1840 while (g_atomic_int_get (&thread_state) == THREAD_STARTING)
1841 g_cond_wait (&roc_init_cond, &roc_init_mutex);
1842 g_mutex_unlock (&roc_init_mutex);
1843
1844 g_assert_cmpint (g_atomic_int_get (&thread_state), ==, THREAD_RUNNING);
1845
1846 while (g_atomic_int_get (&thread_state) == THREAD_RUNNING)
1847 g_cond_wait (&roc_finish_cond, &roc_finish_mutex);
1848 g_mutex_unlock (&roc_finish_mutex);
1849
1850 g_assert_cmpint (g_atomic_int_get (&thread_state), ==, THREAD_CANCELLED);
1851
1852 g_object_unref (cancellable);
1853 }
1854
1855 /* test_return_on_cancel_atomic: turning return-on-cancel on/off is
1856 * non-racy
1857 */
1858
1859 GMutex roca_mutex_1, roca_mutex_2;
1860 GCond roca_cond_1, roca_cond_2;
1861
1862 static void
1863 return_on_cancel_atomic_callback (GObject *object,
1864 GAsyncResult *result,
1865 gpointer user_data)
1866 {
1867 gboolean *callback_ran = user_data;
1868 GError *error = NULL;
1869 gssize ret;
1870
1871 g_assert (g_thread_self () == main_thread);
1872
1873 g_assert (object == NULL);
1874 g_assert (g_task_is_valid (result, object));
1875 g_assert (g_async_result_get_user_data (result) == user_data);
1876 g_assert (g_task_had_error (G_TASK (result)));
1877 g_assert_false (g_task_get_completed (G_TASK (result)));
1878
1879 ret = g_task_propagate_int (G_TASK (result), &error);
1880 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
1881 g_clear_error (&error);
1882 g_assert_cmpint (ret, ==, -1);
1883
1884 g_assert (g_task_had_error (G_TASK (result)));
1885
1886 *callback_ran = TRUE;
1887 g_main_loop_quit (loop);
1888 }
1889
1890 static void
1891 return_on_cancel_atomic_thread (GTask *task,
1892 gpointer source_object,
1893 gpointer task_data,
1894 GCancellable *cancellable)
1895 {
1896 gint *state = task_data; /* (atomic) */
1897
1898 g_assert (source_object == g_task_get_source_object (task));
1899 g_assert (task_data == g_task_get_task_data (task));
1900 g_assert (cancellable == g_task_get_cancellable (task));
1901 g_assert_false (g_task_get_completed (task));
1902
1903 g_assert (g_thread_self () != main_thread);
1904 g_assert_cmpint (g_atomic_int_get (state), ==, 0);
1905
1906 g_mutex_lock (&roca_mutex_1);
1907 g_atomic_int_set (state, 1);
1908 g_cond_signal (&roca_cond_1);
1909 g_mutex_unlock (&roca_mutex_1);
1910
1911 g_mutex_lock (&roca_mutex_2);
1912 if (g_task_set_return_on_cancel (task, FALSE))
1913 g_atomic_int_set (state, 2);
1914 else
1915 g_atomic_int_set (state, 3);
1916 g_cond_signal (&roca_cond_2);
1917 g_mutex_unlock (&roca_mutex_2);
1918
1919 g_mutex_lock (&roca_mutex_1);
1920 if (g_task_set_return_on_cancel (task, TRUE))
1921 g_atomic_int_set (state, 4);
1922 else
1923 g_atomic_int_set (state, 5);
1924 g_cond_signal (&roca_cond_1);
1925 g_mutex_unlock (&roca_mutex_1);
1926
1927 g_mutex_lock (&roca_mutex_2);
1928 if (g_task_set_return_on_cancel (task, TRUE))
1929 g_atomic_int_set (state, 6);
1930 else
1931 g_atomic_int_set (state, 7);
1932 g_cond_signal (&roca_cond_2);
1933 g_mutex_unlock (&roca_mutex_2);
1934
1935 g_task_return_int (task, magic);
1936 }
1937
1938 static void
1939 test_return_on_cancel_atomic (void)
1940 {
1941 GTask *task;
1942 GCancellable *cancellable;
1943 gint state; /* (atomic) */
1944 gboolean notification_emitted = FALSE;
1945 gboolean callback_ran;
1946
1947 cancellable = g_cancellable_new ();
1948 g_mutex_lock (&roca_mutex_1);
1949 g_mutex_lock (&roca_mutex_2);
1950
1951 /* If we don't cancel it, each set_return_on_cancel() call will succeed */
1952 g_atomic_int_set (&state, 0);
1953 callback_ran = FALSE;
1954 task = g_task_new (NULL, cancellable, return_on_cancel_atomic_callback, &callback_ran);
1955 g_task_set_return_on_cancel (task, TRUE);
1956 g_signal_connect (task, "notify::completed",
1957 (GCallback) completed_cb, &notification_emitted);
1958
1959 g_task_set_task_data (task, (gpointer)&state, NULL);
1960 g_task_run_in_thread (task, return_on_cancel_atomic_thread);
1961 g_object_unref (task);
1962
1963 g_assert_cmpint (g_atomic_int_get (&state), ==, 0);
1964
1965 while (g_atomic_int_get (&state) == 0)
1966 g_cond_wait (&roca_cond_1, &roca_mutex_1);
1967 g_assert_cmpint (g_atomic_int_get (&state), ==, 1);
1968
1969 while (g_atomic_int_get (&state) == 1)
1970 g_cond_wait (&roca_cond_2, &roca_mutex_2);
1971 g_assert_cmpint (g_atomic_int_get (&state), ==, 2);
1972
1973 while (g_atomic_int_get (&state) == 2)
1974 g_cond_wait (&roca_cond_1, &roca_mutex_1);
1975 g_assert_cmpint (g_atomic_int_get (&state), ==, 4);
1976
1977 while (g_atomic_int_get (&state) == 4)
1978 g_cond_wait (&roca_cond_2, &roca_mutex_2);
1979 g_assert_cmpint (g_atomic_int_get (&state), ==, 6);
1980
1981 /* callback assumes there'll be a cancelled error */
1982 g_cancellable_cancel (cancellable);
1983
1984 g_assert (callback_ran == FALSE);
1985 g_main_loop_run (loop);
1986 g_assert (callback_ran == TRUE);
1987 g_assert_true (notification_emitted);
1988
1989 g_cancellable_reset (cancellable);
1990
1991
1992 /* If we cancel while it's temporarily not return-on-cancel, the
1993 * task won't complete right away, and further
1994 * g_task_set_return_on_cancel() calls will return FALSE.
1995 */
1996 g_atomic_int_set (&state, 0);
1997 callback_ran = FALSE;
1998 notification_emitted = FALSE;
1999 task = g_task_new (NULL, cancellable, return_on_cancel_atomic_callback, &callback_ran);
2000 g_task_set_return_on_cancel (task, TRUE);
2001 g_signal_connect (task, "notify::completed",
2002 (GCallback) completed_cb, &notification_emitted);
2003
2004 g_task_set_task_data (task, (gpointer)&state, NULL);
2005 g_task_run_in_thread (task, return_on_cancel_atomic_thread);
2006
2007 g_assert_cmpint (g_atomic_int_get (&state), ==, 0);
2008
2009 while (g_atomic_int_get (&state) == 0)
2010 g_cond_wait (&roca_cond_1, &roca_mutex_1);
2011 g_assert_cmpint (g_atomic_int_get (&state), ==, 1);
2012 g_assert (g_task_get_return_on_cancel (task));
2013
2014 while (g_atomic_int_get (&state) == 1)
2015 g_cond_wait (&roca_cond_2, &roca_mutex_2);
2016 g_assert_cmpint (g_atomic_int_get (&state), ==, 2);
2017 g_assert (!g_task_get_return_on_cancel (task));
2018
2019 g_cancellable_cancel (cancellable);
2020 g_idle_add (idle_quit_loop, NULL);
2021 g_main_loop_run (loop);
2022 g_assert (callback_ran == FALSE);
2023
2024 while (g_atomic_int_get (&state) == 2)
2025 g_cond_wait (&roca_cond_1, &roca_mutex_1);
2026 g_assert_cmpint (g_atomic_int_get (&state), ==, 5);
2027 g_assert (!g_task_get_return_on_cancel (task));
2028
2029 g_main_loop_run (loop);
2030 g_assert (callback_ran == TRUE);
2031 g_assert_true (notification_emitted);
2032
2033 while (g_atomic_int_get (&state) == 5)
2034 g_cond_wait (&roca_cond_2, &roca_mutex_2);
2035 g_assert_cmpint (g_atomic_int_get (&state), ==, 7);
2036
2037 g_object_unref (cancellable);
2038 g_mutex_unlock (&roca_mutex_1);
2039 g_mutex_unlock (&roca_mutex_2);
2040 g_object_unref (task);
2041 }
2042
2043 /* test_return_pointer: memory management of pointer returns */
2044
2045 static void
2046 test_return_pointer (void)
2047 {
2048 GObject *object, *ret;
2049 GTask *task;
2050 GCancellable *cancellable;
2051 GError *error = NULL;
2052
2053 /* If we don't read back the return value, the task will
2054 * run its destroy notify.
2055 */
2056 object = (GObject *)g_dummy_object_new ();
2057 g_assert_cmpint (object->ref_count, ==, 1);
2058 g_object_add_weak_pointer (object, (gpointer *)&object);
2059
2060 task = g_task_new (NULL, NULL, NULL, NULL);
2061 g_object_add_weak_pointer (G_OBJECT (task), (gpointer *)&task);
2062 g_task_return_pointer (task, object, g_object_unref);
2063 g_assert_cmpint (object->ref_count, ==, 1);
2064
2065 /* Task and object are reffed until the :completed notification in idle. */
2066 g_object_unref (task);
2067 g_assert_nonnull (task);
2068 g_assert_nonnull (object);
2069
2070 wait_for_completed_notification (task);
2071
2072 g_assert_null (task);
2073 g_assert_null (object);
2074
2075 /* Likewise, if the return value is overwritten by an error */
2076 object = (GObject *)g_dummy_object_new ();
2077 g_assert_cmpint (object->ref_count, ==, 1);
2078 g_object_add_weak_pointer (object, (gpointer *)&object);
2079
2080 cancellable = g_cancellable_new ();
2081 task = g_task_new (NULL, cancellable, NULL, NULL);
2082 g_object_add_weak_pointer (G_OBJECT (task), (gpointer *)&task);
2083 g_task_return_pointer (task, object, g_object_unref);
2084 g_assert_cmpint (object->ref_count, ==, 1);
2085 g_cancellable_cancel (cancellable);
2086 g_assert_cmpint (object->ref_count, ==, 1);
2087
2088 ret = g_task_propagate_pointer (task, &error);
2089 g_assert (ret == NULL);
2090 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
2091 g_clear_error (&error);
2092 g_assert_cmpint (object->ref_count, ==, 1);
2093
2094 g_object_unref (task);
2095 g_object_unref (cancellable);
2096 g_assert_nonnull (task);
2097 g_assert_nonnull (object);
2098
2099 wait_for_completed_notification (task);
2100
2101 g_assert_null (task);
2102 g_assert_null (object);
2103
2104 /* If we read back the return value, we steal its ref */
2105 object = (GObject *)g_dummy_object_new ();
2106 g_assert_cmpint (object->ref_count, ==, 1);
2107 g_object_add_weak_pointer (object, (gpointer *)&object);
2108
2109 task = g_task_new (NULL, NULL, NULL, NULL);
2110 g_object_add_weak_pointer (G_OBJECT (task), (gpointer *)&task);
2111 g_task_return_pointer (task, object, g_object_unref);
2112 g_assert_cmpint (object->ref_count, ==, 1);
2113
2114 ret = g_task_propagate_pointer (task, &error);
2115 g_assert_no_error (error);
2116 g_assert (ret == object);
2117 g_assert_cmpint (object->ref_count, ==, 1);
2118
2119 g_object_unref (task);
2120 g_assert_nonnull (task);
2121 g_assert_cmpint (object->ref_count, ==, 1);
2122 g_object_unref (object);
2123 g_assert (object == NULL);
2124
2125 wait_for_completed_notification (task);
2126 g_assert_null (task);
2127 }
2128
2129 static void
2130 test_return_value (void)
2131 {
2132 GObject *object;
2133 GValue value = G_VALUE_INIT;
2134 GValue ret = G_VALUE_INIT;
2135 GTask *task;
2136 GError *error = NULL;
2137
2138 object = (GObject *)g_dummy_object_new ();
2139 g_assert_cmpint (object->ref_count, ==, 1);
2140 g_object_add_weak_pointer (object, (gpointer *)&object);
2141
2142 g_value_init (&value, G_TYPE_OBJECT);
2143 g_value_set_object (&value, object);
2144 g_assert_cmpint (object->ref_count, ==, 2);
2145
2146 task = g_task_new (NULL, NULL, NULL, NULL);
2147 g_object_add_weak_pointer (G_OBJECT (task), (gpointer *)&task);
2148 g_task_return_value (task, &value);
2149 g_assert_cmpint (object->ref_count, ==, 3);
2150
2151 g_assert_true (g_task_propagate_value (task, &ret, &error));
2152 g_assert_no_error (error);
2153 g_assert_true (g_value_get_object (&ret) == object);
2154 g_assert_cmpint (object->ref_count, ==, 3);
2155
2156 g_object_unref (task);
2157 g_assert_nonnull (task);
2158 wait_for_completed_notification (task);
2159 g_assert_null (task);
2160
2161 g_assert_cmpint (object->ref_count, ==, 3);
2162 g_value_unset (&ret);
2163 g_assert_cmpint (object->ref_count, ==, 2);
2164 g_value_unset (&value);
2165 g_assert_cmpint (object->ref_count, ==, 1);
2166 g_object_unref (object);
2167 g_assert_null (object);
2168 }
2169
2170 static void
2171 test_return_prefixed_error (void)
2172 {
2173 GTask *task;
2174 GError *original_error = NULL;
2175 GError *error = NULL;
2176
2177 g_set_error (&original_error, G_IO_ERROR, G_IO_ERROR_UNKNOWN, "oh no!");
2178
2179 task = g_task_new (NULL, NULL, NULL, NULL);
2180 g_task_return_prefixed_error (task, original_error, "task %s: ", "failed");
2181
2182 wait_for_completed_notification (task);
2183
2184 g_assert_null (g_task_propagate_pointer (task, &error));
2185 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_UNKNOWN);
2186 g_assert_cmpstr (error->message, ==, "task failed: oh no!");
2187
2188 g_assert_finalize_object (task);
2189 g_clear_error (&error);
2190 }
2191
2192 /* test_object_keepalive: GTask takes a ref on its source object */
2193
2194 static GObject *keepalive_object;
2195
2196 static void
2197 keepalive_callback (GObject *object,
2198 GAsyncResult *result,
2199 gpointer user_data)
2200 {
2201 gssize *result_out = user_data;
2202 GError *error = NULL;
2203
2204 g_assert (object == keepalive_object);
2205 g_assert (g_task_is_valid (result, object));
2206 g_assert (g_async_result_get_user_data (result) == user_data);
2207 g_assert (!g_task_had_error (G_TASK (result)));
2208 g_assert_false (g_task_get_completed (G_TASK (result)));
2209
2210 *result_out = g_task_propagate_int (G_TASK (result), &error);
2211 g_assert_no_error (error);
2212
2213 g_assert (!g_task_had_error (G_TASK (result)));
2214
2215 g_main_loop_quit (loop);
2216 }
2217
2218 static void
2219 test_object_keepalive (void)
2220 {
2221 GObject *object;
2222 GTask *task;
2223 gssize result;
2224 int ref_count;
2225 gboolean notification_emitted = FALSE;
2226
2227 keepalive_object = object = (GObject *)g_dummy_object_new ();
2228 g_object_add_weak_pointer (object, (gpointer *)&object);
2229
2230 task = g_task_new (object, NULL, keepalive_callback, &result);
2231 g_object_add_weak_pointer (G_OBJECT (task), (gpointer *)&task);
2232 g_signal_connect (task, "notify::completed",
2233 (GCallback) completed_cb, &notification_emitted);
2234
2235 ref_count = object->ref_count;
2236 g_assert_cmpint (ref_count, >, 1);
2237
2238 g_assert (g_task_get_source_object (task) == object);
2239 g_assert (g_async_result_get_source_object (G_ASYNC_RESULT (task)) == object);
2240 g_assert_cmpint (object->ref_count, ==, ref_count + 1);
2241 g_object_unref (object);
2242
2243 g_object_unref (object);
2244 g_assert (object != NULL);
2245
2246 g_task_return_int (task, magic);
2247 g_main_loop_run (loop);
2248
2249 g_assert (object != NULL);
2250 g_assert_cmpint (result, ==, magic);
2251 g_assert_true (notification_emitted);
2252
2253 g_object_unref (task);
2254 g_assert (task == NULL);
2255 g_assert (object == NULL);
2256 }
2257
2258 /* test_legacy_error: legacy GSimpleAsyncResult handling */
2259 static void test_legacy_error (void);
2260
2261 static void
2262 legacy_error_callback (GObject *object,
2263 GAsyncResult *result,
2264 gpointer user_data)
2265 {
2266 gssize *result_out = user_data;
2267 GError *error = NULL;
2268
2269 g_assert (object == NULL);
2270 g_assert (g_async_result_is_tagged (result, test_legacy_error));
2271 g_assert (g_async_result_get_user_data (result) == user_data);
2272
2273 if (g_async_result_legacy_propagate_error (result, &error))
2274 {
2275 g_assert (!g_task_is_valid (result, object));
2276 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
2277 g_assert (g_simple_async_result_is_valid (result, object, test_legacy_error));
2278 G_GNUC_END_IGNORE_DEPRECATIONS;
2279
2280 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
2281 *result_out = -2;
2282 g_clear_error (&error);
2283 }
2284 else
2285 {
2286 g_assert (g_task_is_valid (result, object));
2287
2288 *result_out = g_task_propagate_int (G_TASK (result), NULL);
2289 /* Might be error, might not */
2290 }
2291
2292 g_main_loop_quit (loop);
2293 }
2294
2295 static gboolean
2296 legacy_error_return (gpointer user_data)
2297 {
2298 if (G_IS_TASK (user_data))
2299 {
2300 GTask *task = user_data;
2301
2302 g_task_return_int (task, magic);
2303 g_object_unref (task);
2304 }
2305 else
2306 {
2307 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
2308
2309 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
2310 g_simple_async_result_set_error (simple,
2311 G_IO_ERROR,
2312 G_IO_ERROR_FAILED,
2313 "Failed");
2314 g_simple_async_result_complete (simple);
2315 G_GNUC_END_IGNORE_DEPRECATIONS;
2316 g_object_unref (simple);
2317 }
2318
2319 return FALSE;
2320 }
2321
2322 static void
2323 test_legacy_error (void)
2324 {
2325 GTask *task;
2326 GSimpleAsyncResult *simple;
2327 gssize result;
2328
2329 /* GTask success */
2330 task = g_task_new (NULL, NULL, legacy_error_callback, &result);
2331 g_task_set_source_tag (task, test_legacy_error);
2332 g_object_add_weak_pointer (G_OBJECT (task), (gpointer *)&task);
2333
2334 g_idle_add (legacy_error_return, task);
2335 g_main_loop_run (loop);
2336
2337 g_assert_cmpint (result, ==, magic);
2338 g_assert (task == NULL);
2339
2340 /* GTask error */
2341 task = g_task_new (NULL, NULL, legacy_error_callback, &result);
2342 g_task_set_source_tag (task, test_legacy_error);
2343 g_object_add_weak_pointer (G_OBJECT (task), (gpointer *)&task);
2344
2345 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED,
2346 "Failed");
2347 g_object_unref (task);
2348 g_main_loop_run (loop);
2349
2350 g_assert_cmpint (result, ==, -1);
2351 g_assert (task == NULL);
2352
2353 /* GSimpleAsyncResult error */
2354 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
2355 simple = g_simple_async_result_new (NULL, legacy_error_callback, &result,
2356 test_legacy_error);
2357 G_GNUC_END_IGNORE_DEPRECATIONS;
2358 g_object_add_weak_pointer (G_OBJECT (simple), (gpointer *)&simple);
2359
2360 g_idle_add (legacy_error_return, simple);
2361 g_main_loop_run (loop);
2362
2363 g_assert_cmpint (result, ==, -2);
2364 g_assert (simple == NULL);
2365 }
2366
2367 /* Various helper functions for the return tests below. */
2368 static void
2369 task_complete_cb (GObject *source,
2370 GAsyncResult *result,
2371 gpointer user_data)
2372 {
2373 GTask *task = G_TASK (result);
2374 guint *calls = user_data;
2375
2376 g_assert_cmpint (++*calls, <=, 1);
2377
2378 /* Propagate the result, so it’s removed from the task’s internal state. */
2379 g_task_propagate_boolean (task, NULL);
2380 }
2381
2382 static void
2383 return_twice (GTask *task)
2384 {
2385 gboolean error_first = GPOINTER_TO_UINT (g_task_get_task_data (task));
2386
2387 if (error_first)
2388 {
2389 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_UNKNOWN, "oh no");
2390 g_task_return_boolean (task, TRUE);
2391 }
2392 else
2393 {
2394 g_task_return_boolean (task, TRUE);
2395 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_UNKNOWN, "oh no");
2396 }
2397 }
2398
2399 static gboolean
2400 idle_cb (gpointer user_data)
2401 {
2402 GTask *task = user_data;
2403 return_twice (task);
2404 g_object_unref (task);
2405
2406 return G_SOURCE_REMOVE;
2407 }
2408
2409 static void
2410 test_return_permutation (gboolean error_first,
2411 gboolean return_in_idle)
2412 {
2413 guint calls = 0;
2414 GTask *task = NULL;
2415
2416 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/issues/1525");
2417
2418 task = g_task_new (NULL, NULL, task_complete_cb, &calls);
2419 g_task_set_task_data (task, GUINT_TO_POINTER (error_first), NULL);
2420
2421 if (return_in_idle)
2422 g_idle_add (idle_cb, g_object_ref (task));
2423 else
2424 return_twice (task);
2425
2426 while (calls == 0)
2427 g_main_context_iteration (NULL, TRUE);
2428
2429 g_assert_cmpint (calls, ==, 1);
2430
2431 g_object_unref (task);
2432 }
2433
2434 /* Test that calling g_task_return_boolean() after g_task_return_error(), when
2435 * returning in an idle callback, correctly results in a critical warning. */
2436 static void
2437 test_return_in_idle_error_first (void)
2438 {
2439 if (g_test_subprocess ())
2440 {
2441 test_return_permutation (TRUE, TRUE);
2442 return;
2443 }
2444
2445 g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
2446 g_test_trap_assert_failed ();
2447 g_test_trap_assert_stderr ("*CRITICAL*assertion '!task->ever_returned' failed*");
2448 }
2449
2450 /* Test that calling g_task_return_error() after g_task_return_boolean(), when
2451 * returning in an idle callback, correctly results in a critical warning. */
2452 static void
2453 test_return_in_idle_value_first (void)
2454 {
2455 if (g_test_subprocess ())
2456 {
2457 test_return_permutation (FALSE, TRUE);
2458 return;
2459 }
2460
2461 g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
2462 g_test_trap_assert_failed ();
2463 g_test_trap_assert_stderr ("*CRITICAL*assertion '!task->ever_returned' failed*");
2464 }
2465
2466 /* Test that calling g_task_return_boolean() after g_task_return_error(), when
2467 * returning synchronously, correctly results in a critical warning. */
2468 static void
2469 test_return_error_first (void)
2470 {
2471 if (g_test_subprocess ())
2472 {
2473 test_return_permutation (TRUE, FALSE);
2474 return;
2475 }
2476
2477 g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
2478 g_test_trap_assert_failed ();
2479 g_test_trap_assert_stderr ("*CRITICAL*assertion '!task->ever_returned' failed*");
2480 }
2481
2482 /* Test that calling g_task_return_error() after g_task_return_boolean(), when
2483 * returning synchronously, correctly results in a critical warning. */
2484 static void
2485 test_return_value_first (void)
2486 {
2487 if (g_test_subprocess ())
2488 {
2489 test_return_permutation (FALSE, FALSE);
2490 return;
2491 }
2492
2493 g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
2494 g_test_trap_assert_failed ();
2495 g_test_trap_assert_stderr ("*CRITICAL*assertion '!task->ever_returned' failed*");
2496 }
2497
2498 static gboolean
2499 source_cb (gpointer user_data)
2500 {
2501 return G_SOURCE_REMOVE;
2502 }
2503
2504 static void
2505 test_attach_source_set_name (void)
2506 {
2507 guint calls = 0;
2508 GTask *task = NULL;
2509 GSource *source = NULL;
2510 GSourceFuncs source_funcs = { NULL, NULL, NULL, NULL, NULL, NULL };
2511
2512 g_test_summary ("Test that attaching a source to a task will set the source’s name if unset");
2513
2514 task = g_task_new (NULL, NULL, task_complete_cb, &calls);
2515 g_task_set_name (task, "test name");
2516
2517 source = g_source_new (&source_funcs, sizeof (GSource));
2518 g_task_attach_source (task, source, source_cb);
2519 g_assert_cmpstr (g_source_get_name (source), ==, "test name");
2520 g_source_unref (source);
2521
2522 source = g_source_new (&source_funcs, sizeof (GSource));
2523 g_source_set_name (source, "not the task name");
2524 g_task_attach_source (task, source, source_cb);
2525 g_assert_cmpstr (g_source_get_name (source), ==, "not the task name");
2526 g_source_unref (source);
2527
2528 g_object_unref (task);
2529 }
2530
2531 static void
2532 test_finalize_without_return (void)
2533 {
2534 GTask *task = NULL;
2535 guint n_calls = 0;
2536
2537 /* With a callback set. */
2538 task = g_task_new (NULL, NULL, task_complete_cb, &n_calls);
2539
2540 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2541 "GTask * (source object: *, source tag: *) finalized without "
2542 "ever returning (using g_task_return_*()). This potentially "
2543 "indicates a bug in the program.");
2544 g_object_unref (task);
2545 g_test_assert_expected_messages ();
2546
2547 /* With a callback and task name set. */
2548 task = g_task_new (NULL, NULL, task_complete_cb, &n_calls);
2549 g_task_set_static_name (task, "oogly boogly");
2550
2551 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2552 "GTask oogly boogly (source object: *, source tag: *) finalized without "
2553 "ever returning (using g_task_return_*()). This potentially "
2554 "indicates a bug in the program.");
2555 g_object_unref (task);
2556 g_test_assert_expected_messages ();
2557
2558 /* Without a callback set. */
2559 task = g_task_new (NULL, NULL, NULL, NULL);
2560
2561 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
2562 "GTask * (source object: *, source tag: *) finalized without "
2563 "ever returning (using g_task_return_*()). This potentially "
2564 "indicates a bug in the program.");
2565 g_object_unref (task);
2566 g_test_assert_expected_messages ();
2567 }
2568
2569 int
2570 main (int argc, char **argv)
2571 {
2572 int ret;
2573
2574 g_test_init (&argc, &argv, NULL);
2575
2576 loop = g_main_loop_new (NULL, FALSE);
2577 main_thread = g_thread_self ();
2578 magic = g_get_monotonic_time ();
2579
2580 g_test_add_func ("/gtask/basic", test_basic);
2581 g_test_add_func ("/gtask/error", test_error);
2582 g_test_add_func ("/gtask/error-literal", test_error_literal);
2583 g_test_add_func ("/gtask/error-literal-from-variable", test_error_literal_from_variable);
2584 g_test_add_func ("/gtask/return-from-same-iteration", test_return_from_same_iteration);
2585 g_test_add_func ("/gtask/return-from-toplevel", test_return_from_toplevel);
2586 g_test_add_func ("/gtask/return-from-anon-thread", test_return_from_anon_thread);
2587 g_test_add_func ("/gtask/return-from-wrong-thread", test_return_from_wrong_thread);
2588 g_test_add_func ("/gtask/no-callback", test_no_callback);
2589 g_test_add_func ("/gtask/report-error", test_report_error);
2590 g_test_add_func ("/gtask/priority", test_priority);
2591 g_test_add_func ("/gtask/name", test_name);
2592 g_test_add_func ("/gtask/name/macro-wrapper", test_name_macro_wrapper);
2593 g_test_add_func ("/gtask/static-name", test_static_name);
2594 g_test_add_func ("/gtask/asynchronous-cancellation", test_asynchronous_cancellation);
2595 g_test_add_func ("/gtask/check-cancellable", test_check_cancellable);
2596 g_test_add_func ("/gtask/return-if-cancelled", test_return_if_cancelled);
2597 g_test_add_func ("/gtask/run-in-thread", test_run_in_thread);
2598 g_test_add_func ("/gtask/run-in-thread-sync", test_run_in_thread_sync);
2599 g_test_add_func ("/gtask/run-in-thread-priority", test_run_in_thread_priority);
2600 g_test_add_func ("/gtask/run-in-thread-nested", test_run_in_thread_nested);
2601 g_test_add_func ("/gtask/run-in-thread-overflow", test_run_in_thread_overflow);
2602 g_test_add_func ("/gtask/return-on-cancel", test_return_on_cancel);
2603 g_test_add_func ("/gtask/return-on-cancel-sync", test_return_on_cancel_sync);
2604 g_test_add_func ("/gtask/return-on-cancel-atomic", test_return_on_cancel_atomic);
2605 g_test_add_func ("/gtask/return-pointer", test_return_pointer);
2606 g_test_add_func ("/gtask/return-value", test_return_value);
2607 g_test_add_func ("/gtask/return-prefixed-error", test_return_prefixed_error);
2608 g_test_add_func ("/gtask/object-keepalive", test_object_keepalive);
2609 g_test_add_func ("/gtask/legacy-error", test_legacy_error);
2610 g_test_add_func ("/gtask/return/in-idle/error-first", test_return_in_idle_error_first);
2611 g_test_add_func ("/gtask/return/in-idle/value-first", test_return_in_idle_value_first);
2612 g_test_add_func ("/gtask/return/error-first", test_return_error_first);
2613 g_test_add_func ("/gtask/return/value-first", test_return_value_first);
2614 g_test_add_func ("/gtask/attach-source/set-name", test_attach_source_set_name);
2615 g_test_add_func ("/gtask/finalize-without-return", test_finalize_without_return);
2616
2617 ret = g_test_run();
2618
2619 g_main_loop_unref (loop);
2620
2621 return ret;
2622 }