summaryrefslogtreecommitdiffstats
path: root/lib/cryptopp/trdlocal.cpp
diff options
context:
space:
mode:
authorworktycho <work.tycho@gmail.com>2013-12-09 18:51:12 +0100
committerworktycho <work.tycho@gmail.com>2013-12-09 18:51:12 +0100
commit843605d59ebc128be0a578dc6f45ef8c05da6e79 (patch)
tree3ffebc6ba27baf7a9e1d4bc51501ffeea9b14226 /lib/cryptopp/trdlocal.cpp
parentmerged makefile changes (diff)
parentFix Undefined behavior at Bindings/LuaWindow line 32 (diff)
downloadcuberite-843605d59ebc128be0a578dc6f45ef8c05da6e79.tar
cuberite-843605d59ebc128be0a578dc6f45ef8c05da6e79.tar.gz
cuberite-843605d59ebc128be0a578dc6f45ef8c05da6e79.tar.bz2
cuberite-843605d59ebc128be0a578dc6f45ef8c05da6e79.tar.lz
cuberite-843605d59ebc128be0a578dc6f45ef8c05da6e79.tar.xz
cuberite-843605d59ebc128be0a578dc6f45ef8c05da6e79.tar.zst
cuberite-843605d59ebc128be0a578dc6f45ef8c05da6e79.zip
Diffstat (limited to 'lib/cryptopp/trdlocal.cpp')
-rw-r--r--lib/cryptopp/trdlocal.cpp73
1 files changed, 73 insertions, 0 deletions
diff --git a/lib/cryptopp/trdlocal.cpp b/lib/cryptopp/trdlocal.cpp
new file mode 100644
index 000000000..6d6b822c0
--- /dev/null
+++ b/lib/cryptopp/trdlocal.cpp
@@ -0,0 +1,73 @@
+// trdlocal.cpp - written and placed in the public domain by Wei Dai
+
+#include "pch.h"
+
+#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()
+{
+#ifdef HAS_WINTHREADS
+ if (!TlsFree(m_index))
+ throw Err("TlsFree", GetLastError());
+#else
+ int error = pthread_key_delete(m_index);
+ if (error)
+ 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