From 38336a9f1c036b49adf8aae3682f20457a02a689 Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Fri, 8 Jan 2021 19:24:46 +0200 Subject: [PATCH] waver : minor data race fix + back to separate Core thread --- examples/waver/common.cpp | 35 +++++++++++++++++++++------- examples/waver/common.h | 3 +++ examples/waver/main.cpp | 49 ++++++++++++++++++++++++++++----------- 3 files changed, 66 insertions(+), 21 deletions(-) diff --git a/examples/waver/common.cpp b/examples/waver/common.cpp index 492c239..765342d 100644 --- a/examples/waver/common.cpp +++ b/examples/waver/common.cpp @@ -14,14 +14,16 @@ #include #include +#include #include #include -#include +#include #include +#include +#include +#include #include #include -#include -#include #if defined(IOS) || defined(ANDROID) #include "imgui-wrapper/icons_font_awesome.h" @@ -32,7 +34,10 @@ #endif namespace { +std::mutex g_mutex; char * toTimeString(const std::chrono::system_clock::time_point & tp) { + std::lock_guard lock(g_mutex); + time_t t = std::chrono::system_clock::to_time_t(tp); std::tm * ptm = std::localtime(&t); static char buffer[32]; @@ -186,6 +191,8 @@ struct Input { }; struct Buffer { + std::mutex mutex; + State stateCore; Input inputCore; @@ -193,7 +200,7 @@ struct Buffer { Input inputUI; }; -bool g_isRunning; +std::atomic g_isRunning; GGWave * g_ggWave; Buffer g_buffer; @@ -430,6 +437,18 @@ bool isFileBroadcastMessage(const std::string & message) { return result; } +std::thread initMainAndRunCore() { + initMain(); + + return std::thread([&]() { + while (g_isRunning) { + updateCore(); + + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + }); +} + void initMain() { g_isRunning = true; g_ggWave = GGWave_instance(); @@ -493,7 +512,7 @@ void updateCore() { static GGWave::TxRxData lastRxData; { - //std::lock_guard lock(g_buffer.mutex); + std::lock_guard lock(g_buffer.mutex); if (g_buffer.inputCore.update) { inputCurrent = std::move(g_buffer.inputCore); g_buffer.inputCore.update = false; @@ -561,7 +580,7 @@ void updateCore() { } { - //std::lock_guard lock(g_buffer.mutex); + std::lock_guard lock(g_buffer.mutex); g_buffer.stateCore.apply(g_buffer.stateUI); } } @@ -626,7 +645,7 @@ void renderMain() { static State stateCurrent; { - //std::lock_guard lock(g_buffer.mutex); + std::lock_guard lock(g_buffer.mutex); g_buffer.stateUI.apply(stateCurrent); } @@ -1505,7 +1524,7 @@ void renderMain() { ImGui::GetIO().KeysDown[ImGui::GetIO().KeyMap[ImGuiKey_Enter]] = false; { - //std::lock_guard lock(g_buffer.mutex); + std::lock_guard lock(g_buffer.mutex); if (g_buffer.inputUI.update) { g_buffer.inputCore = std::move(g_buffer.inputUI); g_buffer.inputUI.update = false; diff --git a/examples/waver/common.h b/examples/waver/common.h index 5bbe55b..f87d538 100644 --- a/examples/waver/common.h +++ b/examples/waver/common.h @@ -2,8 +2,11 @@ #include "ggwave-common-sdl2.h" +#include #include +std::thread initMainAndRunCore(); + void initMain(); void updateCore(); void renderMain(); diff --git a/examples/waver/main.cpp b/examples/waver/main.cpp index b49bb03..ad404fd 100644 --- a/examples/waver/main.cpp +++ b/examples/waver/main.cpp @@ -14,11 +14,31 @@ #include #include +#include #include #include // ImGui helpers +bool ImGui_tryLoadFont(const std::string & filename, float size = 14.0f, bool merge = false) { + std::ifstream f(filename.c_str()); + if (f.good() == false) { + return false; + } + if (merge) { + ImWchar ranges[] = { 0xf000, 0xf3ff, 0 }; + + ImFontConfig config; + config.MergeMode = true; + config.GlyphOffset = { 0.0f, 0.0f }; + + ImGui::GetIO().Fonts->AddFontFromFileTTF(filename.c_str(), size, &config, ranges); + } else { + ImGui::GetIO().Fonts->AddFontFromFileTTF(filename.c_str(), size); + } + return true; +} + bool ImGui_BeginFrame(SDL_Window * window) { SDL_Event event; while (SDL_PollEvent(&event)) @@ -200,21 +220,14 @@ int main(int argc, char** argv) { ImGui_Init(window, gl_context); ImGui::GetIO().IniFilename = nullptr; - ImGui::GetIO().Fonts->AddFontFromFileTTF((getBinaryPath() + "DroidSans.ttf").c_str(), 14.0f); - ImGui::GetIO().Fonts->AddFontFromFileTTF((getBinaryPath() + "../examples/assets/fonts/DroidSans.ttf").c_str(), 14.0f); - ImGui::GetIO().Fonts->AddFontFromFileTTF((getBinaryPath() + "../../examples/assets/fonts/DroidSans.ttf").c_str(), 14.0f); - { - ImWchar ranges[] = { 0xf000, 0xf3ff, 0 }; + ImGui_tryLoadFont(getBinaryPath() + "DroidSans.ttf", 14.0f, false); + ImGui_tryLoadFont(getBinaryPath() + "../examples/assets/fonts/DroidSans.ttf", 14.0f, false); + ImGui_tryLoadFont(getBinaryPath() + "../../examples/assets/fonts/DroidSans.ttf", 14.0f, false); - ImFontConfig config; - config.MergeMode = true; - config.GlyphOffset = { 0.0f, 0.0f }; - - ImGui::GetIO().Fonts->AddFontFromFileTTF((getBinaryPath() + "fontawesome-webfont.ttf").c_str(), 14.0f, &config, ranges); - ImGui::GetIO().Fonts->AddFontFromFileTTF((getBinaryPath() + "../examples/assets/fonts/fontawesome-webfont.ttf").c_str(), 14.0f, &config, ranges); - ImGui::GetIO().Fonts->AddFontFromFileTTF((getBinaryPath() + "../../examples/assets/fonts/fontawesome-webfont.ttf").c_str(), 14.0f, &config, ranges); - } + ImGui_tryLoadFont(getBinaryPath() + "fontawesome-webfont.ttf", 14.0f, true); + ImGui_tryLoadFont(getBinaryPath() + "../examples/assets/fonts/fontawesome-webfont.ttf", 14.0f, true); + ImGui_tryLoadFont(getBinaryPath() + "../../examples/assets/fonts/fontawesome-webfont.ttf", 14.0f, true); ImGui_SetStyle(); @@ -229,6 +242,7 @@ int main(int argc, char** argv) { //addFile("test2.mpv", "test0.mov", std::vector(1024*1024*234 + 53827)); bool isInitialized = false; + std::thread worker; g_doInit = [&]() { if (GGWave_init(playbackId, captureId) == false) { @@ -236,7 +250,11 @@ int main(int argc, char** argv) { return false; } +#ifdef __EMSCRIPTEN__ initMain(); +#else + worker = initMainAndRunCore(); +#endif isInitialized = true; @@ -258,7 +276,10 @@ int main(int argc, char** argv) { renderMain(); updateMain(); + +#ifdef __EMSCRIPTEN__ updateCore(); +#endif ImGui_EndFrame(window); @@ -280,6 +301,8 @@ int main(int argc, char** argv) { deinitMain(); GGWave_deinit(); + worker.join(); + // Cleanup ImGui_Shutdown(); ImGui::DestroyContext();