diff options
| author | Jeffrey Walton <noloader@gmail.com> | 2015-11-05 01:59:46 -0500 |
|---|---|---|
| committer | Jeffrey Walton <noloader@gmail.com> | 2015-11-05 01:59:46 -0500 |
| commit | 48809d4e85c125814425c621d8d0d89f95405924 (patch) | |
| tree | 1010fd16c4b1199f3d27dd726dda241a2bd29f83 /trdlocal.cpp | |
| parent | 025337a94aceb75d188149db70c2094673772816 (diff) | |
| download | cryptopp-git-48809d4e85c125814425c621d8d0d89f95405924.tar.gz | |
CRYPTOPP 5.6.3 RC6 checkin
Diffstat (limited to 'trdlocal.cpp')
| -rw-r--r-- | trdlocal.cpp | 171 |
1 files changed, 96 insertions, 75 deletions
diff --git a/trdlocal.cpp b/trdlocal.cpp index 9c83e125..cff44a72 100644 --- a/trdlocal.cpp +++ b/trdlocal.cpp @@ -1,75 +1,96 @@ -// trdlocal.cpp - written and placed in the public domain by Wei Dai - -#include "pch.h" - -#ifndef CRYPTOPP_IMPORTS -#ifdef THREADS_AVAILABLE - -#include "cryptlib.h" -#include "trdlocal.h" - -#ifdef HAS_WINTHREADS -#include <windows.h> -#endif - -NAMESPACE_BEGIN(CryptoPP) - -ThreadLocalStorage::Err::Err(const std::string& operation, int error) - : OS_Error(OTHER_ERROR, "ThreadLocalStorage: " + operation + " operation failed with error 0x" + IntToString(error, 16), operation, error) -{ -} - -ThreadLocalStorage::ThreadLocalStorage() -{ -#ifdef HAS_WINTHREADS - m_index = TlsAlloc(); - if (m_index == TLS_OUT_OF_INDEXES) - throw Err("TlsAlloc", GetLastError()); -#else - int error = pthread_key_create(&m_index, NULL); - if (error) - throw Err("pthread_key_create", error); -#endif -} - -// TODO: determine a library policy; implement the policy. -ThreadLocalStorage::~ThreadLocalStorage() CRYPTOPP_THROW -{ -#ifdef HAS_WINTHREADS - if (!TlsFree(m_index) && !std::uncaught_exception()) - throw Err("TlsFree", GetLastError()); -#else - int error = pthread_key_delete(m_index); - if (error && !std::uncaught_exception()) - throw Err("pthread_key_delete", error); -#endif -} - -void ThreadLocalStorage::SetValue(void *value) -{ -#ifdef HAS_WINTHREADS - if (!TlsSetValue(m_index, value)) - throw Err("TlsSetValue", GetLastError()); -#else - int error = pthread_setspecific(m_index, value); - if (error) - throw Err("pthread_key_getspecific", error); -#endif -} - -void *ThreadLocalStorage::GetValue() const -{ -#ifdef HAS_WINTHREADS - void *result = TlsGetValue(m_index); - if (!result && GetLastError() != NO_ERROR) - throw Err("TlsGetValue", GetLastError()); -#else - void *result = pthread_getspecific(m_index); -#endif - return result; -} - -NAMESPACE_END - -#endif // #ifdef THREADS_AVAILABLE -#endif +// trdlocal.cpp - written and placed in the public domain by Wei Dai
+
+#include "pch.h"
+#include "config.h"
+
+// TODO: fix this when more complete C++11 support is cut-in
+#if CRYPTOPP_MSC_VERSION
+# pragma warning(disable: 4297)
+#endif
+
+#ifndef CRYPTOPP_IMPORTS
+#ifdef THREADS_AVAILABLE
+
+#include "trdlocal.h"
+
+#ifdef HAS_WINTHREADS
+#include <windows.h>
+#endif
+
+NAMESPACE_BEGIN(CryptoPP)
+
+ThreadLocalStorage::Err::Err(const std::string& operation, int error)
+ : OS_Error(OTHER_ERROR, "ThreadLocalStorage: " + operation + " operation failed with error 0x" + IntToString(error, 16), operation, error)
+{
+}
+
+ThreadLocalStorage::ThreadLocalStorage()
+{
+#ifdef HAS_WINTHREADS
+ m_index = TlsAlloc();
+ if (m_index == TLS_OUT_OF_INDEXES)
+ throw Err("TlsAlloc", GetLastError());
+#else
+ int error = pthread_key_create(&m_index, NULL);
+ if (error)
+ throw Err("pthread_key_create", error);
+#endif
+}
+
+ThreadLocalStorage::~ThreadLocalStorage() CRYPTOPP_THROW
+{
+#ifdef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
+ if (!std::uncaught_exception())
+#else
+ try
+#endif
+#ifdef HAS_WINTHREADS
+ {
+ int rc = TlsFree(m_index);
+ assert(rc);
+ if (!rc)
+ throw Err("TlsFree", GetLastError());
+ }
+#else
+ {
+ int error = pthread_key_delete(m_index);
+ assert(!error);
+ if (error)
+ throw Err("pthread_key_delete", error);
+ }
+#endif
+#ifndef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
+ catch(const Exception&)
+ {
+ }
+#endif
+}
+
+void ThreadLocalStorage::SetValue(void *value)
+{
+#ifdef HAS_WINTHREADS
+ if (!TlsSetValue(m_index, value))
+ throw Err("TlsSetValue", GetLastError());
+#else
+ int error = pthread_setspecific(m_index, value);
+ if (error)
+ throw Err("pthread_key_getspecific", error);
+#endif
+}
+
+void *ThreadLocalStorage::GetValue() const
+{
+#ifdef HAS_WINTHREADS
+ void *result = TlsGetValue(m_index);
+ if (!result && GetLastError() != NO_ERROR)
+ throw Err("TlsGetValue", GetLastError());
+#else
+ void *result = pthread_getspecific(m_index);
+#endif
+ return result;
+}
+
+NAMESPACE_END
+
+#endif // #ifdef THREADS_AVAILABLE
+#endif
|
