diff --git a/src/common/String.cpp b/src/common/String.cpp index 493732f93..6bd593b5b 100644 --- a/src/common/String.cpp +++ b/src/common/String.cpp @@ -4,7 +4,6 @@ #include #include -#include "common/tpt-thread-local.h" #include "String.h" ByteString ConversionError::formatError(ByteString::value_type const *at, ByteString::value_type const *upto) @@ -376,7 +375,7 @@ struct LocaleImpl static LocaleImpl *getLocaleImpl() { - static THREAD_LOCAL(LocaleImpl, li); + thread_local LocaleImpl li; return &li; } diff --git a/src/common/meson.build b/src/common/meson.build index b9f0f0199..e215c0671 100644 --- a/src/common/meson.build +++ b/src/common/meson.build @@ -1,7 +1,6 @@ common_files += files( 'String.cpp', 'tpt-rand.cpp', - 'tpt-thread-local.cpp', ) subdir('clipboard') diff --git a/src/common/tpt-thread-local.cpp b/src/common/tpt-thread-local.cpp deleted file mode 100644 index 0a53422c1..000000000 --- a/src/common/tpt-thread-local.cpp +++ /dev/null @@ -1,61 +0,0 @@ -#include "tpt-thread-local.h" - -#ifdef __MINGW32__ -# include -# include -# include - -void *ThreadLocalCommon::Get() const -{ - // https://stackoverflow.com/questions/16552710/how-do-you-get-the-start-and-end-addresses-of-a-custom-elf-section - extern ThreadLocalCommon __start_tpt_tls; - extern ThreadLocalCommon __stop_tpt_tls; - static pthread_once_t once = PTHREAD_ONCE_INIT; - static pthread_key_t key; - - struct ThreadLocalEntry - { - void *ptr; - }; - - auto *staticsBegin = &__start_tpt_tls; - auto *staticsEnd = &__stop_tpt_tls; - pthread_once(&once, []() -> void { - assert(!pthread_key_create(&key, [](void *opaque) -> void { - auto *staticsBegin = &__start_tpt_tls; - auto *staticsEnd = &__stop_tpt_tls; - auto staticsCount = staticsEnd - staticsBegin; - auto *liveObjects = reinterpret_cast(opaque); - if (liveObjects) - { - for (auto i = 0; i < staticsCount; ++i) - { - if (liveObjects[i].ptr) - { - staticsBegin[i].dtor(liveObjects[i].ptr); - free(liveObjects[i].ptr); - } - } - free(liveObjects); - } - })); - }); - auto *liveObjects = reinterpret_cast(pthread_getspecific(key)); - if (!liveObjects) - { - auto staticsCount = staticsEnd - staticsBegin; - liveObjects = reinterpret_cast(calloc(staticsCount, sizeof(ThreadLocalEntry))); - assert(liveObjects); - assert(!pthread_setspecific(key, reinterpret_cast(liveObjects))); - } - auto idx = this - staticsBegin; - auto &entry = liveObjects[idx]; - if (!entry.ptr) - { - entry.ptr = malloc(staticsBegin[idx].size); - assert(entry.ptr); - staticsBegin[idx].ctor(entry.ptr); - } - return entry.ptr; -} -#endif diff --git a/src/common/tpt-thread-local.h b/src/common/tpt-thread-local.h deleted file mode 100644 index 66f02eb75..000000000 --- a/src/common/tpt-thread-local.h +++ /dev/null @@ -1,63 +0,0 @@ -#pragma once -#ifdef __MINGW32__ -# include - -class ThreadLocalCommon -{ - ThreadLocalCommon(const ThreadLocalCommon &other) = delete; - ThreadLocalCommon &operator =(const ThreadLocalCommon &other) = delete; - -protected: - size_t size; - void (*ctor)(void *); - void (*dtor)(void *); - size_t padding; - - void *Get() const; - -public: - ThreadLocalCommon() = default; - - static constexpr size_t Alignment = 0x20; -}; -// * If this fails, add or remove padding fields, possibly change Alignment to a larger power of 2. -static_assert(sizeof(ThreadLocalCommon) == ThreadLocalCommon::Alignment, "fix me"); - -template -class ThreadLocal : public ThreadLocalCommon -{ - static void Ctor(void *type) - { - new(type) Type(); - } - - static void Dtor(void *type) - { - reinterpret_cast(type)->~Type(); - } - -public: - ThreadLocal() - { - // * If this fails, you're out of luck. - static_assert(sizeof(ThreadLocal) == sizeof(ThreadLocalCommon), "fix me"); - size = sizeof(Type); - ctor = Ctor; - dtor = Dtor; - } - - Type *operator &() const - { - return reinterpret_cast(Get()); - } - - operator Type &() const - { - return *(this->operator &()); - } -}; - -# define THREAD_LOCAL(Type, tl) const ThreadLocal tl __attribute__((section("tpt_tls"), aligned(ThreadLocalCommon::Alignment))) -#else -# define THREAD_LOCAL(Type, tl) thread_local Type tl -#endif diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index 0b7493a5f..b9aaff7c1 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -7,7 +7,6 @@ #include "client/GameSave.h" #include "common/tpt-compat.h" #include "common/tpt-rand.h" -#include "common/tpt-thread-local.h" #include "gui/game/Brush.h" #include "elements/EMP.h" #include "elements/LOLZ.h" @@ -452,7 +451,7 @@ bool Simulation::FloodFillPmapCheck(int x, int y, int type) const CoordStack& Simulation::getCoordStackSingleton() { // Future-proofing in case Simulation is later multithreaded - static THREAD_LOCAL(CoordStack, cs); + thread_local CoordStack cs; return cs; }