src/thread/SDL_thread.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Fri, 24 Jun 2016 19:06:06 +0200
changeset 10182 4980c48b8dbe
parent 10146 471eb08040ce
permissions -rw-r--r--
Mac: Updated availability macro in build script.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5509
diff changeset
     2
  Simple DirectMedia Layer
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5509
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5509
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5509
diff changeset
     7
  arising from the use of this software.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5509
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5509
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5509
diff changeset
    11
  freely, subject to the following restrictions:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5509
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5509
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5509
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5509
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5509
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5509
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5509
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    20
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 7978
diff changeset
    21
#include "../SDL_internal.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    22
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    23
/* System independent thread management routines for SDL */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    24
7978
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
    25
#include "SDL_assert.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    26
#include "SDL_thread.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    27
#include "SDL_thread_c.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    28
#include "SDL_systhread.h"
10145
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    29
#include "SDL_hints.h"
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5969
diff changeset
    30
#include "../SDL_error_c.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    31
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    32
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    33
SDL_TLSID
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    34
SDL_TLSCreate()
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    35
{
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    36
    static SDL_atomic_t SDL_tls_id;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    37
    return SDL_AtomicIncRef(&SDL_tls_id)+1;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    38
}
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    39
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    40
void *
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    41
SDL_TLSGet(SDL_TLSID id)
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    42
{
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    43
    SDL_TLSData *storage;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    44
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    45
    storage = SDL_SYS_GetTLSData();
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    46
    if (!storage || id == 0 || id > storage->limit) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    47
        return NULL;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    48
    }
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    49
    return storage->array[id-1].data;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    50
}
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    51
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    52
int
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    53
SDL_TLSSet(SDL_TLSID id, const void *value, void (*destructor)(void *))
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    54
{
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    55
    SDL_TLSData *storage;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    56
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    57
    if (id == 0) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    58
        return SDL_InvalidParamError("id");
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    59
    }
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    60
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    61
    storage = SDL_SYS_GetTLSData();
