sync thread access to text input buffer

This commit is contained in:
Struma 2021-03-08 06:20:43 -05:00 committed by Roza
parent 10f3d2655d
commit 69a89cf87f
3 changed files with 635 additions and 609 deletions

View file

@ -20,11 +20,13 @@
*/
#include "binding-util.h"
#include "exception.h"
#include "input.h"
#include "util/exception.h"
#include "input/input.h"
#include "sharedstate.h"
#include "src/util/util.h"
#include "eventthread.h"
#include <SDL_joystick.h>
#include <string>
@ -323,10 +325,10 @@ RB_METHOD(inputSetMode) {
RB_METHOD(inputGets) {
RB_UNUSED_PARAM;
shState->eThread().lockText(true);
VALUE ret = rb_utf8_str_new_cstr(shState->input().getText());
shState->input().clearText();
shState->eThread().lockText(false);
return ret;
}

View file

@ -120,7 +120,14 @@ EventThread::EventThread()
fullscreen(false),
showCursor(true),
joystickConnected(false)
{}
{
textInputLock = SDL_CreateMutex();
}
EventThread::~EventThread()
{
SDL_DestroyMutex(textInputLock);
}
void EventThread::process(RGSSThreadData &rtData)
{
@ -266,8 +273,11 @@ void EventThread::process(RGSSThreadData &rtData)
break;
case SDL_TEXTINPUT :
if (textInputBuffer.size() < 512)
lockText(true);
if (textInputBuffer.size() < 512) {
textInputBuffer += event.text.text;
}
lockText(false);
break;
case SDL_QUIT :
@ -459,12 +469,16 @@ void EventThread::process(RGSSThreadData &rtData)
if (event.user.code)
{
SDL_StartTextInput();
lockText(true);
textInputBuffer.clear();
lockText(false);
}
else
{
SDL_StopTextInput();
lockText(true);
textInputBuffer.clear();
lockText(false);
}
break;
@ -766,6 +780,11 @@ void EventThread::notifyGameScreenChange(const SDL_Rect &screen)
SDL_PushEvent(&event);
}
void EventThread::lockText(bool lock)
{
lock ? SDL_LockMutex(textInputLock) : SDL_UnlockMutex(textInputLock);
}
void SyncPoint::haltThreads()
{
if (mainSync.locked)

View file

@ -77,10 +77,13 @@ public:
static TouchState touchState;
std::string textInputBuffer;
void lockText(bool lock);
static bool allocUserEvents();
EventThread();
~EventThread();
void process(RGSSThreadData &rtData);
void cleanup();
@ -129,6 +132,8 @@ private:
AtomicFlag msgBoxDone;
SDL_mutex *textInputLock;
struct
{
AtomicFlag sendUpdates;