monotone

monotone Mtn Source Tree

Root/cryptopp/trdlocal.cpp

1// trdlocal.cpp - written and placed in the public domain by Wei Dai
2
3#include "pch.h"
4#include "trdlocal.h"
5
6#ifdef THREADS_AVAILABLE
7
8NAMESPACE_BEGIN(CryptoPP)
9
10ThreadLocalStorage::Err::Err(const std::string& operation, int error)
11: OS_Error(OTHER_ERROR, "ThreadLocalStorage: " + operation + " operation failed with error 0x" + IntToString(error, 16), operation, error)
12{
13}
14
15ThreadLocalStorage::ThreadLocalStorage()
16{
17#ifdef HAS_WINTHREADS
18m_index = TlsAlloc();
19if (m_index == TLS_OUT_OF_INDEXES)
20throw Err("TlsAlloc", GetLastError());
21#else
22int error = pthread_key_create(&m_index, NULL);
23if (error)
24throw Err("pthread_key_create", error);
25#endif
26}
27
28ThreadLocalStorage::~ThreadLocalStorage()
29{
30#ifdef HAS_WINTHREADS
31if (!TlsFree(m_index))
32throw Err("TlsFree", GetLastError());
33#else
34int error = pthread_key_delete(m_index);
35if (error)
36throw Err("pthread_key_delete", error);
37#endif
38}
39
40void ThreadLocalStorage::SetValue(void *value)
41{
42#ifdef HAS_WINTHREADS
43if (!TlsSetValue(m_index, value))
44throw Err("TlsSetValue", GetLastError());
45#else
46int error = pthread_setspecific(m_index, value);
47if (error)
48throw Err("pthread_key_getspecific", error);
49#endif
50}
51
52void *ThreadLocalStorage::GetValue() const
53{
54#ifdef HAS_WINTHREADS
55void *result = TlsGetValue(m_index);
56if (!result && GetLastError() != NO_ERROR)
57throw Err("TlsGetValue", GetLastError());
58#else
59void *result = pthread_getspecific(m_index);
60#endif
61return result;
62}
63
64NAMESPACE_END
65
66#endif// #ifdef THREADS_AVAILABLE

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status