[gjs/wip/3v1n0/toggle-queue-tests: 9/15] context: Ensure we check atomically if we're destroying from other threads




commit e9a9c0f59e15c1d421d5bc6b52aa1781aadb9388
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Wed Apr 28 19:33:06 2021 +0200

    context: Ensure we check atomically if we're destroying from other threads
    
    At each toggle notification coming from another thread we check if
    we are destroying, however this value is set from the main thread (when
    the final gc starts), so we have to access to this atomically.
    
    Use std::atomic, and make ThreadSanitizer happy.

 gjs/context-private.h | 5 +++--
 gjs/context.cpp       | 3 ++-
 2 files changed, 5 insertions(+), 3 deletions(-)
---
diff --git a/gjs/context-private.h b/gjs/context-private.h
index b0746192..3a771719 100644
--- a/gjs/context-private.h
+++ b/gjs/context-private.h
@@ -10,6 +10,7 @@
 #include <stdint.h>
 #include <sys/types.h>  // for ssize_t
 
+#include <atomic>
 #include <string>
 #include <type_traits>  // for is_same
 #include <unordered_map>
@@ -119,7 +120,7 @@ class GjsContextPrivate : public JS::JobQueue {
     uint8_t m_exit_code;
 
     /* flags */
-    bool m_destroying : 1;
+    std::atomic_bool m_destroying = ATOMIC_VAR_INIT(false);
     bool m_in_gc_sweep : 1;
     bool m_should_exit : 1;
     bool m_force_gc : 1;
@@ -181,7 +182,7 @@ class GjsContextPrivate : public JS::JobQueue {
     }
     [[nodiscard]] GjsProfiler* profiler() const { return m_profiler; }
     [[nodiscard]] const GjsAtoms& atoms() const { return *m_atoms; }
-    [[nodiscard]] bool destroying() const { return m_destroying; }
+    [[nodiscard]] bool destroying() const { return m_destroying.load(); }
     [[nodiscard]] bool sweeping() const { return m_in_gc_sweep; }
     [[nodiscard]] const char* program_name() const { return m_program_name; }
     void set_program_name(char* value) { m_program_name = value; }
diff --git a/gjs/context.cpp b/gjs/context.cpp
index ea0be37c..c9de371b 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -15,6 +15,7 @@
 #    include <process.h>
 #endif
 
+#include <atomic>
 #include <new>
 #include <string>       // for u16string
 #include <unordered_map>
@@ -392,7 +393,7 @@ void GjsContextPrivate::dispose(void) {
         JS_GC(m_cx);
 
         gjs_debug(GJS_DEBUG_CONTEXT, "Destroying JS context");
-        m_destroying = true;
+        m_destroying.store(true);
 
         /* Now, release all native objects, to avoid recursion between
          * the JS teardown and the C teardown.  The JSObject proxies


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