mirror of
https://github.com/gnif/LookingGlass.git
synced 2025-10-11 18:08:08 +00:00
[common/c-host] move agnostic code into common
library
This commit is contained in:
93
common/src/platform/windows/thread.c
Normal file
93
common/src/platform/windows/thread.c
Normal file
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
Looking Glass - KVM FrameRelay (KVMFR) Client
|
||||
Copyright (C) 2017-2020 Geoffrey McRae <geoff@hostfission.com>
|
||||
https://looking-glass.hostfission.com
|
||||
|
||||
This program is free software; you can redistribute it and/or modify it under
|
||||
the terms of the GNU General Public License as published by the Free Software
|
||||
Foundation; either version 2 of the License, or (at your option) any later
|
||||
version.
|
||||
|
||||
This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along with
|
||||
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
||||
Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include "common/thread.h"
|
||||
#include "common/debug.h"
|
||||
#include "common/windebug.h"
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
struct LGThread
|
||||
{
|
||||
const char * name;
|
||||
LGThreadFunction function;
|
||||
void * opaque;
|
||||
HANDLE handle;
|
||||
DWORD threadID;
|
||||
|
||||
int resultCode;
|
||||
};
|
||||
|
||||
static DWORD WINAPI threadWrapper(LPVOID lpParameter)
|
||||
{
|
||||
LGThread * handle = (LGThread *)lpParameter;
|
||||
handle->resultCode = handle->function(handle->opaque);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool lgCreateThread(const char * name, LGThreadFunction function, void * opaque, LGThread ** handle)
|
||||
{
|
||||
*handle = (LGThread *)malloc(sizeof(LGThread));
|
||||
(*handle)->name = name;
|
||||
(*handle)->function = function;
|
||||
(*handle)->opaque = opaque;
|
||||
(*handle)->handle = CreateThread(NULL, 0, threadWrapper, *handle, 0, &(*handle)->threadID);
|
||||
|
||||
if (!(*handle)->handle)
|
||||
{
|
||||
free(*handle);
|
||||
*handle = NULL;
|
||||
DEBUG_WINERROR("CreateThread failed", GetLastError());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool lgJoinThread(LGThread * handle, int * resultCode)
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
switch(WaitForSingleObject(handle->handle, INFINITE))
|
||||
{
|
||||
case WAIT_OBJECT_0:
|
||||
if (resultCode)
|
||||
*resultCode = handle->resultCode;
|
||||
CloseHandle(handle->handle);
|
||||
free(handle);
|
||||
return true;
|
||||
|
||||
case WAIT_ABANDONED:
|
||||
case WAIT_TIMEOUT:
|
||||
continue;
|
||||
|
||||
case WAIT_FAILED:
|
||||
DEBUG_WINERROR("Wait for thread failed", GetLastError());
|
||||
CloseHandle(handle->handle);
|
||||
free(handle);
|
||||
return false;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
DEBUG_WINERROR("Unknown failure waiting for thread", GetLastError());
|
||||
return false;
|
||||
}
|
||||
|
Reference in New Issue
Block a user