Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.5 KiB

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