mirror of
https://github.com/ggerganov/ggwave.git
synced 2026-03-23 20:31:00 +08:00
waver : minor data race fix + back to separate Core thread
This commit is contained in:
@@ -14,14 +14,16 @@
|
||||
#include <SDL.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
#include <mutex>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
#include <cstring>
|
||||
|
||||
#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<std::mutex> 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<bool> 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<std::mutex> lock(g_buffer.mutex);
|
||||
std::lock_guard<std::mutex> 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<std::mutex> lock(g_buffer.mutex);
|
||||
std::lock_guard<std::mutex> lock(g_buffer.mutex);
|
||||
g_buffer.stateCore.apply(g_buffer.stateUI);
|
||||
}
|
||||
}
|
||||
@@ -626,7 +645,7 @@ void renderMain() {
|
||||
static State stateCurrent;
|
||||
|
||||
{
|
||||
//std::lock_guard<std::mutex> lock(g_buffer.mutex);
|
||||
std::lock_guard<std::mutex> 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<std::mutex> lock(g_buffer.mutex);
|
||||
std::lock_guard<std::mutex> lock(g_buffer.mutex);
|
||||
if (g_buffer.inputUI.update) {
|
||||
g_buffer.inputCore = std::move(g_buffer.inputUI);
|
||||
g_buffer.inputUI.update = false;
|
||||
|
||||
@@ -2,8 +2,11 @@
|
||||
|
||||
#include "ggwave-common-sdl2.h"
|
||||
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
std::thread initMainAndRunCore();
|
||||
|
||||
void initMain();
|
||||
void updateCore();
|
||||
void renderMain();
|
||||
|
||||
@@ -14,11 +14,31 @@
|
||||
#include <SDL.h>
|
||||
#include <SDL_opengl.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
// 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<char>(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();
|
||||
|
||||
Reference in New Issue
Block a user