tls.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/tls.h
  3. // Purpose: Implementation of thread local storage
  4. // Author: Vadim Zeitlin
  5. // Created: 2008-08-08
  6. // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
  7. // Licence: wxWindows licence
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_TLS_H_
  10. #define _WX_TLS_H_
  11. #include "wx/defs.h"
  12. // ----------------------------------------------------------------------------
  13. // check for compiler support of thread-specific variables
  14. // ----------------------------------------------------------------------------
  15. // when not using threads at all, there is no need for thread-specific
  16. // values to be really thread-specific
  17. #if !wxUSE_THREADS
  18. #define wxHAS_COMPILER_TLS
  19. #define wxTHREAD_SPECIFIC_DECL
  20. // otherwise try to find the compiler-specific way to handle TLS unless
  21. // explicitly disabled by setting wxUSE_COMPILER_TLS to 0 (it is 1 by default).
  22. #elif wxUSE_COMPILER_TLS
  23. // __thread keyword is not supported correctly by MinGW, at least in some
  24. // configurations, see http://sourceforge.net/support/tracker.php?aid=2837047
  25. // and when in doubt we prefer to not use it at all.
  26. #if defined(HAVE___THREAD_KEYWORD) && !defined(__MINGW32__)
  27. #define wxHAS_COMPILER_TLS
  28. #define wxTHREAD_SPECIFIC_DECL __thread
  29. // MSVC has its own version which might be supported by some other Windows
  30. // compilers, to be tested
  31. #elif wxCHECK_VISUALC_VERSION(7)
  32. #define wxHAS_COMPILER_TLS
  33. #define wxTHREAD_SPECIFIC_DECL __declspec(thread)
  34. #endif // compilers
  35. #endif // wxUSE_COMPILER_TLS
  36. // ----------------------------------------------------------------------------
  37. // define wxTLS_TYPE()
  38. // ----------------------------------------------------------------------------
  39. #ifdef wxHAS_COMPILER_TLS
  40. #define wxTLS_TYPE(T) wxTHREAD_SPECIFIC_DECL T
  41. #define wxTLS_TYPE_REF(T) T&
  42. #define wxTLS_PTR(var) (&(var))
  43. #define wxTLS_VALUE(var) (var)
  44. #else // !wxHAS_COMPILER_TLS
  45. extern "C"
  46. {
  47. typedef void (*wxTlsDestructorFunction)(void*);
  48. }
  49. #if defined(__WINDOWS__)
  50. #include "wx/msw/tls.h"
  51. #elif defined(__OS2__)
  52. #include "wx/os2/tls.h"
  53. #elif defined(__UNIX__)
  54. #include "wx/unix/tls.h"
  55. #else
  56. // TODO: we could emulate TLS for such platforms...
  57. #error Neither compiler nor OS support thread-specific variables.
  58. #endif
  59. #include <stdlib.h> // for calloc()
  60. // wxTlsValue<T> represents a thread-specific value of type T but, unlike
  61. // with native compiler thread-specific variables, it behaves like a
  62. // (never NULL) pointer to T and so needs to be dereferenced before use
  63. //
  64. // Note: T must be a POD!
  65. //
  66. // Note: On Unix, thread-specific T value is freed when the thread exits.
  67. // On Windows, thread-specific values are freed later, when given
  68. // wxTlsValue<T> is destroyed. The only exception to this is the
  69. // value for the main thread, which is always freed when
  70. // wxTlsValue<T> is destroyed.
  71. template <typename T>
  72. class wxTlsValue
  73. {
  74. public:
  75. typedef T ValueType;
  76. // ctor doesn't do anything, the object is created on first access
  77. wxTlsValue() : m_key(free) {}
  78. // dtor is only called in the main thread context and so is not enough
  79. // to free memory allocated by us for the other threads, we use
  80. // destructor function when using Pthreads for this (which is not
  81. // called for the main thread as it doesn't call pthread_exit() but
  82. // just to be safe we also reset the key anyhow)
  83. ~wxTlsValue()
  84. {
  85. if ( m_key.Get() )
  86. m_key.Set(NULL); // this deletes the value
  87. }
  88. // access the object creating it on demand
  89. ValueType *Get()
  90. {
  91. void *value = m_key.Get();
  92. if ( !value )
  93. {
  94. // ValueType must be POD to be used in wxHAS_COMPILER_TLS case
  95. // anyhow (at least gcc doesn't accept non-POD values being
  96. // declared with __thread) so initialize it as a POD too
  97. value = calloc(1, sizeof(ValueType));
  98. if ( !m_key.Set(value) )
  99. {
  100. free(value);
  101. // this will probably result in a crash in the caller but
  102. // it's arguably better to crash immediately instead of
  103. // slowly dying from out-of-memory errors which would
  104. // happen as the next access to this object would allocate
  105. // another ValueType instance and so on forever
  106. value = NULL;
  107. }
  108. }
  109. return static_cast<ValueType *>(value);
  110. }
  111. // pointer-like accessors
  112. ValueType *operator->() { return Get(); }
  113. ValueType& operator*() { return *Get(); }
  114. private:
  115. wxTlsKey m_key;
  116. DECLARE_NO_COPY_TEMPLATE_CLASS(wxTlsValue, T)
  117. };
  118. #define wxTLS_TYPE(T) wxTlsValue<T>
  119. #define wxTLS_TYPE_REF(T) wxTLS_TYPE(T)&
  120. #define wxTLS_PTR(var) ((var).Get())
  121. #define wxTLS_VALUE(var) (*(var))
  122. #endif // wxHAS_COMPILER_TLS/!wxHAS_COMPILER_TLS
  123. #endif // _WX_TLS_H_