author | Sam Lantinga |
Sun, 01 Jan 2017 18:33:28 -0800 | |
changeset 10737 | 3406a0f8b041 |
parent 10617 | 346c02ff71b6 |
child 11811 | 5d94cb6b24d3 |
permissions | -rw-r--r-- |
slouken@7391 | 1 |
/* |
slouken@7391 | 2 |
Simple DirectMedia Layer |
slouken@10737 | 3 |
Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org> |
slouken@7391 | 4 |
|
slouken@7391 | 5 |
This software is provided 'as-is', without any express or implied |
slouken@7391 | 6 |
warranty. In no event will the authors be held liable for any damages |
slouken@7391 | 7 |
arising from the use of this software. |
slouken@7391 | 8 |
|
slouken@7391 | 9 |
Permission is granted to anyone to use this software for any purpose, |
slouken@7391 | 10 |
including commercial applications, and to alter it and redistribute it |
slouken@7391 | 11 |
freely, subject to the following restrictions: |
slouken@7391 | 12 |
|
slouken@7391 | 13 |
1. The origin of this software must not be misrepresented; you must not |
slouken@7391 | 14 |
claim that you wrote the original software. If you use this software |
slouken@7391 | 15 |
in a product, an acknowledgment in the product documentation would be |
slouken@7391 | 16 |
appreciated but is not required. |
slouken@7391 | 17 |
2. Altered source versions must be plainly marked as such, and must not be |
slouken@7391 | 18 |
misrepresented as being the original software. |
slouken@7391 | 19 |
3. This notice may not be removed or altered from any source distribution. |
slouken@7391 | 20 |
*/ |
slouken@7391 | 21 |
|
icculus@8093 | 22 |
#include "../../SDL_internal.h" |
slouken@7391 | 23 |
|
slouken@7391 | 24 |
#if SDL_THREAD_WINDOWS |
slouken@7391 | 25 |
|
slouken@7391 | 26 |
#include "../../core/windows/SDL_windows.h" |
slouken@7391 | 27 |
|
slouken@7828 | 28 |
#include "SDL_thread.h" |
slouken@7828 | 29 |
#include "../SDL_thread_c.h" |
slouken@7391 | 30 |
|
slouken@7391 | 31 |
static DWORD thread_local_storage = TLS_OUT_OF_INDEXES; |
slouken@7393 | 32 |
static SDL_bool generic_local_storage = SDL_FALSE; |
slouken@7391 | 33 |
|
slouken@7393 | 34 |
SDL_TLSData * |
philipp@10617 | 35 |
SDL_SYS_GetTLSData(void) |
slouken@7391 | 36 |
{ |
slouken@7393 | 37 |
if (thread_local_storage == TLS_OUT_OF_INDEXES && !generic_local_storage) { |
slouken@7393 | 38 |
static SDL_SpinLock lock; |
slouken@7393 | 39 |
SDL_AtomicLock(&lock); |
slouken@7393 | 40 |
if (thread_local_storage == TLS_OUT_OF_INDEXES && !generic_local_storage) { |
slouken@7393 | 41 |
DWORD storage = TlsAlloc(); |
slouken@7393 | 42 |
if (storage != TLS_OUT_OF_INDEXES) { |
slouken@7393 | 43 |
SDL_MemoryBarrierRelease(); |
slouken@7393 | 44 |
thread_local_storage = storage; |
slouken@7393 | 45 |
} else { |
slouken@7393 | 46 |
generic_local_storage = SDL_TRUE; |
slouken@7391 | 47 |
} |
slouken@7391 | 48 |
} |
slouken@7393 | 49 |
SDL_AtomicUnlock(&lock); |
slouken@7391 | 50 |
} |
slouken@7393 | 51 |
if (generic_local_storage) { |
slouken@7393 | 52 |
return SDL_Generic_GetTLSData(); |
slouken@7391 | 53 |
} |
slouken@7393 | 54 |
SDL_MemoryBarrierAcquire(); |
slouken@7393 | 55 |
return (SDL_TLSData *)TlsGetValue(thread_local_storage); |
slouken@7391 | 56 |
} |
slouken@7391 | 57 |
|
slouken@7391 | 58 |
int |
slouken@7393 | 59 |
SDL_SYS_SetTLSData(SDL_TLSData *data) |
slouken@7391 | 60 |
{ |
slouken@7393 | 61 |
if (generic_local_storage) { |
slouken@7393 | 62 |
return SDL_Generic_SetTLSData(data); |
slouken@7391 | 63 |
} |
slouken@7393 | 64 |
if (!TlsSetValue(thread_local_storage, data)) { |
slouken@7393 | 65 |
return SDL_SetError("TlsSetValue() failed"); |
slouken@7391 | 66 |
} |
slouken@7391 | 67 |
return 0; |
slouken@7391 | 68 |
} |
slouken@7391 | 69 |
|
slouken@7391 | 70 |
#endif /* SDL_THREAD_WINDOWS */ |
slouken@7391 | 71 |
|
slouken@7391 | 72 |
/* vi: set ts=4 sw=4 expandtab: */ |