Skip to content

Commit a132957

Browse files
committed
fix: std memory ordeal
1 parent 477fc90 commit a132957

File tree

6 files changed

+52
-49
lines changed

6 files changed

+52
-49
lines changed

packages/react-native-audio-api/android/src/main/cpp/audioapi/android/core/AndroidAudioRecorder.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ ReturnStatus<std::string> AndroidAudioRecorder::start() {
114114

115115
jni::ThreadScope::WithClassLoader([this]() { nativeAudioRecorder_->start(); });
116116

117-
state_.store(RecorderState::Recording);
117+
state_.store(RecorderState::Recording, std::memory_order_release);
118118
return ReturnStatus<std::string>::Success(std::format("file://{}", filePath_));
119119
}
120120

@@ -136,7 +136,7 @@ ReturnStatus<std::tuple<std::string, double, double>> AndroidAudioRecorder::stop
136136
"Audio stream is not initialized.");
137137
}
138138

139-
state_.store(RecorderState::Idle);
139+
state_.store(RecorderState::Idle, std::memory_order_release);
140140
jni::ThreadScope::WithClassLoader([this]() { nativeAudioRecorder_->stop(); });
141141
mStream_->requestStop();
142142

@@ -183,13 +183,13 @@ ReturnStatus<std::string> AndroidAudioRecorder::enableFileOutput(
183183
filePath_ = fileResult.getValue();
184184
}
185185

186-
fileOutputEnabled_.store(true);
186+
fileOutputEnabled_.store(true, std::memory_order_release);
187187
return ReturnStatus<std::string>::Success(filePath_);
188188
}
189189

190190
void AndroidAudioRecorder::disableFileOutput() {
191191
Locker fileWriterLock(fileWriterMutex_);
192-
fileOutputEnabled_.store(false);
192+
fileOutputEnabled_.store(false, std::memory_order_release);
193193
fileWriter_ = nullptr;
194194
}
195195

@@ -199,7 +199,7 @@ void AndroidAudioRecorder::pause() {
199199
}
200200

201201
mStream_->pause(0);
202-
state_.store(RecorderState::Paused);
202+
state_.store(RecorderState::Paused, std::memory_order_release);
203203
}
204204

205205
void AndroidAudioRecorder::resume() {
@@ -208,7 +208,7 @@ void AndroidAudioRecorder::resume() {
208208
}
209209

210210
mStream_->start(0);
211-
state_.store(RecorderState::Recording);
211+
state_.store(RecorderState::Recording, std::memory_order_release);
212212
}
213213

214214
ReturnStatus<void> AndroidAudioRecorder::setOnAudioReadyCallback(
@@ -224,14 +224,14 @@ ReturnStatus<void> AndroidAudioRecorder::setOnAudioReadyCallback(
224224
callback_->prepare(streamSampleRate_, streamChannelCount_, streamMaxBufferSizeInFrames_);
225225
}
226226

227-
callbackOutputEnabled_.store(true);
227+
callbackOutputEnabled_.store(true, std::memory_order_release);
228228

229229
return ReturnStatus<void>::Success();
230230
}
231231

232232
void AndroidAudioRecorder::clearOnAudioReadyCallback() {
233233
Locker callbackLock(callbackMutex_);
234-
callbackOutputEnabled_.store(false);
234+
callbackOutputEnabled_.store(false, std::memory_order_release);
235235
callback_ = nullptr;
236236
}
237237

@@ -275,20 +275,20 @@ double AndroidAudioRecorder::getCurrentDuration() const {
275275
}
276276

277277
bool AndroidAudioRecorder::isRecording() const {
278-
return state_.load() == RecorderState::Recording &&
278+
return state_.load(std::memory_order_acquire) == RecorderState::Recording &&
279279
mStream_->getState() == oboe::StreamState::Started;
280280
}
281281

282282
bool AndroidAudioRecorder::isPaused() const {
283-
return state_.load() == RecorderState::Paused;
283+
return state_.load(std::memory_order_acquire) == RecorderState::Paused;
284284
}
285285

286286
bool AndroidAudioRecorder::isIdle() const {
287-
return state_.load() == RecorderState::Idle;
287+
return state_.load(std::memory_order_acquire) == RecorderState::Idle;
288288
}
289289

290290
void AndroidAudioRecorder::cleanup() {
291-
state_.store(RecorderState::Idle);
291+
state_.store(RecorderState::Idle, std::memory_order_release);
292292

293293
if (mStream_) {
294294
mStream_->close();
@@ -308,7 +308,7 @@ void AndroidAudioRecorder::onErrorAfterClose(oboe::AudioStream *stream, oboe::Re
308308
}
309309

310310
mStream_->requestStart();
311-
state_.store(RecorderState::Recording);
311+
state_.store(RecorderState::Recording, std::memory_order_release);
312312
}
313313
}
314314

packages/react-native-audio-api/android/src/main/cpp/audioapi/android/core/utils/miniaudioBackend/MiniAudioFileWriter.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ OpenFileStatus MiniAudioFileWriter::openFile(
8181

8282
isConverterRequired_.store(
8383
(streamSampleRate_ != properties_->sampleRate) ||
84-
(streamChannelCount_ != properties_->channelCount) ||
85-
(getDataFormat(properties_) != ma_format_f32));
84+
(streamChannelCount_ != properties_->channelCount) ||
85+
(getDataFormat(properties_) != ma_format_f32),
86+
std::memory_order_release);
8687

8788
result = initializeConverterIfNeeded();
8889

packages/react-native-audio-api/ios/audioapi/ios/core/IOSAudioPlayer.mm

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
while (processedFrames < numFrames) {
2222
int framesToProcess = std::min(numFrames - processedFrames, RENDER_QUANTUM_SIZE);
2323

24-
if (isRunning_.load()) {
24+
if (isRunning_.load(std::memory_order_acquire)) {
2525
renderAudio_(audioBus_, framesToProcess);
2626
} else {
2727
audioBus_->zero();
@@ -57,13 +57,13 @@
5757
}
5858

5959
bool success = [audioPlayer_ start];
60-
isRunning_.store(success);
60+
isRunning_.store(success, std::memory_order_release);
6161
return success;
6262
}
6363

6464
void IOSAudioPlayer::stop()
6565
{
66-
isRunning_.store(false);
66+
isRunning_.store(false, std::memory_order_release);
6767
[audioPlayer_ stop];
6868
}
6969

@@ -74,21 +74,22 @@
7474
}
7575

7676
bool success = [audioPlayer_ resume];
77-
isRunning_.store(success);
77+
isRunning_.store(success, std::memory_order_release);
7878
return success;
7979
}
8080

8181
void IOSAudioPlayer::suspend()
8282
{
83-
isRunning_.store(false);
83+
isRunning_.store(false, std::memory_order_release);
8484
[audioPlayer_ suspend];
8585
}
8686

8787
bool IOSAudioPlayer::isRunning() const
8888
{
8989
AudioEngine *audioEngine = [AudioEngine sharedInstance];
9090

91-
return isRunning_.load() && [audioEngine getState] == AudioEngineState::AudioEngineStateRunning;
91+
return isRunning_.load(std::memory_order_acquire) &&
92+
[audioEngine getState] == AudioEngineState::AudioEngineStateRunning;
9293
}
9394

9495
void IOSAudioPlayer::cleanup()

packages/react-native-audio-api/ios/audioapi/ios/core/IOSAudioRecorder.mm

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
}
118118

119119
[nativeRecorder_ start];
120-
state_.store(RecorderState::Recording);
120+
state_.store(RecorderState::Recording, std::memory_order_release);
121121
return ReturnStatus<std::string>::Success(filePath_);
122122
}
123123

@@ -134,7 +134,7 @@
134134
return ReturnStatus<std::tuple<std::string, double, double>>::Error("Not recording");
135135
}
136136

137-
state_.store(RecorderState::Idle);
137+
state_.store(RecorderState::Idle, std::memory_order_release);
138138
[nativeRecorder_ stop];
139139

140140
if (usesFileOutput()) {
@@ -177,18 +177,18 @@
177177
}
178178

179179
// TODO: atomic szpont?
180-
if (errorCallbackId_.load() != 0) {
181-
fileWriter_->setOnErrorCallback(errorCallbackId_.load());
180+
if (errorCallbackId_.load(std::memory_order_acquire) != 0) {
181+
fileWriter_->setOnErrorCallback(errorCallbackId_.load(std::memory_order_acquire));
182182
}
183183

184-
fileOutputEnabled_.store(true);
184+
fileOutputEnabled_.store(true, std::memory_order_release);
185185
return ReturnStatus<std::string>::Success(filePath_);
186186
}
187187

188188
void IOSAudioRecorder::disableFileOutput()
189189
{
190190
Locker lock(fileWriterMutex_);
191-
fileOutputEnabled_.store(false);
191+
fileOutputEnabled_.store(false, std::memory_order_release);
192192
fileWriter_ = nullptr;
193193
}
194194

@@ -202,14 +202,14 @@
202202
[nativeRecorder_ getBufferSize], [nativeRecorder_ getInputFormat].channelCount);
203203
}
204204

205-
isConnected_.store(true);
205+
isConnected_.store(true, std::memory_order_release);
206206
}
207207

208208
void IOSAudioRecorder::disconnect()
209209
{
210210
Locker lock(adapterNodeMutex_);
211211
adapterNode_ = nullptr;
212-
isConnected_.store(false);
212+
isConnected_.store(false, std::memory_order_release);
213213
}
214214

215215
void IOSAudioRecorder::pause()
@@ -219,7 +219,7 @@
219219
}
220220

221221
[nativeRecorder_ pause];
222-
state_.store(RecorderState::Paused);
222+
state_.store(RecorderState::Paused, std::memory_order_release);
223223
}
224224

225225
void IOSAudioRecorder::resume()
@@ -229,20 +229,20 @@
229229
}
230230

231231
[nativeRecorder_ resume];
232-
state_.store(RecorderState::Recording);
232+
state_.store(RecorderState::Recording, std::memory_order_release);
233233
}
234234

235235
bool IOSAudioRecorder::isRecording() const
236236
{
237237
AudioEngine *audioEngine = [AudioEngine sharedInstance];
238-
return state_.load() == RecorderState::Recording &&
238+
return state_.load(std::memory_order_acquire) == RecorderState::Recording &&
239239
[audioEngine getState] == AudioEngineState::AudioEngineStateRunning;
240240
}
241241

242242
bool IOSAudioRecorder::isPaused() const
243243
{
244244
AudioEngine *audioEngine = [AudioEngine sharedInstance];
245-
auto currentState = state_.load();
245+
auto currentState = state_.load(std::memory_order_acquire);
246246

247247
if (currentState == RecorderState::Idle) {
248248
return false;
@@ -254,7 +254,7 @@
254254

255255
bool IOSAudioRecorder::isIdle() const
256256
{
257-
return state_.load() == RecorderState::Idle;
257+
return state_.load(std::memory_order_acquire) == RecorderState::Idle;
258258
}
259259

260260
ReturnStatus<void> IOSAudioRecorder::setOnAudioReadyCallback(
@@ -277,18 +277,19 @@
277277
}
278278
}
279279

280-
if (errorCallbackId_.load() != 0) {
281-
callback_->setOnErrorCallback(errorCallbackId_.load());
280+
// TODO: atomic szpont?
281+
if (errorCallbackId_.load(std::memory_order_acquire) != 0) {
282+
callback_->setOnErrorCallback(errorCallbackId_.load(std::memory_order_acquire));
282283
}
283284

284-
callbackOutputEnabled_.store(true);
285+
callbackOutputEnabled_.store(true, std::memory_order_release);
285286
return ReturnStatus<void>::Success();
286287
}
287288

288289
void IOSAudioRecorder::clearOnAudioReadyCallback()
289290
{
290291
Locker lock(callbackMutex_);
291-
callbackOutputEnabled_.store(false);
292+
callbackOutputEnabled_.store(false, std::memory_order_release);
292293
callback_ = nullptr;
293294
}
294295

@@ -301,12 +302,12 @@
301302
if (usesCallback()) {
302303
callback_->setOnErrorCallback(callbackId);
303304
}
304-
errorCallbackId_.store(callbackId);
305+
errorCallbackId_.store(callbackId, std::memory_order_release);
305306
}
306307