7482
249d8ecbbb7d Fixed some compiler warnings that Visual Studio reported.
Ryan C. Gordon <icculus@icculus.org>
parents: 7393
diff changeset
    62
    if (!storage || (id > storage->limit)) {
249d8ecbbb7d Fixed some compiler warnings that Visual Studio reported.
Ryan C. Gordon <icculus@icculus.org>
parents: 7393
diff changeset
    63
        unsigned int i, oldlimit, newlimit;
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    64
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    65
        oldlimit = storage ? storage->limit : 0;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    66
        newlimit = (id + TLS_ALLOC_CHUNKSIZE);
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    67
        storage = (SDL_TLSData *)SDL_realloc(storage, sizeof(*storage)+(newlimit-1)*sizeof(storage->array[0]));
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    68
        if (!storage) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    69
            return SDL_OutOfMemory();
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    70
        }
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    71
        storage->limit = newlimit;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    72
        for (i = oldlimit; i < newlimit; ++i) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    73
            storage->array[i].data = NULL;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    74
            storage->array[i].destructor = NULL;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    75
        }
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    76
        if (SDL_SYS_SetTLSData(storage) != 0) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    77
            return -1;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    78
        }
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    79
    }
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    80
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    81
    storage->array[id-1].data = SDL_const_cast(void*, value);
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    82
    storage->array[id-1].destructor = destructor;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    83
    return 0;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    84
}
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    85
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    86
static void
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    87
SDL_TLSCleanup()
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    88
{
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    89
    SDL_TLSData *storage;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    90
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    91
    storage = SDL_SYS_GetTLSData();
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    92
    if (storage) {
7484
c6c4f8719af6 One more compiler warning.
Ryan C. Gordon <icculus@icculus.org>
parents: 7482
diff changeset
    93
        unsigned int i;
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    94
        for (i = 0; i < storage->limit; ++i) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    95
            if (storage->array[i].destructor) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    96
                storage->array[i].destructor(storage->array[i].data);
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    97
            }
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    98
        }
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    99
        SDL_SYS_SetTLSData(NULL);
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   100
        SDL_free(storage);
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   101
    }
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   102
}
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   103
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   104
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   105
/* This is a generic implementation of thread-local storage which doesn't
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   106
   require additional OS support.
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   107
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   108
   It is not especially efficient and doesn't clean up thread-local storage
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   109
   as threads exit.  If there is a real OS that doesn't support thread-local
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   110
   storage this implementation should be improved to be production quality.
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   111
*/
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   112
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   113
typedef struct SDL_TLSEntry {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   114
    SDL_threadID thread;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   115
    SDL_TLSData *storage;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   116
    struct SDL_TLSEntry *next;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   117
} SDL_TLSEntry;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   118
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   119
static SDL_mutex *SDL_generic_TLS_mutex;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   120
static SDL_TLSEntry *SDL_generic_TLS;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   121
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   122
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   123
SDL_TLSData *
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   124
SDL_Generic_GetTLSData()
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   125
{
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   126
    SDL_threadID thread = SDL_ThreadID();
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   127
    SDL_TLSEntry *entry;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   128
    SDL_TLSData *storage = NULL;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   129
7730
e928464b98ec Fixed bug 2076 - OpenGL doesn't work with --disable-threads
Sam Lantinga <slouken@libsdl.org>
parents: 7606
diff changeset
   130
#if !SDL_THREADS_DISABLED
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   131
    if (!SDL_generic_TLS_mutex) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   132
        static SDL_SpinLock tls_lock;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   133
        SDL_AtomicLock(&tls_lock);
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   134
        if (!SDL_generic_TLS_mutex) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   135
            SDL_mutex *mutex = SDL_CreateMutex();
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   136
            SDL_MemoryBarrierRelease();
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   137
            SDL_generic_TLS_mutex = mutex;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   138
            if (!SDL_generic_TLS_mutex) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   139
                SDL_AtomicUnlock(&tls_lock);
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   140
                return NULL;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   141
            }
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   142
        }
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   143
        SDL_AtomicUnlock(&tls_lock);
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   144
    }
7730
e928464b98ec Fixed bug 2076 - OpenGL doesn't work with --disable-threads
Sam Lantinga <slouken@libsdl.org>
parents: 7606
diff changeset
   145
#endif /* SDL_THREADS_DISABLED */
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   146
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   147
    SDL_MemoryBarrierAcquire();
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   148
    SDL_LockMutex(SDL_generic_TLS_mutex);
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   149
    for (entry = SDL_generic_TLS; entry; entry = entry->next) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   150
        if (entry->thread == thread) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   151
            storage = entry->storage;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   152
            break;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   153
        }
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   154
    }
7730
e928464b98ec Fixed bug 2076 - OpenGL doesn't work with --disable-threads
Sam Lantinga <slouken@libsdl.org>
parents: 7606
diff changeset
   155
#if !SDL_THREADS_DISABLED
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   156
    SDL_UnlockMutex(SDL_generic_TLS_mutex);
7730
e928464b98ec Fixed bug 2076 - OpenGL doesn't work with --disable-threads
Sam Lantinga <slouken@libsdl.org>
parents: 7606
diff changeset
   157
