Gabe Black has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/13329

Change subject: systemc: Don't depend on the order of static initializers.
......................................................................

systemc: Don't depend on the order of static initializers.

STL containers may need to be constructed before they're used. Don't
count on being able to insert into them during a static initializer.

Change-Id: Icb05d5084a470e1ebd976ae6e1954b1a78aabd6a
---
M src/systemc/core/python.cc
M src/systemc/core/python.hh
M src/systemc/utils/report.cc
M src/systemc/utils/report.hh
4 files changed, 69 insertions(+), 12 deletions(-)



diff --git a/src/systemc/core/python.cc b/src/systemc/core/python.cc
index 99d6cc9..e4a0dd0 100644
--- a/src/systemc/core/python.cc
+++ b/src/systemc/core/python.cc
@@ -40,14 +40,25 @@
 namespace
 {

-std::vector<PythonReadyFunc *> pythonReadyFuncs;
-std::vector<PythonInitFunc *> pythonInitFuncs;
+PythonReadyFunc *&
+firstReadyFunc()
+{
+    static PythonReadyFunc *first = nullptr;
+    return first;
+}
+
+PythonInitFunc *&
+firstInitFunc()
+{
+    static PythonInitFunc *first = nullptr;
+    return first;
+}

 void
 python_ready(pybind11::args args)
 {
-    for (auto &func: pythonReadyFuncs)
-        func->run();
+    for (auto ptr = firstReadyFunc(); ptr; ptr = ptr->next)
+        ptr->run();
 }

 void
@@ -55,21 +66,21 @@
 {
     pybind11::module m = m_internal.def_submodule("systemc");
     m.def("python_ready", &python_ready);
-    for (auto &func: pythonInitFuncs)
-        func->run(m);
+    for (auto ptr = firstInitFunc(); ptr; ptr = ptr->next)
+        ptr->run(m);
 }
 EmbeddedPyBind embed_("systemc", &systemc_pybind);

 } // anonymous namespace

-PythonReadyFunc::PythonReadyFunc()
+PythonReadyFunc::PythonReadyFunc() : next(firstReadyFunc())
 {
-    pythonReadyFuncs.push_back(this);
+    firstReadyFunc() = this;
 }

-PythonInitFunc::PythonInitFunc()
+PythonInitFunc::PythonInitFunc() : next(firstInitFunc())
 {
-    pythonInitFuncs.push_back(this);
+    firstInitFunc() = this;
 }

 } // namespace sc_gem5
diff --git a/src/systemc/core/python.hh b/src/systemc/core/python.hh
index 0d68c59..d11310e 100644
--- a/src/systemc/core/python.hh
+++ b/src/systemc/core/python.hh
@@ -37,6 +37,8 @@

 struct PythonReadyFunc
 {
+    PythonReadyFunc *next;
+
     PythonReadyFunc();
     ~PythonReadyFunc() {}
     virtual void run() = 0;
@@ -44,6 +46,8 @@

 struct PythonInitFunc
 {
+    PythonInitFunc *next;
+
     PythonInitFunc();
     ~PythonInitFunc() {}
     virtual void run(pybind11::module &systemc) = 0;
diff --git a/src/systemc/utils/report.cc b/src/systemc/utils/report.cc
index 755542f..87671f1 100644
--- a/src/systemc/utils/report.cc
+++ b/src/systemc/utils/report.cc
@@ -29,6 +29,8 @@

 #include "systemc/utils/report.hh"

+#include "systemc/core/python.hh"
+
 namespace sc_gem5
 {

@@ -63,11 +65,42 @@

 bool reportWarningsAsErrors = false;

-DefaultReportMessages::DefaultReportMessages(
-        std::initializer_list<std::pair<int, const char *>> msgs)
+DefaultReportMessages *&
+DefaultReportMessages::top()
+{
+    static DefaultReportMessages *top_ptr = nullptr;
+    return top_ptr;
+}
+
+void
+DefaultReportMessages::install()
 {
     for (auto &p: msgs)
         sc_core::sc_report::register_id(p.first, p.second);
 }

+DefaultReportMessages::DefaultReportMessages(
+        std::initializer_list<std::pair<int, const char *>> msgs) :
+    next(top()), msgs(msgs)
+{
+    top() = this;
+}
+
+void
+DefaultReportMessages::installAll()
+{
+    for (DefaultReportMessages *ptr = top(); ptr; ptr = ptr->next)
+        ptr->install();
+}
+
+namespace
+{
+
+struct InstallDefaultReportMessages : public PythonReadyFunc
+{
+    void run() override { DefaultReportMessages::installAll(); }
+} messageInstaller;
+
+} // anonymous namespace
+
 } // namespace sc_gem5
diff --git a/src/systemc/utils/report.hh b/src/systemc/utils/report.hh
index ddbf62f..a0840c6 100644
--- a/src/systemc/utils/report.hh
+++ b/src/systemc/utils/report.hh
@@ -110,8 +110,17 @@

 struct DefaultReportMessages
 {
+  protected:
+    static DefaultReportMessages *&top();
+    DefaultReportMessages *next;
+
+    std::initializer_list<std::pair<int, const char *>> msgs;
+    void install();
+
   public:
DefaultReportMessages(std::initializer_list<std::pair<int, const char *>>);
+
+    static void installAll();
 };

 } // namespace sc_gem5

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/13329
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: master
Gerrit-Change-Id: Icb05d5084a470e1ebd976ae6e1954b1a78aabd6a
Gerrit-Change-Number: 13329
Gerrit-PatchSet: 1
Gerrit-Owner: Gabe Black <[email protected]>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/gem5-dev

Reply via email to