1 /// Translated from C to D 2 module glfw3.posix_thread; 3 4 extern(C): @nogc: nothrow: __gshared: 5 //======================================================================== 6 // GLFW 3.3 POSIX - 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 // It is fine to use C99 in this file because it will not be built with VS 32 //======================================================================== 33 34 import glfw3.internal; 35 36 import core.stdc.assert_; 37 import core.stdc.string; 38 import core.sys.posix.pthread; 39 40 // POSIX-specific thread local storage data 41 // 42 struct _GLFWtlsPOSIX 43 { 44 GLFWbool allocated; 45 pthread_key_t key; 46 } 47 48 // POSIX-specific mutex data 49 // 50 struct _GLFWmutexPOSIX 51 { 52 import core.sys.posix.pthread: pthread_mutex_t; 53 GLFWbool allocated; 54 pthread_mutex_t handle; 55 56 } 57 58 mixin template _GLFW_PLATFORM_TLS_STATE() { _GLFWtlsPOSIX posix;} 59 mixin template _GLFW_PLATFORM_MUTEX_STATE() { _GLFWmutexPOSIX posix;} 60 61 ////////////////////////////////////////////////////////////////////////// 62 ////// GLFW platform API ////// 63 ////////////////////////////////////////////////////////////////////////// 64 65 GLFWbool _glfwPlatformCreateTls(_GLFWtls* tls) { 66 assert(tls.posix.allocated == GLFW_FALSE); 67 68 if (pthread_key_create(&tls.posix.key, null) != 0) 69 { 70 _glfwInputError(GLFW_PLATFORM_ERROR, 71 "POSIX: Failed to create context TLS"); 72 return GLFW_FALSE; 73 } 74 75 tls.posix.allocated = GLFW_TRUE; 76 return GLFW_TRUE; 77 } 78 79 void _glfwPlatformDestroyTls(_GLFWtls* tls) { 80 if (tls.posix.allocated) 81 pthread_key_delete(tls.posix.key); 82 memset(tls, 0, _GLFWtls.sizeof); 83 } 84 85 void* _glfwPlatformGetTls(_GLFWtls* tls) { 86 assert(tls.posix.allocated == GLFW_TRUE); 87 return pthread_getspecific(tls.posix.key); 88 } 89 90 void _glfwPlatformSetTls(_GLFWtls* tls, void* value) { 91 assert(tls.posix.allocated == GLFW_TRUE); 92 pthread_setspecific(tls.posix.key, value); 93 } 94 95 GLFWbool _glfwPlatformCreateMutex(_GLFWmutex* mutex) { 96 assert(mutex.posix.allocated == GLFW_FALSE); 97 98 if (pthread_mutex_init(&mutex.posix.handle, null) != 0) 99 { 100 _glfwInputError(GLFW_PLATFORM_ERROR, "POSIX: Failed to create mutex"); 101 return GLFW_FALSE; 102 } 103 104 return mutex.posix.allocated = GLFW_TRUE; 105 } 106 107 void _glfwPlatformDestroyMutex(_GLFWmutex* mutex) { 108 if (mutex.posix.allocated) 109 pthread_mutex_destroy(&mutex.posix.handle); 110 memset(mutex, 0, _GLFWmutex.sizeof); 111 } 112 113 void _glfwPlatformLockMutex(_GLFWmutex* mutex) { 114 assert(mutex.posix.allocated == GLFW_TRUE); 115 pthread_mutex_lock(&mutex.posix.handle); 116 } 117 118 void _glfwPlatformUnlockMutex(_GLFWmutex* mutex) { 119 assert(mutex.posix.allocated == GLFW_TRUE); 120 pthread_mutex_unlock(&mutex.posix.handle); 121 }