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.

201 lines
7.2KB

  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. #ifndef JUCE_THREADLOCALVALUE_H_INCLUDED
  24. #define JUCE_THREADLOCALVALUE_H_INCLUDED
  25. // (NB: on win32, native thread-locals aren't possible in a dynamically loaded DLL in XP).
  26. #if ! ((JUCE_MSVC && (JUCE_64BIT || ! defined (JucePlugin_PluginCode))) \
  27. || (JUCE_MAC && JUCE_CLANG && defined (MAC_OS_X_VERSION_10_7) \
  28. && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7))
  29. #define JUCE_NO_COMPILER_THREAD_LOCAL 1
  30. #endif
  31. //==============================================================================
  32. /**
  33. Provides cross-platform support for thread-local objects.
  34. This class holds an internal list of objects of the templated type, keeping
  35. an instance for each thread that requests one. The first time a thread attempts
  36. to access its value, an object is created and added to the list for that thread.
  37. Typically, you'll probably want to create a static instance of a ThreadLocalValue
  38. object, or hold one within a singleton.
  39. The templated class for your value must be a primitive type, or a simple POD struct.
  40. When a thread no longer needs to use its value, it can call releaseCurrentThreadStorage()
  41. to allow the storage to be re-used by another thread. If a thread exits without calling
  42. this method, the object storage will be left allocated until the ThreadLocalValue object
  43. is deleted.
  44. */
  45. template <typename Type>
  46. class ThreadLocalValue
  47. {
  48. public:
  49. /** */
  50. ThreadLocalValue() noexcept
  51. {
  52. }
  53. /** Destructor.
  54. When this object is deleted, all the value objects for all threads will be deleted.
  55. */
  56. ~ThreadLocalValue()
  57. {
  58. #if JUCE_NO_COMPILER_THREAD_LOCAL
  59. for (ObjectHolder* o = first.value; o != nullptr;)
  60. {
  61. ObjectHolder* const next = o->next;
  62. delete o;
  63. o = next;
  64. }
  65. #endif
  66. }
  67. /** Returns a reference to this thread's instance of the value.
  68. Note that the first time a thread tries to access the value, an instance of the
  69. value object will be created - so if your value's class has a non-trivial
  70. constructor, be aware that this method could invoke it.
  71. */
  72. Type& operator*() const noexcept { return get(); }
  73. /** Returns a pointer to this thread's instance of the value.
  74. Note that the first time a thread tries to access the value, an instance of the
  75. value object will be created - so if your value's class has a non-trivial
  76. constructor, be aware that this method could invoke it.
  77. */
  78. operator Type*() const noexcept { return &get(); }
  79. /** Accesses a method or field of the value object.
  80. Note that the first time a thread tries to access the value, an instance of the
  81. value object will be created - so if your value's class has a non-trivial
  82. constructor, be aware that this method could invoke it.
  83. */
  84. Type* operator->() const noexcept { return &get(); }
  85. /** Assigns a new value to the thread-local object. */
  86. ThreadLocalValue& operator= (const Type& newValue) { get() = newValue; return *this; }
  87. /** Returns a reference to this thread's instance of the value.
  88. Note that the first time a thread tries to access the value, an instance of the
  89. value object will be created - so if your value's class has a non-trivial
  90. constructor, be aware that this method could invoke it.
  91. */
  92. Type& get() const noexcept
  93. {
  94. #if JUCE_NO_COMPILER_THREAD_LOCAL
  95. const Thread::ThreadID threadId = Thread::getCurrentThreadId();
  96. for (ObjectHolder* o = first.get(); o != nullptr; o = o->next)
  97. if (o->threadId == threadId)
  98. return o->object;
  99. for (ObjectHolder* o = first.get(); o != nullptr; o = o->next)
  100. {
  101. if (o->threadId == nullptr)
  102. {
  103. {
  104. SpinLock::ScopedLockType sl (lock);
  105. if (o->threadId != nullptr)
  106. continue;
  107. o->threadId = threadId;
  108. }
  109. o->object = Type();
  110. return o->object;
  111. }
  112. }
  113. ObjectHolder* const newObject = new ObjectHolder (threadId);
  114. do
  115. {
  116. newObject->next = first.get();
  117. }
  118. while (! first.compareAndSetBool (newObject, newObject->next));
  119. return newObject->object;
  120. #elif JUCE_MAC
  121. static __thread Type object;
  122. return object;
  123. #elif JUCE_MSVC
  124. static __declspec(thread) Type object;
  125. return object;
  126. #endif
  127. }
  128. /** Called by a thread before it terminates, to allow this class to release
  129. any storage associated with the thread.
  130. */
  131. void releaseCurrentThreadStorage()
  132. {
  133. #if JUCE_NO_COMPILER_THREAD_LOCAL
  134. const Thread::ThreadID threadId = Thread::getCurrentThreadId();
  135. for (ObjectHolder* o = first.get(); o != nullptr; o = o->next)
  136. {
  137. if (o->threadId == threadId)
  138. {
  139. SpinLock::ScopedLockType sl (lock);
  140. o->threadId = nullptr;
  141. }
  142. }
  143. #endif
  144. }
  145. private:
  146. //==============================================================================
  147. #if JUCE_NO_COMPILER_THREAD_LOCAL
  148. struct ObjectHolder
  149. {
  150. ObjectHolder (const Thread::ThreadID& tid)
  151. : threadId (tid), next (nullptr), object()
  152. {}
  153. Thread::ThreadID threadId;
  154. ObjectHolder* next;
  155. Type object;
  156. JUCE_DECLARE_NON_COPYABLE (ObjectHolder)
  157. };
  158. mutable Atomic<ObjectHolder*> first;
  159. SpinLock lock;
  160. #endif
  161. JUCE_DECLARE_NON_COPYABLE (ThreadLocalValue)
  162. };
  163. #endif // JUCE_THREADLOCALVALUE_H_INCLUDED