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.

197 lines
7.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #pragma once
  24. // (NB: on win32, native thread-locals aren't possible in a dynamically loaded DLL in XP).
  25. #if ! ((JUCE_MSVC && (JUCE_64BIT || ! defined (JucePlugin_PluginCode))) \
  26. || (JUCE_MAC && JUCE_CLANG && defined (MAC_OS_X_VERSION_10_7) \
  27. && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7))
  28. #define JUCE_NO_COMPILER_THREAD_LOCAL 1
  29. #endif
  30. //==============================================================================
  31. /**
  32. Provides cross-platform support for thread-local objects.
  33. This class holds an internal list of objects of the templated type, keeping
  34. an instance for each thread that requests one. The first time a thread attempts
  35. to access its value, an object is created and added to the list for that thread.
  36. Typically, you'll probably want to create a static instance of a ThreadLocalValue
  37. object, or hold one within a singleton.
  38. The templated class for your value must be a primitive type, or a simple POD struct.
  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. };