File: trdlocal.cpp

package info (click to toggle)
libcrypto++ 5.6.4-8
  • links: PTS
  • area: main
  • in suites: buster, sid
  • size: 11,896 kB
  • ctags: 13,256
  • sloc: cpp: 69,231; sh: 4,117; asm: 4,090; makefile: 373
file content (106 lines) | stat: -rw-r--r-- 2,449 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// 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

#if !defined(NO_OS_DEPENDENCE) && defined(THREADS_AVAILABLE)

#include "trdlocal.h"

#ifdef HAS_WINTHREADS
#define WIN32_LEAN_AND_MEAN
#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();
	assert(m_index != TLS_OUT_OF_INDEXES);
	if (m_index == TLS_OUT_OF_INDEXES)
		throw Err("TlsAlloc", GetLastError());
#else
	m_index = 0;
	int error = pthread_key_create(&m_index, NULL);
	assert(!error);
	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);
	const DWORD dwRet = GetLastError();

	assert(result || (!result && (dwRet == NO_ERROR)));
	if (!result && dwRet != NO_ERROR)
		throw Err("TlsGetValue", dwRet);
#else
	// 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).
	void *result = pthread_getspecific(m_index);
#endif
	return result;
}

NAMESPACE_END

#endif	// THREADS_AVAILABLE
#endif  // CRYPTOPP_IMPORTS