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.

149 lines
5.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. Provides cross-platform support for thread-local objects.
  22. This class holds an internal list of objects of the templated type, keeping
  23. an instance for each thread that requests one. The first time a thread attempts
  24. to access its value, an object is created and added to the list for that thread.
  25. Typically, you'll probably want to create a static instance of a ThreadLocalValue
  26. object, or hold one within a singleton.
  27. The templated class for your value must be a primitive type, or a simple POD struct.
  28. When a thread no longer needs to use its value, it can call releaseCurrentThreadStorage()
  29. to allow the storage to be re-used by another thread. If a thread exits without calling
  30. this method, the object storage will be left allocated until the ThreadLocalValue object
  31. is deleted.
  32. @tags{Core}
  33. */
  34. template <typename Type>
  35. class ThreadLocalValue
  36. {
  37. public:
  38. /** */
  39. ThreadLocalValue() = default;
  40. /** Destructor.
  41. When this object is deleted, all the value objects for all threads will be deleted.
  42. */
  43. ~ThreadLocalValue()
  44. {
  45. for (auto* o = first.get(); o != nullptr;)
  46. {
  47. auto* next = o->next;
  48. delete o;
  49. o = next;
  50. }
  51. }
  52. /** Returns a reference to this thread's instance of the value.
  53. Note that the first time a thread tries to access the value, an instance of the
  54. value object will be created - so if your value's class has a non-trivial
  55. constructor, be aware that this method could invoke it.
  56. */
  57. Type& operator*() const noexcept { return get(); }
  58. /** Returns a pointer to this thread's instance of the value.
  59. Note that the first time a thread tries to access the value, an instance of the
  60. value object will be created - so if your value's class has a non-trivial
  61. constructor, be aware that this method could invoke it.
  62. */
  63. operator Type*() const noexcept { return &get(); }
  64. /** Accesses a method or field of the value object.
  65. Note that the first time a thread tries to access the value, an instance of the
  66. value object will be created - so if your value's class has a non-trivial
  67. constructor, be aware that this method could invoke it.
  68. */
  69. Type* operator->() const noexcept { return &get(); }
  70. /** Assigns a new value to the thread-local object. */
  71. ThreadLocalValue& operator= (const Type& newValue) { get() = newValue; return *this; }
  72. /** Returns a reference 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. Type& get() const noexcept
  78. {
  79. auto threadId = Thread::getCurrentThreadId();
  80. ObjectHolder* o = nullptr;
  81. for (o = first.get(); o != nullptr; o = o->next)
  82. if (o->threadId.get() == threadId)
  83. return o->object;
  84. for (o = first.get(); o != nullptr; o = o->next)
  85. if (o->threadId.compareAndSetBool (threadId, nullptr))
  86. break;
  87. if (o != nullptr)
  88. o->object = Type();
  89. else
  90. for (o = new ObjectHolder (threadId, first.get());
  91. ! first.compareAndSetBool (o, o->next);
  92. o->next = first.get());
  93. return o->object;
  94. }
  95. /** Called by a thread before it terminates, to allow this class to release
  96. any storage associated with the thread.
  97. */
  98. void releaseCurrentThreadStorage()
  99. {
  100. auto threadId = Thread::getCurrentThreadId();
  101. for (auto* o = first.get(); o != nullptr; o = o->next)
  102. if (o->threadId.compareAndSetBool (nullptr, threadId))
  103. return;
  104. }
  105. private:
  106. //==============================================================================
  107. struct ObjectHolder
  108. {
  109. ObjectHolder (Thread::ThreadID idToUse, ObjectHolder* n) : threadId (idToUse), next (n), object() {}
  110. Atomic<Thread::ThreadID> threadId;
  111. ObjectHolder* next;
  112. Type object;
  113. JUCE_DECLARE_NON_COPYABLE (ObjectHolder)
  114. };
  115. mutable Atomic<ObjectHolder*> first;
  116. JUCE_DECLARE_NON_COPYABLE (ThreadLocalValue)
  117. };
  118. } // namespace juce