00001
00002
00003 #include "pch.h"
00004
00005 #ifndef CRYPTOPP_IMPORTS
00006
00007 #include "trdlocal.h"
00008 #include <windows.h>
00009
00010 #ifdef THREADS_AVAILABLE
00011
00012 NAMESPACE_BEGIN(CryptoPP)
00013
00014 ThreadLocalStorage::Err::Err(const std::string& operation, int error)
00015 : OS_Error(OTHER_ERROR, "ThreadLocalStorage: " + operation + " operation failed with error 0x" + IntToString(error, 16), operation, error)
00016 {
00017 }
00018
00019 ThreadLocalStorage::ThreadLocalStorage()
00020 {
00021 #ifdef HAS_WINTHREADS
00022 m_index = TlsAlloc();
00023 if (m_index == TLS_OUT_OF_INDEXES)
00024 throw Err("TlsAlloc", GetLastError());
00025 #else
00026 int error = pthread_key_create(&m_index, NULL);
00027 if (error)
00028 throw Err("pthread_key_create", error);
00029 #endif
00030 }
00031
00032 ThreadLocalStorage::~ThreadLocalStorage()
00033 {
00034 #ifdef HAS_WINTHREADS
00035 if (!TlsFree(m_index))
00036 throw Err("TlsFree", GetLastError());
00037 #else
00038 int error = pthread_key_delete(m_index);
00039 if (error)
00040 throw Err("pthread_key_delete", error);
00041 #endif
00042 }
00043
00044 void ThreadLocalStorage::SetValue(void *value)
00045 {
00046 #ifdef HAS_WINTHREADS
00047 if (!TlsSetValue(m_index, value))
00048 throw Err("TlsSetValue", GetLastError());
00049 #else
00050 int error = pthread_setspecific(m_index, value);
00051 if (error)
00052 throw Err("pthread_key_getspecific", error);
00053 #endif
00054 }
00055
00056 void *ThreadLocalStorage::GetValue() const
00057 {
00058 #ifdef HAS_WINTHREADS
00059 void *result = TlsGetValue(m_index);
00060 if (!result && GetLastError() != NO_ERROR)
00061 throw Err("TlsGetValue", GetLastError());
00062 #else
00063 void *result = pthread_getspecific(m_index);
00064 #endif
00065 return result;
00066 }
00067
00068 NAMESPACE_END
00069
00070 #endif // #ifdef THREADS_AVAILABLE
00071
00072 #endif