so i don't actually need that mutex, let it race

This commit is contained in:
maksw committed 2026-04-16 21:40:46 +02:00
1 parent cd6332fe0a
commit 57a73f49d9
1 file changed
+6 -20
+6 -20
View File
@@ -11,7 +11,6 @@
#ifdef _WIN32 #ifdef _WIN32
#include <thread> #include <thread>
#include <mutex>
#include <atomic> #include <atomic>
#endif #endif
@@ -32,10 +31,7 @@ struct window_t {
std::atomic<bool> running{true}; std::atomic<bool> running{true};
std::atomic<bool> quit_requested{false}; std::atomic<bool> quit_requested{false};
std::mutex buffer_mutex;
uint32_t* shared_fb; uint32_t* shared_fb;
std::mutex input_mutex;
uint8_t kb_copy[SDL_NUM_SCANCODES]; uint8_t kb_copy[SDL_NUM_SCANCODES];
#endif #endif
}; };
@@ -56,10 +52,7 @@ static void win32_internal_render(window_t* s) {
void* pixels; void* pixels;
int pitch; int pitch;
if (SDL_LockTexture(s->texture, nullptr, &pixels, &pitch) == 0) { if (SDL_LockTexture(s->texture, nullptr, &pixels, &pitch) == 0) {
{ std::memcpy(pixels, s->shared_fb, s->width * s->height * 4);
std::lock_guard<std::mutex> lock(s->buffer_mutex);
std::memcpy(pixels, s->shared_fb, s->width * s->height * 4);
}
SDL_UnlockTexture(s->texture); SDL_UnlockTexture(s->texture);
} }
@@ -68,7 +61,7 @@ static void win32_internal_render(window_t* s) {
SDL_RenderPresent(s->renderer); SDL_RenderPresent(s->renderer);
} }
// THE FIX: This callback is triggered by SDL even when Windows is in a modal drag loop. // This callback is triggered by SDL even when Windows is in a modal drag loop.
static int SDLCALL win32_event_watch(void* userdata, SDL_Event* event) { static int SDLCALL win32_event_watch(void* userdata, SDL_Event* event) {
window_t* s = (window_t*)userdata; window_t* s = (window_t*)userdata;
if (event->type == SDL_WINDOWEVENT) { if (event->type == SDL_WINDOWEVENT) {
@@ -99,12 +92,9 @@ static void win32_window_worker(window_t* s, const char* title) {
} }
// Sync input state for the main thread // Sync input state for the main thread
{ const uint8_t* state = SDL_GetKeyboardState(nullptr);
std::lock_guard<std::mutex> lock(s->input_mutex); std::memcpy(s->kb_copy, state, SDL_NUM_SCANCODES);
const uint8_t* state = SDL_GetKeyboardState(nullptr); s->mouse_buttons = SDL_GetMouseState(&s->mouse_x, &s->mouse_y);
std::memcpy(s->kb_copy, state, SDL_NUM_SCANCODES);
s->mouse_buttons = SDL_GetMouseState(&s->mouse_x, &s->mouse_y);
}
// Normal frame render // Normal frame render
win32_internal_render(s); win32_internal_render(s);
@@ -161,9 +151,7 @@ void window_destroy(window_t* s) {
bool window_pollevent(window_t* s) { bool window_pollevent(window_t* s) {
#ifdef _WIN32 #ifdef _WIN32
// Main thread just checks the status reported by the worker return s->quit_requested.load();
std::lock_guard<std::mutex> lock(s->input_mutex);
return s->quit_requested;
#else #else
SDL_Event ev; SDL_Event ev;
bool quit = false; bool quit = false;
@@ -177,8 +165,6 @@ bool window_pollevent(window_t* s) {
void window_blit(window_t* s, const uint32_t* fb) { void window_blit(window_t* s, const uint32_t* fb) {
#ifdef _WIN32 #ifdef _WIN32
// Just update the shared buffer; the worker thread will pick it up and present it
std::lock_guard<std::mutex> lock(s->buffer_mutex);
std::memcpy(s->shared_fb, fb, s->width * s->height * 4); std::memcpy(s->shared_fb, fb, s->width * s->height * 4);
#else #else
// Original immediate blit logic // Original immediate blit logic