No Description
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.

ThreadLocalValueImpl.h 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #pragma once
  2. #include "il2cpp-config.h"
  3. #if IL2CPP_THREADS_WIN32
  4. #include "os/c-api/il2cpp-config-platforms.h"
  5. #include "os/ErrorCodes.h"
  6. #include "utils/NonCopyable.h"
  7. // We declare windows APIs here instead of including windows system headers
  8. // to avoid leaking windows system headers to all of il2cpp and il2cpp-generated
  9. // code.
  10. // On UWP, old Windows SDKs (10586 and older) did not have support for Tls* functions
  11. // so instead we used forwarded them to Fls* equivalents. Using Tls* functions directly
  12. // with these old SDKs causes linker errors.
  13. extern "C" {
  14. __declspec(dllimport) unsigned long __stdcall TlsAlloc(void);
  15. __declspec(dllimport) void* __stdcall TlsGetValue(unsigned long dwTlsIndex);
  16. __declspec(dllimport) int __stdcall TlsSetValue(unsigned long dwTlsIndex, void* lpTlsValue);
  17. __declspec(dllimport) int __stdcall TlsFree(unsigned long dwTlsIndex);
  18. __declspec(dllimport) unsigned long __stdcall GetLastError(void);
  19. }
  20. #define IL2CPP_TLS_OUT_OF_INDEXES ((unsigned long)0xFFFFFFFF)
  21. namespace il2cpp
  22. {
  23. namespace os
  24. {
  25. class ThreadLocalValueImpl : public il2cpp::utils::NonCopyable
  26. {
  27. public:
  28. inline ThreadLocalValueImpl()
  29. {
  30. m_Index = TlsAlloc();
  31. IL2CPP_ASSERT(m_Index != IL2CPP_TLS_OUT_OF_INDEXES);
  32. }
  33. inline ~ThreadLocalValueImpl()
  34. {
  35. bool success = TlsFree(m_Index);
  36. NO_UNUSED_WARNING(success);
  37. IL2CPP_ASSERT(success);
  38. }
  39. inline ErrorCode SetValue(void* value)
  40. {
  41. if (TlsSetValue(m_Index, value) == false)
  42. return static_cast<ErrorCode>(GetLastError());
  43. return kErrorCodeSuccess;
  44. }
  45. inline ErrorCode GetValue(void** value)
  46. {
  47. *value = TlsGetValue(m_Index);
  48. if (*value)
  49. return kErrorCodeSuccess;
  50. unsigned long lastError = GetLastError();
  51. if (lastError == 0)
  52. return kErrorCodeSuccess;
  53. return static_cast<ErrorCode>(lastError);
  54. }
  55. private:
  56. unsigned long m_Index;
  57. };
  58. }
  59. }
  60. #endif // IL2CPP_THREADS_WIN32