src/thread/SDL_thread.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 10 Jul 2013 18:31:17 -0700
changeset 7393 358696c354a8
parent 7391 a29895dc5e9a
child 7482 249d8ecbbb7d
permissions -rw-r--r--
Added release/acquire memory barriers to the atomic API
* Added a destructor to clean up TLS memory at thread shutdown
* Refactored the TLS code to have platform independent code and a small platform dependent core with a fallback to generic code if platform dependent functions fail.
* Fixed recursion issues with SDL_GetErrBuf()
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@1402
    21
#include "SDL_config.h"
slouken@0
    22
slouken@0
    23
/* System independent thread management routines for SDL */
slouken@0
    24
slouken@0
    25
#include "SDL_thread.h"
slouken@0
    26
#include "SDL_thread_c.h"
slouken@0
    27
#include "SDL_systhread.h"
slouken@6044
    28
#include "../SDL_error_c.h"
slouken@0
    29
slouken@0
    30
slouken@7393
    31
SDL_TLSID
slouken@7393
    32
SDL_TLSCreate()
slouken@7393
    33
{
slouken@7393
    34
    static SDL_atomic_t SDL_tls_id;
slouken@7393
    35
    return SDL_AtomicIncRef(&SDL_tls_id)+1;
slouken@7393
    36
}
slouken@7393
    37
slouken@7393
    38
void *
slouken@7393
    39
SDL_TLSGet(SDL_TLSID id)
slouken@7393
    40
{
slouken@7393
    41
    SDL_TLSData *storage;
slouken@7393
    42
slouken@7393
    43
    storage = SDL_SYS_GetTLSData();
slouken@7393
    44
    if (!storage || id == 0 || id > storage->limit) {
slouken@7393
    45
        return NULL;
slouken@7393
    46
    }
slouken@7393
    47
    return storage->array[id-1].data;
slouken@7393
    48
}
slouken@7393
    49
slouken@7393
    50
int
slouken@7393
    51
SDL_TLSSet(SDL_TLSID id, const void *value, void (*destructor)(void *))
slouken@7393
    52
{
slouken@7393
    53
    SDL_TLSData *storage;
slouken@7393
    54
slouken@7393
    55
    if (id == 0) {
slouken@7393
    56
        return SDL_InvalidParamError("id");
slouken@7393
    57
    }
slouken@7393
    58
slouken@7393
    59
    storage = SDL_SYS_GetTLSData();
slouken@7393
    60
    if (!storage || id > storage->limit) {
slouken@7393
    61
        int i, oldlimit, newlimit;
slouken@7393
    62
slouken@7393
    63
        oldlimit = storage ? storage->limit : 0;
slouken@7393
    64
        newlimit = (id + TLS_ALLOC_CHUNKSIZE);
slouken@7393
    65
        storage = (SDL_TLSData *)SDL_realloc(storage, sizeof(*storage)+(newlimit-1)*sizeof(storage->array[0]));
slouken@7393
    66
        if (!storage) {
slouken@7393
    67
            return SDL_OutOfMemory();
slouken@7393
    68
        }
slouken@7393
    69
        storage->limit = newlimit;
slouken@7393
    70
        for (i = oldlimit; i < newlimit; ++i) {
slouken@7393
    71
            storage->array[i].data = NULL;
slouken@7393
    72
            storage->array[i].destructor = NULL;
slouken@7393
    73
        }
slouken@7393
    74
        if (SDL_SYS_SetTLSData(storage) != 0) {
slouken@7393
    75
            return -1;
slouken@7393
    76
        }
slouken@7393
    77
    }
slouken@7393
    78
slouken@7393
    79
    storage->array[id-1].data = SDL_const_cast(void*, value);
slouken@7393
    80
    storage->array[id-1].destructor = destructor;
slouken@7393
    81
    return 0;
slouken@7393
    82
}
slouken@7393
    83
slouken@7393
    84
static void
slouken@7393
    85
