win32_thread.c 3.1 KB

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