Files
externals/linux/include/ActivMiddleware/ActivBase/Detail/ThreadSpecificDataDetail.h
T
2023-12-15 13:15:08 -05:00

219 lines
7.7 KiB
C++

/**
* @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 <pthread.h>
#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 <typename TYPE, typename DERIVED>
inline ThreadSpecificDataBase<TYPE, DERIVED>::ThreadSpecificDataBase() :
m_index(::TlsAlloc()),
m_pSharedInfo(CxxYZ::make_shared<SharedInfo>(CxxYZ::ref(*this)))
{
if (TLS_OUT_OF_INDEXES == m_index)
ACTIV_THROW(OsException, ::GetLastError());
ThreadSpecificDataCleanupAddKey(m_index, &StaticDestructor);
}
// ---------------------------------------------------------------------------------------------------------------------------------
template <typename TYPE, typename DERIVED>
inline ThreadSpecificDataBase<TYPE, DERIVED>::~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<CxxYZ::mutex> scopedMutexLocker(m_pSharedInfo->m_mutex);
m_pSharedInfo->m_pThreadSpecificData = 0;
}
ThreadSpecificDataCleanupRemoveKey(m_index);
(void) ::TlsFree(m_index);
}
// ---------------------------------------------------------------------------------------------------------------------------------
template <typename TYPE, typename DERIVED>
inline TYPE *ThreadSpecificDataBase<TYPE, DERIVED>::Get() const
{
PerThreadInfo * const pPerThreadInfo = reinterpret_cast<PerThreadInfo *>(::TlsGetValue(m_index));
if (ACTIV_UNLIKELY(0 == pPerThreadInfo))
return 0;
return pPerThreadInfo->m_pValue;
}
// ---------------------------------------------------------------------------------------------------------------------------------
template <typename TYPE, typename DERIVED>
inline void ThreadSpecificDataBase<TYPE, DERIVED>::Set(TYPE * const pValue)
{
PerThreadInfo *pPerThreadInfo = reinterpret_cast<PerThreadInfo *>(::TlsGetValue(m_index));
if (0 == pPerThreadInfo)
{
pPerThreadInfo = new PerThreadInfo(m_pSharedInfo);
(void) ::TlsSetValue(m_index, reinterpret_cast<void *>(pPerThreadInfo));
}
pPerThreadInfo->m_pValue = pValue;
}
// ---------------------------------------------------------------------------------------------------------------------------------
#elif (defined ACTIV_PLATFORM_POSIX)
template <typename TYPE, typename DERIVED>
inline ThreadSpecificDataBase<TYPE, DERIVED>::ThreadSpecificDataBase() :
m_pSharedInfo(new SharedInfo(*this))
{
int res = ::pthread_key_create(&m_key, &StaticDestructor);
if (0 != res)
ACTIV_THROW(OsException, res);
}
// ---------------------------------------------------------------------------------------------------------------------------------
template <typename TYPE, typename DERIVED>
inline ThreadSpecificDataBase<TYPE, DERIVED>::~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<CxxYZ::mutex> scopedMutexLocker(m_pSharedInfo->m_mutex);
m_pSharedInfo->m_pThreadSpecificData = 0;
}
(void) ::pthread_key_delete(m_key);
}
// ---------------------------------------------------------------------------------------------------------------------------------
template <typename TYPE, typename DERIVED>
inline TYPE *ThreadSpecificDataBase<TYPE, DERIVED>::Get() const
{
PerThreadInfo * const pPerThreadInfo = reinterpret_cast<PerThreadInfo *>(::pthread_getspecific(m_key));
if (ACTIV_UNLIKELY(0 == pPerThreadInfo))
return 0;
return pPerThreadInfo->m_pValue;
}
// ---------------------------------------------------------------------------------------------------------------------------------
template <typename TYPE, typename DERIVED>
inline void ThreadSpecificDataBase<TYPE, DERIVED>::Set(TYPE * const pValue)
{
PerThreadInfo *pPerThreadInfo = reinterpret_cast<PerThreadInfo *>(::pthread_getspecific(m_key));
if (0 == pPerThreadInfo)
{
pPerThreadInfo = new PerThreadInfo(m_pSharedInfo);
(void) ::pthread_setspecific(m_key, reinterpret_cast<void *>(pPerThreadInfo));
}
pPerThreadInfo->m_pValue = pValue;
}
// ---------------------------------------------------------------------------------------------------------------------------------
#else
#error Configure for this platform
#endif
// ---------------------------------------------------------------------------------------------------------------------------------
template <typename TYPE, typename DERIVED>
inline void ThreadSpecificDataBase<TYPE, DERIVED>::InvokeOnThreadExit(TYPE * const pValue)
{
DERIVED &derived = static_cast<DERIVED &>(*this);
derived.OnThreadExit(pValue);
}
// ---------------------------------------------------------------------------------------------------------------------------------
template <typename TYPE, typename DERIVED>
void ThreadSpecificDataBase<TYPE, DERIVED>::StaticDestructor(void *pData)
{
if (0 != pData)
{
PerThreadInfo * const pPerThreadInfo = reinterpret_cast<PerThreadInfo *>(pData);
delete pPerThreadInfo;
}
}
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
template <typename TYPE, typename DERIVED>
ThreadSpecificDataBase<TYPE, DERIVED>::SharedInfo::SharedInfo(ThreadSpecificDataType &threadSpecificData) :
m_pThreadSpecificData(&threadSpecificData)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
template <typename TYPE, typename DERIVED>
ThreadSpecificDataBase<TYPE, DERIVED>::PerThreadInfo::PerThreadInfo(SharedInfoPointer &pSharedInfo) :
m_pSharedInfo(pSharedInfo),
m_pValue(0)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
template <typename TYPE, typename DERIVED>
ThreadSpecificDataBase<TYPE, DERIVED>::PerThreadInfo::~PerThreadInfo()
{
CxxYZ::lock_guard<CxxYZ::mutex> 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)