SDL_TLSCleanup()
slouken@7393
    86
{
slouken@7393
    87
    SDL_TLSData *storage;
slouken@7393
    88
slouken@7393
    89
    storage = SDL_SYS_GetTLSData();
slouken@7393
    90
    if (storage) {
slouken@7393
    91
        int i;
slouken@7393
    92
        for (i = 0; i < storage->limit; ++i) {
slouken@7393
    93
            if (storage->array[i].destructor) {
slouken@7393
    94
                storage->array[i].destructor(storage->array[i].data);
slouken@7393
    95
            }
slouken@7393
    96
        }
slouken@7393
    97
        SDL_SYS_SetTLSData(NULL);
slouken@7393
    98
        SDL_free(storage);
slouken@7393
    99
    }
slouken@7393
   100
}
slouken@7393
   101
slouken@7393
   102
slouken@7393
   103
/* This is a generic implementation of thread-local storage which doesn't
slouken@7393
   104
   require additional OS support.
slouken@7393
   105
slouken@7393
   106
   It is not especially efficient and doesn't clean up thread-local storage
slouken@7393
   107
   as threads exit.  If there is a real OS that doesn't support thread-local
slouken@7393
   108
   storage this implementation should be improved to be production quality.
slouken@7393
   109
*/
slouken@7393
   110
slouken@7393
   111
typedef struct SDL_TLSEntry {
slouken@7393
   112
    SDL_threadID thread;
slouken@7393
   113
    SDL_TLSData *storage;
slouken@7393
   114
    struct SDL_TLSEntry *next;
slouken@7393
   115
} SDL_TLSEntry;
slouken@7393
   116
slouken@7393
   117
static SDL_mutex *SDL_generic_TLS_mutex;
slouken@7393
   118
static SDL_TLSEntry *SDL_generic_TLS;
slouken@7393
   119
slouken@7393
   120
slouken@7393
   121
SDL_TLSData *
slouken@7393
   122
SDL_Generic_GetTLSData()
slouken@7393
   123
{
slouken@7393
   124
    SDL_threadID thread = SDL_ThreadID();
slouken@7393
   125
    SDL_TLSEntry *entry;
slouken@7393
   126
    SDL_TLSData *storage = NULL;
slouken@7393
   127
slouken@7393
   128
    if (!SDL_generic_TLS_mutex) {
slouken@7393
   129
        static SDL_SpinLock tls_lock;
slouken@7393
   130
        SDL_AtomicLock(&tls_lock);
slouken@7393
   131
        if (!SDL_generic_TLS_mutex) {
slouken@7393
   132
            SDL_mutex *mutex = SDL_CreateMutex();
slouken@7393
   133
            SDL_MemoryBarrierRelease();
slouken@7393
   134
            SDL_generic_TLS_mutex = mutex;
slouken@7393
   135
            if (!SDL_generic_TLS_mutex) {
slouken@7393
   136
                SDL_AtomicUnlock(&tls_lock);
slouken@7393
   137
                return NULL;
slouken@7393
   138
            }
slouken@7393
   139
        }
slouken@7393
   140
        SDL_AtomicUnlock(&tls_lock);
slouken@7393
   141
    }
slouken@7393
   142
slouken@7393
   143
    SDL_MemoryBarrierAcquire();
slouken@7393
   144
    SDL_LockMutex(SDL_generic_TLS_mutex);
slouken@7393
   145
    for (entry = SDL_generic_TLS; entry; entry = entry->next) {
slouken@7393
   146
        if (entry->thread == thread) {
slouken@7393
   147
            storage = entry->storage;
slouken@7393
   148
            break;
slouken@7393
   149
        }
slouken@7393
   150
    }
slouken@7393
   151
    SDL_UnlockMutex(SDL_generic_TLS_mutex);
slouken@7393
   152
slouken@7393
   153
    return storage;
slouken@7393
   154
}
slouken@7393
   155
slouken@7393
   156
int
slouken@7393
   157
