7 #if CRYPTOPP_MSC_VERSION
8 # pragma warning(disable: 4297)
11 #ifndef CRYPTOPP_IMPORTS
13 #if !defined(NO_OS_DEPENDENCE) && defined(THREADS_AVAILABLE)
18 #define WIN32_LEAN_AND_MEAN
24 ThreadLocalStorage::Err::Err(
const std::string& operation,
int error)
25 :
OS_Error(OTHER_ERROR,
"ThreadLocalStorage: " + operation +
" operation failed with error 0x" +
IntToString(error, 16), operation, error)
29 ThreadLocalStorage::ThreadLocalStorage()
33 assert(m_index != TLS_OUT_OF_INDEXES);
34 if (m_index == TLS_OUT_OF_INDEXES)
35 throw Err(
"TlsAlloc", GetLastError());
38 int error = pthread_key_create(&m_index, NULL);
41 throw Err(
"pthread_key_create", error);
45 ThreadLocalStorage::~ThreadLocalStorage() CRYPTOPP_THROW
47 #ifdef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
48 if (!std::uncaught_exception())
54 int rc = TlsFree(m_index);
57 throw Err(
"TlsFree", GetLastError());
61 int error = pthread_key_delete(m_index);
64 throw Err(
"pthread_key_delete", error);
67 #ifndef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
74 void ThreadLocalStorage::SetValue(
void *value)
77 if (!TlsSetValue(m_index, value))
78 throw Err(
"TlsSetValue", GetLastError());
80 int error = pthread_setspecific(m_index, value);
82 throw Err(
"pthread_key_getspecific", error);
86 void *ThreadLocalStorage::GetValue()
const
89 void *result = TlsGetValue(m_index);
90 const DWORD dwRet = GetLastError();
92 assert(result || (!result && (dwRet == NO_ERROR)));
93 if (!result && dwRet != NO_ERROR)
94 throw Err(
"TlsGetValue", dwRet);
98 void *result = pthread_getspecific(m_index);
105 #endif // THREADS_AVAILABLE
106 #endif // CRYPTOPP_IMPORTS