Factor out some thread communication code (window size, bindings)
This commit is contained in:
parent
c63a8947ce
commit
3411435138
4 changed files with 25 additions and 69 deletions
|
|
@ -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
|
||||
{
|
||||
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<Vec2i> windowSizeMsg;
|
||||
UnidirMessage<BDescVec> bindingUpdateMsg;
|
||||
|
||||
const char *argv0;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue