/** * @file include/ActivMiddleware/ActivBase/Detail/ThreadSpecificDataDetail.h * * @brief Implementation details thread for specific data storage class. */ #if (!defined ACTIV_BASE_DETAIL_THREAD_SPECIFIC_DATA_DETAIL_H) #define ACTIV_BASE_DETAIL_THREAD_SPECIFIC_DATA_DETAIL_H #include "ActivMiddleware/ActivBase/OsException.h" #include "ActivMiddleware/ActivBase/StatusCodeException.h" #include "ActivMiddleware/CxxYZ/functional.h" #if (defined ACTIV_PLATFORM_POSIX) #include #endif //! Activ namespace. namespace Activ { #if (defined ACTIV_PLATFORM_WINDOWS) typedef void (*TsdDestructorFn)(void *); ACTIV_BASE_API void ThreadSpecificDataCleanupAddKey(const DWORD key, TsdDestructorFn pTsdDestructorFn); ACTIV_BASE_API void ThreadSpecificDataCleanupRemoveKey(const DWORD key); // --------------------------------------------------------------------------------------------------------------------------------- template inline ThreadSpecificDataBase::ThreadSpecificDataBase() : m_index(::TlsAlloc()), m_pSharedInfo(CxxYZ::make_shared(CxxYZ::ref(*this))) { if (TLS_OUT_OF_INDEXES == m_index) ACTIV_THROW(OsException, ::GetLastError()); ThreadSpecificDataCleanupAddKey(m_index, &StaticDestructor); } // --------------------------------------------------------------------------------------------------------------------------------- template inline ThreadSpecificDataBase::~ThreadSpecificDataBase() { void * const pData = ::TlsGetValue(m_index); if (0 != pData) StaticDestructor(pData); { // Lock the shared portion. We don't want to destroy ourself whilst another thread is cleaning up; if we've gone by the // time they start to cleanup then they won't be able to do much as we set the shared hook back here to 0. CxxYZ::lock_guard scopedMutexLocker(m_pSharedInfo->m_mutex); m_pSharedInfo->m_pThreadSpecificData = 0; } ThreadSpecificDataCleanupRemoveKey(m_index); (void) ::TlsFree(m_index); } // --------------------------------------------------------------------------------------------------------------------------------- template inline TYPE *ThreadSpecificDataBase::Get() const { PerThreadInfo * const pPerThreadInfo = reinterpret_cast(::TlsGetValue(m_index)); if (ACTIV_UNLIKELY(0 == pPerThreadInfo)) return 0; return pPerThreadInfo->m_pValue; } // --------------------------------------------------------------------------------------------------------------------------------- template inline void ThreadSpecificDataBase::Set(TYPE * const pValue) { PerThreadInfo *pPerThreadInfo = reinterpret_cast(::TlsGetValue(m_index)); if (0 == pPerThreadInfo) { pPerThreadInfo = new PerThreadInfo(m_pSharedInfo); (void) ::TlsSetValue(m_index, reinterpret_cast(pPerThreadInfo)); } pPerThreadInfo->m_pValue = pValue; } // --------------------------------------------------------------------------------------------------------------------------------- #elif (defined ACTIV_PLATFORM_POSIX) template inline ThreadSpecificDataBase::ThreadSpecificDataBase() : m_pSharedInfo(new SharedInfo(*this)) { int res = ::pthread_key_create(&m_key, &StaticDestructor); if (0 != res) ACTIV_THROW(OsException, res); } // --------------------------------------------------------------------------------------------------------------------------------- template inline ThreadSpecificDataBase::~ThreadSpecificDataBase() { void * const pData = ::pthread_getspecific(m_key); if (0 != pData) StaticDestructor(pData); { // Lock the shared portion. We don't want to destroy ourself whilst another thread is cleaning up; if we've gone by the // time they start to cleanup then they won't be able to do much as we set the shared hook back here to 0. CxxYZ::lock_guard scopedMutexLocker(m_pSharedInfo->m_mutex); m_pSharedInfo->m_pThreadSpecificData = 0; } (void) ::pthread_key_delete(m_key); } // --------------------------------------------------------------------------------------------------------------------------------- template inline TYPE *ThreadSpecificDataBase::Get() const { PerThreadInfo * const pPerThreadInfo = reinterpret_cast(::pthread_getspecific(m_key)); if (ACTIV_UNLIKELY(0 == pPerThreadInfo)) return 0; return pPerThreadInfo->m_pValue; } // --------------------------------------------------------------------------------------------------------------------------------- template inline void ThreadSpecificDataBase::Set(TYPE * const pValue) { PerThreadInfo *pPerThreadInfo = reinterpret_cast(::pthread_getspecific(m_key)); if (0 == pPerThreadInfo) { pPerThreadInfo = new PerThreadInfo(m_pSharedInfo); (void) ::pthread_setspecific(m_key, reinterpret_cast(pPerThreadInfo)); } pPerThreadInfo->m_pValue = pValue; } // --------------------------------------------------------------------------------------------------------------------------------- #else #error Configure for this platform #endif // --------------------------------------------------------------------------------------------------------------------------------- template inline void ThreadSpecificDataBase::InvokeOnThreadExit(TYPE * const pValue) { DERIVED &derived = static_cast(*this); derived.OnThreadExit(pValue); } // --------------------------------------------------------------------------------------------------------------------------------- template void ThreadSpecificDataBase::StaticDestructor(void *pData) { if (0 != pData) { PerThreadInfo * const pPerThreadInfo = reinterpret_cast(pData); delete pPerThreadInfo; } } // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- template ThreadSpecificDataBase::SharedInfo::SharedInfo(ThreadSpecificDataType &threadSpecificData) : m_pThreadSpecificData(&threadSpecificData) { } // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- template ThreadSpecificDataBase::PerThreadInfo::PerThreadInfo(SharedInfoPointer &pSharedInfo) : m_pSharedInfo(pSharedInfo), m_pValue(0) { } // --------------------------------------------------------------------------------------------------------------------------------- template ThreadSpecificDataBase::PerThreadInfo::~PerThreadInfo() { CxxYZ::lock_guard scopedMutexLocker(m_pSharedInfo->m_mutex); // If pThreadSpecificData == 0, the ThreadSpecificDataBase object has been destroyed before this thread exited. // In this case, there is very little we can do; there will probably be a small memory leak. ThreadSpecificDataType *pThreadSpecificData = m_pSharedInfo->m_pThreadSpecificData; if ((0 != pThreadSpecificData) && (0 != m_pValue)) pThreadSpecificData->InvokeOnThreadExit(m_pValue); } } // namespace Activ #endif // (!defined ACTIV_BASE_DETAIL_THREAD_SPECIFIC_DATA_DETAIL_H)