307308
void IOSAudioRecorder::clearOnErrorCallback()
308309
{
309-
errorCallbackId_.store(0);
310+
errorCallbackId_.store(0, std::memory_order_release);
310311

311312
if (usesFileOutput()) {
312313
fileWriter_->clearOnErrorCallback();

packages/react-native-audio-api/ios/audioapi/ios/core/utils/FileWriter.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
}
121121

122122
fileURL_ = nil;
123-
framesWritten_.store(0);
123+
framesWritten_.store(0, std::memory_order_release);
124124

125125
return ReturnStatus<std::tuple<double, double>>::Success(
126126
std::make_tuple(fileDuration, fileSizeBytesMb));
@@ -224,7 +224,7 @@
224224

225225
void FileWriter::setOnErrorCallback(uint64_t callbackId)
226226
{
227-
errorCallbackId_.store(callbackId);
227+
errorCallbackId_.store(callbackId, std::memory_order_release);
228228
}
229229

230230
void FileWriter::clearOnErrorCallback()
@@ -234,7 +234,7 @@
234234

235235
void FileWriter::invokeOnErrorCallback(const std::string &message)
236236
{
237-
uint64_t callbackId = errorCallbackId_.load();
237+
uint64_t callbackId = errorCallbackId_.load(std::memory_order_acquire);
238238

239239
// TODO: only the line above is atomic, which means that between reading the callbackId and invoking the callback,
240240
// the callback could be cleared. We need to ensure that the callback is still valid when invoking it.

packages/react-native-audio-api/ios/audioapi/ios/core/utils/RecorderCallback.mm

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@
3434
circularBus_[i] = busAudioArray;
3535
}
3636

37-
isInitialized_.store(true);
37+
isInitialized_.store(true, std::memory_order_release);
3838
}
3939

4040
RecorderCallback::~RecorderCallback()
4141
{
42-
isInitialized_.store(false);
42+
isInitialized_.store(false, std::memory_order_release);
4343
@autoreleasepool {
4444
converter_ = nil;
4545
bufferFormat_ = nil;
@@ -114,7 +114,7 @@
114114

115115
void RecorderCallback::receiveAudioData(const AudioBufferList *inputBuffer, int numFrames)
116116
{
117-
if (!isInitialized_.load()) {
117+
if (!isInitialized_.load(std::memory_order_acquire)) {
118118
return;
119119
}
120120

@@ -222,17 +222,17 @@
222222

223223
void RecorderCallback::setOnErrorCallback(uint64_t callbackId)
224224
{
225-
errorCallbackId_.store(callbackId);
225+
errorCallbackId_.store(callbackId, std::memory_order_release);
226226
}
227227

228228
void RecorderCallback::clearOnErrorCallback()
229229
{
230-
errorCallbackId_.store(0);
230+
errorCallbackId_.store(0, std::memory_order_release);
231231
}
232232

233233
void RecorderCallback::invokeOnErrorCallback(const std::string &message)
234234
{
235-
uint64_t callbackId = errorCallbackId_.load();
235+
uint64_t callbackId = errorCallbackId_.load(std::memory_order_acquire);
236236

237237
// TODO: only the line above is atomic, which means that between reading the callbackId and invoking the callback,
238238
// the callback could be cleared. We need to ensure that the callback is still valid when invoking it.

0 commit comments

Comments
 (0)