]> git.tdb.fi Git - libs/game.git/commitdiff
Hide thread-local data behind accessor functions
authorMikko Rasa <tdb@tdb.fi>
Sun, 26 Jan 2025 20:42:00 +0000 (22:42 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 27 Jan 2025 11:28:31 +0000 (13:28 +0200)
MSVC doesn't like them as static members of classes with DLL interface.

source/game/accessguard.cpp
source/game/accessguard.h
source/game/system.cpp
source/game/system.h

index d4c211300b2f093cb24ec0e9193743d6290eb40b..6e35a3f1bd749b1d4b11a1e2d6284da0a909ee3e 100644 (file)
@@ -2,10 +2,9 @@
 
 namespace Msp::Game {
 
-thread_local AccessGuard *AccessGuard::instance = nullptr;
-
 AccessGuard::AccessGuard()
 {
+       AccessGuard *&instance = get_instance_ptr();
        if(!instance)
                instance = this;
 }
@@ -13,12 +12,14 @@ AccessGuard::AccessGuard()
 AccessGuard::AccessGuard(AccessGuard &&other):
        flags(std::move(other.flags))
 {
+       AccessGuard *&instance = get_instance_ptr();
        if(&other==instance)
                instance = this;
 }
 
 AccessGuard &AccessGuard::operator=(AccessGuard &&other)
 {
+       AccessGuard *&instance = get_instance_ptr();
        flags = std::move(other.flags);
        if(&other==instance)
                instance = this;
@@ -27,17 +28,25 @@ AccessGuard &AccessGuard::operator=(AccessGuard &&other)
 
 AccessGuard::~AccessGuard()
 {
+       AccessGuard *&instance = get_instance_ptr();
        if(this==instance)
                instance = nullptr;
 }
 
 AccessGuard &AccessGuard::get_instance()
 {
+       AccessGuard *&instance = get_instance_ptr();
        if(!instance)
                throw std::logic_error("no AccessGuard instance");
        return *instance;
 }
 
+AccessGuard *&AccessGuard::get_instance_ptr()
+{
+       static thread_local AccessGuard *instance = nullptr;
+       return instance;
+}
+
 void AccessGuard::block_all()
 {
        default_flag = BLOCKED;
index 2d9a3053b8324a8e06abdb8713f8469bd5907d84..004a5a63eef23effce6152156239c21e737fd9b5 100644 (file)
@@ -40,8 +40,6 @@ private:
        std::uint8_t default_flag = UNBLOCKED;
        std::vector<std::uint8_t> flags;
 
-       static thread_local AccessGuard *instance;
-
 public:
        AccessGuard();
        AccessGuard(AccessGuard &&);
@@ -49,7 +47,10 @@ public:
        ~AccessGuard();
 
        static AccessGuard &get_instance();
+private:
+       static AccessGuard *&get_instance_ptr();
 
+public:
        void block_all();
        void unblock_all();
 
index 4c1f4fbba01fa46ecc8b39c9a9dcb80172925c6b..ad81eac9fe935ccbcf83833ca1b833496be04840 100644 (file)
@@ -4,10 +4,15 @@ using namespace std;
 
 namespace Msp::Game {
 
-thread_local System *System::active = nullptr;
+System *&System::get_active_ptr()
+{
+       thread_local System *active = nullptr;
+       return active;
+}
 
 void System::begin_tick()
 {
+       System *&active = get_active_ptr();
        if(active)
                throw logic_error("System::active != nullptr");
        active = this;
@@ -35,6 +40,7 @@ void System::end_tick()
 #endif
        }
 
+       System *&active = get_active_ptr();
        if(active==this)
                active = nullptr;
 }
index 2f0f8ba56ca8aeb8a640af992dd29472f8f7348e..ec1fb61b1af277e2a5a0dcfcd04f4d1ea8796bfd 100644 (file)
@@ -64,8 +64,6 @@ protected:
        std::vector<std::function<void()>> deferred_queue;
        std::vector<Dependency> dependencies;
 
-       static thread_local System *active;
-
        System(Stage &s): stage(s) { }
 public:
        virtual ~System() = default;
@@ -79,6 +77,10 @@ protected:
 public:
        const std::vector<Dependency> &get_dependencies() const { return dependencies; }
 
+private:
+       static System *&get_active_ptr();
+
+public:
        void begin_tick();
        virtual void tick(Time::TimeDelta) = 0;
        void end_tick();