1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
#if JUCE_AUDIOWORKGROUP_TYPES_AVAILABLE
class WorkgroupToken::TokenProvider
{
public:
explicit TokenProvider (os_workgroup_t wg)
: workgroup (wg), attached (attach (wg, token)) {}
~TokenProvider()
{
if (attached)
detach (workgroup, token);
}
TokenProvider (const TokenProvider&) = delete;
TokenProvider (TokenProvider&& other) noexcept
: workgroup (std::exchange (other.workgroup, os_workgroup_t{})),
token (std::exchange (other.token, os_workgroup_join_token_s{})),
attached (std::exchange (other.attached, false)) {}
TokenProvider& operator= (const TokenProvider&) = delete;
TokenProvider& operator= (TokenProvider&& other) noexcept
{
TokenProvider { std::move (other) }.swap (*this);
return *this;
}
bool isAttached() const { return attached; }
os_workgroup_t getHandle() const { return workgroup; }
private:
static void detach (os_workgroup_t wg, os_workgroup_join_token_s token)
{
if (@available (macos 11.0, ios 14.0, *))
os_workgroup_leave (wg, &token);
}
static bool attach (os_workgroup_t wg, os_workgroup_join_token_s& tokenOut)
{
if (@available (macos 11.0, ios 14.0, *))
{
if (wg != nullptr && os_workgroup_join (wg, &tokenOut) == 0)
return true;
}
return false;
}
void swap (TokenProvider& other) noexcept
{
std::swap (other.workgroup, workgroup);
std::swap (other.token, token);
std::swap (other.attached, attached);
}
os_workgroup_t workgroup;
os_workgroup_join_token_s token;
bool attached;
};
class AudioWorkgroup::WorkgroupProvider
{
public:
explicit WorkgroupProvider (os_workgroup_t ptr) : handle { ptr } {}
void join (WorkgroupToken& token) const
{
if (const auto* tokenProvider = token.getTokenProvider())
if (tokenProvider->isAttached() && tokenProvider->getHandle() == handle.get())
return;
// Explicit reset before constructing the new workgroup to ensure that the old workgroup
// is left before the new one is joined.
token.reset();
if (handle.get() != nullptr)
token = WorkgroupToken { [provider = WorkgroupToken::TokenProvider { handle.get() }] { return &provider; } };
}
static os_workgroup_t getWorkgroup (const AudioWorkgroup& wg)
{
if (auto* provider = wg.getWorkgroupProvider())
return provider->handle.get();
return nullptr;
}
private:
struct ScopedWorkgroupRetainer
{
ScopedWorkgroupRetainer (os_workgroup_t wg) : handle { wg }
{
if (handle != nullptr)
os_retain (handle);
}
~ScopedWorkgroupRetainer()
{
if (handle != nullptr)
os_release (handle);
}
ScopedWorkgroupRetainer (const ScopedWorkgroupRetainer& other)
: ScopedWorkgroupRetainer { other.handle } {}
ScopedWorkgroupRetainer& operator= (const ScopedWorkgroupRetainer& other)
{
ScopedWorkgroupRetainer { other }.swap (*this);
return *this;
}
ScopedWorkgroupRetainer (ScopedWorkgroupRetainer&& other) noexcept
{
swap (other);
}
ScopedWorkgroupRetainer& operator= (ScopedWorkgroupRetainer&& other) noexcept
{
swap (other);
return *this;
}
void swap (ScopedWorkgroupRetainer& other) noexcept
{
std::swap (handle, other.handle);
}
os_workgroup_t get() const noexcept { return handle; }
private:
os_workgroup_t handle { nullptr };
};
ScopedWorkgroupRetainer handle;
};
#else
class WorkgroupToken::TokenProvider {};
class AudioWorkgroup::WorkgroupProvider
{
public:
explicit WorkgroupProvider() = default;
void join (WorkgroupToken& t) const { t.reset(); }
static void* getWorkgroup (const AudioWorkgroup&) { return nullptr; }
};
#endif
AudioWorkgroup::AudioWorkgroup (const AudioWorkgroup& other)
: erased ([&]() -> Erased
{
if (auto* p = other.getWorkgroupProvider())
return [provider = *p] { return &provider; };
return nullptr;
}()) {}
bool AudioWorkgroup::operator== (const AudioWorkgroup& other) const
{
return WorkgroupProvider::getWorkgroup (*this) == WorkgroupProvider::getWorkgroup (other);
}
void AudioWorkgroup::join (WorkgroupToken& token) const
{
#if JUCE_AUDIOWORKGROUP_TYPES_AVAILABLE
if (const auto* p = getWorkgroupProvider())
{
p->join (token);
return;
}
#endif
token.reset();
}
size_t AudioWorkgroup::getMaxParallelThreadCount() const
{
#if JUCE_AUDIOWORKGROUP_TYPES_AVAILABLE
if (@available (macos 11.0, ios 14.0, *))
{
if (auto wg = WorkgroupProvider::getWorkgroup (*this))
return (size_t) os_workgroup_max_parallel_threads (wg, nullptr);
}
#endif
return 0;
}
AudioWorkgroup::operator bool() const { return WorkgroupProvider::getWorkgroup (*this) != nullptr; }
#if JUCE_AUDIOWORKGROUP_TYPES_AVAILABLE
AudioWorkgroup makeRealAudioWorkgroup (os_workgroup_t handle)
{
if (handle == nullptr)
return AudioWorkgroup{};
return AudioWorkgroup { [provider = AudioWorkgroup::WorkgroupProvider { handle }] { return &provider; } };
}
#endif
} // namespace juce
|