[gjs: 3/4] maint: Avoid g_once_init_enter error in GCC 11




commit f02eaf3a9d3465915eb849428c2d9615e2184a4c
Author: Philip Chimento <philip chimento gmail com>
Date:   Sun Feb 14 12:20:09 2021 -0800

    maint: Avoid g_once_init_enter error in GCC 11
    
    On platforms where g_once_init_enter() is defined to use C11 atomic
    builtins, passing a pointer to a volatile value is an error in GCC 11 and
    later, in C++.
    
    More info about the GCC change:
    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95378
    https://gcc.gnu.org/pipermail/gcc-patches/2020-June/548283.html
    
    However, it's my understanding that in modern C++ there is no longer a
    need to guard the initialization of these variables. Since C++11, static
    local variables in a function are guaranteed to be initialized only once,
    the first time control passes through that function. So we can just remove
    the g_once_init_enter guard.
    
    More info:
    https://en.cppreference.com/w/cpp/language/storage_duration#Static_local_variables
    
    Stack Overflow answers with quotations from the C++ standard:
    https://stackoverflow.com/a/58804/172999
    https://stackoverflow.com/a/8102145/172999
    
    Closes: #376

 gjs/error-types.cpp | 32 +++++++++++++-------------------
 gjs/objectbox.cpp   | 31 +++++++++++++------------------
 gjs/objectbox.h     |  3 +++
 3 files changed, 29 insertions(+), 37 deletions(-)
---
diff --git a/gjs/error-types.cpp b/gjs/error-types.cpp
index cdfd7fa3..4c652da4 100644
--- a/gjs/error-types.cpp
+++ b/gjs/error-types.cpp
@@ -12,24 +12,18 @@ G_DEFINE_QUARK(gjs-js-error-quark, gjs_js_error)
 // clang-format on
 
 GType gjs_js_error_get_type(void) {
-    static volatile GType g_type_id;
-
-    if (g_once_init_enter(&g_type_id)) {
-        static GEnumValue errors[] = {
-            { GJS_JS_ERROR_ERROR, "Error", "error" },
-            { GJS_JS_ERROR_EVAL_ERROR, "EvalError", "eval-error" },
-            { GJS_JS_ERROR_INTERNAL_ERROR, "InternalError", "internal-error" },
-            { GJS_JS_ERROR_RANGE_ERROR, "RangeError", "range-error" },
-            { GJS_JS_ERROR_REFERENCE_ERROR, "ReferenceError", "reference-error" },
-            { GJS_JS_ERROR_STOP_ITERATION, "StopIteration", "stop-iteration" },
-            { GJS_JS_ERROR_SYNTAX_ERROR, "SyntaxError", "syntax-error" },
-            { GJS_JS_ERROR_TYPE_ERROR, "TypeError", "type-error" },
-            { GJS_JS_ERROR_URI_ERROR, "URIError", "uri-error" },
-            { 0, nullptr, nullptr }
-        };
-
-        g_type_id = g_enum_register_static("GjsJSError", errors);
-    }
-
+    static const GEnumValue errors[] = {
+        {GJS_JS_ERROR_ERROR, "Error", "error"},
+        {GJS_JS_ERROR_EVAL_ERROR, "EvalError", "eval-error"},
+        {GJS_JS_ERROR_INTERNAL_ERROR, "InternalError", "internal-error"},
+        {GJS_JS_ERROR_RANGE_ERROR, "RangeError", "range-error"},
+        {GJS_JS_ERROR_REFERENCE_ERROR, "ReferenceError", "reference-error"},
+        {GJS_JS_ERROR_STOP_ITERATION, "StopIteration", "stop-iteration"},
+        {GJS_JS_ERROR_SYNTAX_ERROR, "SyntaxError", "syntax-error"},
+        {GJS_JS_ERROR_TYPE_ERROR, "TypeError", "type-error"},
+        {GJS_JS_ERROR_URI_ERROR, "URIError", "uri-error"},
+        {0, nullptr, nullptr}};
+    // Initialization of static local variable guaranteed only once in C++11
+    static GType g_type_id = g_enum_register_static("GjsJSError", errors);
     return g_type_id;
 }
diff --git a/gjs/objectbox.cpp b/gjs/objectbox.cpp
index 3c60c05e..2ad6cddc 100644
--- a/gjs/objectbox.cpp
+++ b/gjs/objectbox.cpp
@@ -4,8 +4,6 @@
 
 #include <config.h>
 
-#include <stddef.h>  // for size_t
-
 #include <algorithm>  // for find
 
 #include <glib.h>
@@ -114,24 +112,21 @@ JSObject* ObjectBox::object_for_c_ptr(JSContext* cx, ObjectBox* box) {
     return box->m_impl->m_root.get();
 }
 
-GType ObjectBox::gtype() {
-    static volatile size_t type_id = 0;
+void* ObjectBox::boxed_copy(void* boxed) {
+    auto* box = static_cast<ObjectBox*>(boxed);
+    box->m_impl->ref();
+    return box;
+}
 
-    if (g_once_init_enter(&type_id)) {
-        auto objectbox_copy = [](void* boxed) -> void* {
-            auto* box = static_cast<ObjectBox*>(boxed);
-            box->m_impl->ref();
-            return box;
-        };
-        auto objectbox_free = [](void* boxed) {
-            auto* box = static_cast<ObjectBox*>(boxed);
-            box->m_impl->unref();
-        };
-        GType type = g_boxed_type_register_static("JSObject", objectbox_copy,
-                                                  objectbox_free);
-        g_once_init_leave(&type_id, type);
-    }
+void ObjectBox::boxed_free(void* boxed) {
+    auto* box = static_cast<ObjectBox*>(boxed);
+    box->m_impl->unref();
+}
 
+GType ObjectBox::gtype() {
+    // Initialization of static local variable guaranteed only once in C++11
+    static GType type_id = g_boxed_type_register_static(
+        "JSObject", &ObjectBox::boxed_copy, &ObjectBox::boxed_free);
     return type_id;
 }
 
diff --git a/gjs/objectbox.h b/gjs/objectbox.h
index 276fd5b8..b2d3f349 100644
--- a/gjs/objectbox.h
+++ b/gjs/objectbox.h
@@ -32,6 +32,9 @@ struct ObjectBox {
  private:
     explicit ObjectBox(JSObject*);
 
+    static void* boxed_copy(void*);
+    static void boxed_free(void*);
+
     struct impl;
     std::unique_ptr<impl> m_impl;
 };


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