SDL_systls.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_THREAD_OS2
  20. #include "../../core/os2/SDL_os2.h"
  21. #include "SDL_thread.h"
  22. #include "../SDL_thread_c.h"
  23. #define INCL_DOSPROCESS
  24. #define INCL_DOSERRORS
  25. #include <os2.h>
  26. SDL_TLSData **ppSDLTLSData = NULL;
  27. static ULONG cTLSAlloc = 0;
  28. /* SDL_OS2TLSAlloc() called from SDL_InitSubSystem() */
  29. void SDL_OS2TLSAlloc(void)
  30. {
  31. ULONG ulRC;
  32. if (cTLSAlloc == 0 || ppSDLTLSData == NULL) {
  33. /* First call - allocate the thread local memory (1 DWORD) */
  34. ulRC = DosAllocThreadLocalMemory(1, (PULONG *)&ppSDLTLSData);
  35. if (ulRC != NO_ERROR) {
  36. debug_os2("DosAllocThreadLocalMemory() failed, rc = %u", ulRC);
  37. }
  38. }
  39. cTLSAlloc++;
  40. }
  41. /* SDL_OS2TLSFree() called from SDL_QuitSubSystem() */
  42. void SDL_OS2TLSFree(void)
  43. {
  44. ULONG ulRC;
  45. if (cTLSAlloc != 0)
  46. cTLSAlloc--;
  47. if (cTLSAlloc == 0 && ppSDLTLSData != NULL) {
  48. /* Last call - free the thread local memory */
  49. ulRC = DosFreeThreadLocalMemory((PULONG)ppSDLTLSData);
  50. if (ulRC != NO_ERROR) {
  51. debug_os2("DosFreeThreadLocalMemory() failed, rc = %u", ulRC);
  52. } else {
  53. ppSDLTLSData = NULL;
  54. }
  55. }
  56. }
  57. SDL_TLSData *SDL_SYS_GetTLSData(void)
  58. {
  59. return (ppSDLTLSData == NULL)? NULL : *ppSDLTLSData;
  60. }
  61. int SDL_SYS_SetTLSData(SDL_TLSData *data)
  62. {
  63. if (!ppSDLTLSData)
  64. return -1;
  65. *ppSDLTLSData = data;
  66. return 0;
  67. }
  68. #endif /* SDL_THREAD_OS2 */
  69. /* vi: set ts=4 sw=4 expandtab: */