src/thread/generic/SDL_systls.c
author Sam Lantinga <slouken@libsdl.org>
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
/* This is a generic implementation of thread-local storage which doesn't
slouken@7391
    26
   require additional OS support.
slouken@7391
    27
slouken@7391
    28
   It is not especially efficient and doesn't clean up thread-local storage
slouken@7391
    29
   as threads exit.  If there is a real OS that doesn't support thread-local
slouken@7391
    30
   storage this implementation should be improved to be production quality.
slouken@7391
    31
*/
slouken@7391
    32
slouken@7391
    33
#define TLS_ALLOC_CHUNKSIZE 8
slouken@7391
    34
slouken@7391
    35
typedef struct {
slouken@7391
    36
    int limit;
slouken@7391
    37
    void *data[1];
slouken@7391
    38
} SDL_TLSData;
slouken@7391
    39
slouken@7391
    40
typedef struct SDL_TLSEntry {
slouken@7391
    41
    SDL_threadID thread;
slouken@7391
    42
    SDL_TLSData *data;
slouken@7391
    43
    struct SDL_TLSEntry *next;
slouken@7391
    44
} SDL_TLSEntry;
slouken@7391
    45
slouken@7391
    46
static SDL_SpinLock tls_lock;
slouken@7391
    47
static SDL_mutex *tls_mutex;
slouken@7391
    48
static SDL_TLSEntry *thread_local_storage;
slouken@7391
    49
static SDL_atomic_t tls_id;
slouken@7391
    50
slouken@7391
    51
slouken@7391
    52
static SDL_TLSData *GetTLSData()
slouken@7391
    53
{
slouken@7391
    54
    SDL_threadID thread = SDL_ThreadID();
slouken@7391
    55
    SDL_TLSEntry *entry;
slouken@7391
    56
    SDL_TLSData *data = NULL;
slouken@7391
    57
slouken@7391
    58
    if (!tls_mutex) {
slouken@7391
    59
        SDL_AtomicLock(&tls_lock);
slouken@7391
    60
        if (!tls_mutex) {
slouken@7391
    61
            tls_mutex = SDL_CreateMutex();
slouken@7391
    62
            if (!tls_mutex) {
slouken@7391
    63
                SDL_AtomicUnlock(&tls_lock);
slouken@7391
    64
                return NULL;
slouken@7391
    65
            }
slouken@7391
    66
        }
slouken@7391
    67
        SDL_AtomicUnlock(&tls_lock);
slouken@7391
    68
    }
slouken@7391
    69
slouken@7391
    70
    SDL_LockMutex(tls_mutex);
slouken@7391
    71
    for (entry = thread_local_storage; entry; entry = entry->next) {
slouken@7391
    72
        if (entry->thread == thread) {
slouken@7391
    73
            data = entry->data;
slouken@7391
    74
            break;
slouken@7391
    75
        }
slouken@7391
    76
    }
slouken@7391
    77
    SDL_UnlockMutex(tls_mutex);
slouken@7391
    78
slouken@7391
    79
    return data;
slouken@7391
    80
}
slouken@7391
    81
slouken@7391
    82
static int SetTLSData(SDL_TLSData *data)
slouken@7391
    83
{
slouken@7391
    84
    SDL_threadID thread = SDL_ThreadID();
slouken@7391
    85
    SDL_TLSEntry *entry;
slouken@7391
    86
slouken@7391
    87
    /* GetTLSData() is always called first, so we can assume tls_mutex */
slouken@7391
    88
    SDL_LockMutex(tls_mutex);
slouken@7391
    89
    for (entry = thread_local_storage; entry; entry = entry->next) {
slouken@7391
    90
        if (entry->thread == thread) {
slouken@7391
    91
            entry->data = data;
slouken@7391
    92
            break;
slouken@7391
    93
        }
slouken@7391
    94
    }
slouken@7391
    95
    if (!entry) {
slouken@7391
    96
        entry = (SDL_TLSEntry *)SDL_malloc(sizeof(*entry));
slouken@7391
    97
        if (entry) {
slouken@7391
    98
            entry->thread = thread;
slouken@7391
    99
            entry->data = data;
slouken@7391
   100
            entry->next = thread_local_storage;
slouken@7391
   101
            thread_local_storage = entry;
slouken@7391
   102
        }
slouken@7391
   103
    }
slouken@7391
   104
    SDL_UnlockMutex(tls_mutex);
slouken@7391
   105
slouken@7391
   106
    if (!entry) {
slouken@7391
   107
        return SDL_OutOfMemory();
slouken@7391
   108
    }
slouken@7391
   109
    return 0;
slouken@7391
   110
}
slouken@7391
   111
slouken@7391
   112
slouken@7391
   113
SDL_TLSID
slouken@7391
   114
SDL_TLSCreate()
slouken@7391
   115
{
slouken@7391
   116
    return SDL_AtomicIncRef(&tls_id)+1;
slouken@7391
   117
}
slouken@7391
   118
slouken@7391
   119
void *
slouken@7391
   120
SDL_TLSGet(SDL_TLSID id)
slouken@7391
   121
{
slouken@7391
   122
    SDL_TLSData *data;
slouken@7391
   123
slouken@7391
   124
    data = GetTLSData();
slouken@7391
   125
    if (!data || id <= 0 || id > data->limit) {
slouken@7391
   126
        return NULL;
slouken@7391
   127
    }
slouken@7391
   128
    return data->data[id-1];
slouken@7391
   129
}
slouken@7391
   130
slouken@7391
   131
int
slouken@7391
   132
SDL_TLSSet(SDL_TLSID id, const void *value)
slouken@7391
   133
{
slouken@7391
   134
    SDL_TLSData *data;
slouken@7391
   135
slouken@7391
   136
    if (id <= 0) {
slouken@7391
   137
        return SDL_InvalidParamError(id);
slouken@7391
   138
    }
slouken@7391
   139
slouken@7391
   140
    data = GetTLSData();
slouken@7391
   141
    if (!data || id > data->limit) {
slouken@7391
   142
        int i, oldlimit, newlimit;
slouken@7391
   143
slouken@7391
   144
        oldlimit = data ? data->limit : 0;
slouken@7391
   145
        newlimit = (id + TLS_ALLOC_CHUNKSIZE);
slouken@7391
   146
        data = (SDL_TLSData *)SDL_realloc(data, sizeof(*data)+(newlimit-1)*sizeof(void*));
slouken@7391
   147
        if (!data) {
slouken@7391
   148
            return SDL_OutOfMemory();
slouken@7391
   149
        }
slouken@7391
   150
        data->limit = newlimit;
slouken@7391
   151
        for (i = oldlimit; i < newlimit; ++i) {
slouken@7391
   152
            data->data[i] = NULL;
slouken@7391
   153
        }
slouken@7391
   154
        if (SetTLSData(data) != 0) {
slouken@7391
   155
            return -1;
slouken@7391
   156
        }
slouken@7391
   157
    }
slouken@7391
   158
slouken@7391
   159
    data->data[id-1] = SDL_const_cast(void*, value);
slouken@7391
   160
    return 0;
slouken@7391
   161
}
slouken@7391
   162
slouken@7391
   163
/* vi: set ts=4 sw=4 expandtab: */