Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

trdlocal.cpp

00001 // trdlocal.cpp - written and placed in the public domain by Wei Dai
00002 
00003 #include "pch.h"
00004 #include "trdlocal.h"
00005 
00006 #ifdef THREADS_AVAILABLE
00007 
00008 NAMESPACE_BEGIN(CryptoPP)
00009 
00010 ThreadLocalStorage::Err::Err(const std::string& operation, int error)
00011         : OS_Error(OTHER_ERROR, "ThreadLocalStorage: " + operation + " operation failed with error 0x" + IntToString(error, 16), operation, error)
00012 {
00013 }
00014 
00015 ThreadLocalStorage::ThreadLocalStorage()
00016 {
00017 #ifdef HAS_WINTHREADS
00018         m_index = TlsAlloc();
00019         if (m_index == TLS_OUT_OF_INDEXES)
00020                 throw Err("TlsAlloc", GetLastError());
00021 #else
00022         int error = pthread_key_create(&m_index, NULL);
00023         if (error)
00024                 throw Err("pthread_key_create", error);
00025 #endif
00026 }
00027 
00028 ThreadLocalStorage::~ThreadLocalStorage()
00029 {
00030 #ifdef HAS_WINTHREADS
00031         if (!TlsFree(m_index))
00032                 throw Err("TlsFree", GetLastError());
00033 #else
00034         int error = pthread_key_delete(m_index);
00035         if (error)
00036                 throw Err("pthread_key_delete", error);
00037 #endif
00038 }
00039 
00040 void ThreadLocalStorage::SetValue(void *value)
00041 {
00042 #ifdef HAS_WINTHREADS
00043         if (!TlsSetValue(m_index, value))
00044                 throw Err("TlsSetValue", GetLastError());
00045 #else
00046         int error = pthread_setspecific(m_index, value);
00047         if (error)
00048                 throw Err("pthread_key_getspecific", error);
00049 #endif
00050 }
00051 
00052 void *ThreadLocalStorage::GetValue() const
00053 {
00054 #ifdef HAS_WINTHREADS
00055         void *result = TlsGetValue(m_index);
00056         if (!result && GetLastError() != NO_ERROR)
00057                 throw Err("TlsGetValue", GetLastError());
00058 #else
00059         void *result = pthread_getspecific(m_index);
00060 #endif
00061         return result;
00062 }
00063 
00064 NAMESPACE_END
00065 
00066 #endif  // #ifdef THREADS_AVAILABLE

Generated on Mon Apr 19 18:12:33 2004 for Crypto++ by doxygen 1.3.6-20040222