1b877906bSopenharmony_ci//========================================================================
2b877906bSopenharmony_ci// GLFW 3.5 POSIX - www.glfw.org
3b877906bSopenharmony_ci//------------------------------------------------------------------------
4b877906bSopenharmony_ci// Copyright (c) 2002-2006 Marcus Geelnard
5b877906bSopenharmony_ci// Copyright (c) 2006-2017 Camilla Löwy <elmindreda@glfw.org>
6b877906bSopenharmony_ci//
7b877906bSopenharmony_ci// This software is provided 'as-is', without any express or implied
8b877906bSopenharmony_ci// warranty. In no event will the authors be held liable for any damages
9b877906bSopenharmony_ci// arising from the use of this software.
10b877906bSopenharmony_ci//
11b877906bSopenharmony_ci// Permission is granted to anyone to use this software for any purpose,
12b877906bSopenharmony_ci// including commercial applications, and to alter it and redistribute it
13b877906bSopenharmony_ci// freely, subject to the following restrictions:
14b877906bSopenharmony_ci//
15b877906bSopenharmony_ci// 1. The origin of this software must not be misrepresented; you must not
16b877906bSopenharmony_ci//    claim that you wrote the original software. If you use this software
17b877906bSopenharmony_ci//    in a product, an acknowledgment in the product documentation would
18b877906bSopenharmony_ci//    be appreciated but is not required.
19b877906bSopenharmony_ci//
20b877906bSopenharmony_ci// 2. Altered source versions must be plainly marked as such, and must not
21b877906bSopenharmony_ci//    be misrepresented as being the original software.
22b877906bSopenharmony_ci//
23b877906bSopenharmony_ci// 3. This notice may not be removed or altered from any source
24b877906bSopenharmony_ci//    distribution.
25b877906bSopenharmony_ci//
26b877906bSopenharmony_ci//========================================================================
27b877906bSopenharmony_ci
28b877906bSopenharmony_ci#include "internal.h"
29b877906bSopenharmony_ci
30b877906bSopenharmony_ci#if defined(GLFW_BUILD_POSIX_THREAD)
31b877906bSopenharmony_ci
32b877906bSopenharmony_ci#include <assert.h>
33b877906bSopenharmony_ci#include <string.h>
34b877906bSopenharmony_ci
35b877906bSopenharmony_ci
36b877906bSopenharmony_ci//////////////////////////////////////////////////////////////////////////
37b877906bSopenharmony_ci//////                       GLFW platform API                      //////
38b877906bSopenharmony_ci//////////////////////////////////////////////////////////////////////////
39b877906bSopenharmony_ci
40b877906bSopenharmony_ciGLFWbool _glfwPlatformCreateTls(_GLFWtls* tls)
41b877906bSopenharmony_ci{
42b877906bSopenharmony_ci    assert(tls->posix.allocated == GLFW_FALSE);
43b877906bSopenharmony_ci
44b877906bSopenharmony_ci    if (pthread_key_create(&tls->posix.key, NULL) != 0)
45b877906bSopenharmony_ci    {
46b877906bSopenharmony_ci        _glfwInputError(GLFW_PLATFORM_ERROR,
47b877906bSopenharmony_ci                        "POSIX: Failed to create context TLS");
48b877906bSopenharmony_ci        return GLFW_FALSE;
49b877906bSopenharmony_ci    }
50b877906bSopenharmony_ci
51b877906bSopenharmony_ci    tls->posix.allocated = GLFW_TRUE;
52b877906bSopenharmony_ci    return GLFW_TRUE;
53b877906bSopenharmony_ci}
54b877906bSopenharmony_ci
55b877906bSopenharmony_civoid _glfwPlatformDestroyTls(_GLFWtls* tls)
56b877906bSopenharmony_ci{
57b877906bSopenharmony_ci    if (tls->posix.allocated)
58b877906bSopenharmony_ci        pthread_key_delete(tls->posix.key);
59b877906bSopenharmony_ci    memset(tls, 0, sizeof(_GLFWtls));
60b877906bSopenharmony_ci}
61b877906bSopenharmony_ci
62b877906bSopenharmony_civoid* _glfwPlatformGetTls(_GLFWtls* tls)
63b877906bSopenharmony_ci{
64b877906bSopenharmony_ci    assert(tls->posix.allocated == GLFW_TRUE);
65b877906bSopenharmony_ci    return pthread_getspecific(tls->posix.key);
66b877906bSopenharmony_ci}
67b877906bSopenharmony_ci
68b877906bSopenharmony_civoid _glfwPlatformSetTls(_GLFWtls* tls, void* value)
69b877906bSopenharmony_ci{
70b877906bSopenharmony_ci    assert(tls->posix.allocated == GLFW_TRUE);
71b877906bSopenharmony_ci    pthread_setspecific(tls->posix.key, value);
72b877906bSopenharmony_ci}
73b877906bSopenharmony_ci
74b877906bSopenharmony_ciGLFWbool _glfwPlatformCreateMutex(_GLFWmutex* mutex)
75b877906bSopenharmony_ci{
76b877906bSopenharmony_ci    assert(mutex->posix.allocated == GLFW_FALSE);
77b877906bSopenharmony_ci
78b877906bSopenharmony_ci    if (pthread_mutex_init(&mutex->posix.handle, NULL) != 0)
79b877906bSopenharmony_ci    {
80b877906bSopenharmony_ci        _glfwInputError(GLFW_PLATFORM_ERROR, "POSIX: Failed to create mutex");
81b877906bSopenharmony_ci        return GLFW_FALSE;
82b877906bSopenharmony_ci    }
83b877906bSopenharmony_ci
84b877906bSopenharmony_ci    return mutex->posix.allocated = GLFW_TRUE;
85b877906bSopenharmony_ci}
86b877906bSopenharmony_ci
87b877906bSopenharmony_civoid _glfwPlatformDestroyMutex(_GLFWmutex* mutex)
88b877906bSopenharmony_ci{
89b877906bSopenharmony_ci    if (mutex->posix.allocated)
90b877906bSopenharmony_ci        pthread_mutex_destroy(&mutex->posix.handle);
91b877906bSopenharmony_ci    memset(mutex, 0, sizeof(_GLFWmutex));
92b877906bSopenharmony_ci}
93b877906bSopenharmony_ci
94b877906bSopenharmony_civoid _glfwPlatformLockMutex(_GLFWmutex* mutex)
95b877906bSopenharmony_ci{
96b877906bSopenharmony_ci    assert(mutex->posix.allocated == GLFW_TRUE);
97b877906bSopenharmony_ci    pthread_mutex_lock(&mutex->posix.handle);
98b877906bSopenharmony_ci}
99b877906bSopenharmony_ci
100b877906bSopenharmony_civoid _glfwPlatformUnlockMutex(_GLFWmutex* mutex)
101b877906bSopenharmony_ci{
102b877906bSopenharmony_ci    assert(mutex->posix.allocated == GLFW_TRUE);
103b877906bSopenharmony_ci    pthread_mutex_unlock(&mutex->posix.handle);
104b877906bSopenharmony_ci}
105b877906bSopenharmony_ci
106b877906bSopenharmony_ci#endif // GLFW_BUILD_POSIX_THREAD
107b877906bSopenharmony_ci
108