aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2023-12-03 23:36:07 -0800
committerChris Robinson <[email protected]>2023-12-03 23:36:07 -0800
commitb6a68e8d510610e181d638ff993e327059bd6018 (patch)
tree3c58b34d96036c3f6036bc96bc8a2f80015b1310
parent859319fa7864ed6a81af1f12c68627ab58bb7a7d (diff)
Remove some unnecessary atomic wrappers
-rw-r--r--al/auxeffectslot.cpp7
-rw-r--r--al/auxeffectslot.h2
-rw-r--r--al/buffer.cpp17
-rw-r--r--al/buffer.h2
-rw-r--r--al/source.cpp5
-rw-r--r--alc/alc.cpp3
-rw-r--r--alc/backends/base.cpp2
-rw-r--r--alc/backends/pipewire.cpp2
-rw-r--r--common/atomic.h13
-rw-r--r--common/intrusive_ptr.h2
-rw-r--r--core/context.h2
-rw-r--r--core/device.h2
-rw-r--r--core/hrtf.cpp6
-rw-r--r--core/hrtf.h2
14 files changed, 34 insertions, 33 deletions
diff --git a/al/auxeffectslot.cpp b/al/auxeffectslot.cpp
index fb646389..31e9542b 100644
--- a/al/auxeffectslot.cpp
+++ b/al/auxeffectslot.cpp
@@ -370,7 +370,7 @@ FORCE_ALIGN void AL_APIENTRY alDeleteAuxiliaryEffectSlotsDirect(ALCcontext *cont
context->setError(AL_INVALID_NAME, "Invalid effect slot ID %u", effectslots[0]);
return;
}
- if(ReadRef(slot->ref) != 0) UNLIKELY
+ if(slot->ref.load(std::memory_order_relaxed) != 0) UNLIKELY
{
context->setError(AL_INVALID_OPERATION, "Deleting in-use effect slot %u",
effectslots[0]);
@@ -390,7 +390,7 @@ FORCE_ALIGN void AL_APIENTRY alDeleteAuxiliaryEffectSlotsDirect(ALCcontext *cont
context->setError(AL_INVALID_NAME, "Invalid effect slot ID %u", effectslots[i]);
return;
}
- if(ReadRef(slot->ref) != 0) UNLIKELY
+ if(slot->ref.load(std::memory_order_relaxed) != 0) UNLIKELY
{
context->setError(AL_INVALID_OPERATION, "Deleting in-use effect slot %u",
effectslots[i]);
@@ -1530,7 +1530,8 @@ void eax_delete_al_effect_slot(ALCcontext& context, ALeffectslot& effect_slot)
std::lock_guard<std::mutex> effect_slot_lock{context.mEffectSlotLock};
- if(ReadRef(effect_slot.ref) != 0) {
+ if(effect_slot.ref.load(std::memory_order_relaxed) != 0)
+ {
ERR(EAX_PREFIX "Deleting in-use effect slot %u.\n", effect_slot.id);
return;
}
diff --git a/al/auxeffectslot.h b/al/auxeffectslot.h
index 1ad0ffc4..36216022 100644
--- a/al/auxeffectslot.h
+++ b/al/auxeffectslot.h
@@ -63,7 +63,7 @@ struct ALeffectslot {
SlotState mState{SlotState::Initial};
- RefCount ref{0u};
+ std::atomic<ALuint> ref{0u};
EffectSlot *mSlot{nullptr};
diff --git a/al/buffer.cpp b/al/buffer.cpp
index 7d043036..ae41585f 100644
--- a/al/buffer.cpp
+++ b/al/buffer.cpp
@@ -286,7 +286,7 @@ void LoadData(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq, ALuint size,
const FmtChannels DstChannels, const FmtType DstType, const std::byte *SrcData,
ALbitfieldSOFT access)
{
- if(ReadRef(ALBuf->ref) != 0 || ALBuf->MappedAccess != 0) UNLIKELY
+ if(ALBuf->ref.load(std::memory_order_relaxed) != 0 || ALBuf->MappedAccess != 0) UNLIKELY
return context->setError(AL_INVALID_OPERATION, "Modifying storage for in-use buffer %u",
ALBuf->id);
@@ -393,7 +393,7 @@ void PrepareCallback(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq,
const FmtChannels DstChannels, const FmtType DstType, ALBUFFERCALLBACKTYPESOFT callback,
void *userptr)
{
- if(ReadRef(ALBuf->ref) != 0 || ALBuf->MappedAccess != 0) UNLIKELY
+ if(ALBuf->ref.load(std::memory_order_relaxed) != 0 || ALBuf->MappedAccess != 0) UNLIKELY
return context->setError(AL_INVALID_OPERATION, "Modifying callback for in-use buffer %u",
ALBuf->id);
@@ -445,7 +445,7 @@ void PrepareCallback(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq,
void PrepareUserPtr(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq,
const FmtChannels DstChannels, const FmtType DstType, std::byte *sdata, const ALuint sdatalen)
{
- if(ReadRef(ALBuf->ref) != 0 || ALBuf->MappedAccess != 0) UNLIKELY
+ if(ALBuf->ref.load(std::memory_order_relaxed) != 0 || ALBuf->MappedAccess != 0) UNLIKELY
return context->setError(AL_INVALID_OPERATION, "Modifying storage for in-use buffer %u",
ALBuf->id);
@@ -711,7 +711,7 @@ FORCE_ALIGN void AL_APIENTRY alDeleteBuffersDirect(ALCcontext *context, ALsizei
context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", bid);
return false;
}
- if(ReadRef(ALBuf->ref) != 0) UNLIKELY
+ if(ALBuf->ref.load(std::memory_order_relaxed) != 0) UNLIKELY
{
context->setError(AL_INVALID_OPERATION, "Deleting in-use buffer %u", bid);
return false;
@@ -826,7 +826,8 @@ FORCE_ALIGN void* AL_APIENTRY alMapBufferDirectSOFT(ALCcontext *context, ALuint
else
{
ALbitfieldSOFT unavailable = (albuf->Access^access) & access;
- if(ReadRef(albuf->ref) != 0 && !(access&AL_MAP_PERSISTENT_BIT_SOFT)) UNLIKELY
+ if(albuf->ref.load(std::memory_order_relaxed) != 0
+ && !(access&AL_MAP_PERSISTENT_BIT_SOFT)) UNLIKELY
context->setError(AL_INVALID_OPERATION,
"Mapping in-use buffer %u without persistent mapping", buffer);
else if(albuf->MappedAccess != 0) UNLIKELY
@@ -1042,7 +1043,7 @@ FORCE_ALIGN void AL_APIENTRY alBufferiDirect(ALCcontext *context, ALuint buffer,
break;
case AL_AMBISONIC_LAYOUT_SOFT:
- if(ReadRef(albuf->ref) != 0) UNLIKELY
+ if(albuf->ref.load(std::memory_order_relaxed) != 0) UNLIKELY
context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's ambisonic layout",
buffer);
else if(const auto layout = AmbiLayoutFromEnum(value))
@@ -1052,7 +1053,7 @@ FORCE_ALIGN void AL_APIENTRY alBufferiDirect(ALCcontext *context, ALuint buffer,
break;
case AL_AMBISONIC_SCALING_SOFT:
- if(ReadRef(albuf->ref) != 0) UNLIKELY
+ if(albuf->ref.load(std::memory_order_relaxed) != 0) UNLIKELY
context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's ambisonic scaling",
buffer);
else if(const auto scaling = AmbiScalingFromEnum(value))
@@ -1116,7 +1117,7 @@ FORCE_ALIGN void AL_APIENTRY alBufferivDirect(ALCcontext *context, ALuint buffer
else switch(param)
{
case AL_LOOP_POINTS_SOFT:
- if(ReadRef(albuf->ref) != 0) UNLIKELY
+ if(albuf->ref.load(std::memory_order_relaxed) != 0) UNLIKELY
context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's loop points",
buffer);
else if(values[0] < 0 || values[0] >= values[1]
diff --git a/al/buffer.h b/al/buffer.h
index f936cf98..cb864aff 100644
--- a/al/buffer.h
+++ b/al/buffer.h
@@ -43,7 +43,7 @@ struct ALbuffer : public BufferStorage {
ALuint mLoopEnd{0u};
/* Number of times buffer was attached to a source (deletion can only occur when 0) */
- RefCount ref{0u};
+ std::atomic<ALuint> ref{0u};
/* Self ID */
ALuint id{0};
diff --git a/al/source.cpp b/al/source.cpp
index 73a357d4..cb24c09f 100644
--- a/al/source.cpp
+++ b/al/source.cpp
@@ -1604,10 +1604,11 @@ NOINLINE void SetProperty(ALsource *const Source, ALCcontext *const Context, con
if(!buffer) UNLIKELY
return Context->setError(AL_INVALID_VALUE, "Invalid buffer ID %s",
std::to_string(values[0]).c_str());
- if(buffer->MappedAccess && !(buffer->MappedAccess&AL_MAP_PERSISTENT_BIT_SOFT)) UNLIKELY
+ if(buffer->MappedAccess
+ && !(buffer->MappedAccess&AL_MAP_PERSISTENT_BIT_SOFT)) UNLIKELY
return Context->setError(AL_INVALID_OPERATION,
"Setting non-persistently mapped buffer %u", buffer->id);
- if(buffer->mCallback && ReadRef(buffer->ref) != 0) UNLIKELY
+ if(buffer->mCallback && buffer->ref.load(std::memory_order_relaxed) != 0) UNLIKELY
return Context->setError(AL_INVALID_OPERATION,
"Setting already-set callback buffer %u", buffer->id);
diff --git a/alc/alc.cpp b/alc/alc.cpp
index ab4cc7ba..d974777b 100644
--- a/alc/alc.cpp
+++ b/alc/alc.cpp
@@ -2514,7 +2514,8 @@ ALC_API void ALC_APIENTRY alcGetInteger64vSOFT(ALCdevice *device, ALCenum pname,
refcount = dev->waitForMix();
basecount = dev->mClockBase.load(std::memory_order_relaxed);
samplecount = dev->mSamplesDone.load(std::memory_order_relaxed);
- } while(refcount != ReadRef(dev->MixCount));
+ std::atomic_thread_fence(std::memory_order_acquire);
+ } while(refcount != dev->MixCount.load(std::memory_order_relaxed));
basecount += nanoseconds{seconds{samplecount}} / dev->Frequency;
*values = basecount.count();
}
diff --git a/alc/backends/base.cpp b/alc/backends/base.cpp
index 675d8043..b2b567a6 100644
--- a/alc/backends/base.cpp
+++ b/alc/backends/base.cpp
@@ -52,7 +52,7 @@ ClockLatency BackendBase::getClockLatency()
refcount = mDevice->waitForMix();
ret.ClockTime = mDevice->getClockTime();
std::atomic_thread_fence(std::memory_order_acquire);
- } while(refcount != ReadRef(mDevice->MixCount));
+ } while(refcount != mDevice->MixCount.load(std::memory_order_relaxed));
/* NOTE: The device will generally have about all but one periods filled at
* any given time during playback. Without a more accurate measurement from
diff --git a/alc/backends/pipewire.cpp b/alc/backends/pipewire.cpp
index e2f32758..754feb6c 100644
--- a/alc/backends/pipewire.cpp
+++ b/alc/backends/pipewire.cpp
@@ -1827,7 +1827,7 @@ ClockLatency PipeWirePlayback::getClockLatency()
mixtime = mDevice->getClockTime();
clock_gettime(CLOCK_MONOTONIC, &tspec);
std::atomic_thread_fence(std::memory_order_acquire);
- } while(refcount != ReadRef(mDevice->MixCount));
+ } while(refcount != mDevice->MixCount.load(std::memory_order_relaxed));
/* Convert the monotonic clock, stream ticks, and stream delay to
* nanoseconds.
diff --git a/common/atomic.h b/common/atomic.h
index 5e9b04c6..a579dcab 100644
--- a/common/atomic.h
+++ b/common/atomic.h
@@ -4,15 +4,12 @@
#include <atomic>
-using RefCount = std::atomic<unsigned int>;
-
-inline void InitRef(RefCount &ref, unsigned int value)
-{ ref.store(value, std::memory_order_relaxed); }
-inline unsigned int ReadRef(RefCount &ref)
-{ return ref.load(std::memory_order_acquire); }
-inline unsigned int IncrementRef(RefCount &ref)
+template<typename T>
+auto IncrementRef(std::atomic<T> &ref) noexcept
{ return ref.fetch_add(1u, std::memory_order_acq_rel)+1u; }
-inline unsigned int DecrementRef(RefCount &ref)
+
+template<typename T>
+auto DecrementRef(std::atomic<T> &ref) noexcept
{ return ref.fetch_sub(1u, std::memory_order_acq_rel)-1u; }
diff --git a/common/intrusive_ptr.h b/common/intrusive_ptr.h
index 27075347..714a5617 100644
--- a/common/intrusive_ptr.h
+++ b/common/intrusive_ptr.h
@@ -11,7 +11,7 @@ namespace al {
template<typename T>
class intrusive_ref {
- RefCount mRef{1u};
+ std::atomic<unsigned int> mRef{1u};
public:
unsigned int add_ref() noexcept { return IncrementRef(mRef); }
diff --git a/core/context.h b/core/context.h
index ccb7dd3b..6f65663f 100644
--- a/core/context.h
+++ b/core/context.h
@@ -85,7 +85,7 @@ struct ContextBase {
/* Counter for the pre-mixing updates, in 31.1 fixed point (lowest bit
* indicates if updates are currently happening).
*/
- RefCount mUpdateCount{0u};
+ std::atomic<unsigned int> mUpdateCount{0u};
std::atomic<bool> mHoldUpdates{false};
std::atomic<bool> mStopVoicesOnDisconnect{true};
diff --git a/core/device.h b/core/device.h
index 842f1d82..1f3c5105 100644
--- a/core/device.h
+++ b/core/device.h
@@ -284,7 +284,7 @@ struct DeviceBase {
* the end, so the bottom bit indicates if the device is currently mixing
* and the upper bits indicates how many mixes have been done.
*/
- RefCount MixCount{0u};
+ std::atomic<uint> MixCount{0u};
// Contexts created on this device
std::atomic<al::FlexArray<ContextBase*>*> mContexts{nullptr};
diff --git a/core/hrtf.cpp b/core/hrtf.cpp
index 9a13a004..1b7da3f9 100644
--- a/core/hrtf.cpp
+++ b/core/hrtf.cpp
@@ -392,7 +392,7 @@ std::unique_ptr<HrtfStore> CreateHrtfStore(uint rate, uint8_t irSize,
if(void *ptr{al_calloc(16, total)})
{
Hrtf.reset(al::construct_at(static_cast<HrtfStore*>(ptr)));
- InitRef(Hrtf->mRef, 1u);
+ Hrtf->mRef.store(1u, std::memory_order_relaxed);
Hrtf->mSampleRate = rate & 0xff'ff'ff;
Hrtf->mIrSize = irSize;
@@ -1459,9 +1459,9 @@ void HrtfStore::dec_ref()
auto remove_unused = [](LoadedHrtf &hrtf) -> bool
{
HrtfStore *entry{hrtf.mEntry.get()};
- if(entry && ReadRef(entry->mRef) == 0)
+ if(entry && entry->mRef.load() == 0)
{
- TRACE("Unloading unused HRTF %s\n", hrtf.mFilename.data());
+ TRACE("Unloading unused HRTF %s\n", hrtf.mFilename.c_str());
hrtf.mEntry = nullptr;
return true;
}
diff --git a/core/hrtf.h b/core/hrtf.h
index 5e6e09a8..50c4f450 100644
--- a/core/hrtf.h
+++ b/core/hrtf.h
@@ -18,7 +18,7 @@
struct HrtfStore {
- RefCount mRef;
+ std::atomic<uint> mRef;
uint mSampleRate : 24;
uint mIrSize : 8;