[c-host] app: fix updateEvent race problem

This commit is contained in:
Geoffrey McRae 2019-03-04 13:38:17 +11:00
parent 6e35033f2e
commit a8b018d5da
4 changed files with 24 additions and 17 deletions

View File

@ -62,11 +62,8 @@ static int pointerThread(void * opaque)
{ {
DEBUG_INFO("Cursor thread started"); DEBUG_INFO("Cursor thread started");
while(app.running) while(os_waitEvent(app.pointerEvent) && app.running)
{ {
if (!os_waitEvent(app.pointerEvent) || !app.running)
break;
#if 0 #if 0
CapturePointer pointer; CapturePointer pointer;
pointer->data = app.pointerData; pointer->data = app.pointerData;
@ -87,11 +84,8 @@ static int frameThread(void * opaque)
int frameIndex = 0; int frameIndex = 0;
volatile KVMFRFrame * fi = &(app.shmHeader->frame); volatile KVMFRFrame * fi = &(app.shmHeader->frame);
while(app.running) while(os_waitEvent(app.frameEvent) && app.running)
{ {
if (!os_waitEvent(app.frameEvent) || !app.running)
break;
CaptureFrame frame; CaptureFrame frame;
frame.data = app.frame[frameIndex]; frame.data = app.frame[frameIndex];
if (!app.iface->getFrame(&frame)) if (!app.iface->getFrame(&frame))
@ -105,7 +99,7 @@ static int frameThread(void * opaque)
os_signalEvent(app.updateEvent); os_signalEvent(app.updateEvent);
// wait for the client to finish with the previous frame // wait for the client to finish with the previous frame
while(fi->flags & KVMFR_FRAME_FLAG_UPDATE) while(fi->flags & KVMFR_FRAME_FLAG_UPDATE && app.running)
{ {
DEBUG_WARN("Waiting for the client"); DEBUG_WARN("Waiting for the client");
// this generally never happens // this generally never happens
@ -277,7 +271,7 @@ int app_main()
} }
app.iface = iface; app.iface = iface;
app.frameEvent = os_createEvent(); app.frameEvent = os_createEvent(true);
if (!app.frameEvent) if (!app.frameEvent)
{ {
DEBUG_ERROR("Failed to create the frame event"); DEBUG_ERROR("Failed to create the frame event");
@ -285,7 +279,7 @@ int app_main()
goto exit; goto exit;
} }
app.updateEvent = os_createEvent(); app.updateEvent = os_createEvent(false);
if (!app.updateEvent) if (!app.updateEvent)
{ {
DEBUG_ERROR("Failed to create the update event"); DEBUG_ERROR("Failed to create the update event");
@ -293,7 +287,7 @@ int app_main()
goto exit; goto exit;
} }
app.pointerEvent = os_createEvent(); app.pointerEvent = os_createEvent(true);
if (!app.pointerEvent) if (!app.pointerEvent)
{ {
DEBUG_ERROR("Failed to create the pointer event"); DEBUG_ERROR("Failed to create the pointer event");
@ -365,6 +359,7 @@ retry_capture:
if (!frameUpdate && !pointerUpdate) if (!frameUpdate && !pointerUpdate)
goto retry_capture; goto retry_capture;
os_resetEvent(app.updateEvent);
if (frameUpdate && !os_signalEvent(app.frameEvent)) if (frameUpdate && !os_signalEvent(app.frameEvent))
{ {
DEBUG_ERROR("Failed to signal the frame thread"); DEBUG_ERROR("Failed to signal the frame thread");

View File

@ -42,7 +42,8 @@ bool os_joinThread (osThreadHandle * handle, int * resultCode);
typedef struct osEventHandle osEventHandle; typedef struct osEventHandle osEventHandle;
osEventHandle * os_createEvent(); osEventHandle * os_createEvent(bool autoReset);
void os_freeEvent (osEventHandle * handle); void os_freeEvent (osEventHandle * handle);
bool os_waitEvent (osEventHandle * handle); bool os_waitEvent (osEventHandle * handle);
bool os_signalEvent(osEventHandle * handle); bool os_signalEvent(osEventHandle * handle);
bool os_resetEvent (osEventHandle * handle);

View File

@ -76,7 +76,7 @@ static bool dxgi_create()
return false; return false;
} }
this->copyEvent = os_createEvent(); this->copyEvent = os_createEvent(true);
if (!this->copyEvent) if (!this->copyEvent)
{ {
DEBUG_ERROR("failed to create the copy event"); DEBUG_ERROR("failed to create the copy event");

View File

@ -300,9 +300,15 @@ bool os_joinThread(osThreadHandle * handle, int * resultCode)
return false; return false;
} }
osEventHandle * os_createEvent() osEventHandle * os_createEvent(bool autoReset)
{ {
HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL); HANDLE event = CreateEvent(NULL, autoReset ? FALSE : TRUE, FALSE, NULL);
if (!event)
{
DEBUG_WINERROR("Failed to create the event", GetLastError());
return NULL;
}
return (osEventHandle*)event; return (osEventHandle*)event;
} }
@ -338,3 +344,8 @@ bool os_signalEvent(osEventHandle * handle)
{ {
return SetEvent((HANDLE)handle); return SetEvent((HANDLE)handle);
} }
bool os_resetEvent(osEventHandle * handle)
{
return ResetEvent((HANDLE)handle);
}