Fabcoin Core  0.16.2
P2P Digital Currency
trdlocal.cpp
Go to the documentation of this file.
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 
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  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 
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 
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  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 
104 
105 #endif // THREADS_AVAILABLE
106 #endif // CRYPTOPP_IMPORTS
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:140
bool error(const char *fmt, const Args &...args)
Definition: util.h:178
The operating system reported an error.
Definition: cryptlib.h:219
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
Library configuration file.
std::hash for asio::adress
Definition: Common.h:323
#define CRYPTOPP_THROW
Definition: config.h:883
#define x(i)
#define CRYPTOPP_ASSERT(exp)
Definition: trap.h:92
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:539
#define NAMESPACE_END
Definition: config.h:201