diff --git a/VERSION b/VERSION index dc107586..5eef5933 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -B1-40-g7a3eab4c3e+1 \ No newline at end of file +B1-40-g491ffc3576+1 \ No newline at end of file diff --git a/c-host/include/interface/platform.h b/c-host/include/interface/platform.h index 646eb52e..8e4f201d 100644 --- a/c-host/include/interface/platform.h +++ b/c-host/include/interface/platform.h @@ -1,6 +1,6 @@ /* Looking Glass - KVM FrameRelay (KVMFR) Client -Copyright (C) 2017-2019 Geoffrey McRae +Copyright (C) 2017-2020 Geoffrey McRae https://looking-glass.hostfission.com This program is free software; you can redistribute it and/or modify it under @@ -31,24 +31,3 @@ const char * os_getExecutable(); unsigned int os_shmemSize(); bool os_shmemMmap(void **ptr); void os_shmemUnmap(); - -// os specific thread functions - -typedef struct osThreadHandle osThreadHandle; -typedef int (*osThreadFunction)(void * opaque); - -bool os_createThread(const char * name, osThreadFunction function, void * opaque, osThreadHandle ** handle); -bool os_joinThread (osThreadHandle * handle, int * resultCode); - -// os specific event functions - -#define TIMEOUT_INFINITE ((unsigned int)~0) - -typedef struct osEventHandle osEventHandle; - -osEventHandle * os_createEvent(bool autoReset, unsigned int msSpinTime); -void os_freeEvent (osEventHandle * handle); -bool os_waitEvent (osEventHandle * handle, unsigned int timeout); -bool os_waitEvents (osEventHandle * handles[], int count, bool waitAll, unsigned int timeout); -bool os_signalEvent(osEventHandle * handle); -bool os_resetEvent (osEventHandle * handle); \ No newline at end of file diff --git a/c-host/platform/Windows/CMakeLists.txt b/c-host/platform/Windows/CMakeLists.txt index a1ca4075..f6a01737 100644 --- a/c-host/platform/Windows/CMakeLists.txt +++ b/c-host/platform/Windows/CMakeLists.txt @@ -7,7 +7,6 @@ include_directories( add_library(platform_Windows STATIC src/platform.c - src/windebug.c src/mousehook.c ) diff --git a/c-host/platform/Windows/capture/DXGI/src/dxgi.c b/c-host/platform/Windows/capture/DXGI/src/dxgi.c index bf832835..e6974606 100644 --- a/c-host/platform/Windows/capture/DXGI/src/dxgi.c +++ b/c-host/platform/Windows/capture/DXGI/src/dxgi.c @@ -20,10 +20,10 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include "interface/capture.h" #include "interface/platform.h" #include "common/debug.h" +#include "common/windebug.h" #include "common/option.h" #include "common/locking.h" -#include "windows/debug.h" -#include "windows/platform.h" +#include "common/event.h" #include #include @@ -83,8 +83,8 @@ struct iface int texWIndex; volatile int texReady; bool needsRelease; - osEventHandle * pointerEvent; - osEventHandle * frameEvent; + LGEvent * pointerEvent; + LGEvent * frameEvent; unsigned int width; unsigned int height; @@ -165,7 +165,7 @@ static bool dxgi_create() return false; } - this->pointerEvent = os_createEvent(true, 10); + this->pointerEvent = lgCreateEvent(true, 10); if (!this->pointerEvent) { DEBUG_ERROR("failed to create the pointer event"); @@ -173,7 +173,7 @@ static bool dxgi_create() return false; } - this->frameEvent = os_createEvent(true, 17); // 60Hz = 16.7ms + this->frameEvent = lgCreateEvent(true, 17); // 60Hz = 16.7ms if (!this->frameEvent) { DEBUG_ERROR("failed to create the frame event"); @@ -222,8 +222,8 @@ static bool dxgi_init(void * pointerShape, const unsigned int pointerSize) this->texWIndex = 0; this->texReady = 0; - os_resetEvent(this->frameEvent ); - os_resetEvent(this->pointerEvent); + lgResetEvent(this->frameEvent ); + lgResetEvent(this->pointerEvent); status = CreateDXGIFactory1(&IID_IDXGIFactory1, (void **)&this->factory); if (FAILED(status)) @@ -520,7 +520,7 @@ fail: static void dxgi_stop() { this->stop = true; - os_signalEvent(this->pointerEvent); + lgSignalEvent(this->pointerEvent); } static bool dxgi_deinit() @@ -598,7 +598,7 @@ static void dxgi_free() if (this->initialized) dxgi_deinit(); - os_freeEvent(this->pointerEvent); + lgFreeEvent(this->pointerEvent); free(this->texture); free(this); @@ -683,7 +683,7 @@ static CaptureResult dxgi_capture() // set the state, and signal tex->state = TEXTURE_STATE_PENDING_MAP; INTERLOCKED_INC(&this->texReady); - os_signalEvent(this->frameEvent); + lgSignalEvent(this->frameEvent); // advance the write index if (++this->texWIndex == this->maxTextures) @@ -749,7 +749,7 @@ static CaptureResult dxgi_capture() // signal about the pointer update if (signalPointer) - os_signalEvent(this->pointerEvent); + lgSignalEvent(this->pointerEvent); return CAPTURE_RESULT_OK; } @@ -762,7 +762,7 @@ static CaptureResult dxgi_waitFrame(CaptureFrame * frame) // NOTE: the event may be signaled when there are no frames available if(this->texReady == 0) { - if (!os_waitEvent(this->frameEvent, 1000)) + if (!lgWaitEvent(this->frameEvent, 1000)) return CAPTURE_RESULT_TIMEOUT; if (this->texReady == 0) @@ -817,7 +817,7 @@ static CaptureResult dxgi_getPointer(CapturePointer * pointer) assert(this); assert(this->initialized); - if (!os_waitEvent(this->pointerEvent, 1000)) + if (!lgWaitEvent(this->pointerEvent, 1000)) return CAPTURE_RESULT_TIMEOUT; if (this->stop) diff --git a/c-host/platform/Windows/capture/NVFBC/src/nvfbc.c b/c-host/platform/Windows/capture/NVFBC/src/nvfbc.c index 02867a7b..a63adccc 100644 --- a/c-host/platform/Windows/capture/NVFBC/src/nvfbc.c +++ b/c-host/platform/Windows/capture/NVFBC/src/nvfbc.c @@ -1,6 +1,6 @@ /* Looking Glass - KVM FrameRelay (KVMFR) Client -Copyright (C) 2017-2019 Geoffrey McRae +Copyright (C) 2017-2020 Geoffrey McRae https://looking-glass.hostfission.com This program is free software; you can redistribute it and/or modify it under @@ -19,11 +19,11 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include "interface/capture.h" #include "interface/platform.h" -#include "windows/platform.h" -#include "windows/debug.h" +#include "common/windebug.h" #include "windows/mousehook.h" #include "common/option.h" #include "common/framebuffer.h" +#include "common/event.h" #include #include #include @@ -46,8 +46,8 @@ struct iface NvFBCFrameGrabInfo grabInfo; - osEventHandle * frameEvent; - osEventHandle * cursorEvents[2]; + LGEvent * frameEvent; + LGEvent * cursorEvents[2]; int mouseX, mouseY, mouseHotX, mouseHotY; bool mouseVisible; @@ -75,7 +75,7 @@ static void on_mouseMove(int x, int y) { this->mouseX = x; this->mouseY = y; - os_signalEvent(this->cursorEvents[0]); + lgSignalEvent(this->cursorEvents[0]); } static const char * nvfbc_getName() @@ -135,7 +135,7 @@ static bool nvfbc_create() } free(privData); - this->frameEvent = os_createEvent(true, 17); + this->frameEvent = lgCreateEvent(true, 17); if (!this->frameEvent) { DEBUG_ERROR("failed to create the frame event"); @@ -155,7 +155,7 @@ static bool nvfbc_init(void * pointerShape, const unsigned int pointerSize) this->pointerSize = pointerSize; getDesktopSize(&this->width, &this->height); - os_resetEvent(this->frameEvent); + lgResetEvent(this->frameEvent); HANDLE event; @@ -174,11 +174,11 @@ static bool nvfbc_init(void * pointerShape, const unsigned int pointerSize) return false; } - this->cursorEvents[0] = os_createEvent(true, 10); + this->cursorEvents[0] = lgCreateEvent(true, 10); mouseHook_install(on_mouseMove); if (this->seperateCursor) - this->cursorEvents[1] = os_wrapEvent(event); + this->cursorEvents[1] = lgWrapEvent(event); DEBUG_INFO("Cursor mode : %s", this->seperateCursor ? "decoupled" : "integrated"); @@ -189,8 +189,8 @@ static bool nvfbc_init(void * pointerShape, const unsigned int pointerSize) static void nvfbc_stop() { this->stop = true; - os_signalEvent(this->cursorEvents[0]); - os_signalEvent(this->frameEvent); + lgSignalEvent(this->cursorEvents[0]); + lgSignalEvent(this->frameEvent); } static bool nvfbc_deinit() @@ -204,7 +204,7 @@ static void nvfbc_free() NvFBCToSysRelease(&this->nvfbc); if (this->frameEvent) - os_freeEvent(this->frameEvent); + lgFreeEvent(this->frameEvent); free(this); this = NULL; @@ -233,13 +233,13 @@ static CaptureResult nvfbc_capture() return result; memcpy(&this->grabInfo, &grabInfo, sizeof(grabInfo)); - os_signalEvent(this->frameEvent); + lgSignalEvent(this->frameEvent); return CAPTURE_RESULT_OK; } static CaptureResult nvfbc_waitFrame(CaptureFrame * frame) { - if (!os_waitEvent(this->frameEvent, 1000)) + if (!lgWaitEvent(this->frameEvent, 1000)) return CAPTURE_RESULT_TIMEOUT; if (this->stop) @@ -282,9 +282,9 @@ static CaptureResult nvfbc_getFrame(FrameBuffer frame) static CaptureResult nvfbc_getPointer(CapturePointer * pointer) { - osEventHandle * events[2]; - memcpy(&events, &this->cursorEvents, sizeof(osEventHandle *) * 2); - if (!os_waitEvents(events, this->seperateCursor ? 2 : 1, false, 1000)) + LGEvent * events[2]; + memcpy(&events, &this->cursorEvents, sizeof(LGEvent *) * 2); + if (!lgWaitEvents(events, this->seperateCursor ? 2 : 1, false, 1000)) return CAPTURE_RESULT_TIMEOUT; if (this->stop) diff --git a/c-host/platform/Windows/capture/NVFBC/src/wrapper.cpp b/c-host/platform/Windows/capture/NVFBC/src/wrapper.cpp index 6e17aca5..044134fe 100644 --- a/c-host/platform/Windows/capture/NVFBC/src/wrapper.cpp +++ b/c-host/platform/Windows/capture/NVFBC/src/wrapper.cpp @@ -1,6 +1,6 @@ /* Looking Glass - KVM FrameRelay (KVMFR) Client -Copyright (C) 2017-2019 Geoffrey McRae +Copyright (C) 2017-2020 Geoffrey McRae https://looking-glass.hostfission.com This program is free software; you can redistribute it and/or modify it under @@ -18,7 +18,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA */ #include "wrapper.h" -#include "windows/debug.h" +#include "common/windebug.h" #include #include diff --git a/c-host/platform/Windows/src/mousehook.c b/c-host/platform/Windows/src/mousehook.c index 40a3d8da..d8cd40ea 100644 --- a/c-host/platform/Windows/src/mousehook.c +++ b/c-host/platform/Windows/src/mousehook.c @@ -18,7 +18,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA */ #include "windows/mousehook.h" -#include "windows/debug.h" +#include "common/windebug.h" #include "platform.h" #include diff --git a/c-host/platform/Windows/src/platform.c b/c-host/platform/Windows/src/platform.c index 414e7ce8..1715d540 100644 --- a/c-host/platform/Windows/src/platform.c +++ b/c-host/platform/Windows/src/platform.c @@ -1,6 +1,6 @@ /* Looking Glass - KVM FrameRelay (KVMFR) Client -Copyright (C) 2017-2019 Geoffrey McRae +Copyright (C) 2017-2020 Geoffrey McRae https://looking-glass.hostfission.com This program is free software; you can redistribute it and/or modify it under @@ -18,7 +18,6 @@ Place, Suite 330, Boston, MA 02111-1307 USA */ #include "platform.h" -#include "windows/platform.h" #include "windows/mousehook.h" #include @@ -29,9 +28,10 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include "interface/platform.h" #include "common/debug.h" +#include "common/windebug.h" #include "common/option.h" #include "common/locking.h" -#include "windows/debug.h" +#include "common/thread.h" #include "ivshmem.h" #define ID_MENU_OPEN_LOG 3000 @@ -60,27 +60,6 @@ static struct AppState app = .shmemMap = {0} }; -struct osThreadHandle -{ - const char * name; - osThreadFunction function; - void * opaque; - HANDLE handle; - DWORD threadID; - - int resultCode; -}; - -struct osEventHandle -{ - volatile int lock; - bool reset; - HANDLE handle; - bool wrapped; - unsigned int msSpinTime; - volatile bool signaled; -}; - // undocumented API to adjust the system timer resolution (yes, its a nasty hack) typedef NTSTATUS (__stdcall *ZwSetTimerResolution_t)(ULONG RequestedResolution, BOOLEAN Set, PULONG ActualResolution); static ZwSetTimerResolution_t ZwSetTimerResolution = NULL; @@ -261,8 +240,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine AppendMenu(app.trayMenu, MF_STRING , ID_MENU_EXIT , "Exit" ); // create the application thread - osThreadHandle * thread; - if (!os_createThread("appThread", appThread, NULL, &thread)) + LGThread * thread; + if (!lgCreateThread("appThread", appThread, NULL, &thread)) { DEBUG_ERROR("Failed to create the main application thread"); result = -1; @@ -292,7 +271,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine shutdown: DestroyMenu(app.trayMenu); app_quit(); - if (!os_joinThread(thread, &result)) + if (!lgJoinThread(thread, &result)) { DEBUG_ERROR("Failed to join the main application thread"); result = -1; @@ -444,249 +423,4 @@ void os_shmemUnmap() DEBUG_WINERROR("DeviceIoControl failed", GetLastError()); else app.shmemOwned = false; -} - -static DWORD WINAPI threadWrapper(LPVOID lpParameter) -{ - osThreadHandle * handle = (osThreadHandle *)lpParameter; - handle->resultCode = handle->function(handle->opaque); - return 0; -} - -bool os_createThread(const char * name, osThreadFunction function, void * opaque, osThreadHandle ** handle) -{ - *handle = (osThreadHandle *)malloc(sizeof(osThreadHandle)); - (*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 os_joinThread(osThreadHandle * 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; -} - -osEventHandle * os_createEvent(bool autoReset, unsigned int msSpinTime) -{ - osEventHandle * event = (osEventHandle *)malloc(sizeof(osEventHandle)); - if (!event) - { - DEBUG_ERROR("out of ram"); - return NULL; - } - - event->lock = 0; - event->reset = autoReset; - event->handle = CreateEvent(NULL, autoReset ? FALSE : TRUE, FALSE, NULL); - event->wrapped = false; - event->msSpinTime = msSpinTime; - event->signaled = false; - - if (!event->handle) - { - DEBUG_WINERROR("Failed to create the event", GetLastError()); - free(event); - return NULL; - } - - return event; -} - -osEventHandle * os_wrapEvent(HANDLE handle) -{ - osEventHandle * event = (osEventHandle *)malloc(sizeof(osEventHandle)); - if (!event) - { - DEBUG_ERROR("out of ram"); - return NULL; - } - - event->lock = 0; - event->reset = false; - event->handle = handle; - event->wrapped = true; - event->msSpinTime = 0; - event->signaled = false; - return event; -} - -void os_freeEvent(osEventHandle * event) -{ - CloseHandle(event->handle); -} - -bool os_waitEvent(osEventHandle * event, unsigned int timeout) -{ - // wrapped events can't be enahnced - if (!event->wrapped) - { - if (event->signaled) - { - if (event->reset) - event->signaled = false; - return true; - } - - if (timeout == 0) - { - bool ret = event->signaled; - if (event->reset) - event->signaled = false; - return ret; - } - - if (event->msSpinTime) - { - unsigned int spinTime = event->msSpinTime; - if (timeout != TIMEOUT_INFINITE) - { - if (timeout > event->msSpinTime) - timeout -= event->msSpinTime; - else - { - spinTime -= timeout; - timeout = 0; - } - } - - LARGE_INTEGER end, now; - QueryPerformanceCounter(&end); - end.QuadPart += (app.perfFreq.QuadPart / 1000) * spinTime; - while(!event->signaled) - { - QueryPerformanceCounter(&now); - if (now.QuadPart >= end.QuadPart) - break; - } - - if (event->signaled) - { - if (event->reset) - event->signaled = false; - return true; - } - } - } - - const DWORD to = (timeout == TIMEOUT_INFINITE) ? INFINITE : (DWORD)timeout; - while(true) - { - switch(WaitForSingleObject(event->handle, to)) - { - case WAIT_OBJECT_0: - if (!event->reset) - event->signaled = true; - return true; - - case WAIT_ABANDONED: - continue; - - case WAIT_TIMEOUT: - if (timeout == TIMEOUT_INFINITE) - continue; - - return false; - - case WAIT_FAILED: - DEBUG_WINERROR("Wait for event failed", GetLastError()); - return false; - } - - DEBUG_ERROR("Unknown wait event return code"); - return false; - } -} - -bool os_waitEvents(osEventHandle * events[], int count, bool waitAll, unsigned int timeout) -{ - const DWORD to = (timeout == TIMEOUT_INFINITE) ? INFINITE : (DWORD)timeout; - - HANDLE * handles = (HANDLE *)malloc(sizeof(HANDLE) * count); - for(int i = 0; i < count; ++i) - handles[i] = events[i]->handle; - - while(true) - { - DWORD result = WaitForMultipleObjects(count, handles, waitAll, to); - if (result >= WAIT_OBJECT_0 && result < count) - { - // null non signaled events from the handle list - for(int i = 0; i < count; ++i) - if (i != result && !os_waitEvent(events[i], 0)) - handles[i] = NULL; - free(handles); - return true; - } - - if (result >= WAIT_ABANDONED_0 && result - WAIT_ABANDONED_0 < count) - continue; - - switch(result) - { - case WAIT_TIMEOUT: - if (timeout == TIMEOUT_INFINITE) - continue; - - free(handles); - return false; - - case WAIT_FAILED: - DEBUG_WINERROR("Wait for event failed", GetLastError()); - free(handles); - return false; - } - - DEBUG_ERROR("Unknown wait event return code"); - free(handles); - return false; - } -} - -bool os_signalEvent(osEventHandle * event) -{ - event->signaled = true; - return SetEvent(event->handle); -} - -bool os_resetEvent(osEventHandle * event) -{ - event->signaled = false; - return ResetEvent(event->handle); } \ No newline at end of file diff --git a/c-host/src/app.c b/c-host/src/app.c index ea0717be..ec383fe6 100644 --- a/c-host/src/app.c +++ b/c-host/src/app.c @@ -1,6 +1,6 @@ /* Looking Glass - KVM FrameRelay (KVMFR) Client -Copyright (C) 2017-2019 Geoffrey McRae +Copyright (C) 2017-2020 Geoffrey McRae https://looking-glass.hostfission.com This program is free software; you can redistribute it and/or modify it under @@ -25,6 +25,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include "common/locking.h" #include "common/KVMFR.h" #include "common/crash.h" +#include "common/thread.h" #include #include @@ -52,10 +53,10 @@ struct app FrameBuffer frame[MAX_FRAMES]; unsigned int frameOffset[MAX_FRAMES]; - bool running; - bool reinit; - osThreadHandle * pointerThread; - osThreadHandle * frameThread; + bool running; + bool reinit; + LGThread * pointerThread; + LGThread * frameThread; }; static struct app app; @@ -244,13 +245,13 @@ static int frameThread(void * opaque) bool startThreads() { app.running = true; - if (!os_createThread("CursorThread", pointerThread, NULL, &app.pointerThread)) + if (!lgCreateThread("CursorThread", pointerThread, NULL, &app.pointerThread)) { DEBUG_ERROR("Failed to create the pointer thread"); return false; } - if (!os_createThread("FrameThread", frameThread, NULL, &app.frameThread)) + if (!lgCreateThread("FrameThread", frameThread, NULL, &app.frameThread)) { DEBUG_ERROR("Failed to create the frame thread"); return false; @@ -266,14 +267,14 @@ bool stopThreads() app.running = false; app.iface->stop(); - if (app.frameThread && !os_joinThread(app.frameThread, NULL)) + if (app.frameThread && !lgJoinThread(app.frameThread, NULL)) { DEBUG_WARN("Failed to join the frame thread"); ok = false; } app.frameThread = NULL; - if (app.pointerThread && !os_joinThread(app.pointerThread, NULL)) + if (app.pointerThread && !lgJoinThread(app.pointerThread, NULL)) { DEBUG_WARN("Failed to join the pointer thread"); ok = false; diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 7b22d8ef..82d28c08 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -9,6 +9,8 @@ if(ENABLE_BACKTRACE) add_definitions(-DENABLE_BACKTRACE) endif() +add_subdirectory(src/platform) + set(COMMON_SOURCES src/stringutils.c src/stringlist.c @@ -16,26 +18,8 @@ set(COMMON_SOURCES src/framebuffer.c ) -set(LINUX_SOURCES - src/crash.linux.c - src/sysinfo.linux.c -) - -set(WINDOWS_SOURCES - src/crash.windows.c - src/sysinfo.windows.c -) - -if(WIN32) - set(SOURCES ${COMMON_SOURCES} ${WINDOWS_SOURCES}) - add_library(lg_common STATIC ${SOURCES}) -else() - set(SOURCES ${COMMON_SOURCES} ${LINUX_SOURCES}) - add_library(lg_common STATIC ${SOURCES}) - if(ENABLE_BACKTRACE) - target_link_libraries(lg_common bfd) - endif() -endif() +add_library(lg_common STATIC ${COMMON_SOURCES}) +target_link_libraries(lg_common lg_common_platform) target_include_directories(lg_common INTERFACE diff --git a/common/include/common/event.h b/common/include/common/event.h new file mode 100644 index 00000000..072a24c5 --- /dev/null +++ b/common/include/common/event.h @@ -0,0 +1,37 @@ +/* +Looking Glass - KVM FrameRelay (KVMFR) Client +Copyright (C) 2017-2020 Geoffrey McRae +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 +*/ + +#pragma once + +#include + +#define TIMEOUT_INFINITE ((unsigned int)~0) + +typedef struct LGEvent LGEvent; + +LGEvent * lgCreateEvent(bool autoReset, unsigned int msSpinTime); +void lgFreeEvent (LGEvent * handle); +bool lgWaitEvent (LGEvent * handle, unsigned int timeout); +bool lgWaitEvents (LGEvent * handles[], int count, bool waitAll, unsigned int timeout); +bool lgSignalEvent(LGEvent * handle); +bool lgResetEvent (LGEvent * handle); + +// os specific method to wrap/convert a native event into a LGEvent +// for windows this is an event HANDLE +LGEvent * lgWrapEvent(void * handle); \ No newline at end of file diff --git a/common/include/common/locking.h b/common/include/common/locking.h index edde8856..0f5afebd 100644 --- a/common/include/common/locking.h +++ b/common/include/common/locking.h @@ -18,7 +18,6 @@ Place, Suite 330, Boston, MA 02111-1307 USA */ #pragma once -#if defined(__GCC__) || defined(__GNUC__) #define INTERLOCKED_AND8 __sync_fetch_and_and #define INTERLOCKED_OR8 __sync_fetch_and_or #define INTERLOCKED_INC(x) __sync_fetch_and_add((x), 1) @@ -30,12 +29,3 @@ Place, Suite 330, Boston, MA 02111-1307 USA while(__sync_lock_test_and_set(&(lock), 1)) while((lock)); \ x\ __sync_lock_release(&(lock)); - -#else -#define INTERLOCKED_OR8 InterlockedOr8 -#define INTERLOCKED_AND8 InterlockedAnd8 -#define INTERLOCKED_INC InterlockedIncrement -#define INTERLOCKED_DEC InterlockedDecrement -#define INTERLOCKED_GET(x) InterlockedAdd((x), 0) -#define INTERLOCKED_CE(x, c, v) InterlockedCompareExchange((x), (v), (c)) -#endif \ No newline at end of file diff --git a/c-host/platform/Windows/include/windows/platform.h b/common/include/common/thread.h similarity index 69% rename from c-host/platform/Windows/include/windows/platform.h rename to common/include/common/thread.h index 1a098324..5ed03b9d 100644 --- a/c-host/platform/Windows/include/windows/platform.h +++ b/common/include/common/thread.h @@ -1,6 +1,6 @@ /* Looking Glass - KVM FrameRelay (KVMFR) Client -Copyright (C) 2017-2019 Geoffrey McRae +Copyright (C) 2017-2020 Geoffrey McRae https://looking-glass.hostfission.com This program is free software; you can redistribute it and/or modify it under @@ -17,7 +17,12 @@ this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "interface/platform.h" -#include +#pragma once -osEventHandle * os_wrapEvent(HANDLE event); \ No newline at end of file +#include + +typedef struct LGThread LGThread; +typedef int (*LGThreadFunction)(void * opaque); + +bool lgCreateThread(const char * name, LGThreadFunction function, void * opaque, LGThread ** handle); +bool lgJoinThread (LGThread * handle, int * resultCode); diff --git a/common/include/common/time.h b/common/include/common/time.h new file mode 100644 index 00000000..9c4ef217 --- /dev/null +++ b/common/include/common/time.h @@ -0,0 +1,45 @@ +/* +Looking Glass - KVM FrameRelay (KVMFR) Client +Copyright (C) 2017-2020 Geoffrey McRae +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 +*/ + +#pragma once + +#include + +#if defined(_WIN32) +#include +#else +#include +#endif + +static inline uint64_t getMicrotime() +{ +#if defined(_WIN32) + static LARGE_INTEGER freq = { 0 }; + if (!freq.QuadPart) + QueryPerformanceFrequency(&freq); + + LARGE_INTEGER time; + QueryPerformanceCounter(&time); + return time.QuadPart / (freq.QuadPart / 1000000LL); +#else + struct timespec time; + clock_gettime(CLOCK_MONOTONIC, &time); + return (uint64_t)time.tv_sec * 1000000LL + time.tv_nsec / 1000LL; +#endif +} \ No newline at end of file diff --git a/c-host/platform/Windows/include/windows/debug.h b/common/include/common/windebug.h similarity index 92% rename from c-host/platform/Windows/include/windows/debug.h rename to common/include/common/windebug.h index 7bfd68b6..729aa0d5 100644 --- a/c-host/platform/Windows/include/windows/debug.h +++ b/common/include/common/windebug.h @@ -1,6 +1,6 @@ /* Looking Glass - KVM FrameRelay (KVMFR) Client -Copyright (C) 2017-2019 Geoffrey McRae +Copyright (C) 2017-2020 Geoffrey McRae https://looking-glass.hostfission.com This program is free software; you can redistribute it and/or modify it under @@ -19,7 +19,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA #pragma once -#include "common/debug.h" +#include "debug.h" #include #ifdef __cplusplus diff --git a/common/src/platform/CMakeLists.txt b/common/src/platform/CMakeLists.txt new file mode 100644 index 00000000..58e764be --- /dev/null +++ b/common/src/platform/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.0) +project(lg_common_platform LANGUAGES C) + +if (UNIX) + add_subdirectory("linux") +elseif(WIN32) + add_subdirectory("windows") +endif() + +add_library(lg_common_platform INTERFACE) +target_link_libraries(lg_common_platform INTERFACE lg_common_platform_code) diff --git a/common/src/platform/linux/CMakeLists.txt b/common/src/platform/linux/CMakeLists.txt new file mode 100644 index 00000000..695c8c91 --- /dev/null +++ b/common/src/platform/linux/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.0) +project(lg_common_platform_code LANGUAGES C) + +include_directories( + ${PROJECT_SOURCE_DIR}/include +) + +add_library(lg_common_platform_code STATIC + crash.c + sysinfo.c +) diff --git a/common/src/bfd.inc.h b/common/src/platform/linux/bfd.inc.h similarity index 100% rename from common/src/bfd.inc.h rename to common/src/platform/linux/bfd.inc.h diff --git a/common/src/crash.linux.c b/common/src/platform/linux/crash.c similarity index 100% rename from common/src/crash.linux.c rename to common/src/platform/linux/crash.c diff --git a/common/src/sysinfo.linux.c b/common/src/platform/linux/sysinfo.c similarity index 100% rename from common/src/sysinfo.linux.c rename to common/src/platform/linux/sysinfo.c diff --git a/common/src/platform/windows/CMakeLists.txt b/common/src/platform/windows/CMakeLists.txt new file mode 100644 index 00000000..8702d35e --- /dev/null +++ b/common/src/platform/windows/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.0) +project(lg_common_platform_code LANGUAGES C) + +include_directories( + ${PROJECT_SOURCE_DIR}/include +) + +add_library(lg_common_platform_code STATIC + crash.c + sysinfo.c + thread.c + event.c + windebug.c +) diff --git a/common/src/crash.windows.c b/common/src/platform/windows/crash.c similarity index 100% rename from common/src/crash.windows.c rename to common/src/platform/windows/crash.c diff --git a/common/src/platform/windows/event.c b/common/src/platform/windows/event.c new file mode 100644 index 00000000..e1158e26 --- /dev/null +++ b/common/src/platform/windows/event.c @@ -0,0 +1,221 @@ +/* +Looking Glass - KVM FrameRelay (KVMFR) Client +Copyright (C) 2017-2020 Geoffrey McRae +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/event.h" +#include "common/windebug.h" +#include "common/time.h" + +#include + +struct LGEvent +{ + volatile int lock; + bool reset; + HANDLE handle; + bool wrapped; + unsigned int msSpinTime; + volatile bool signaled; +}; + +LGEvent * lgCreateEvent(bool autoReset, unsigned int msSpinTime) +{ + LGEvent * event = (LGEvent *)malloc(sizeof(LGEvent)); + if (!event) + { + DEBUG_ERROR("out of ram"); + return NULL; + } + + event->lock = 0; + event->reset = autoReset; + event->handle = CreateEvent(NULL, autoReset ? FALSE : TRUE, FALSE, NULL); + event->wrapped = false; + event->msSpinTime = msSpinTime; + event->signaled = false; + + if (!event->handle) + { + DEBUG_WINERROR("Failed to create the event", GetLastError()); + free(event); + return NULL; + } + + return event; +} + +LGEvent * lgWrapEvent(void * handle) +{ + LGEvent * event = (LGEvent *)malloc(sizeof(LGEvent)); + if (!event) + { + DEBUG_ERROR("out of ram"); + return NULL; + } + + event->lock = 0; + event->reset = false; + event->handle = (HANDLE)handle; + event->wrapped = true; + event->msSpinTime = 0; + event->signaled = false; + return event; +} + +void lgFreeEvent(LGEvent * event) +{ + CloseHandle(event->handle); +} + +bool lgWaitEvent(LGEvent * event, unsigned int timeout) +{ + // wrapped events can't be enahnced + if (!event->wrapped) + { + if (event->signaled) + { + if (event->reset) + event->signaled = false; + return true; + } + + if (timeout == 0) + { + bool ret = event->signaled; + if (event->reset) + event->signaled = false; + return ret; + } + + if (event->msSpinTime) + { + unsigned int spinTime = event->msSpinTime; + if (timeout != TIMEOUT_INFINITE) + { + if (timeout > event->msSpinTime) + timeout -= event->msSpinTime; + else + { + spinTime -= timeout; + timeout = 0; + } + } + + uint64_t now = getMicrotime(); + uint64_t end = now + spinTime * 1000; + while(!event->signaled) + { + now = getMicrotime(); + if (now >= end) + break; + } + + if (event->signaled) + { + if (event->reset) + event->signaled = false; + return true; + } + } + } + + const DWORD to = (timeout == TIMEOUT_INFINITE) ? INFINITE : (DWORD)timeout; + while(true) + { + switch(WaitForSingleObject(event->handle, to)) + { + case WAIT_OBJECT_0: + if (!event->reset) + event->signaled = true; + return true; + + case WAIT_ABANDONED: + continue; + + case WAIT_TIMEOUT: + if (timeout == TIMEOUT_INFINITE) + continue; + + return false; + + case WAIT_FAILED: + DEBUG_WINERROR("Wait for event failed", GetLastError()); + return false; + } + + DEBUG_ERROR("Unknown wait event return code"); + return false; + } +} + +bool lgWaitEvents(LGEvent * events[], int count, bool waitAll, unsigned int timeout) +{ + const DWORD to = (timeout == TIMEOUT_INFINITE) ? INFINITE : (DWORD)timeout; + + HANDLE * handles = (HANDLE *)malloc(sizeof(HANDLE) * count); + for(int i = 0; i < count; ++i) + handles[i] = events[i]->handle; + + while(true) + { + DWORD result = WaitForMultipleObjects(count, handles, waitAll, to); + if (result >= WAIT_OBJECT_0 && result < count) + { + // null non signaled events from the handle list + for(int i = 0; i < count; ++i) + if (i != result && !lgWaitEvent(events[i], 0)) + handles[i] = NULL; + free(handles); + return true; + } + + if (result >= WAIT_ABANDONED_0 && result - WAIT_ABANDONED_0 < count) + continue; + + switch(result) + { + case WAIT_TIMEOUT: + if (timeout == TIMEOUT_INFINITE) + continue; + + free(handles); + return false; + + case WAIT_FAILED: + DEBUG_WINERROR("Wait for event failed", GetLastError()); + free(handles); + return false; + } + + DEBUG_ERROR("Unknown wait event return code"); + free(handles); + return false; + } +} + +bool lgSignalEvent(LGEvent * event) +{ + event->signaled = true; + return SetEvent(event->handle); +} + +bool lgResetEvent(LGEvent * event) +{ + event->signaled = false; + return ResetEvent(event->handle); +} \ No newline at end of file diff --git a/common/src/sysinfo.windows.c b/common/src/platform/windows/sysinfo.c similarity index 100% rename from common/src/sysinfo.windows.c rename to common/src/platform/windows/sysinfo.c diff --git a/common/src/platform/windows/thread.c b/common/src/platform/windows/thread.c new file mode 100644 index 00000000..66530ea4 --- /dev/null +++ b/common/src/platform/windows/thread.c @@ -0,0 +1,93 @@ +/* +Looking Glass - KVM FrameRelay (KVMFR) Client +Copyright (C) 2017-2020 Geoffrey McRae +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 + +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; +} + diff --git a/c-host/platform/Windows/src/windebug.c b/common/src/platform/windows/windebug.c similarity index 93% rename from c-host/platform/Windows/src/windebug.c rename to common/src/platform/windows/windebug.c index 33d20b11..2ae65002 100644 --- a/c-host/platform/Windows/src/windebug.c +++ b/common/src/platform/windows/windebug.c @@ -1,6 +1,6 @@ /* Looking Glass - KVM FrameRelay (KVMFR) Client -Copyright (C) 2017-2019 Geoffrey McRae +Copyright (C) 2017-2020 Geoffrey McRae https://looking-glass.hostfission.com This program is free software; you can redistribute it and/or modify it under @@ -17,7 +17,7 @@ this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "windows/debug.h" +#include "common/windebug.h" #include void DebugWinError(const char * file, const unsigned int line, const char * function, const char * desc, HRESULT status)