From 98b15ef4975d0247d8ce5f35e4a89870e59f3661 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Thu, 8 Jul 2010 02:29:29 +0000 Subject: [PATCH] Switch from __thread to pthread_[gs]etspecific(). --- Local.hpp | 40 +++++++++++++++++++++++++++++----------- Pooling.hpp | 4 ++-- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/Local.hpp b/Local.hpp index c223792..1b97267 100644 --- a/Local.hpp +++ b/Local.hpp @@ -40,24 +40,42 @@ #ifndef CYLOCAL_HPP #define CYLOCAL_HPP +#include + template -struct CYLocal { - Type_ last_; +class CYLocal { + private: + static ::pthread_key_t key_; + + Type_ *last_; + + protected: + static _finline void Set(Type_ *value) { + _assert(::pthread_setspecific(key_, value) == 0); + } - CYLocal(Type_ next) { - Type_ &top(Top()); - last_ = top; - top = next; + static ::pthread_key_t Key_() { + ::pthread_key_t key; + ::pthread_key_create(&key, NULL); + return key; } - ~CYLocal() { - Top() = last_; + public: + CYLocal(Type_ *next) { + last_ = Get(); + Set(next); } - static Type_ &Top() { - static __thread Type_ top; - return top; + _finline ~CYLocal() { + Set(last_); + } + + static _finline Type_ *Get() { + return reinterpret_cast(::pthread_getspecific(key_)); } }; +template +::pthread_key_t CYLocal::key_ = Key_(); + #endif/*CYLOCAL_HPP*/ diff --git a/Pooling.hpp b/Pooling.hpp index 96b4932..c416b90 100644 --- a/Pooling.hpp +++ b/Pooling.hpp @@ -174,7 +174,7 @@ class CYLocalPool : public CYPool { private: - CYLocal local_; + CYLocal local_; public: CYLocalPool() : @@ -185,6 +185,6 @@ class CYLocalPool : }; #define $pool \ - CYLocal::Top() + CYLocal::Get() #endif/*CYPOOLING_HPP*/ -- 2.47.2