I am converting the example code in Learning CoreAudio by Adamson & Avila to Swift. In one of the examples, they use Apple's CARingBuffer C++ code. In trying to get that working, I get a warning:
OSAtomicCompareAndSwap32Barrier' is deprecated: first deprecated in macOS 10.12 - Use std::atomic_compare_exchange_strong() from <atomic> instead
I'm not familiar with C++ and I'm having trouble figuring out how to use atomic_compare_exchange_strong(). I've also had trouble figuring out what OSAtomicCompareAndSwap32Barrier is supposed to do.
The only place it is called in CARingBuffer is
void CARingBuffer::SetTimeBounds(SampleTime startTime, SampleTime endTime)
{
UInt32 nextPtr = mTimeBoundsQueuePtr + 1;
UInt32 index = nextPtr & kGeneralRingTimeBoundsQueueMask;
mTimeBoundsQueue[index].mStartTime = startTime;
mTimeBoundsQueue[index].mEndTime = endTime;
mTimeBoundsQueue[index].mUpdateCounter = nextPtr;
CAAtomicCompareAndSwap32Barrier(mTimeBoundsQueuePtr, mTimeBoundsQueuePtr + 1, (SInt32*)&mTimeBoundsQueuePtr);
}
The call to CAAtomicCompareAndSwap32Barrier directly calls OSAtomicCompareAndSwap32Barrier.
Even with the deprecation warning, the code performs as expected, but I'd like to eliminate the warning.
In this case, I found an answer. My C++ is rusty, but this works.
The purpose of the call was to increment a pointer (mTimeBoundsQueuePtr) in a thread-safe manner.
In CARingBuffer.h, at the top of the file, I added
#include <atomic>
and changed the declaration of mTimeBoundsQueuePtr at the bottom from
UInt32 mTimeBoundsQueuePtr;
to
std::atomic<UInt32> mTimeBoundsQueuePtr;
Then in CARingBuffer.cpp, I changed the last line of CARingBuffer::SetTimeBounds
from
CAAtomicCompareAndSwap32Barrier(mTimeBoundsQueuePtr, mTimeBoundsQueuePtr + 1, (SInt32*)&mTimeBoundsQueuePtr);
to
mTimeBoundsQueuePtr++;