The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

200 lines
7.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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 could be a primitive type, or any class that
  38. has a default constructor and copy operator.
  39. When a thread no longer needs to use its value, it can call releaseCurrentThreadStorage()
  40. to allow the storage to be re-used by another thread. If a thread exits without calling
  41. this method, the object storage will be left allocated until the ThreadLocalValue object
  42. is deleted.
  43. */
  44. template <typename Type>
  45. class ThreadLocalValue
  46. {
  47. public:
  48. /** */
  49. ThreadLocalValue() noexcept
  50. {
  51. }
  52. /** Destructor.
  53. When this object is deleted, all the value objects for all threads will be deleted.
  54. */
  55. ~ThreadLocalValue()
  56. {
  57. #if JUCE_NO_COMPILER_THREAD_LOCAL
  58. for (ObjectHolder* o = first.value; o != nullptr;)
  59. {
  60. ObjectHolder* const next = o->next;
  61. delete o;
  62. o = next;
  63. }
  64. #endif
  65. }
  66. /** Returns a reference to this thread's instance of the value.
  67. Note that the first time a thread tries to access the value, an instance of the
  68. value object will be created - so if your value's class has a non-trivial
  69. constructor, be aware that this method could invoke it.
  70. */
  71. Type& operator*() const noexcept { return get(); }
  72. /** Returns a pointer to this thread's instance of the value.
  73. Note that the first time a thread tries to access the value, an instance of the
  74. value object will be created - so if your value's class has a non-trivial
  75. constructor, be aware that this method could invoke it.
  76. */
  77. operator Type*() const noexcept { return &get(); }
  78. /** Accesses a method or field of the value object.
  79. Note that the first time a thread tries to access the value, an instance of the
  80. value object will be created - so if your value's class has a non-trivial
  81. constructor, be aware that this method could invoke it.
  82. */
  83. Type* operator->() const noexcept { return &get(); }
  84. /** Assigns a new value to the thread-local object. */
  85. ThreadLocalValue& operator= (const Type& newValue) { get() = newValue; return *this; }
  86. /** Returns a reference to this thread's instance of the value.
  87. Note that the first time a thread tries to access the value, an instance of the
  88. value object will be created - so if your value's class has a non-trivial
  89. constructor, be aware that this method could invoke it.
  90. */
  91. Type& get() const noexcept
  92. {
  93. #if JUCE_NO_COMPILER_THREAD_LOCAL
  94. const Thread::ThreadID threadId = Thread::getCurrentThreadId();
  95. for (ObjectHolder* o = first.get(); o != nullptr; o = o->next)
  96. if (o->threadId == threadId)
  97. return o->object;
  98. for (ObjectHolder* o = first.get(); o != nullptr; o = o->next)
  99. {
  100. if (o->threadId == nullptr)
  101. {
  102. {
  103. SpinLock::ScopedLockType sl (lock);
  104. if (o->threadId != nullptr)
  105. continue;
  106. o->threadId = threadId;
  107. }
  108. o->object = Type();
  109. return o->object;
  110. }
  111. }
  112. ObjectHolder* const newObject = new ObjectHolder (threadId);
  113. do
  114. {
  115. newObject->next = first.get();
  116. }
  117. while (! first.compareAndSetBool (newObject, newObject->next));
  118. return newObject->object;
  119. #elif JUCE_MAC
  120. static __thread Type object;
  121. return object;
  122. #elif JUCE_MSVC
  123. static __declspec(thread) Type object;
  124. return object;
  125. #endif
  126. }
  127. /** Called by a thread before it terminates, to allow this class to release
  128. any storage associated with the thread.
  129. */
  130. void releaseCurrentThreadStorage()
  131. {
  132. #if JUCE_NO_COMPILER_THREAD_LOCAL
  133. const Thread::ThreadID threadId = Thread::getCurrentThreadId();
  134. for (ObjectHolder* o = first.get(); o != nullptr; o = o->next)
  135. {
  136. if (o->threadId == threadId)
  137. {
  138. SpinLock::ScopedLockType sl (lock);
  139. o->threadId = nullptr;
  140. }
  141. }
  142. #endif
  143. }
  144. private:
  145. //==============================================================================
  146. #if JUCE_NO_COMPILER_THREAD_LOCAL
  147. struct ObjectHolder
  148. {
  149. ObjectHolder (const Thread::ThreadID& tid)
  150. : threadId (tid), next (nullptr), object()
  151. {}
  152. Thread::ThreadID threadId;
  153. ObjectHolder* next;
  154. Type object;
  155. JUCE_DECLARE_NON_COPYABLE (ObjectHolder)
  156. };
  157. mutable Atomic<ObjectHolder*> first;
  158. SpinLock lock;
  159. #endif
  160. JUCE_DECLARE_NON_COPYABLE (ThreadLocalValue)
  161. };
  162. #endif // JUCE_THREADLOCALVALUE_H_INCLUDED