1 // trdlocal.cpp - written and placed in the public domain by Wei Dai
2
3 #include "pch.h"
4 #include "config.h"
5
6 // TODO: fix this when more complete C++11 support is cut-in
7 #if CRYPTOPP_MSC_VERSION
8 # pragma warning(disable: 4297)
9 #endif
10
11 #ifndef CRYPTOPP_IMPORTS
12
13 #if !defined(NO_OS_DEPENDENCE) && defined(THREADS_AVAILABLE)
14
15 #include "trdlocal.h"
16
17 #ifdef HAS_WINTHREADS
18 #define WIN32_LEAN_AND_MEAN
19 #include <windows.h>
20 #endif
21
NAMESPACE_BEGIN(CryptoPP)22 NAMESPACE_BEGIN(CryptoPP)
23
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)
26 {
27 }
28
ThreadLocalStorage()29 ThreadLocalStorage::ThreadLocalStorage()
30 {
31 #ifdef HAS_WINTHREADS
32 m_index = TlsAlloc();
33 CRYPTOPP_ASSERT(m_index != TLS_OUT_OF_INDEXES);
34 if (m_index == TLS_OUT_OF_INDEXES)
35 throw Err("TlsAlloc", GetLastError());
36 #else
37 m_index = 0;
38 int error = pthread_key_create(&m_index, NULL);
39 CRYPTOPP_ASSERT(!error);
40 if (error)
41 throw Err("pthread_key_create", error);
42 #endif
43 }
44
~ThreadLocalStorage()45 ThreadLocalStorage::~ThreadLocalStorage() CRYPTOPP_THROW
46 {
47 #ifdef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
48 if (!std::uncaught_exception())
49 #else
50 try
51 #endif
52 #ifdef HAS_WINTHREADS
53 {
54 int rc = TlsFree(m_index);
55 CRYPTOPP_ASSERT(rc);
56 if (!rc)
57 throw Err("TlsFree", GetLastError());
58 }
59 #else
60 {
61 int error = pthread_key_delete(m_index);
62 CRYPTOPP_ASSERT(!error);
63 if (error)
64 throw Err("pthread_key_delete", error);
65 }
66 #endif
67 #ifndef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
68 catch(const Exception&)
69 {
70 }
71 #endif
72 }
73
SetValue(void * value)74 void ThreadLocalStorage::SetValue(void *value)
75 {
76 #ifdef HAS_WINTHREADS
77 if (!TlsSetValue(m_index, value))
78 throw Err("TlsSetValue", GetLastError());
79 #else
80 int error = pthread_setspecific(m_index, value);
81 if (error)
82 throw Err("pthread_key_getspecific", error);
83 #endif
84 }
85
GetValue() const86 void *ThreadLocalStorage::GetValue() const
87 {
88 #ifdef HAS_WINTHREADS
89 void *result = TlsGetValue(m_index);
90 const DWORD dwRet = GetLastError();
91
92 CRYPTOPP_ASSERT(result || (!result && (dwRet == NO_ERROR)));
93 if (!result && dwRet != NO_ERROR)
94 throw Err("TlsGetValue", dwRet);
95 #else
96 // Null is a valid return value. Posix does not provide a way to
97 // check for a "good" Null vs a "bad" Null (errno is not set).
98 void *result = pthread_getspecific(m_index);
99 #endif
100 return result;
101 }
102
103 NAMESPACE_END
104
105 #endif // THREADS_AVAILABLE
106 #endif // CRYPTOPP_IMPORTS
107