Skip to content

Commit

Permalink
Fix macOS support for tss
Browse files Browse the repository at this point in the history
  • Loading branch information
kcat committed Feb 13, 2024
1 parent 0584aec commit 00bb2ce
Showing 1 changed file with 55 additions and 37 deletions.
92 changes: 55 additions & 37 deletions common/althreads.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#elif defined(__APPLE__)

#include <pthread.h>

#else

#include <threads.h>
Expand All @@ -23,7 +27,8 @@ class tss {
static_assert(sizeof(T) <= sizeof(void*));
static_assert(std::is_trivially_destructible_v<T> && std::is_trivially_copy_constructible_v<T>);

static void *as_ptr(const T &value) noexcept
[[nodiscard]]
static auto to_ptr(const T &value) noexcept -> void*
{
if constexpr(std::is_pointer_v<T>)
{
Expand All @@ -38,45 +43,67 @@ class tss {
return al::bit_cast<void*>(static_cast<std::uintptr_t>(value));
}

[[nodiscard]]
static auto from_ptr(void *ptr) noexcept -> T
{
if constexpr(std::is_pointer_v<T>)
return static_cast<T>(ptr);
else if constexpr(sizeof(T) == sizeof(void*))
return al::bit_cast<T>(ptr);
else if constexpr(std::is_integral_v<T>)
return static_cast<T>(al::bit_cast<std::uintptr_t>(ptr));
}

#ifdef _WIN32
DWORD mTss;

public:
tss()
tss() : mTss{TlsAlloc()}
{
mTss = TlsAlloc();
if(mTss == TLS_OUT_OF_INDEXES)
throw std::runtime_error{"al::tss::tss()"};
}
explicit tss(const T &init) : tss{}
{
if(TlsSetValue(mTss, as_ptr(init)) == FALSE)
if(TlsSetValue(mTss, to_ptr(init)) == FALSE)
throw std::runtime_error{"al::tss::tss(T)"};
}
tss(const tss&) = delete;
tss(tss&&) = delete;
~tss() { TlsFree(mTss); }

void operator=(const tss&) = delete;
void operator=(tss&&) = delete;

void set(const T &value)
void set(const T &value) const
{
if(TlsSetValue(mTss, as_ptr(value)) == FALSE)
if(TlsSetValue(mTss, to_ptr(value)) == FALSE)
throw std::runtime_error{"al::tss::set(T)"};
}

[[nodiscard]]
auto get() const noexcept -> T
auto get() const noexcept -> T { return from_ptr(TlsGetValue(mTss)); }

#elif defined(__APPLE__)

pthread_key_t mTss;

public:
tss()
{
void *res{TlsGetValue(mTss)};
if constexpr(std::is_pointer_v<T>)
return static_cast<T>(res);
else if constexpr(sizeof(T) == sizeof(void*))
return al::bit_cast<T>(res);
else if constexpr(std::is_integral_v<T>)
return static_cast<T>(al::bit_cast<std::uintptr_t>(res));
if(int res{pthread_key_create(&mTss, nullptr)}; res != 0)
throw std::runtime_error{"al::tss::tss()"};
}
explicit tss(const T &init) : tss{}
{
if(int res{pthread_setspecific(mTss, to_ptr(init))}; res != 0)
throw std::runtime_error{"al::tss::tss(T)"};
}
~tss() { tss_delete(mTss); }

void set(const T &value) const
{
if(int res{pthread_setspecific(mTss, to_ptr(value))}; res != 0)
throw std::runtime_error{"al::tss::set(T)"};
}

[[nodiscard]]
auto get() const noexcept -> T { return from_ptr(pthread_getspecific(mTss)); }

#else

Expand All @@ -90,34 +117,25 @@ class tss {
}
explicit tss(const T &init) : tss{}
{
if(int res{tss_set(mTss, as_ptr(init))}; res != thrd_success)
if(int res{tss_set(mTss, to_ptr(init))}; res != thrd_success)
throw std::runtime_error{"al::tss::tss(T)"};
}
tss(const tss&) = delete;
tss(tss&&) = delete;
~tss() { tss_delete(mTss); }

void operator=(const tss&) = delete;
void operator=(tss&&) = delete;

void set(const T &value)
void set(const T &value) const
{
if(int res{tss_set(mTss, as_ptr(value))}; res != thrd_success)
if(int res{tss_set(mTss, to_ptr(value))}; res != thrd_success)
throw std::runtime_error{"al::tss::set(T)"};
}

[[nodiscard]]
auto get() const noexcept -> T
{
void *res{tss_get(mTss)};
if constexpr(std::is_pointer_v<T>)
return static_cast<T>(res);
else if constexpr(sizeof(T) == sizeof(void*))
return al::bit_cast<T>(res);
else if constexpr(std::is_integral_v<T>)
return static_cast<T>(al::bit_cast<std::uintptr_t>(res));
}
auto get() const noexcept -> T { return from_ptr(tss_get(mTss)); }
#endif /* _WIN32 */

tss(const tss&) = delete;
tss(tss&&) = delete;
void operator=(const tss&) = delete;
void operator=(tss&&) = delete;
};

} // namespace al
Expand Down

0 comments on commit 00bb2ce

Please sign in to comment.