Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 1 | From ea746c79faf554d980c21b0e4381753e003d2dc6 Mon Sep 17 00:00:00 2001 |
| 2 | From: Philip Withnall <pwithnall@endlessos.org> |
| 3 | Date: Wed, 11 Nov 2020 18:17:23 +0000 |
| 4 | Subject: [PATCH 03/29] tests: Fix non-atomic access to a shared variable |
| 5 | MIME-Version: 1.0 |
| 6 | Content-Type: text/plain; charset=UTF-8 |
| 7 | Content-Transfer-Encoding: 8bit |
| 8 | |
| 9 | And drop the `volatile` qualifier from the variable, as that doesn’t |
| 10 | help with thread safety. |
| 11 | |
| 12 | Signed-off-by: Philip Withnall <pwithnall@endlessos.org> |
| 13 | |
| 14 | Helps: #600 |
| 15 | Upstream-Status: Backport [https://gitlab.gnome.org/GNOME/glib/-/merge_requests/1719] |
| 16 | --- |
| 17 | glib/tests/mainloop.c | 20 ++++++++++---------- |
| 18 | 1 file changed, 10 insertions(+), 10 deletions(-) |
| 19 | |
| 20 | diff --git a/glib/tests/mainloop.c b/glib/tests/mainloop.c |
| 21 | index 16763a0ea..563a951de 100644 |
| 22 | --- a/glib/tests/mainloop.c |
| 23 | +++ b/glib/tests/mainloop.c |
| 24 | @@ -918,7 +918,7 @@ test_mainloop_overflow (void) |
| 25 | g_main_context_unref (ctx); |
| 26 | } |
| 27 | |
| 28 | -static volatile gint ready_time_dispatched; |
| 29 | +static gint ready_time_dispatched; /* (atomic) */ |
| 30 | |
| 31 | static gboolean |
| 32 | ready_time_dispatch (GSource *source, |
| 33 | @@ -964,7 +964,7 @@ test_ready_time (void) |
| 34 | /* A source with no ready time set should not fire */ |
| 35 | g_assert_cmpint (g_source_get_ready_time (source), ==, -1); |
| 36 | while (g_main_context_iteration (NULL, FALSE)); |
| 37 | - g_assert_false (ready_time_dispatched); |
| 38 | + g_assert_false (g_atomic_int_get (&ready_time_dispatched)); |
| 39 | |
| 40 | /* The ready time should not have been changed */ |
| 41 | g_assert_cmpint (g_source_get_ready_time (source), ==, -1); |
| 42 | @@ -978,37 +978,37 @@ test_ready_time (void) |
| 43 | */ |
| 44 | g_source_set_ready_time (source, g_get_monotonic_time () + G_TIME_SPAN_DAY); |
| 45 | while (g_main_context_iteration (NULL, FALSE)); |
| 46 | - g_assert_false (ready_time_dispatched); |
| 47 | + g_assert_false (g_atomic_int_get (&ready_time_dispatched)); |
| 48 | /* Make sure it didn't get reset */ |
| 49 | g_assert_cmpint (g_source_get_ready_time (source), !=, -1); |
| 50 | |
| 51 | /* Ready time of -1 -> don't fire */ |
| 52 | g_source_set_ready_time (source, -1); |
| 53 | while (g_main_context_iteration (NULL, FALSE)); |
| 54 | - g_assert_false (ready_time_dispatched); |
| 55 | + g_assert_false (g_atomic_int_get (&ready_time_dispatched)); |
| 56 | /* Not reset, but should still be -1 from above */ |
| 57 | g_assert_cmpint (g_source_get_ready_time (source), ==, -1); |
| 58 | |
| 59 | /* A ready time of the current time should fire immediately */ |
| 60 | g_source_set_ready_time (source, g_get_monotonic_time ()); |
| 61 | while (g_main_context_iteration (NULL, FALSE)); |
| 62 | - g_assert_true (ready_time_dispatched); |
| 63 | - ready_time_dispatched = FALSE; |
| 64 | + g_assert_true (g_atomic_int_get (&ready_time_dispatched)); |
| 65 | + g_atomic_int_set (&ready_time_dispatched, FALSE); |
| 66 | /* Should have gotten reset by the handler function */ |
| 67 | g_assert_cmpint (g_source_get_ready_time (source), ==, -1); |
| 68 | |
| 69 | /* As well as one in the recent past... */ |
| 70 | g_source_set_ready_time (source, g_get_monotonic_time () - G_TIME_SPAN_SECOND); |
| 71 | while (g_main_context_iteration (NULL, FALSE)); |
| 72 | - g_assert_true (ready_time_dispatched); |
| 73 | - ready_time_dispatched = FALSE; |
| 74 | + g_assert_true (g_atomic_int_get (&ready_time_dispatched)); |
| 75 | + g_atomic_int_set (&ready_time_dispatched, FALSE); |
| 76 | g_assert_cmpint (g_source_get_ready_time (source), ==, -1); |
| 77 | |
| 78 | /* Zero is the 'official' way to get a source to fire immediately */ |
| 79 | g_source_set_ready_time (source, 0); |
| 80 | while (g_main_context_iteration (NULL, FALSE)); |
| 81 | - g_assert_true (ready_time_dispatched); |
| 82 | - ready_time_dispatched = FALSE; |
| 83 | + g_assert_true (g_atomic_int_get (&ready_time_dispatched)); |
| 84 | + g_atomic_int_set (&ready_time_dispatched, FALSE); |
| 85 | g_assert_cmpint (g_source_get_ready_time (source), ==, -1); |
| 86 | |
| 87 | /* Now do some tests of cross-thread wakeups. |
| 88 | -- |
| 89 | 2.30.1 |
| 90 | |