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.

179 lines
6.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. //==============================================================================
  19. /**
  20. Provides cross-platform support for thread-local objects.
  21. This class holds an internal list of objects of the templated type, keeping
  22. an instance for each thread that requests one. The first time a thread attempts
  23. to access its value, an object is created and added to the list for that thread.
  24. Typically, you'll probably want to create a static instance of a ThreadLocalValue
  25. object, or hold one within a singleton.
  26. The templated class for your value must be a primitive type, or a simple POD struct.
  27. When a thread no longer needs to use its value, it can call releaseCurrentThreadStorage()
  28. to allow the storage to be re-used by another thread. If a thread exits without calling
  29. this method, the object storage will be left allocated until the ThreadLocalValue object
  30. is deleted.
  31. */
  32. template <typename Type>
  33. class ThreadLocalValue
  34. {
  35. public:
  36. /** */
  37. ThreadLocalValue() noexcept
  38. {
  39. }
  40. /** Destructor.
  41. When this object is deleted, all the value objects for all threads will be deleted.
  42. */
  43. ~ThreadLocalValue()
  44. {
  45. #if ! JUCE_COMPILER_SUPPORTS_THREAD_LOCAL
  46. for (ObjectHolder* o = first.value; o != nullptr;)
  47. {
  48. ObjectHolder* const next = o->next;
  49. delete o;
  50. o = next;
  51. }
  52. #endif
  53. }
  54. /** Returns a reference to this thread's instance of the value.
  55. Note that the first time a thread tries to access the value, an instance of the
  56. value object will be created - so if your value's class has a non-trivial
  57. constructor, be aware that this method could invoke it.
  58. */
  59. Type& operator*() const noexcept { return get(); }
  60. /** Returns a pointer to this thread's instance of the value.
  61. Note that the first time a thread tries to access the value, an instance of the
  62. value object will be created - so if your value's class has a non-trivial
  63. constructor, be aware that this method could invoke it.
  64. */
  65. operator Type*() const noexcept { return &get(); }
  66. /** Accesses a method or field of the value object.
  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. /** Assigns a new value to the thread-local object. */
  73. ThreadLocalValue& operator= (const Type& newValue) { get() = newValue; return *this; }
  74. /** Returns a reference to this thread's instance of the value.
  75. Note that the first time a thread tries to access the value, an instance of the
  76. value object will be created - so if your value's class has a non-trivial
  77. constructor, be aware that this method could invoke it.
  78. */
  79. Type& get() const noexcept
  80. {
  81. #if JUCE_COMPILER_SUPPORTS_THREAD_LOCAL
  82. static thread_local HashMap<const void*, Type> holder;
  83. return holder.getReference (this);
  84. #else
  85. const Thread::ThreadID threadId = Thread::getCurrentThreadId();
  86. for (ObjectHolder* o = first.get(); o != nullptr; o = o->next)
  87. if (o->threadId == threadId)
  88. return o->object;
  89. for (ObjectHolder* o = first.get(); o != nullptr; o = o->next)
  90. {
  91. if (o->threadId == nullptr)
  92. {
  93. {
  94. SpinLock::ScopedLockType sl (lock);
  95. if (o->threadId != nullptr)
  96. continue;
  97. o->threadId = threadId;
  98. }
  99. o->object = Type();
  100. return o->object;
  101. }
  102. }
  103. ObjectHolder* const newObject = new ObjectHolder (threadId);
  104. do
  105. {
  106. newObject->next = first.get();
  107. }
  108. while (! first.compareAndSetBool (newObject, newObject->next));
  109. return newObject->object;
  110. #endif
  111. }
  112. /** Called by a thread before it terminates, to allow this class to release
  113. any storage associated with the thread.
  114. */
  115. void releaseCurrentThreadStorage()
  116. {
  117. #if ! JUCE_COMPILER_SUPPORTS_THREAD_LOCAL
  118. const Thread::ThreadID threadId = Thread::getCurrentThreadId();
  119. for (ObjectHolder* o = first.get(); o != nullptr; o = o->next)
  120. {
  121. if (o->threadId == threadId)
  122. {
  123. SpinLock::ScopedLockType sl (lock);
  124. o->threadId = nullptr;
  125. }
  126. }
  127. #endif
  128. }
  129. private:
  130. //==============================================================================
  131. #if ! JUCE_COMPILER_SUPPORTS_THREAD_LOCAL
  132. struct ObjectHolder
  133. {
  134. ObjectHolder (const Thread::ThreadID& tid)
  135. : threadId (tid), next (nullptr), object()
  136. {}
  137. Thread::ThreadID threadId;
  138. ObjectHolder* next;
  139. Type object;
  140. JUCE_DECLARE_NON_COPYABLE (ObjectHolder)
  141. };
  142. mutable Atomic<ObjectHolder*> first;
  143. SpinLock lock;
  144. #endif
  145. JUCE_DECLARE_NON_COPYABLE (ThreadLocalValue)
  146. };