src/thread/SDL_thread.c
author Sam Lantinga
Sat, 31 Dec 2011 09:28:07 -0500
changeset 6138 4c64952a58fb
parent 6044 35448a5ea044
child 6885 700f1b25f77f
permissions -rwxr-xr-x
Happy New Year!
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 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_mutex.h"
slouken@0
    26
#include "SDL_thread.h"
slouken@0
    27
#include "SDL_thread_c.h"
slouken@0
    28
#include "SDL_systhread.h"
slouken@6044
    29
#include "../SDL_error_c.h"
slouken@0
    30
slouken@0
    31
#define ARRAY_CHUNKSIZE	32
slouken@0
    32
/* The array of threads currently active in the application
slouken@0
    33
   (except the main thread)
slouken@0
    34
   The manipulation of an array here is safer than using a linked list.
slouken@0
    35
*/
slouken@0
    36
static int SDL_maxthreads = 0;
slouken@0
    37
static int SDL_numthreads = 0;
slouken@0
    38
static SDL_Thread **SDL_Threads = NULL;
slouken@0
    39
static SDL_mutex *thread_lock = NULL;
slouken@0
    40
slouken@4472
    41
static int
slouken@1895
    42
SDL_ThreadsInit(void)
slouken@0
    43
{
slouken@1895
    44
    int retval;
slouken@0
    45
slouken@1895
    46
    retval = 0;
slouken@1895
    47
    thread_lock = SDL_CreateMutex();
slouken@1895
    48
    if (thread_lock == NULL) {
slouken@1895
    49
        retval = -1;
slouken@1895
    50
    }
slouken@1895
    51
    return (retval);
slouken@0
    52
}
slouken@0
    53
slouken@0
    54
/* This should never be called...
slouken@0
    55
   If this is called by SDL_Quit(), we don't know whether or not we should
slouken@0
    56
   clean up threads here.  If any threads are still running after this call,
slouken@0
    57
   they will no longer have access to any per-thread data.
slouken@0
    58
 */
slouken@5425
    59
#if 0
slouken@4472
    60
static void
slouken@1895
    61
SDL_ThreadsQuit(void)
slouken@0
    62
{
slouken@1895
    63
    SDL_mutex *mutex;
slouken@0
    64
slouken@1895
    65
    mutex = thread_lock;
slouken@1895
    66
    thread_lock = NULL;
slouken@1895
    67
    if (mutex != NULL) {
slouken@1895
    68
        SDL_DestroyMutex(mutex);
slouken@1895
    69
    }
slouken@0
    70
}
slouken@5425
    71
#endif
slouken@0
    72
slouken@0
    73
/* Routines for manipulating the thread list */
slouken@1895
    74
static void
slouken@1895
    75
SDL_AddThread(SDL_Thread * thread)
slouken@0
    76
{
slouken@1895
    77
    /* WARNING:
slouken@1895
    78
       If the very first threads are created simultaneously, then
slouken@1895
    79
       there could be a race condition causing memory corruption.
slouken@1895
    80
       In practice, this isn't a problem because by definition there
slouken@1895
    81
       is only one thread running the first time this is called.
slouken@1895
    82
     */
slouken@1895
    83
    if (!thread_lock) {
slouken@1895
    84
        if (SDL_ThreadsInit() < 0) {
slouken@1895
    85
            return;
slouken@1895
    86
        }
slouken@1895
    87
    }
slouken@1895
    88
    SDL_mutexP(thread_lock);
slouken@0
    89
slouken@1895
    90
    /* Expand the list of threads, if necessary */
slouken@0
    91
#ifdef DEBUG_THREADS
slouken@1895
    92
    printf("Adding thread (%d already - %d max)\n",
slouken@1895
    93
           SDL_numthreads, SDL_maxthreads);
slouken@0
    94
#endif
slouken@1895
    95
    if (SDL_numthreads == SDL_maxthreads) {
slouken@1895
    96
        SDL_Thread **threads;
slouken@1895
    97
        threads = (SDL_Thread **) SDL_realloc(SDL_Threads,
slouken@1895
    98
                                              (SDL_maxthreads +
slouken@1895
    99
                                               ARRAY_CHUNKSIZE) *
slouken@1895
   100
                                              (sizeof *threads));
slouken@1895
   101
        if (threads == NULL) {
slouken@1895
   102
            SDL_OutOfMemory();
slouken@1895
   103
            goto done;
slouken@1895
   104
        }
slouken@1895
   105
        SDL_maxthreads += ARRAY_CHUNKSIZE;
slouken@1895
   106
        SDL_Threads = threads;
slouken@1895
   107
    }
slouken@1895
   108
    SDL_Threads[SDL_numthreads++] = thread;
slouken@1895
   109
  done:
slouken@1895
   110
    SDL_mutexV(thread_lock);
slouken@0
   111
}
slouken@0
   112
slouken@1895
   113
static void
slouken@1895
   114
SDL_DelThread(SDL_Thread * thread)
slouken@0
   115
{
slouken@1895
   116
    int i;
slouken@0
   117
slouken@1895
   118
    if (!thread_lock) {
slouken@1895
   119
        return;
slouken@1895
   120
    }
slouken@1895
   121
    SDL_mutexP(thread_lock);
slouken@1895
   122
    for (i = 0; i < SDL_numthreads; ++i) {
slouken@1895
   123
        if (thread == SDL_Threads[i]) {
slouken@1895
   124
            break;
slouken@1895
   125
        }
slouken@1895
   126
    }
slouken@1895
   127
    if (i < SDL_numthreads) {
slouken@1895
   128
        if (--SDL_numthreads > 0) {
slouken@1895
   129
            while (i < SDL_numthreads) {
slouken@1895
   130
                SDL_Threads[i] = SDL_Threads[i + 1];
slouken@1895
   131
                ++i;
slouken@1895
   132
            }
slouken@1895
   133
        } else {
slouken@1895
   134
            SDL_maxthreads = 0;
slouken@1895
   135
            SDL_free(SDL_Threads);
slouken@1895
   136
            SDL_Threads = NULL;
slouken@1895
   137
        }
slouken@0
   138
#ifdef DEBUG_THREADS
slouken@1895
   139
        printf("Deleting thread (%d left - %d max)\n",
slouken@1895
   140
               SDL_numthreads, SDL_maxthreads);
slouken@0
   141
#endif
slouken@1895
   142
    }
slouken@1895
   143
    SDL_mutexV(thread_lock);
slouken@1499
   144
slouken@3300
   145
#if 0   /* There could be memory corruption if another thread is starting */
slouken@1895
   146
    if (SDL_Threads == NULL) {
slouken@1895
   147
        SDL_ThreadsQuit();
slouken@1895
   148
    }
slouken@3300
   149
#endif
slouken@0
   150
}
slouken@0
   151
slouken@0
   152
/* The default (non-thread-safe) global error variable */
slouken@0
   153
static SDL_error SDL_global_error;
slouken@0
   154
slouken@0
   155
/* Routine to get the thread-specific error variable */
slouken@1895
   156
SDL_error *
slouken@1895
   157
SDL_GetErrBuf(void)
slouken@0
   158
{
slouken@1895
   159
    SDL_error *errbuf;
slouken@0
   160
slouken@1895
   161
    errbuf = &SDL_global_error;
slouken@1895
   162
    if (SDL_Threads) {
slouken@1895
   163
        int i;
slouken@3578
   164
        SDL_threadID this_thread;
slouken@0
   165
slouken@1895
   166
        this_thread = SDL_ThreadID();
slouken@1895
   167
        SDL_mutexP(thread_lock);
slouken@1895
   168
        for (i = 0; i < SDL_numthreads; ++i) {
slouken@1895
   169
            if (this_thread == SDL_Threads[i]->threadid) {
slouken@1895
   170
                errbuf = &SDL_Threads[i]->errbuf;
slouken@1895
   171
                break;
slouken@1895
   172
            }
slouken@1895
   173
        }
slouken@1895
   174
        SDL_mutexV(thread_lock);
slouken@1895
   175
    }
slouken@1895
   176
    return (errbuf);
slouken@0
   177
}
slouken@0
   178