SDL_Generic_SetTLSData(SDL_TLSData *storage)
slouken@7393
   158
{
slouken@7393
   159
    SDL_threadID thread = SDL_ThreadID();
slouken@7393
   160
    SDL_TLSEntry *prev, *entry;
slouken@7393
   161
slouken@7393
   162
    /* SDL_Generic_GetTLSData() is always called first, so we can assume SDL_generic_TLS_mutex */
slouken@7393
   163
    SDL_LockMutex(SDL_generic_TLS_mutex);
slouken@7393
   164
    prev = NULL;
slouken@7393
   165
    for (entry = SDL_generic_TLS; entry; entry = entry->next) {
slouken@7393
   166
        if (entry->thread == thread) {
slouken@7393
   167
            if (storage) {
slouken@7393
   168
                entry->storage = storage;
slouken@7393
   169
            } else {
slouken@7393
   170
                if (prev) {
slouken@7393
   171
                    prev->next = entry->next;
slouken@7393
   172
                } else {
slouken@7393
   173
                    SDL_generic_TLS = entry->next;
slouken@7393
   174
                }
slouken@7393
   175
                SDL_free(entry);
slouken@7393
   176
            }
slouken@7393
   177
            break;
slouken@7393
   178
        }
slouken@7393
   179
        prev = entry;
slouken@7393
   180
    }
slouken@7393
   181
    if (!entry) {
slouken@7393
   182
        entry = (SDL_TLSEntry *)SDL_malloc(sizeof(*entry));
slouken@7393
   183
        if (entry) {
slouken@7393
   184
            entry->thread = thread;
slouken@7393
   185
            entry->storage = storage;
slouken@7393
   186
            entry->next = SDL_generic_TLS;
slouken@7393
   187
            SDL_generic_TLS = entry;
slouken@7393
   188
        }
slouken@7393
   189
    }
slouken@7393
   190
    SDL_UnlockMutex(SDL_generic_TLS_mutex);
slouken@7393
   191
slouken@7393
   192
    if (!entry) {
slouken@7393
   193
        return SDL_OutOfMemory();
slouken@7393
   194
    }
slouken@7393
   195
    return 0;
slouken@7393
   196
}
slouken@7393
   197
slouken@0
   198
/* Routine to get the thread-specific error variable */
slouken@1895
   199
SDL_error *
slouken@1895
   200
SDL_GetErrBuf(void)
slouken@0
   201
{
slouken@7393
   202
    static SDL_SpinLock tls_lock;
slouken@7391
   203
    static SDL_bool tls_being_created;
slouken@7391
   204
    static SDL_TLSID tls_errbuf;
slouken@7391
   205
    static SDL_error SDL_global_errbuf;
slouken@7393
   206
    const SDL_error *ALLOCATION_IN_PROGRESS = (SDL_error *)-1;
slouken@1895
   207
    SDL_error *errbuf;
slouken@0
   208
slouken@7393
   209
    /* tls_being_created is there simply to prevent recursion if SDL_TLSCreate() fails.
slouken@7393
   210
       It also means it's possible for another thread to also use SDL_global_errbuf,
slouken@7393
   211
       but that's very unlikely and hopefully won't cause issues.
slouken@7393
   212
     */
slouken@7391
   213
    if (!tls_errbuf && !tls_being_created) {
slouken@7393
   214
        SDL_AtomicLock(&tls_lock);
slouken@7391
   215
        if (!tls_errbuf) {
slouken@7393
   216
            SDL_TLSID slot;
slouken@7391
   217
            tls_being_created = SDL_TRUE;
slouken@7393
   218
            slot = SDL_TLSCreate();
slouken@7391
   219
            tls_being_created = SDL_FALSE;
slouken@7393
   220
            SDL_MemoryBarrierRelease();
slouken@7393
   221
            tls_errbuf = slot;
slouken@7391
   222
        }
slouken@7393
   223
        SDL_AtomicUnlock(&tls_lock);
slouken@7391
   224
    }
slouken@7391
   225
    if (!tls_errbuf) {
slouken@7391
   226
        return &SDL_global_errbuf;
slouken@7391
   227
    }
slouken@0
   228
slouken@7393
   229
    SDL_MemoryBarrierAcquire();
slouken@7393
   230
    errbuf = (SDL_error *)SDL_TLSGet(tls_errbuf);
slouken@7393
   231
    if (errbuf == ALLOCATION_IN_PROGRESS) {
slouken@7393
   232
        return &SDL_global_errbuf;
slouken@7393
   233
    }
slouken@7391
   234
    if (!errbuf) {
slouken@7393
   235
        /* Mark that we're in the middle of allocating our buffer */
slouken@7393
   236
        SDL_TLSSet(tls_errbuf, ALLOCATION_IN_PROGRESS, NULL);
slouken@7391
   237
        errbuf = (SDL_error *)SDL_malloc(sizeof(*errbuf));
slouken@7391
   238
        if (!errbuf) {
slouken@7393
   239
            SDL_TLSSet(tls_errbuf, NULL, NULL);
slouken@7391
   240
            return &SDL_global_errbuf;
slouken@1895
   241
        }
slouken@7391
   242
        SDL_zerop(errbuf);
slouken@7393
   243
        SDL_TLSSet(tls_errbuf, errbuf, SDL_free);
slouken@1895
   244
    }
slouken@7391
   245
    return errbuf;
slouken@0
   246
}
slouken@0
   247
