JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_ThreadLocalValue.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26//==============================================================================
46template <typename Type>
48{
49public:
51 ThreadLocalValue() = default;
52
57 {
58 for (auto* o = first.get(); o != nullptr;)
59 {
60 auto* next = o->next;
61 delete o;
62 o = next;
63 }
64 }
65
71 Type& operator*() const noexcept { return get(); }
72
78 operator Type*() const noexcept { return &get(); }
79
85 Type* operator->() const noexcept { return &get(); }
86
88 ThreadLocalValue& operator= (const Type& newValue) { get() = newValue; return *this; }
89
95 Type& get() const noexcept
96 {
97 auto threadId = Thread::getCurrentThreadId();
98 ObjectHolder* o = nullptr;
99
100 for (o = first.get(); o != nullptr; o = o->next)
101 if (o->threadId.get() == threadId)
102 return o->object;
103
104 for (o = first.get(); o != nullptr; o = o->next)
105 if (o->threadId.compareAndSetBool (threadId, nullptr))
106 break;
107
108 if (o != nullptr)
109 o->object = Type();
110 else
111 for (o = new ObjectHolder (threadId, first.get());
112 ! first.compareAndSetBool (o, o->next);
113 o->next = first.get());
114
115 return o->object;
116 }
117
122 {
123 auto threadId = Thread::getCurrentThreadId();
124
125 for (auto* o = first.get(); o != nullptr; o = o->next)
126 if (o->threadId.compareAndSetBool (nullptr, threadId))
127 return;
128 }
129
130private:
131 //==============================================================================
132 struct ObjectHolder
133 {
134 ObjectHolder (Thread::ThreadID idToUse, ObjectHolder* n) : threadId (idToUse), next (n), object() {}
135
136 Atomic<Thread::ThreadID> threadId;
137 ObjectHolder* next;
138 Type object;
139
140 JUCE_DECLARE_NON_COPYABLE (ObjectHolder)
141 };
142
143 mutable Atomic<ObjectHolder*> first;
144
145 JUCE_DECLARE_NON_COPYABLE (ThreadLocalValue)
146};
147
148} // namespace juce
Provides cross-platform support for thread-local objects.
Type & operator*() const noexcept
Returns a reference to this thread's instance of the value.
Type * operator->() const noexcept
Accesses a method or field of the value object.
void releaseCurrentThreadStorage()
Called by a thread before it terminates, to allow this class to release any storage associated with t...
Type & get() const noexcept
Returns a reference to this thread's instance of the value.
ThreadLocalValue & operator=(const Type &newValue)
Assigns a new value to the thread-local object.
void * ThreadID
A value type used for thread IDs.
static ThreadID JUCE_CALLTYPE getCurrentThreadId()
Returns an id that identifies the caller thread.
#define JUCE_DECLARE_NON_COPYABLE(className)
This is a shorthand macro for deleting a class's copy constructor and copy assignment operator.
JUCE Namespace.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
Type get() const noexcept
Atomically reads and returns the current value.
Definition juce_Atomic.h:64
bool compareAndSetBool(Type newValue, Type valueToCompare) noexcept
Atomically compares this value with a target value, and if it is equal, sets this to be equal to a ne...
Definition juce_Atomic.h:96