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 public import glfw3.internal;
35 
36 import core.stdc.assert_;
37 import core.stdc.string: memset;
38 
39 //////////////////////////////////////////////////////////////////////////
40 //////                       GLFW platform API                      //////
41 //////////////////////////////////////////////////////////////////////////
42 
43 GLFWbool _glfwPlatformCreateTls(_GLFWtls* tls) {
44     assert(tls.win32.allocated == GLFW_FALSE);
45 
46     tls.win32.index = TlsAlloc();
47     if (tls.win32.index == TLS_OUT_OF_INDEXES)
48     {
49         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
50                              "Win32: Failed to allocate TLS index");
51         return GLFW_FALSE;
52     }
53 
54     tls.win32.allocated = GLFW_TRUE;
55     return GLFW_TRUE;
56 }
57 
58 void _glfwPlatformDestroyTls(_GLFWtls* tls) {
59     if (tls.win32.allocated)
60         TlsFree(tls.win32.index);
61     memset(tls, 0, _GLFWtls.sizeof);
62 }
63 
64 void* _glfwPlatformGetTls(_GLFWtls* tls) {
65     assert(tls.win32.allocated == GLFW_TRUE);
66     return TlsGetValue(tls.win32.index);
67 }
68 
69 void _glfwPlatformSetTls(_GLFWtls* tls, void* value) {
70     assert(tls.win32.allocated == GLFW_TRUE);
71     TlsSetValue(tls.win32.index, value);
72 }
73 
74 GLFWbool _glfwPlatformCreateMutex(_GLFWmutex* mutex) {
75     assert(mutex.win32.allocated == GLFW_FALSE);
76     InitializeCriticalSection(&mutex.win32.section);
77     return mutex.win32.allocated = GLFW_TRUE;
78 }
79 
80 void _glfwPlatformDestroyMutex(_GLFWmutex* mutex) {
81     if (mutex.win32.allocated)
82         DeleteCriticalSection(&mutex.win32.section);
83     memset(mutex, 0, _GLFWmutex.sizeof);
84 }
85 
86 void _glfwPlatformLockMutex(_GLFWmutex* mutex) {
87     assert(mutex.win32.allocated == GLFW_TRUE);
88     EnterCriticalSection(&mutex.win32.section);
89 }
90 
91 void _glfwPlatformUnlockMutex(_GLFWmutex* mutex) {
92     assert(mutex.win32.allocated == GLFW_TRUE);
93     LeaveCriticalSection(&mutex.win32.section);
94 }