src/thread/windows/SDL_systls.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 19 Mar 2019 17:20:54 -0700
changeset 12668 dd6b61160cdc
parent 12667 4891e138886a
permissions -rw-r--r--
Hopefully fixed the mingw32 build
slouken@7391
     1
/*
slouken@7391
     2
  Simple DirectMedia Layer
slouken@12503
     3
  Copyright (C) 1997-2019 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@12667
    31
#if WINAPI_FAMILY_WINRT
slouken@12668
    32
#include <fibersapi.h>
slouken@12668
    33
slouken@12668
    34
#ifndef TLS_OUT_OF_INDEXES
slouken@12665
    35
#define TLS_OUT_OF_INDEXES  FLS_OUT_OF_INDEXES
slouken@12668
    36
#endif
slouken@12668
    37
slouken@12665
    38
#define TlsAlloc()  FlsAlloc(NULL)
slouken@12665
    39
#define TlsSetValue FlsSetValue
slouken@12665
    40
#define TlsGetValue FlsGetValue
slouken@12665
    41
#endif
slouken@12665
    42
slouken@7393
    43
static DWORD thread_local_storage = TLS_OUT_OF_INDEXES;
slouken@7393
    44
static SDL_bool generic_local_storage = SDL_FALSE;
slouken@7391
    45
slouken@7393
    46
SDL_TLSData *
philipp@10617
    47
SDL_SYS_GetTLSData(void)
slouken@7391
    48
{
slouken@7393
    49
    if (thread_local_storage == TLS_OUT_OF_INDEXES && !generic_local_storage) {
slouken@7393
    50
        static SDL_SpinLock lock;
slouken@7393
    51
        SDL_AtomicLock(&lock);
slouken@7393
    52
        if (thread_local_storage == TLS_OUT_OF_INDEXES && !generic_local_storage) {
slouken@7393
    53
            DWORD storage = TlsAlloc();
slouken@7393
    54
            if (storage != TLS_OUT_OF_INDEXES) {
slouken@7393
    55
                SDL_MemoryBarrierRelease();
slouken@7393
    56
                thread_local_storage = storage;
slouken@7393
    57
            } else {
slouken@7393
    58
                generic_local_storage = SDL_TRUE;
slouken@7391
    59
            }
slouken@7391
    60
        }
slouken@7393
    61
        SDL_AtomicUnlock(&lock);
slouken@7391
    62
    }
slouken@7393
    63
    if (generic_local_storage) {
slouken@7393
    64
        return SDL_Generic_GetTLSData();
slouken@7391
    65
    }
slouken@7393
    66
    SDL_MemoryBarrierAcquire();
slouken@7393
    67
    return (SDL_TLSData *)TlsGetValue(thread_local_storage);
slouken@7391
    68
}
slouken@7391
    69
slouken@7391
    70
int
slouken@7393
    71
SDL_SYS_SetTLSData(SDL_TLSData *data)
slouken@7391
    72
{
slouken@7393
    73
    if (generic_local_storage) {
slouken@7393
    74
        return SDL_Generic_SetTLSData(data);
slouken@7391
    75
    }
slouken@7393
    76
    if (!TlsSetValue(thread_local_storage, data)) {
slouken@7393
    77
        return SDL_SetError("TlsSetValue() failed");
slouken@7391
    78
    }
slouken@7391
    79
    return 0;
slouken@7391
    80
}
slouken@7391
    81
slouken@7391
    82
#endif /* SDL_THREAD_WINDOWS */
slouken@7391
    83
slouken@7391
    84
/* vi: set ts=4 sw=4 expandtab: */