slouken@0
   248
slouken@0
   249
/* Arguments and callback to setup and run the user thread function */
slouken@1895
   250
typedef struct
slouken@1895
   251
{
slouken@1895
   252
    int (SDLCALL * func) (void *);
slouken@1895
   253
    void *data;
slouken@1895
   254
    SDL_Thread *info;
slouken@1895
   255
    SDL_sem *wait;
slouken@0
   256
} thread_args;
slouken@0
   257
slouken@1895
   258
void
slouken@1895
   259
SDL_RunThread(void *data)
slouken@0
   260
{
icculus@5969
   261
    thread_args *args = (thread_args *) data;
icculus@5969
   262
    int (SDLCALL * userfunc) (void *) = args->func;
icculus@5969
   263
    void *userdata = args->data;
icculus@5969
   264
    int *statusloc = &args->info->status;
slouken@0
   265
slouken@7393
   266
    /* Perform any system-dependent setup - this function may not fail */
icculus@5969
   267
    SDL_SYS_SetupThread(args->info->name);
slouken@0
   268
slouken@1895
   269
    /* Get the thread id */
slouken@1895
   270
    args->info->threadid = SDL_ThreadID();
slouken@0
   271
slouken@1895
   272
    /* Wake up the parent thread */
slouken@1895
   273
    SDL_SemPost(args->wait);
slouken@0
   274
slouken@1895
   275
    /* Run the function */
slouken@1895
   276
    *statusloc = userfunc(userdata);
slouken@7393
   277
slouken@7393
   278
    /* Clean up thread-local storage */
slouken@7393
   279
    SDL_TLSCleanup();
slouken@0
   280
}
slouken@0
   281
slouken@1471
   282
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
slouken@1330
   283
#undef SDL_CreateThread
slouken@1895
   284
DECLSPEC SDL_Thread *SDLCALL
icculus@5969
   285
SDL_CreateThread(int (SDLCALL * fn) (void *),
icculus@5969
   286
                 const char *name, void *data,
slouken@1895
   287
                 pfnSDL_CurrentBeginThread pfnBeginThread,
slouken@1895
   288
                 pfnSDL_CurrentEndThread pfnEndThread)
icculus@1190
   289
#else
slouken@1895
   290
DECLSPEC SDL_Thread *SDLCALL
icculus@5969
   291
SDL_CreateThread(int (SDLCALL * fn) (void *),
icculus@5969
   292
                 const char *name, void *data)
icculus@1190
   293
