From 3411435138ab6ecd25d9c9fb0b10265b6e9b3a28 Mon Sep 17 00:00:00 2001 From: Jonas Kulla Date: Fri, 2 Jan 2015 01:41:23 +0100 Subject: [PATCH] Factor out some thread communication code (window size, bindings) --- src/eventthread.cpp | 5 ++- src/eventthread.h | 85 +++++++++++---------------------------------- src/graphics.cpp | 2 +- src/main.cpp | 2 +- 4 files changed, 25 insertions(+), 69 deletions(-) diff --git a/src/eventthread.cpp b/src/eventthread.cpp index a077862..0ae1acf 100644 --- a/src/eventthread.cpp +++ b/src/eventthread.cpp @@ -80,7 +80,7 @@ void EventThread::process(RGSSThreadData &rtData) { SDL_Event event; SDL_Window *win = rtData.window; - WindowSizeNotify &windowSizeMsg = rtData.windowSizeMsg; + UnidirMessage &windowSizeMsg = rtData.windowSizeMsg; fullscreen = rtData.config.fullscreen; int toggleFSMod = rtData.config.anyAltToggleFS ? KMOD_ALT : KMOD_LALT; @@ -140,8 +140,7 @@ void EventThread::process(RGSSThreadData &rtData) switch (event.window.event) { case SDL_WINDOWEVENT_SIZE_CHANGED : - windowSizeMsg.notifyChange(event.window.data1, - event.window.data2); + windowSizeMsg.post(Vec2i(event.window.data1, event.window.data2)); break; case SDL_WINDOWEVENT_ENTER : diff --git a/src/eventthread.h b/src/eventthread.h index c1ff08d..164148e 100644 --- a/src/eventthread.h +++ b/src/eventthread.h @@ -107,102 +107,59 @@ private: }; /* Used to asynchronously inform the RGSS thread - * about window size changes */ -struct WindowSizeNotify + * about certain value changes */ +template +struct UnidirMessage { - SDL_mutex *mutex; + UnidirMessage() + : mutex(SDL_CreateMutex()), + current(T()) + {} - AtomicFlag changed; - int w, h; - - WindowSizeNotify() - { - mutex = SDL_CreateMutex(); - w = h = 0; - } - - ~WindowSizeNotify() + ~UnidirMessage() { SDL_DestroyMutex(mutex); } /* Done from the sending side */ - void notifyChange(int w, int h) + void post(const T &value) { SDL_LockMutex(mutex); - this->w = w; - this->h = h; changed.set(); + current = value; SDL_UnlockMutex(mutex); } /* Done from the receiving side */ - bool pollChange(int *w, int *h) + bool poll(T &out) const { if (!changed) return false; SDL_LockMutex(mutex); - *w = this->w; - *h = this->h; + out = current; changed.clear(); SDL_UnlockMutex(mutex); return true; } -}; -struct BindingNotify -{ - BindingNotify() + /* Done from either */ + void get(T &out) const { - mut = SDL_CreateMutex(); - } - ~BindingNotify() - { - SDL_DestroyMutex(mut); - } - - bool poll(BDescVec &out) const - { - if (!changed) - return false; - - SDL_LockMutex(mut); - - out = data; - changed.clear(); - - SDL_UnlockMutex(mut); - - return true; - } - - void get(BDescVec &out) const - { - SDL_LockMutex(mut); - out = data; - SDL_UnlockMutex(mut); - } - - void post(const BDescVec &d) - { - SDL_LockMutex(mut); - - changed.set(); - data = d; - - SDL_UnlockMutex(mut); + SDL_LockMutex(mutex); + out = current; + SDL_UnlockMutex(mutex); } private: - SDL_mutex *mut; - BDescVec data; + SDL_mutex *mutex; mutable AtomicFlag changed; + T current; }; struct RGSSThreadData @@ -220,8 +177,8 @@ struct RGSSThreadData AtomicFlag rqResetFinish; EventThread *ethread; - WindowSizeNotify windowSizeMsg; - BindingNotify bindingUpdateMsg; + UnidirMessage windowSizeMsg; + UnidirMessage bindingUpdateMsg; const char *argv0; diff --git a/src/graphics.cpp b/src/graphics.cpp index 5537b03..76fb02c 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -558,7 +558,7 @@ struct GraphicsPrivate void checkResize() { - if (threadData->windowSizeMsg.pollChange(&winSize.x, &winSize.y)) + if (threadData->windowSizeMsg.poll(winSize)) { /* some GL drivers change the viewport on window resize */ glState.viewport.refresh(); diff --git a/src/main.cpp b/src/main.cpp index 48b98a8..4a87c28 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -289,7 +289,7 @@ int main(int argc, char *argv[]) int winW, winH; SDL_GetWindowSize(win, &winW, &winH); - rtData.windowSizeMsg.notifyChange(winW, winH); + rtData.windowSizeMsg.post(Vec2i(winW, winH)); /* Load and post key bindings */ rtData.bindingUpdateMsg.post(loadBindings(conf));