1 /// Translated from C to D 2 module glfw3.win32_thread; 3 4 extern(C): @nogc: nothrow: __gshared: 5 //======================================================================== 6 // GLFW 3.3 Win32 - www.glfw.org 7 //------------------------------------------------------------------------ 8 // Copyright (c) 2002-2006 Marcus Geelnard 9 // Copyright (c) 2006-2017 Camilla Löwy <elmindreda@glfw.org> 10 // 11 // This software is provided 'as-is', without any express or implied 12 // warranty. In no event will the authors be held liable for any damages 13 // arising from the use of this software. 14 // 15 // Permission is granted to anyone to use this software for any purpose, 16 // including commercial applications, and to alter it and redistribute it 17 // freely, subject to the following restrictions: 18 // 19 // 1. The origin of this software must not be misrepresented; you must not 20 // claim that you wrote the original software. If you use this software 21 // in a product, an acknowledgment in the product documentation would 22 // be appreciated but is not required. 23 // 24 // 2. Altered source versions must be plainly marked as such, and must not 25 // be misrepresented as being the original software. 26 // 27 // 3. This notice may not be removed or altered from any source 28 // distribution. 29 // 30 //======================================================================== 31 // Please use C89 style variable declarations in this file because VS 2010 32 //======================================================================== 33 34 import glfw3.internal; 35 36 import core.stdc.assert_; 37 import core.stdc.string: memset; 38 package: 39 40 ////////////////////////////////////////////////////////////////////////// 41 ////// GLFW platform API ////// 42 ////////////////////////////////////////////////////////////////////////// 43 44 GLFWbool _glfwPlatformCreateTls(_GLFWtls* tls) { 45 assert(tls.win32.allocated == GLFW_FALSE); 46 47 tls.win32.index = TlsAlloc(); 48 if (tls.win32.index == TLS_OUT_OF_INDEXES) 49 { 50 _glfwInputErrorWin32(GLFW_PLATFORM_ERROR, 51 "Win32: Failed to allocate TLS index"); 52 return GLFW_FALSE; 53 } 54 55 tls.win32.allocated = GLFW_TRUE; 56 return GLFW_TRUE; 57 } 58 59 void _glfwPlatformDestroyTls(_GLFWtls* tls) { 60 if (tls.win32.allocated) 61 TlsFree(tls.win32.index); 62 memset(tls, 0, _GLFWtls.sizeof); 63 } 64 65 void* _glfwPlatformGetTls(_GLFWtls* tls) { 66 assert(tls.win32.allocated == GLFW_TRUE); 67 return TlsGetValue(tls.win32.index); 68 } 69 70 void _glfwPlatformSetTls(_GLFWtls* tls, void* value) { 71 assert(tls.win32.allocated == GLFW_TRUE); 72 TlsSetValue(tls.win32.index, value); 73 } 74 75 GLFWbool _glfwPlatformCreateMutex(_GLFWmutex* mutex) { 76 assert(mutex.win32.allocated == GLFW_FALSE); 77 InitializeCriticalSection(&mutex.win32.section); 78 return mutex.win32.allocated = GLFW_TRUE; 79 } 80 81 void _glfwPlatformDestroyMutex(_GLFWmutex* mutex) { 82 if (mutex.win32.allocated) 83 DeleteCriticalSection(&mutex.win32.section); 84 memset(mutex, 0, _GLFWmutex.sizeof); 85 } 86 87 void _glfwPlatformLockMutex(_GLFWmutex* mutex) { 88 assert(mutex.win32.allocated == GLFW_TRUE); 89 EnterCriticalSection(&mutex.win32.section); 90 } 91 92 void _glfwPlatformUnlockMutex(_GLFWmutex* mutex) { 93 assert(mutex.win32.allocated == GLFW_TRUE); 94 LeaveCriticalSection(&mutex.win32.section); 95 }