#endif
slouken@0
   294
{
slouken@1895
   295
    SDL_Thread *thread;
slouken@1895
   296
    thread_args *args;
slouken@1895
   297
    int ret;
slouken@0
   298
slouken@1895
   299
    /* Allocate memory for the thread info structure */
slouken@1895
   300
    thread = (SDL_Thread *) SDL_malloc(sizeof(*thread));
slouken@1895
   301
    if (thread == NULL) {
slouken@1895
   302
        SDL_OutOfMemory();
slouken@1895
   303
        return (NULL);
slouken@1895
   304
    }
slouken@1895
   305
    SDL_memset(thread, 0, (sizeof *thread));
slouken@1895
   306
    thread->status = -1;
slouken@0
   307
slouken@1895
   308
    /* Set up the arguments for the thread */
icculus@5969
   309
    if (name != NULL) {
icculus@5969
   310
        thread->name = SDL_strdup(name);
icculus@5969
   311
        if (thread->name == NULL) {
icculus@5969
   312
            SDL_OutOfMemory();
icculus@5969
   313
            SDL_free(thread);
icculus@5969
   314
            return (NULL);
icculus@5969
   315
        }
icculus@5969
   316
    }
icculus@5969
   317
icculus@5969
   318
    /* Set up the arguments for the thread */
slouken@1895
   319
    args = (thread_args *) SDL_malloc(sizeof(*args));
slouken@1895
   320
    if (args == NULL) {
slouken@1895
   321
        SDL_OutOfMemory();
icculus@5969
   322
        SDL_free(thread->name);
slouken@1895
   323
        SDL_free(thread);
slouken@1895
   324
        return (NULL);
slouken@1895
   325
    }
slouken@1895
   326
    args->func = fn;
slouken@1895
   327
    args->data = data;
slouken@1895
   328
    args->info = thread;
slouken@1895
   329
    args->wait = SDL_CreateSemaphore(0);
slouken@1895
   330
    if (args->wait == NULL) {
icculus@5969
   331
        SDL_free(thread->name);
slouken@1895
   332
        SDL_free(thread);
slouken@1895
   333
        SDL_free(args);
slouken@1895
   334
        return (NULL);
slouken@1895
   335
    }
slouken@0
   336
slouken@1895
   337
    /* Create the thread and go! */
slouken@1471
   338
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
slouken@1895
   339
    ret = SDL_SYS_CreateThread(thread, args, pfnBeginThread, pfnEndThread);
icculus@1190
   340
#else
slouken@1895
   341
    ret = SDL_SYS_CreateThread(thread, args);
icculus@1190
   342
#endif
slouken@1895
   343
    if (ret >= 0) {
slouken@1895
   344
        /* Wait for the thread function to use arguments */
slouken@1895
   345
        SDL_SemWait(args->wait);
slouken@1895
   346
    } else {
slouken@1895
   347
        /* Oops, failed.  Gotta free everything */
icculus@5969
   348
        SDL_free(thread->name);
slouken@1895
   349
        SDL_free(thread);
slouken@1895
   350
        thread = NULL;
slouken@1895
   351
    }
slouken@1895
   352
    SDL_DestroySemaphore(args->wait);
slouken@1895
   353
    SDL_free(args);
slouken@0
   354
slouken@1895
   355
    /* Everything is running now */
slouken@1895
   356
    return (thread);
slouken@0
   357
}
slouken@0
   358
slouken@5506
   359
SDL_threadID
slouken@5506
   360
SDL_GetThreadID(SDL_Thread * thread)
slouken@5506
   361
{
slouken@5506
   362
    SDL_threadID id;
slouken@5506
   363
slouken@5506
   364
    if (thread) {
slouken@5506
   365
        id = thread->threadid;
slouken@5506
   366
    } else {
slouken@5506
   367
        id = SDL_ThreadID();
slouken@5506
   368
    }
slouken@5506
   369
    return id;
slouken@5506
   370
}
slouken@5506
   371
icculus@5969
   372
const char *
icculus@5969
   373
SDL_GetThreadName(SDL_Thread * thread)
icculus@5969
   374
{
icculus@5969
   375
    return thread->name;
icculus@5969
   376
}
icculus@5969
   377
slouken@5506
   378
int
slouken@5509
   379
SDL_SetThreadPriority(SDL_ThreadPriority priority)
slouken@5506
   380
{
slouken@5509
   381
    return SDL_SYS_SetThreadPriority(priority);
slouken@5506
   382
}
slouken@5506
   383
slouken@1895
   384
void
slouken@1895
   385
SDL_WaitThread(SDL_Thread * thread, int *status)
slouken@0
   386
{
slouken@1895
   387
    if (thread) {
slouken@1895
   388
        SDL_SYS_WaitThread(thread);
slouken@1895
   389
        if (status) {
slouken@1895
   390
            *status = thread->status;
slouken@1895
   391
        }
icculus@5969
   392
        SDL_free(thread->name);
slouken@1895
   393
        SDL_free(thread);
slouken@1895
   394
    }
slouken@0
   395
}
slouken@0
   396
slouken@1895
   397
/* vi: set ts=4 sw=4 expandtab: */