#endif
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   158
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   159
    return storage;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   160
}
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   161
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   162
int
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   163
SDL_Generic_SetTLSData(SDL_TLSData *storage)
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   164
{
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   165
    SDL_threadID thread = SDL_ThreadID();
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   166
    SDL_TLSEntry *prev, *entry;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   167
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   168
    /* SDL_Generic_GetTLSData() is always called first, so we can assume SDL_generic_TLS_mutex */
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   169
    SDL_LockMutex(SDL_generic_TLS_mutex);
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   170
    prev = NULL;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   171
    for (entry = SDL_generic_TLS; entry; entry = entry->next) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   172
        if (entry->thread == thread) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   173
            if (storage) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   174
                entry->storage = storage;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   175
            } else {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   176
                if (prev) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   177
                    prev->next = entry->next;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   178
                } else {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   179
                    SDL_generic_TLS = entry->next;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   180
                }
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   181
                SDL_free(entry);
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   182
            }
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   183
            break;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   184
        }
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   185
        prev = entry;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   186
    }
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   187
    if (!entry) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   188
        entry = (SDL_TLSEntry *)SDL_malloc(sizeof(*entry));
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   189
        if (entry) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   190
            entry->thread = thread;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   191
            entry->storage = storage;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   192
            entry->next = SDL_generic_TLS;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   193
            SDL_generic_TLS = entry;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   194
        }
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   195
    }
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   196
    SDL_UnlockMutex(SDL_generic_TLS_mutex);
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   197
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   198
    if (!entry) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   199
        return SDL_OutOfMemory();
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   200
    }
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   201
    return 0;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   202
}
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   203
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   204
/* Routine to get the thread-specific error variable */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   205
SDL_error *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   206
SDL_GetErrBuf(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   207
{
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   208
    static SDL_SpinLock tls_lock;
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   209
    static SDL_bool tls_being_created;
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   210
    static SDL_TLSID tls_errbuf;
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   211
    static SDL_error SDL_global_errbuf;
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   212
    const SDL_error *ALLOCATION_IN_PROGRESS = (SDL_error *)-1;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   213
    SDL_error *errbuf;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   214
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   215
    /* tls_being_created is there simply to prevent recursion if SDL_TLSCreate() fails.
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   216
       It also means it's possible for another thread to also use SDL_global_errbuf,
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   217
       but that's very unlikely and hopefully won't cause issues.
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   218
     */
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   219
    if (!tls_errbuf && !tls_being_created) {
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   220
        SDL_AtomicLock(&tls_lock);
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   221
        if (!tls_errbuf) {
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   222
            SDL_TLSID slot;
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   223
            tls_being_created = SDL_TRUE;
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   224
            slot = SDL_TLSCreate();
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   225
            tls_being_created = SDL_FALSE;
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   226
            SDL_MemoryBarrierRelease();
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   227
            tls_errbuf = slot;
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   228
        }
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   229
        SDL_AtomicUnlock(&tls_lock);
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   230
    }
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   231
    if (!tls_errbuf) {
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   232
        return &SDL_global_errbuf;
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   233
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   234
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   235
    SDL_MemoryBarrierAcquire();
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   236
    errbuf = (SDL_error *)SDL_TLSGet(tls_errbuf);
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   237
    if (errbuf == ALLOCATION_IN_PROGRESS) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   238
        return &SDL_global_errbuf;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   239
    }
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   240
    if (!errbuf) {
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   241
        /* Mark that we're in the middle of allocating our buffer */
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   242
        SDL_TLSSet(tls_errbuf, ALLOCATION_IN_PROGRESS, NULL);
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   243
        errbuf = (SDL_error *)SDL_malloc(sizeof(*errbuf));
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   244
        if (!errbuf) {
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   245
            SDL_TLSSet(tls_errbuf, NULL, NULL);
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   246
            return &SDL_global_errbuf;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   247
        }
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   248
        SDL_zerop(errbuf);
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   249
        SDL_TLSSet(tls_errbuf, errbuf, SDL_free);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   250
    }
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   251
    return errbuf;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   252
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   253
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   254
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   255
/* Arguments and callback to setup and run the user thread function */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   256
typedef struct
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   257
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   258
    int (SDLCALL * func) (void *);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   259
    void *data;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   260
    SDL_Thread *info;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   261
    SDL_sem *wait;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   262
} thread_args;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   263
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   264
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   265
SDL_RunThread(void *data)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   266
{
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   267
    thread_args *args = (thread_args *) data;
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   268
    int (SDLCALL * userfunc) (void *) = args->func;
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   269
    void *userdata = args->data;
7978
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   270
    SDL_Thread *thread = args->info;
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   271
    int *statusloc = &thread->status;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   272
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   273
    /* Perform any system-dependent setup - this function may not fail */
7978
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   274
    SDL_SYS_SetupThread(thread->name);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   275
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   276
    /* Get the thread id */
7978
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   277
    thread->threadid = SDL_ThreadID();
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   278
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   279
    /* Wake up the parent thread */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   280
    SDL_SemPost(args->wait);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   281
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   282
    /* Run the function */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   283
    *statusloc = userfunc(userdata);
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   284
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   285
    /* Clean up thread-local storage */
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
   286
    SDL_TLSCleanup();
7978
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   287
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   288
    /* Mark us as ready to be joined (or detached) */
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   289
    if (!SDL_AtomicCAS(&thread->state, SDL_THREAD_STATE_ALIVE, SDL_THREAD_STATE_ZOMBIE)) {
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   290
        /* Clean up if something already detached us. */
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   291
        if (SDL_AtomicCAS(&thread->state, SDL_THREAD_STATE_DETACHED, SDL_THREAD_STATE_CLEANED)) {
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   292
            if (thread->name) {
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   293
                SDL_free(thread->name);
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   294
            }
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   295
            SDL_free(thread);
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   296
        }
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   297
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   298
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   299
8094
9efaae827924 Implemented the Dynamic API magic.
Ryan C. Gordon <icculus@icculus.org>
parents: 8093
diff changeset
   300
#ifdef SDL_CreateThread
9efaae827924 Implemented the Dynamic API magic.
Ryan C. Gordon <icculus@icculus.org>
parents: 8093
diff changeset
   301
#undef SDL_CreateThread
9efaae827924 Implemented the Dynamic API magic.
Ryan C. Gordon <icculus@icculus.org>
parents: 8093
diff changeset
   302
#endif
9efaae827924 Implemented the Dynamic API magic.
Ryan C. Gordon <icculus@icculus.org>
parents: 8093
diff changeset
   303
#if SDL_DYNAMIC_API
9efaae827924 Implemented the Dynamic API magic.
Ryan C. Gordon <icculus@icculus.org>
parents: 8093
diff changeset
   304
#define SDL_CreateThread SDL_CreateThread_REAL
9efaae827924 Implemented the Dynamic API magic.
Ryan C. Gordon <icculus@icculus.org>
parents: 8093
diff changeset
   305
#endif
9efaae827924 Implemented the Dynamic API magic.
Ryan C. Gordon <icculus@icculus.org>
parents: 8093
diff changeset
   306
1471
9fb0eee04dd9 Enabled libc support on Win32, so we don't break binary compatibility in 1.2
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   307
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
10145
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   308
static SDL_Thread *
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   309
SDL_CreateThreadWithStackSize(int (SDLCALL * fn) (void *),
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   310
                 const char *name, const size_t stacksize, void *data,
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   311
                 pfnSDL_CurrentBeginThread pfnBeginThread,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   312
                 pfnSDL_CurrentEndThread pfnEndThread)
1190
173c063d4f55 OS/2 port!
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   313
#else
10145
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   314
static SDL_Thread *
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   315
SDL_CreateThreadWithStackSize(int (SDLCALL * fn) (void *),
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   316
                const char *name, const size_t stacksize, void *data)
1190
173c063d4f55 OS/2 port!
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   317
#endif
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   318
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   319
    SDL_Thread *thread;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   320
    thread_args *args;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   321
    int ret;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   322
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   323
    /* Allocate memory for the thread info structure */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   324
    thread = (SDL_Thread *) SDL_malloc(sizeof(*thread));
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   325
    if (thread == NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   326
        SDL_OutOfMemory();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   327
        return (NULL);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   328
    }
7978
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   329
    SDL_zerop(thread);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   330
    thread->status = -1;
7978
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   331
    SDL_AtomicSet(&thread->state, SDL_THREAD_STATE_ALIVE);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   332
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   333
    /* Set up the arguments for the thread */
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   334
    if (name != NULL) {
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   335
        thread->name = SDL_strdup(name);
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   336
        if (thread->name == NULL) {
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   337
            SDL_OutOfMemory();
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   338
            SDL_free(thread);
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   339
            return (NULL);
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   340
        }
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   341
    }
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   342
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   343
    /* Set up the arguments for the thread */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   344
    args = (thread_args *) SDL_malloc(sizeof(*args));
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   345
    if (args == NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   346
        SDL_OutOfMemory();
7606
bb2aa25cca1c Fixed crash if SDL_GetThreadName() is passed a NULL thread.
Sam Lantinga <slouken@libsdl.org>
parents: 7484
diff changeset
   347
        if (thread->name) {
bb2aa25cca1c Fixed crash if SDL_GetThreadName() is passed a NULL thread.
Sam Lantinga <slouken@libsdl.org>
parents: 7484
diff changeset
   348
            SDL_free(thread->name);
bb2aa25cca1c Fixed crash if SDL_GetThreadName() is passed a NULL thread.
Sam Lantinga <slouken@libsdl.org>
parents: 7484
diff changeset
   349
        }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   350
        SDL_free(thread);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   351
        return (NULL);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   352
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   353
    args->func = fn;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   354
    args->data = data;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   355
    args->info = thread;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   356
    args->wait = SDL_CreateSemaphore(0);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   357
    if (args->wait == NULL) {
7606
bb2aa25cca1c Fixed crash if SDL_GetThreadName() is passed a NULL thread.
Sam Lantinga <slouken@libsdl.org>
parents: 7484
diff changeset
   358
        if (thread->name) {
bb2aa25cca1c Fixed crash if SDL_GetThreadName() is passed a NULL thread.
Sam Lantinga <slouken@libsdl.org>
parents: 7484
diff changeset
   359
            SDL_free(thread->name);
bb2aa25cca1c Fixed crash if SDL_GetThreadName() is passed a NULL thread.
Sam Lantinga <slouken@libsdl.org>
parents: 7484
diff changeset
   360
        }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   361
        SDL_free(thread);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   362
        SDL_free(args);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   363
        return (NULL);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   364
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   365
10145
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   366
    thread->stacksize = stacksize;
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   367
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   368
    /* Create the thread and go! */
1471
9fb0eee04dd9 Enabled libc support on Win32, so we don't break binary compatibility in 1.2
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   369
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   370
    ret = SDL_SYS_CreateThread(thread, args, pfnBeginThread, pfnEndThread);
1190
173c063d4f55 OS/2 port!
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   371
#else
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   372
    ret = SDL_SYS_CreateThread(thread, args);
1190
173c063d4f55 OS/2 port!
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   373
#endif
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   374
    if (ret >= 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   375
        /* Wait for the thread function to use arguments */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   376
        SDL_SemWait(args->wait);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   377
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   378
        /* Oops, failed.  Gotta free everything */
7606
bb2aa25cca1c Fixed crash if SDL_GetThreadName() is passed a NULL thread.
Sam Lantinga <slouken@libsdl.org>
parents: 7484
diff changeset
   379
        if (thread->name) {
bb2aa25cca1c Fixed crash if SDL_GetThreadName() is passed a NULL thread.
Sam Lantinga <slouken@libsdl.org>
parents: 7484
diff changeset
   380
            SDL_free(thread->name);
bb2aa25cca1c Fixed crash if SDL_GetThreadName() is passed a NULL thread.
Sam Lantinga <slouken@libsdl.org>
parents: 7484
diff changeset
   381
        }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   382
        SDL_free(thread);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   383
        thread = NULL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   384
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   385
    SDL_DestroySemaphore(args->wait);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   386
    SDL_free(args);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   387
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   388
    /* Everything is running now */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   389
    return (thread);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   390
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   391
10145
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   392
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   393
DECLSPEC SDL_Thread *SDLCALL
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   394
SDL_CreateThread(int (SDLCALL * fn) (void *),
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   395
                 const char *name, void *data,
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   396
                 pfnSDL_CurrentBeginThread pfnBeginThread,
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   397
                 pfnSDL_CurrentEndThread pfnEndThread)
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   398
#else
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   399
DECLSPEC SDL_Thread *SDLCALL
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   400
SDL_CreateThread(int (SDLCALL * fn) (void *),
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   401
                 const char *name, void *data)
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   402
#endif
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   403
{
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   404
    /* !!! FIXME: in 2.1, just make stackhint part of the usual API. */
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   405
    const char *stackhint = SDL_GetHint(SDL_HINT_THREAD_STACK_SIZE);
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   406
    size_t stacksize = 0;
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   407
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   408
    /* If the SDL_HINT_THREAD_STACK_SIZE exists, use it */
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   409
    if (stackhint != NULL) {
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   410
        char *endp = NULL;
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   411
        const Sint64 hintval = SDL_strtoll(stackhint, &endp, 10);
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   412
        if ((*stackhint != '\0') && (*endp == '\0')) {  /* a valid number? */
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   413
            if (hintval > 0) {  /* reject bogus values. */
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   414
                stacksize = (size_t) hintval;
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   415
            }
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   416
        }
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   417
    }
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   418
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   419
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   420
    return SDL_CreateThreadWithStackSize(fn, name, stacksize, data, pfnBeginThread, pfnEndThread);
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   421
#else
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   422
    return SDL_CreateThreadWithStackSize(fn, name, stacksize, data);
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   423
#endif
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   424
}
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   425
10146
471eb08040ce threads: Move SDL's own thread creation to a new internal API.
Ryan C. Gordon <icculus@icculus.org>
parents: 10145
diff changeset
   426
SDL_Thread *
471eb08040ce threads: Move SDL's own thread creation to a new internal API.
Ryan C. Gordon <icculus@icculus.org>
parents: 10145
diff changeset
   427
SDL_CreateThreadInternal(int (SDLCALL * fn) (void *), const char *name,
471eb08040ce threads: Move SDL's own thread creation to a new internal API.
Ryan C. Gordon <icculus@icculus.org>
parents: 10145
diff changeset
   428
                         const size_t stacksize, void *data) {
471eb08040ce threads: Move SDL's own thread creation to a new internal API.
Ryan C. Gordon <icculus@icculus.org>
parents: 10145
diff changeset
   429
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
471eb08040ce threads: Move SDL's own thread creation to a new internal API.
Ryan C. Gordon <icculus@icculus.org>
parents: 10145
diff changeset
   430
    return SDL_CreateThreadWithStackSize(fn, name, stacksize, data, NULL, NULL);
471eb08040ce threads: Move SDL's own thread creation to a new internal API.
Ryan C. Gordon <icculus@icculus.org>
parents: 10145
diff changeset
   431
#else
471eb08040ce threads: Move SDL's own thread creation to a new internal API.
Ryan C. Gordon <icculus@icculus.org>
parents: 10145
diff changeset
   432
    return SDL_CreateThreadWithStackSize(fn, name, stacksize, data);
471eb08040ce threads: Move SDL's own thread creation to a new internal API.
Ryan C. Gordon <icculus@icculus.org>
parents: 10145
diff changeset
   433
#endif
471eb08040ce threads: Move SDL's own thread creation to a new internal API.
Ryan C. Gordon <icculus@icculus.org>
parents: 10145
diff changeset
   434
}
471eb08040ce threads: Move SDL's own thread creation to a new internal API.
Ryan C. Gordon <icculus@icculus.org>
parents: 10145
diff changeset
   435
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5425
diff changeset
   436
SDL_threadID
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5425
diff changeset
   437
SDL_GetThreadID(SDL_Thread * thread)
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5425
diff changeset
   438
{
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5425
diff changeset
   439
    SDL_threadID id;
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5425
diff changeset
   440
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5425
diff changeset
   441
    if (thread) {
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5425
diff changeset
   442
        id = thread->threadid;
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5425
diff changeset
   443
    } else {
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5425
diff changeset
   444
        id = SDL_ThreadID();
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5425
diff changeset
   445
    }
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5425
diff changeset
   446
    return id;
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5425
diff changeset
   447
}
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5425
diff changeset
   448
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   449
const char *
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   450
SDL_GetThreadName(SDL_Thread * thread)
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   451
{
7606
bb2aa25cca1c Fixed crash if SDL_GetThreadName() is passed a NULL thread.
Sam Lantinga <slouken@libsdl.org>
parents: 7484
diff changeset
   452
    if (thread) {
bb2aa25cca1c Fixed crash if SDL_GetThreadName() is passed a NULL thread.
Sam Lantinga <slouken@libsdl.org>
parents: 7484
diff changeset
   453
        return thread->name;
bb2aa25cca1c Fixed crash if SDL_GetThreadName() is passed a NULL thread.
Sam Lantinga <slouken@libsdl.org>
parents: 7484
diff changeset
   454
    } else {
bb2aa25cca1c Fixed crash if SDL_GetThreadName() is passed a NULL thread.
Sam Lantinga <slouken@libsdl.org>
parents: 7484
diff changeset
   455
        return NULL;
bb2aa25cca1c Fixed crash if SDL_GetThreadName() is passed a NULL thread.
Sam Lantinga <slouken@libsdl.org>
parents: 7484
diff changeset
   456
    }
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   457
}
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   458
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5425
diff changeset
   459
int
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   460
SDL_SetThreadPriority(SDL_ThreadPriority priority)
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5425
diff changeset
   461
{
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   462
    return SDL_SYS_SetThreadPriority(priority);
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5425
diff changeset
   463
}
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5425
diff changeset
   464
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   465
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   466
SDL_WaitThread(SDL_Thread * thread, int *status)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   467
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   468
    if (thread) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   469
        SDL_SYS_WaitThread(thread);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   470
        if (status) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   471
            *status = thread->status;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   472
        }
7606
bb2aa25cca1c Fixed crash if SDL_GetThreadName() is passed a NULL thread.
Sam Lantinga <slouken@libsdl.org>
parents: 7484
diff changeset
   473
        if (thread->name) {
bb2aa25cca1c Fixed crash if SDL_GetThreadName() is passed a NULL thread.
Sam Lantinga <slouken@libsdl.org>
parents: 7484
diff changeset
   474
            SDL_free(thread->name);
bb2aa25cca1c Fixed crash if SDL_GetThreadName() is passed a NULL thread.
Sam Lantinga <slouken@libsdl.org>
parents: 7484
diff changeset
   475
        }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   476
        SDL_free(thread);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   477
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   478
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   479
7978
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   480
void
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   481
SDL_DetachThread(SDL_Thread * thread)
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   482
{
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   483
    if (!thread) {
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   484
        return;
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   485
    }
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   486
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   487
    /* Grab dibs if the state is alive+joinable. */
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   488
    if (SDL_AtomicCAS(&thread->state, SDL_THREAD_STATE_ALIVE, SDL_THREAD_STATE_DETACHED)) {
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   489
        SDL_SYS_DetachThread(thread);
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   490
    } else {
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   491
        /* all other states are pretty final, see where we landed. */
8922
dfb6f8611ebe Fixed bug 2556 - add compilation flag -Wshadow
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   492
        const int thread_state = SDL_AtomicGet(&thread->state);
dfb6f8611ebe Fixed bug 2556 - add compilation flag -Wshadow
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   493
        if ((thread_state == SDL_THREAD_STATE_DETACHED) || (thread_state == SDL_THREAD_STATE_CLEANED)) {
7978
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   494
            return;  /* already detached (you shouldn't call this twice!) */
8922
dfb6f8611ebe Fixed bug 2556 - add compilation flag -Wshadow
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   495
        } else if (thread_state == SDL_THREAD_STATE_ZOMBIE) {
7978
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   496
            SDL_WaitThread(thread, NULL);  /* already done, clean it up. */
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   497
        } else {
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   498
            SDL_assert(0 && "Unexpected thread state");
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   499
        }
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   500
    }
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   501
}
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7730
diff changeset
   502
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   503
/* vi: set ts=4 sw=4 expandtab: */