2015-11-05 06:59:46 +00:00
|
|
|
// 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
|
2016-05-03 04:23:05 +00:00
|
|
|
|
|
|
|
#if !defined(NO_OS_DEPENDENCE) && defined(THREADS_AVAILABLE)
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
#include "trdlocal.h"
|
|
|
|
|
|
|
|
#ifdef HAS_WINTHREADS
|
2016-04-28 05:05:47 +00:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2015-11-05 06:59:46 +00:00
|
|
|
#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();
|
2016-09-16 15:27:15 +00:00
|
|
|
CRYPTOPP_ASSERT(m_index != TLS_OUT_OF_INDEXES);
|
2015-11-05 06:59:46 +00:00
|
|
|
if (m_index == TLS_OUT_OF_INDEXES)
|
|
|
|
throw Err("TlsAlloc", GetLastError());
|
|
|
|
#else
|
2015-11-18 20:32:28 +00:00
|
|
|
m_index = 0;
|
2015-11-05 06:59:46 +00:00
|
|
|
int error = pthread_key_create(&m_index, NULL);
|
2016-09-16 15:27:15 +00:00
|
|
|
CRYPTOPP_ASSERT(!error);
|
2015-11-05 06:59:46 +00:00
|
|
|
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);
|
2016-09-16 15:27:15 +00:00
|
|
|
CRYPTOPP_ASSERT(rc);
|
2015-11-05 06:59:46 +00:00
|
|
|
if (!rc)
|
|
|
|
throw Err("TlsFree", GetLastError());
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
int error = pthread_key_delete(m_index);
|
2016-09-16 15:27:15 +00:00
|
|
|
CRYPTOPP_ASSERT(!error);
|
2015-11-05 06:59:46 +00:00
|
|
|
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);
|
2015-11-18 20:32:28 +00:00
|
|
|
const DWORD dwRet = GetLastError();
|
|
|
|
|
2016-09-16 15:27:15 +00:00
|
|
|
CRYPTOPP_ASSERT(result || (!result && (dwRet == NO_ERROR)));
|
2015-11-18 20:32:28 +00:00
|
|
|
if (!result && dwRet != NO_ERROR)
|
|
|
|
throw Err("TlsGetValue", dwRet);
|
2015-11-05 06:59:46 +00:00
|
|
|
#else
|
2015-11-18 20:32:28 +00:00
|
|
|
// Null is a valid return value. Posix does not provide a way to
|
|
|
|
// check for a "good" Null vs a "bad" Null (errno is not set).
|
2015-11-05 06:59:46 +00:00
|
|
|
void *result = pthread_getspecific(m_index);
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NAMESPACE_END
|
|
|
|
|
2016-05-03 04:23:05 +00:00
|
|
|
#endif // THREADS_AVAILABLE
|
|
|
|
#endif // CRYPTOPP_IMPORTS
|