Crypto++  5.6.3
Free C++ class library of cryptographic schemes
trdlocal.cpp
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 
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 
29 ThreadLocalStorage::ThreadLocalStorage()
30 {
31 #ifdef HAS_WINTHREADS
32  m_index = TlsAlloc();
33  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  assert(!error);
40  if (error)
41  throw Err("pthread_key_create", error);
42 #endif
43 }
44 
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  assert(rc);
56  if (!rc)
57  throw Err("TlsFree", GetLastError());
58  }
59 #else
60  {
61  int error = pthread_key_delete(m_index);
62  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 
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 
86 void *ThreadLocalStorage::GetValue() const
87 {
88 #ifdef HAS_WINTHREADS
89  void *result = TlsGetValue(m_index);
90  const DWORD dwRet = GetLastError();
91 
92  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
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:139
The operating system reported an error.
Definition: cryptlib.h:217
Library configuration file.
STL namespace.
thread local storage
Definition: trdlocal.h:20
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:513
Crypto++ library namespace.