Skip to content

Commit

Permalink
waver : fix data race with ggwave instance + v1.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ggerganov committed Jul 2, 2021
1 parent e2ef350 commit 59f576f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
15 changes: 8 additions & 7 deletions examples/ggwave-common-sdl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ SDL_AudioDeviceID g_devIdOut = 0;
SDL_AudioSpec g_obtainedSpecInp;
SDL_AudioSpec g_obtainedSpecOut;

GGWave *g_ggWave = nullptr;
std::shared_ptr<GGWave> g_ggWave = nullptr;

}

Expand Down Expand Up @@ -213,9 +213,7 @@ bool GGWave_init(
}

if (reinit) {
if (g_ggWave) delete g_ggWave;

g_ggWave = new GGWave({
g_ggWave = std::make_shared<GGWave>(GGWave::Parameters {
payloadLength,
(float) g_obtainedSpecInp.freq,
(float) g_obtainedSpecOut.freq,
Expand All @@ -228,7 +226,11 @@ bool GGWave_init(
return true;
}

GGWave *& GGWave_instance() { return g_ggWave; }
std::shared_ptr<GGWave> GGWave_instance() { return g_ggWave; }

void GGWave_reset(void * parameters) {
g_ggWave = std::make_shared<GGWave>(*(GGWave::Parameters *)(parameters));
}

bool GGWave_mainLoop() {
if (g_devIdInp == 0 && g_devIdOut == 0) {
Expand Down Expand Up @@ -278,8 +280,7 @@ bool GGWave_deinit() {
return false;
}

delete g_ggWave;
g_ggWave = nullptr;
g_ggWave.reset();

SDL_PauseAudioDevice(g_devIdInp, 1);
SDL_CloseAudioDevice(g_devIdInp);
Expand Down
4 changes: 3 additions & 1 deletion examples/ggwave-common-sdl2.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#pragma once

#include <string>
#include <memory>

class GGWave;

// GGWave helpers

void GGWave_setDefaultCaptureDeviceName(std::string name);
bool GGWave_init(const int playbackId, const int captureId, const int payloadLength = -1, const float sampleRateOffset = 0);
GGWave *& GGWave_instance();
std::shared_ptr<GGWave> GGWave_instance();
void GGWave_reset(void * parameters);
bool GGWave_mainLoop();
bool GGWave_deinit();
18 changes: 12 additions & 6 deletions examples/waver/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,11 @@ void updateCore() {
static int rxDataLengthLast = 0;
static float rxTimestampLast = 0.0f;
static GGWave::TxRxData rxDataLast;
static auto & ggWave = GGWave_instance();

auto ggWave = GGWave_instance();
if (ggWave == nullptr) {
return;
}

{
std::lock_guard<std::mutex> lock(g_buffer.mutex);
Expand All @@ -613,16 +617,18 @@ void updateCore() {
GGWave::SampleFormat sampleFormatOutOld = ggWave->getSampleFormatOut();
auto rxProtocolsOld = ggWave->getRxProtocols();

if (ggWave) delete ggWave;

ggWave = new GGWave({
GGWave::Parameters parameters {
inputCurrent.payloadLength,
sampleRateInpOld,
sampleRateOutOld + inputCurrent.sampleRateOffset,
GGWave::kDefaultSamplesPerFrame,
GGWave::kDefaultSoundMarkerThreshold,
sampleFormatInpOld,
sampleFormatOutOld});
sampleFormatOutOld
};

GGWave_reset(&parameters);
ggWave = GGWave_instance();

ggWave->setRxProtocols(rxProtocolsOld);
}
Expand Down Expand Up @@ -1016,7 +1022,7 @@ void renderMain() {

if (windowId == WindowId::Settings) {
ImGui::BeginChild("Settings:main", ImGui::GetContentRegionAvail(), true, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse);
ImGui::Text("Waver v1.4.0");
ImGui::Text("Waver v1.4.1");
ImGui::Separator();

ImGui::Text("%s", "");
Expand Down

0 comments on commit 59f576f

Please sign in to comment.