[host] windows: use system thread pool to wait for exit event

This is much less annoying than creating our own thread just to wait on
a single event, then creating another event to signal that thread to exit.
This commit is contained in:
Quantum 2021-07-21 00:07:43 -04:00 committed by Geoffrey McRae
parent b1c26aaa95
commit 8a70efafb5

View File

@ -57,9 +57,7 @@ struct AppState
NOTIFYICONDATA iconData; NOTIFYICONDATA iconData;
UINT trayRestartMsg; UINT trayRestartMsg;
HMENU trayMenu; HMENU trayMenu;
HANDLE exitWait;
HANDLE exitThreadEvent;
LGThread * exitThread;
}; };
static struct AppState app = {0}; static struct AppState app = {0};
@ -263,27 +261,6 @@ static int appThread(void * opaque)
return result; return result;
} }
static int exitThread(void * opaque)
{
HANDLE handles[2] = { (HANDLE) opaque, app.exitThreadEvent };
switch (WaitForMultipleObjects(2, handles, FALSE, INFINITE))
{
case WAIT_OBJECT_0:
DEBUG_INFO("Received exit event");
SendMessage(app.messageWnd, WM_CLOSE, 0, 0);
break;
case WAIT_OBJECT_0 + 1:
break;
case WAIT_FAILED:
DEBUG_ERROR("WaitForMultipleObjects failed: 0x%lx", GetLastError());
}
return 0;
}
LRESULT sendAppMessage(UINT Msg, WPARAM wParam, LPARAM lParam) LRESULT sendAppMessage(UINT Msg, WPARAM wParam, LPARAM lParam)
{ {
return SendMessage(app.messageWnd, Msg, wParam, lParam); return SendMessage(app.messageWnd, Msg, wParam, lParam);
@ -413,8 +390,6 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
app.messageWnd = CreateWindowEx(0, MAKEINTATOM(class), NULL, 0, 0, 0, 0, 0, NULL, NULL, hInstance, NULL); app.messageWnd = CreateWindowEx(0, MAKEINTATOM(class), NULL, 0, 0, 0, 0, 0, NULL, NULL, hInstance, NULL);
app.exitThreadEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
// this is needed so that unprivileged processes can send us this message // this is needed so that unprivileged processes can send us this message
ChangeWindowMessageFilterEx(app.messageWnd, app.trayRestartMsg, MSGFLT_ALLOW, NULL); ChangeWindowMessageFilterEx(app.messageWnd, app.trayRestartMsg, MSGFLT_ALLOW, NULL);
@ -458,7 +433,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
shutdown: shutdown:
DestroyMenu(app.trayMenu); DestroyMenu(app.trayMenu);
app_shutdown(); app_shutdown();
SetEvent(app.exitThreadEvent); UnregisterWait(app.exitWait);
if (!lgJoinThread(thread, &result)) if (!lgJoinThread(thread, &result))
{ {
@ -466,12 +441,6 @@ shutdown:
result = LG_HOST_EXIT_FAILED; result = LG_HOST_EXIT_FAILED;
} }
if (app.exitThread && !lgJoinThread(app.exitThread, &result))
{
DEBUG_ERROR("Failed to join the exit thread");
result = LG_HOST_EXIT_FAILED;
}
finish: finish:
for(int i = 0; i < app.argc; ++i) for(int i = 0; i < app.argc; ++i)
@ -508,6 +477,12 @@ void boostPriority(void)
} }
} }
void CALLBACK exitEventCallback(PVOID opaque, BOOLEAN timedOut)
{
DEBUG_INFO("Received exit event");
SendMessage(app.messageWnd, WM_CLOSE, 0, 0);
}
bool app_init(void) bool app_init(void)
{ {
const char * logFile = option_get_string("os", "logFile"); const char * logFile = option_get_string("os", "logFile");
@ -537,8 +512,9 @@ bool app_init(void)
DEBUG_WARN("Failed to open exitEvent with error 0x%lx: %s", GetLastError(), exitEventName); DEBUG_WARN("Failed to open exitEvent with error 0x%lx: %s", GetLastError(), exitEventName);
} }
if (exitEvent && !lgCreateThread("exitThread", exitThread, exitEvent, &app.exitThread)) if (!RegisterWaitForSingleObject(&app.exitWait, exitEvent, exitEventCallback, NULL,
DEBUG_ERROR("Failed to create the exit thread"); INFINITE, WT_EXECUTEONLYONCE))
DEBUG_ERROR("Failed to create register wait for exit event: 0x%lx", GetLastError());
return true; return true;
} }