slouken@0
   179
slouken@0
   180
/* Arguments and callback to setup and run the user thread function */
slouken@1895
   181
typedef struct
slouken@1895
   182
{
slouken@1895
   183
    int (SDLCALL * func) (void *);
slouken@1895
   184
    void *data;
slouken@1895
   185
    SDL_Thread *info;
slouken@1895
   186
    SDL_sem *wait;
slouken@0
   187
} thread_args;
slouken@0
   188
slouken@1895
   189
void
slouken@1895
   190
SDL_RunThread(void *data)
slouken@0
   191
{
icculus@5969
   192
    thread_args *args = (thread_args *) data;
icculus@5969
   193
    int (SDLCALL * userfunc) (void *) = args->func;
icculus@5969
   194
    void *userdata = args->data;
icculus@5969
   195
    int *statusloc = &args->info->status;
slouken@0
   196
slouken@1895
   197
    /* Perform any system-dependent setup
slouken@1895
   198
       - this function cannot fail, and cannot use SDL_SetError()
slouken@1895
   199
     */
icculus@5969
   200
    SDL_SYS_SetupThread(args->info->name);
slouken@0
   201
slouken@1895
   202
    /* Get the thread id */
slouken@1895
   203
    args->info->threadid = SDL_ThreadID();
slouken@0
   204
slouken@1895
   205
    /* Wake up the parent thread */
slouken@1895
   206
    SDL_SemPost(args->wait);
slouken@0
   207
slouken@1895
   208
    /* Run the function */
slouken@1895
   209
    *statusloc = userfunc(userdata);
slouken@0
   210
}
slouken@0
   211
slouken@1471
   212
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
slouken@1330
   213
#undef SDL_CreateThread
slouken@1895
   214
DECLSPEC SDL_Thread *SDLCALL
icculus@5969
   215
SDL_CreateThread(int (SDLCALL * fn) (void *),
icculus@5969
   216
                 const char *name, void *data,
slouken@1895
   217
                 pfnSDL_CurrentBeginThread pfnBeginThread,
slouken@1895
   218
                 pfnSDL_CurrentEndThread pfnEndThread)
icculus@1190
   219
#else
slouken@1895
   220
DECLSPEC SDL_Thread *SDLCALL
icculus@5969
   221
SDL_CreateThread(int (SDLCALL * fn) (void *),
icculus@5969
   222
                 const char *name, void *data)
icculus@1190
   223
