src/thread/windows/SDL_systls.c
author Sam Lantinga
Wed, 10 Jul 2013 02:32:04 -0700
changeset 7391 a29895dc5e9a
child 7393 358696c354a8
permissions -rw-r--r--
Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
slouken@7391
     1
/*
slouken@7391
     2
  Simple DirectMedia Layer
slouken@7391
     3
  Copyright (C) 1997-2013 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
slouken@7391
    22
#include "SDL_config.h"
slouken@7391
    23
#include "SDL_thread.h"
slouken@7391
    24
slouken@7391
    25
#if SDL_THREAD_WINDOWS
slouken@7391
    26
slouken@7391
    27
#include "../../core/windows/SDL_windows.h"
slouken@7391
    28
slouken@7391
    29
slouken@7391
    30
#define TLS_ALLOC_CHUNKSIZE 8
slouken@7391
    31
slouken@7391
    32
typedef struct {
slouken@7391
    33
    int limit;
slouken@7391
    34
    void *data[1];
slouken@7391
    35
} SDL_TLSData;
slouken@7391
    36
slouken@7391
    37
static SDL_SpinLock tls_lock;
slouken@7391
    38
static DWORD thread_local_storage = TLS_OUT_OF_INDEXES;
slouken@7391
    39
static SDL_atomic_t tls_id;
slouken@7391
    40
slouken@7391
    41
slouken@7391
    42
SDL_TLSID
slouken@7391
    43
SDL_TLSCreate()
slouken@7391
    44
{
slouken@7391
    45
    if (thread_local_storage == TLS_OUT_OF_INDEXES) {
slouken@7391
    46
        SDL_AtomicLock(&tls_lock);
slouken@7391
    47
        if (thread_local_storage == TLS_OUT_OF_INDEXES) {
slouken@7391
    48
            thread_local_storage = TlsAlloc();
slouken@7391
    49
            if (thread_local_storage == TLS_OUT_OF_INDEXES) {
slouken@7391
    50
                SDL_SetError("TlsAlloc() failed");
slouken@7391
    51
                SDL_AtomicUnlock(&tls_lock);
slouken@7391
    52
                return 0;
slouken@7391
    53
            }
slouken@7391
    54
        }
slouken@7391
    55
        SDL_AtomicUnlock(&tls_lock);
slouken@7391
    56
    }
slouken@7391
    57
    return SDL_AtomicIncRef(&tls_id)+1;
slouken@7391
    58
}
slouken@7391
    59
slouken@7391
    60
void *
slouken@7391
    61
SDL_TLSGet(SDL_TLSID id)
slouken@7391
    62
{
slouken@7391
    63
    SDL_TLSData *data;
slouken@7391
    64
slouken@7391
    65
    data = (SDL_TLSData *)TlsGetValue(thread_local_storage);
slouken@7391
    66
    if (!data || id <= 0 || id > data->limit) {
slouken@7391
    67
        return NULL;
slouken@7391
    68
    }
slouken@7391
    69
    return data->data[id-1];
slouken@7391
    70
}
slouken@7391
    71
slouken@7391
    72
int
slouken@7391
    73
SDL_TLSSet(SDL_TLSID id, const void *value)
slouken@7391
    74
{
slouken@7391
    75
    SDL_TLSData *data;
slouken@7391
    76
slouken@7391
    77
    if (thread_local_storage == TLS_OUT_OF_INDEXES || id <= 0) {
slouken@7391
    78
        return SDL_InvalidParamError(id);
slouken@7391
    79
    }
slouken@7391
    80
slouken@7391
    81
    data = (SDL_TLSData *)TlsGetValue(thread_local_storage);
slouken@7391
    82
    if (!data || id > data->limit) {
slouken@7391
    83
        int i, oldlimit, newlimit;
slouken@7391
    84
slouken@7391
    85
        oldlimit = data ? data->limit : 0;
slouken@7391
    86
        newlimit = (id + TLS_ALLOC_CHUNKSIZE);
slouken@7391
    87
        data = (SDL_TLSData *)SDL_realloc(data, sizeof(*data)+(newlimit-1)*sizeof(void*));
slouken@7391
    88
        if (!data) {
slouken@7391
    89
            return SDL_OutOfMemory();
slouken@7391
    90
        }
slouken@7391
    91
        data->limit = newlimit;
slouken@7391
    92
        for (i = oldlimit; i < newlimit; ++i) {
slouken@7391
    93
            data->data[i] = NULL;
slouken@7391
    94
        }
slouken@7391
    95
        if (!TlsSetValue(thread_local_storage, data)) {
slouken@7391
    96
            return SDL_SetError("TlsSetValue() failed");
slouken@7391
    97
        }
slouken@7391
    98
    }
slouken@7391
    99
slouken@7391
   100
    data->data[id-1] = SDL_const_cast(void*, value);
slouken@7391
   101
    return 0;
slouken@7391
   102
}
slouken@7391
   103
slouken@7391
   104
#endif /* SDL_THREAD_WINDOWS */
slouken@7391
   105
slouken@7391
   106
/* vi: set ts=4 sw=4 expandtab: */