Factor out some thread communication code (window size, bindings)

This commit is contained in:
Jonas Kulla 2015-01-02 01:41:23 +01:00
parent c63a8947ce
commit 3411435138
4 changed files with 25 additions and 69 deletions

View File

@ -80,7 +80,7 @@ void EventThread::process(RGSSThreadData &rtData)
{
SDL_Event event;
SDL_Window *win = rtData.window;
WindowSizeNotify &windowSizeMsg = rtData.windowSizeMsg;
UnidirMessage<Vec2i> &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 :

View File

@ -107,102 +107,59 @@ private:
};
/* Used to asynchronously inform the RGSS thread
* about window size changes */
struct WindowSizeNotify
* about certain value changes */
template<typename T>
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
/* Done from either */
void get(T &out) const
{
BindingNotify()
{
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<Vec2i> windowSizeMsg;
UnidirMessage<BDescVec> bindingUpdateMsg;
const char *argv0;

View File

@ -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();

View File

@ -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));