[glib: 12/30] tests: Fix non-atomic access to some shared variables




commit 1a7f0002a052725fb646e136fadd5dad66222d7f
Author: Philip Withnall <pwithnall endlessos org>
Date:   Wed Nov 11 18:31:01 2020 +0000

    tests: Fix non-atomic access to some shared variables
    
    And drop the `volatile` qualifier from the variables, as that doesn’t
    help with thread safety.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>
    
    Helps: #600

 tests/refcount/objects.c     | 8 ++++----
 tests/refcount/properties3.c | 8 ++++----
 2 files changed, 8 insertions(+), 8 deletions(-)
---
diff --git a/tests/refcount/objects.c b/tests/refcount/objects.c
index 963766d00..0c471a42b 100644
--- a/tests/refcount/objects.c
+++ b/tests/refcount/objects.c
@@ -26,7 +26,7 @@ struct _GTestClass
 };
 
 static GType my_test_get_type (void);
-static volatile gboolean stopping;
+static gint stopping;  /* (atomic) */
 
 static void my_test_class_init (GTestClass * klass);
 static void my_test_init (GTest * test);
@@ -101,7 +101,7 @@ run_thread (GTest * test)
 {
   gint i = 1;
 
-  while (!stopping) {
+  while (!g_atomic_int_get (&stopping)) {
     my_test_do_refcount (test);
     if ((i++ % 10000) == 0) {
       g_print (".");
@@ -128,7 +128,7 @@ main (int argc, char **argv)
 
   test_threads = g_array_new (FALSE, FALSE, sizeof (GThread *));
 
-  stopping = FALSE;
+  g_atomic_int_set (&stopping, 0);
 
   for (i = 0; i < n_threads; i++) {
     GThread *thread;
@@ -141,7 +141,7 @@ main (int argc, char **argv)
   }
   g_usleep (5000000);
 
-  stopping = TRUE;
+  g_atomic_int_set (&stopping, 1);
 
   g_print ("\nstopping\n");
 
diff --git a/tests/refcount/properties3.c b/tests/refcount/properties3.c
index bc8820661..31f26a46e 100644
--- a/tests/refcount/properties3.c
+++ b/tests/refcount/properties3.c
@@ -34,7 +34,7 @@ struct _GTestClass
 static GType my_test_get_type (void);
 G_DEFINE_TYPE (GTest, my_test, G_TYPE_OBJECT)
 
-static volatile gboolean stopping;
+static gint stopping;  /* (atomic) */
 
 static void my_test_get_property (GObject    *object,
                                  guint       prop_id,
@@ -140,7 +140,7 @@ run_thread (GTest * test)
 {
   gint i = 1;
 
-  while (!stopping) {
+  while (!g_atomic_int_get (&stopping)) {
     my_test_do_property (test);
     if ((i++ % 10000) == 0)
       {
@@ -170,7 +170,7 @@ main (int argc, char **argv)
 
   test_threads = g_array_new (FALSE, FALSE, sizeof (GThread *));
 
-  stopping = FALSE;
+  g_atomic_int_set (&stopping, 0);
 
   for (i = 0; i < n_threads; i++) {
     GThread *thread;
@@ -180,7 +180,7 @@ main (int argc, char **argv)
   }
   g_usleep (30000000);
 
-  stopping = TRUE;
+  g_atomic_int_set (&stopping, 1);
   g_print ("\nstopping\n");
 
   /* join all threads */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]