SDL_systls.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2016 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_WINDOWS
  20. #include "../../core/windows/SDL_windows.h"
  21. #include "SDL_thread.h"
  22. #include "../SDL_thread_c.h"
  23. static DWORD thread_local_storage = TLS_OUT_OF_INDEXES;
  24. static SDL_bool generic_local_storage = SDL_FALSE;
  25. SDL_TLSData *
  26. SDL_SYS_GetTLSData()
  27. {
  28. if (thread_local_storage == TLS_OUT_OF_INDEXES && !generic_local_storage) {
  29. static SDL_SpinLock lock;
  30. SDL_AtomicLock(&lock);
  31. if (thread_local_storage == TLS_OUT_OF_INDEXES && !generic_local_storage) {
  32. DWORD storage = TlsAlloc();
  33. if (storage != TLS_OUT_OF_INDEXES) {
  34. SDL_MemoryBarrierRelease();
  35. thread_local_storage = storage;
  36. } else {
  37. generic_local_storage = SDL_TRUE;
  38. }
  39. }
  40. SDL_AtomicUnlock(&lock);
  41. }
  42. if (generic_local_storage) {
  43. return SDL_Generic_GetTLSData();
  44. }
  45. SDL_MemoryBarrierAcquire();
  46. return (SDL_TLSData *)TlsGetValue(thread_local_storage);
  47. }
  48. int
  49. SDL_SYS_SetTLSData(SDL_TLSData *data)
  50. {
  51. if (generic_local_storage) {
  52. return SDL_Generic_SetTLSData(data);
  53. }
  54. if (!TlsSetValue(thread_local_storage, data)) {
  55. return SDL_SetError("TlsSetValue() failed");
  56. }
  57. return 0;
  58. }
  59. #endif /* SDL_THREAD_WINDOWS */
  60. /* vi: set ts=4 sw=4 expandtab: */