juce_ThreadLocalValue.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. #ifndef JUCE_THREADLOCALVALUE_H_INCLUDED
  22. #define JUCE_THREADLOCALVALUE_H_INCLUDED
  23. // (NB: on win32, native thread-locals aren't possible in a dynamically loaded DLL in XP).
  24. #if ! ((JUCE_MSVC && (JUCE_64BIT || ! defined (JucePlugin_PluginCode))) \
  25. || (JUCE_MAC && JUCE_CLANG && defined (MAC_OS_X_VERSION_10_7) \
  26. && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7))
  27. #define JUCE_NO_COMPILER_THREAD_LOCAL 1
  28. #endif
  29. //==============================================================================
  30. /**
  31. Provides cross-platform support for thread-local objects.
  32. This class holds an internal list of objects of the templated type, keeping
  33. an instance for each thread that requests one. The first time a thread attempts
  34. to access its value, an object is created and added to the list for that thread.
  35. Typically, you'll probably want to create a static instance of a ThreadLocalValue
  36. object, or hold one within a singleton.
  37. The templated class for your value must be a primitive type, or a simple POD struct.
  38. When a thread no longer needs to use its value, it can call releaseCurrentThreadStorage()
  39. to allow the storage to be re-used by another thread. If a thread exits without calling
  40. this method, the object storage will be left allocated until the ThreadLocalValue object
  41. is deleted.
  42. */
  43. template <typename Type>
  44. class ThreadLocalValue
  45. {
  46. public:
  47. /** */
  48. ThreadLocalValue() noexcept
  49. {
  50. }
  51. /** Destructor.
  52. When this object is deleted, all the value objects for all threads will be deleted.
  53. */
  54. ~ThreadLocalValue()
  55. {
  56. #if JUCE_NO_COMPILER_THREAD_LOCAL
  57. for (ObjectHolder* o = first.value; o != nullptr;)
  58. {
  59. ObjectHolder* const next = o->next;
  60. delete o;
  61. o = next;
  62. }
  63. #endif
  64. }
  65. /** Returns a reference to this thread's instance of the value.
  66. Note that the first time a thread tries to access the value, an instance of the
  67. value object will be created - so if your value's class has a non-trivial
  68. constructor, be aware that this method could invoke it.
  69. */
  70. Type& operator*() const noexcept { return get(); }
  71. /** Returns a pointer to this thread's instance of the value.
  72. Note that the first time a thread tries to access the value, an instance of the
  73. value object will be created - so if your value's class has a non-trivial
  74. constructor, be aware that this method could invoke it.
  75. */
  76. operator Type*() const noexcept { return &get(); }
  77. /** Accesses a method or field of the value object.
  78. Note that the first time a thread tries to access the value, an instance of the
  79. value object will be created - so if your value's class has a non-trivial
  80. constructor, be aware that this method could invoke it.
  81. */
  82. Type* operator->() const noexcept { return &get(); }
  83. /** Assigns a new value to the thread-local object. */
  84. ThreadLocalValue& operator= (const Type& newValue) { get() = newValue; return *this; }
  85. /** Returns a reference to this thread's instance of the value.
  86. Note that the first time a thread tries to access the value, an instance of the
  87. value object will be created - so if your value's class has a non-trivial
  88. constructor, be aware that this method could invoke it.
  89. */
  90. Type& get() const noexcept
  91. {
  92. #if JUCE_NO_COMPILER_THREAD_LOCAL
  93. const Thread::ThreadID threadId = Thread::getCurrentThreadId();
  94. for (ObjectHolder* o = first.get(); o != nullptr; o = o->next)
  95. if (o->threadId == threadId)
  96. return o->object;
  97. for (ObjectHolder* o = first.get(); o != nullptr; o = o->next)
  98. {
  99. if (o->threadId == nullptr)
  100. {
  101. {
  102. SpinLock::ScopedLockType sl (lock);
  103. if (o->threadId != nullptr)
  104. continue;
  105. o->threadId = threadId;
  106. }
  107. o->object = Type();
  108. return o->object;
  109. }
  110. }
  111. ObjectHolder* const newObject = new ObjectHolder (threadId);
  112. do
  113. {
  114. newObject->next = first.get();
  115. }
  116. while (! first.compareAndSetBool (newObject, newObject->next));
  117. return newObject->object;
  118. #elif JUCE_MAC
  119. static __thread Type object;
  120. return object;
  121. #elif JUCE_MSVC
  122. static __declspec(thread) Type object;
  123. return object;
  124. #endif
  125. }
  126. /** Called by a thread before it terminates, to allow this class to release
  127. any storage associated with the thread.
  128. */
  129. void releaseCurrentThreadStorage()
  130. {
  131. #if JUCE_NO_COMPILER_THREAD_LOCAL
  132. const Thread::ThreadID threadId = Thread::getCurrentThreadId();
  133. for (ObjectHolder* o = first.get(); o != nullptr; o = o->next)
  134. {
  135. if (o->threadId == threadId)
  136. {
  137. SpinLock::ScopedLockType sl (lock);
  138. o->threadId = nullptr;
  139. }
  140. }
  141. #endif
  142. }
  143. private:
  144. //==============================================================================
  145. #if JUCE_NO_COMPILER_THREAD_LOCAL
  146. struct ObjectHolder
  147. {
  148. ObjectHolder (const Thread::ThreadID& tid)
  149. : threadId (tid), next (nullptr), object()
  150. {}
  151. Thread::ThreadID threadId;
  152. ObjectHolder* next;
  153. Type object;
  154. JUCE_DECLARE_NON_COPYABLE (ObjectHolder)
  155. };
  156. mutable Atomic<ObjectHolder*> first;
  157. SpinLock lock;
  158. #endif
  159. JUCE_DECLARE_NON_COPYABLE (ThreadLocalValue)
  160. };
  161. #endif // JUCE_THREADLOCALVALUE_H_INCLUDED