#endif
slouken@0
   224
{
slouken@1895
   225
    SDL_Thread *thread;
slouken@1895
   226
    thread_args *args;
slouken@1895
   227
    int ret;
slouken@0
   228
slouken@1895
   229
    /* Allocate memory for the thread info structure */
slouken@1895
   230
    thread = (SDL_Thread *) SDL_malloc(sizeof(*thread));
slouken@1895
   231
    if (thread == NULL) {
slouken@1895
   232
        SDL_OutOfMemory();
slouken@1895
   233
        return (NULL);
slouken@1895
   234
    }
slouken@1895
   235
    SDL_memset(thread, 0, (sizeof *thread));
slouken@1895
   236
    thread->status = -1;
slouken@0
   237
slouken@1895
   238
    /* Set up the arguments for the thread */
icculus@5969
   239
    if (name != NULL) {
icculus@5969
   240
        thread->name = SDL_strdup(name);
icculus@5969
   241
        if (thread->name == NULL) {
icculus@5969
   242
            SDL_OutOfMemory();
icculus@5969
   243
            SDL_free(thread);
icculus@5969
   244
            return (NULL);
icculus@5969
   245
        }
icculus@5969
   246
    }
icculus@5969
   247
icculus@5969
   248
    /* Set up the arguments for the thread */
slouken@1895
   249
    args = (thread_args *) SDL_malloc(sizeof(*args));
slouken@1895
   250
    if (args == NULL) {
slouken@1895
   251
        SDL_OutOfMemory();
icculus@5969
   252
        SDL_free(thread->name);
slouken@1895
   253
        SDL_free(thread);
slouken@1895
   254
        return (NULL);
slouken@1895
   255
    }
slouken@1895
   256
    args->func = fn;
slouken@1895
   257
    args->data = data;
slouken@1895
   258
    args->info = thread;
slouken@1895
   259
    args->wait = SDL_CreateSemaphore(0);
slouken@1895
   260
    if (args->wait == NULL) {
icculus@5969
   261
        SDL_free(thread->name);
slouken@1895
   262
        SDL_free(thread);
slouken@1895
   263
        SDL_free(args);
slouken@1895
   264
        return (NULL);
slouken@1895
   265
    }
slouken@0
   266
slouken@1895
   267
    /* Add the thread to the list of available threads */
slouken@1895
   268
    SDL_AddThread(thread);
slouken@0
   269
slouken@1895
   270
    /* Create the thread and go! */
slouken@1471
   271
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
slouken@1895
   272
    ret = SDL_SYS_CreateThread(thread, args, pfnBeginThread, pfnEndThread);
icculus@1190
   273
#else
slouken@1895
   274
    ret = SDL_SYS_CreateThread(thread, args);
icculus@1190
   275
#endif
slouken@1895
   276
    if (ret >= 0) {
slouken@1895
   277
        /* Wait for the thread function to use arguments */
slouken@1895
   278
        SDL_SemWait(args->wait);
slouken@1895
   279
    } else {
slouken@1895
   280
        /* Oops, failed.  Gotta free everything */
slouken@1895
   281
        SDL_DelThread(thread);
icculus@5969
   282
        SDL_free(thread->name);
slouken@1895
   283
        SDL_free(thread);
slouken@1895
   284
        thread = NULL;
slouken@1895
   285
    }
slouken@1895
   286
    SDL_DestroySemaphore(args->wait);
slouken@1895
   287
    SDL_free(args);
slouken@0
   288
slouken@1895
   289
    /* Everything is running now */
slouken@1895
   290
    return (thread);
slouken@0
   291
}
slouken@0
   292
slouken@5506
   293
SDL_threadID
slouken@5506
   294
SDL_GetThreadID(SDL_Thread * thread)
slouken@5506
   295
{
slouken@5506
   296
    SDL_threadID id;
slouken@5506
   297
slouken@5506
   298
    if (thread) {
slouken@5506
   299
        id = thread->threadid;
slouken@5506
   300
    } else {
slouken@5506
   301
        id = SDL_ThreadID();
slouken@5506
   302
    }
slouken@5506
   303
    return id;
slouken@5506
   304
}
slouken@5506
   305
icculus@5969
   306
const char *
icculus@5969
   307
SDL_GetThreadName(SDL_Thread * thread)
icculus@5969
   308
{
icculus@5969
   309
    return thread->name;
icculus@5969
   310
}
icculus@5969
   311
slouken@5506
   312
int
slouken@5509
   313
SDL_SetThreadPriority(SDL_ThreadPriority priority)
slouken@5506
   314
{
slouken@5509
   315
    return SDL_SYS_SetThreadPriority(priority);
slouken@5506
   316
}
slouken@5506
   317
slouken@1895
   318
void
slouken@1895
   319
SDL_WaitThread(SDL_Thread * thread, int *status)
slouken@0
   320
{
slouken@1895
   321
    if (thread) {
slouken@1895
   322
        SDL_SYS_WaitThread(thread);
slouken@1895
   323
        if (status) {
slouken@1895
   324
            *status = thread->status;
slouken@1895
   325
        }
slouken@1895
   326
        SDL_DelThread(thread);
icculus@5969
   327
        SDL_free(thread->name);
slouken@1895
   328
        SDL_free(thread);
slouken@1895
   329
    }
slouken@0
   330
}
slouken@0
   331
slouken@1895
   332
/* vi: set ts=4 sw=4 expandtab: */