From 35f3b58c219345e3b42a043609ff2ce33250f77d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Sun, 18 Jul 2021 20:31:26 +0200 Subject: [PATCH] Win32: Make thread functions backend independent This is work towards allowing the Null backend to build for Windows using the Win32 threading API, without the Win32 backend being enabled. --- src/CMakeLists.txt | 2 +- src/win32_platform.h | 19 +----------------- src/win32_thread.c | 3 +-- src/win32_thread.h | 48 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 21 deletions(-) create mode 100644 src/win32_thread.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 964bb909..035b57a6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -22,7 +22,7 @@ if (_GLFW_COCOA) nsgl_context.m egl_context.c osmesa_context.c) elseif (_GLFW_WIN32) target_compile_definitions(glfw PRIVATE _GLFW_WIN32) - target_sources(glfw PRIVATE win32_platform.h win32_joystick.h + target_sources(glfw PRIVATE win32_platform.h win32_joystick.h win32_thread.h win32_init.c win32_joystick.c win32_monitor.c win32_time.c win32_thread.c win32_window.c wgl_context.c egl_context.c osmesa_context.c) diff --git a/src/win32_platform.h b/src/win32_platform.h index a258c355..a33e4e4b 100644 --- a/src/win32_platform.h +++ b/src/win32_platform.h @@ -356,6 +356,7 @@ typedef struct VkWin32SurfaceCreateInfoKHR typedef VkResult (APIENTRY *PFN_vkCreateWin32SurfaceKHR)(VkInstance,const VkWin32SurfaceCreateInfoKHR*,const VkAllocationCallbacks*,VkSurfaceKHR*); typedef VkBool32 (APIENTRY *PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR)(VkPhysicalDevice,uint32_t); +#include "win32_thread.h" #include "win32_joystick.h" #if !defined(_GLFW_WNDCLASSNAME) @@ -367,8 +368,6 @@ typedef VkBool32 (APIENTRY *PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR)( #define _GLFW_PLATFORM_LIBRARY_TIMER_STATE _GLFWtimerWin32 win32 #define _GLFW_PLATFORM_MONITOR_STATE _GLFWmonitorWin32 win32 #define _GLFW_PLATFORM_CURSOR_STATE _GLFWcursorWin32 win32 -#define _GLFW_PLATFORM_TLS_STATE _GLFWtlsWin32 win32 -#define _GLFW_PLATFORM_MUTEX_STATE _GLFWmutexWin32 win32 #define _GLFW_PLATFORM_CONTEXT_STATE _GLFWcontextWGL wgl #define _GLFW_PLATFORM_LIBRARY_CONTEXT_STATE _GLFWlibraryWGL wgl @@ -536,22 +535,6 @@ typedef struct _GLFWtimerWin32 uint64_t frequency; } _GLFWtimerWin32; -// Win32-specific thread local storage data -// -typedef struct _GLFWtlsWin32 -{ - GLFWbool allocated; - DWORD index; -} _GLFWtlsWin32; - -// Win32-specific mutex data -// -typedef struct _GLFWmutexWin32 -{ - GLFWbool allocated; - CRITICAL_SECTION section; -} _GLFWmutexWin32; - GLFWbool _glfwRegisterWindowClassWin32(void); void _glfwUnregisterWindowClassWin32(void); diff --git a/src/win32_thread.c b/src/win32_thread.c index 53b34af2..35b8f99e 100644 --- a/src/win32_thread.c +++ b/src/win32_thread.c @@ -43,8 +43,7 @@ GLFWbool _glfwPlatformCreateTls(_GLFWtls* tls) tls->win32.index = TlsAlloc(); if (tls->win32.index == TLS_OUT_OF_INDEXES) { - _glfwInputErrorWin32(GLFW_PLATFORM_ERROR, - "Win32: Failed to allocate TLS index"); + _glfwInputError(GLFW_PLATFORM_ERROR, "Win32: Failed to allocate TLS index"); return GLFW_FALSE; } diff --git a/src/win32_thread.h b/src/win32_thread.h new file mode 100644 index 00000000..afc8f546 --- /dev/null +++ b/src/win32_thread.h @@ -0,0 +1,48 @@ +//======================================================================== +// GLFW 3.4 Win32 - www.glfw.org +//------------------------------------------------------------------------ +// Copyright (c) 2002-2006 Marcus Geelnard +// Copyright (c) 2006-2017 Camilla Löwy +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would +// be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not +// be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source +// distribution. +// +//======================================================================== + +#include + +#define _GLFW_PLATFORM_TLS_STATE _GLFWtlsWin32 win32 +#define _GLFW_PLATFORM_MUTEX_STATE _GLFWmutexWin32 win32 + +// Win32-specific thread local storage data +// +typedef struct _GLFWtlsWin32 +{ + GLFWbool allocated; + DWORD index; +} _GLFWtlsWin32; + +// Win32-specific mutex data +// +typedef struct _GLFWmutexWin32 +{ + GLFWbool allocated; + CRITICAL_SECTION section; +} _GLFWmutexWin32; +