src/thread/SDL_thread.c
author Sam Lantinga
Sun, 24 Jan 2010 21:10:53 +0000
changeset 3697 f7b03b6838cb
parent 3578 0d1b16ee0bca
child 4468 82f357a91d43
permissions -rw-r--r--
Fixed bug #926

Updated copyright to LGPL version 2.1 and year 2010
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* System independent thread management routines for SDL */
slouken@0
    25
slouken@0
    26
#include "SDL_mutex.h"
slouken@0
    27
#include "SDL_thread.h"
slouken@0
    28
#include "SDL_thread_c.h"
slouken@0
    29
#include "SDL_systhread.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@1895
    41
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@1895
    59
void
slouken@1895
    60
SDL_ThreadsQuit(void)
slouken@0
    61
{
slouken@1895
    62
    SDL_mutex *mutex;
slouken@0
    63
slouken@1895
    64
    mutex = thread_lock;
slouken@1895
    65
    thread_lock = NULL;
slouken@1895
    66
    if (mutex != NULL) {
slouken@1895
    67
        SDL_DestroyMutex(mutex);
slouken@1895
    68
    }
slouken@0
    69
}
slouken@0
    70
slouken@0
    71
/* Routines for manipulating the thread list */
slouken@1895
    72
static void
slouken@1895
    73
SDL_AddThread(SDL_Thread * thread)
slouken@0
    74
{
slouken@1895
    75
    /* WARNING:
slouken@1895
    76
       If the very first threads are created simultaneously, then
slouken@1895
    77
       there could be a race condition causing memory corruption.
slouken@1895
    78
       In practice, this isn't a problem because by definition there
slouken@1895
    79
       is only one thread running the first time this is called.
slouken@1895
    80
     */
slouken@1895
    81
    if (!thread_lock) {
slouken@1895
    82
        if (SDL_ThreadsInit() < 0) {
slouken@1895
    83
            return;
slouken@1895
    84
        }
slouken@1895
    85
    }
slouken@1895
    86
    SDL_mutexP(thread_lock);
slouken@0
    87
slouken@1895
    88
    /* Expand the list of threads, if necessary */
slouken@0
    89
#ifdef DEBUG_THREADS
slouken@1895
    90
    printf("Adding thread (%d already - %d max)\n",
slouken@1895
    91
           SDL_numthreads, SDL_maxthreads);
slouken@0
    92
#endif
slouken@1895
    93
    if (SDL_numthreads == SDL_maxthreads) {
slouken@1895
    94
        SDL_Thread **threads;
slouken@1895
    95
        threads = (SDL_Thread **) SDL_realloc(SDL_Threads,
slouken@1895
    96
                                              (SDL_maxthreads +
slouken@1895
    97
                                               ARRAY_CHUNKSIZE) *
slouken@1895
    98
                                              (sizeof *threads));
slouken@1895
    99
        if (threads == NULL) {
slouken@1895
   100
            SDL_OutOfMemory();
slouken@1895
   101
            goto done;
slouken@1895
   102
        }
slouken@1895
   103
        SDL_maxthreads += ARRAY_CHUNKSIZE;
slouken@1895
   104
        SDL_Threads = threads;
slouken@1895
   105
    }
slouken@1895
   106
    SDL_Threads[SDL_numthreads++] = thread;
slouken@1895
   107
  done:
slouken@1895
   108
    SDL_mutexV(thread_lock);
slouken@0
   109
}
slouken@0
   110
slouken@1895
   111
static void
slouken@1895
   112
SDL_DelThread(SDL_Thread * thread)
slouken@0
   113
{
slouken@1895
   114
    int i;
slouken@0
   115
slouken@1895
   116
    if (!thread_lock) {
slouken@1895
   117
        return;
slouken@1895
   118
    }
slouken@1895
   119
    SDL_mutexP(thread_lock);
slouken@1895
   120
    for (i = 0; i < SDL_numthreads; ++i) {
slouken@1895
   121
        if (thread == SDL_Threads[i]) {
slouken@1895
   122
            break;
slouken@1895
   123
        }
slouken@1895
   124
    }
slouken@1895
   125
    if (i < SDL_numthreads) {
slouken@1895
   126
        if (--SDL_numthreads > 0) {
slouken@1895
   127
            while (i < SDL_numthreads) {
slouken@1895
   128
                SDL_Threads[i] = SDL_Threads[i + 1];
slouken@1895
   129
                ++i;
slouken@1895
   130
            }
slouken@1895
   131
        } else {
slouken@1895
   132
            SDL_maxthreads = 0;
slouken@1895
   133
            SDL_free(SDL_Threads);
slouken@1895
   134
            SDL_Threads = NULL;
slouken@1895
   135
        }
slouken@0
   136
#ifdef DEBUG_THREADS
slouken@1895
   137
        printf("Deleting thread (%d left - %d max)\n",
slouken@1895
   138
               SDL_numthreads, SDL_maxthreads);
slouken@0
   139
#endif
slouken@1895
   140
    }
slouken@1895
   141
    SDL_mutexV(thread_lock);
slouken@1499
   142
slouken@3300
   143
#if 0   /* There could be memory corruption if another thread is starting */
slouken@1895
   144
    if (SDL_Threads == NULL) {
slouken@1895
   145
        SDL_ThreadsQuit();
slouken@1895
   146
    }
slouken@3300
   147
#endif
slouken@0
   148
}
slouken@0
   149
slouken@0
   150
/* The default (non-thread-safe) global error variable */
slouken@0
   151
static SDL_error SDL_global_error;
slouken@0
   152
slouken@0
   153
/* Routine to get the thread-specific error variable */
slouken@1895
   154
SDL_error *
slouken@1895
   155
SDL_GetErrBuf(void)
slouken@0
   156
{
slouken@1895
   157
    SDL_error *errbuf;
slouken@0
   158
slouken@1895
   159
    errbuf = &SDL_global_error;
slouken@1895
   160
    if (SDL_Threads) {
slouken@1895
   161
        int i;
slouken@3578
   162
        SDL_threadID this_thread;
slouken@0
   163
slouken@1895
   164
        this_thread = SDL_ThreadID();
slouken@1895
   165
        SDL_mutexP(thread_lock);
slouken@1895
   166
        for (i = 0; i < SDL_numthreads; ++i) {
slouken@1895
   167
            if (this_thread == SDL_Threads[i]->threadid) {
slouken@1895
   168
                errbuf = &SDL_Threads[i]->errbuf;
slouken@1895
   169
                break;
slouken@1895
   170
            }
slouken@1895
   171
        }
slouken@1895
   172
        SDL_mutexV(thread_lock);
slouken@1895
   173
    }
slouken@1895
   174
    return (errbuf);
slouken@0
   175
}
slouken@0
   176
slouken@0
   177
slouken@0
   178
/* Arguments and callback to setup and run the user thread function */
slouken@1895
   179
typedef struct
slouken@1895
   180
{
slouken@1895
   181
    int (SDLCALL * func) (void *);
slouken@1895
   182
    void *data;
slouken@1895
   183
    SDL_Thread *info;
slouken@1895
   184
    SDL_sem *wait;
slouken@0
   185
} thread_args;
slouken@0
   186
slouken@1895
   187
void
slouken@1895
   188
SDL_RunThread(void *data)
slouken@0
   189
{
slouken@1895
   190
    thread_args *args;
slouken@1895
   191
    int (SDLCALL * userfunc) (void *);
slouken@1895
   192
    void *userdata;
slouken@1895
   193
    int *statusloc;
slouken@0
   194
slouken@1895
   195
    /* Perform any system-dependent setup
slouken@1895
   196
       - this function cannot fail, and cannot use SDL_SetError()
slouken@1895
   197
     */
slouken@1895
   198
    SDL_SYS_SetupThread();
slouken@0
   199
slouken@1895
   200
    /* Get the thread id */
slouken@1895
   201
    args = (thread_args *) data;
slouken@1895
   202
    args->info->threadid = SDL_ThreadID();
slouken@0
   203
slouken@1895
   204
    /* Figure out what function to run */
slouken@1895
   205
    userfunc = args->func;
slouken@1895
   206
    userdata = args->data;
slouken@1895
   207
    statusloc = &args->info->status;
slouken@0
   208
slouken@1895
   209
    /* Wake up the parent thread */
slouken@1895
   210
    SDL_SemPost(args->wait);
slouken@0
   211
slouken@1895
   212
    /* Run the function */
slouken@1895
   213
    *statusloc = userfunc(userdata);
slouken@0
   214
}
slouken@0
   215
slouken@1471
   216
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
slouken@1330
   217
#undef SDL_CreateThread
slouken@1895
   218
DECLSPEC SDL_Thread *SDLCALL
slouken@1895
   219
SDL_CreateThread(int (SDLCALL * fn) (void *), void *data,
slouken@1895
   220
                 pfnSDL_CurrentBeginThread pfnBeginThread,
slouken@1895
   221
                 pfnSDL_CurrentEndThread pfnEndThread)
icculus@1190
   222
#else
slouken@1895
   223
DECLSPEC SDL_Thread *SDLCALL
slouken@1895
   224
SDL_CreateThread(int (SDLCALL * fn) (void *), void *data)
icculus@1190
   225
#endif
slouken@0
   226
{
slouken@1895
   227
    SDL_Thread *thread;
slouken@1895
   228
    thread_args *args;
slouken@1895
   229
    int ret;
slouken@0
   230
slouken@1895
   231
    /* Allocate memory for the thread info structure */
slouken@1895
   232
    thread = (SDL_Thread *) SDL_malloc(sizeof(*thread));
slouken@1895
   233
    if (thread == NULL) {
slouken@1895
   234
        SDL_OutOfMemory();
slouken@1895
   235
        return (NULL);
slouken@1895
   236
    }
slouken@1895
   237
    SDL_memset(thread, 0, (sizeof *thread));
slouken@1895
   238
    thread->status = -1;
slouken@0
   239
slouken@1895
   240
    /* Set up the arguments for the thread */
slouken@1895
   241
    args = (thread_args *) SDL_malloc(sizeof(*args));
slouken@1895
   242
    if (args == NULL) {
slouken@1895
   243
        SDL_OutOfMemory();
slouken@1895
   244
        SDL_free(thread);
slouken@1895
   245
        return (NULL);
slouken@1895
   246
    }
slouken@1895
   247
    args->func = fn;
slouken@1895
   248
    args->data = data;
slouken@1895
   249
    args->info = thread;
slouken@1895
   250
    args->wait = SDL_CreateSemaphore(0);
slouken@1895
   251
    if (args->wait == NULL) {
slouken@1895
   252
        SDL_free(thread);
slouken@1895
   253
        SDL_free(args);
slouken@1895
   254
        return (NULL);
slouken@1895
   255
    }
slouken@0
   256
slouken@1895
   257
    /* Add the thread to the list of available threads */
slouken@1895
   258
    SDL_AddThread(thread);
slouken@0
   259
slouken@1895
   260
    /* Create the thread and go! */
slouken@1471
   261
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
slouken@1895
   262
    ret = SDL_SYS_CreateThread(thread, args, pfnBeginThread, pfnEndThread);
icculus@1190
   263
#else
slouken@1895
   264
    ret = SDL_SYS_CreateThread(thread, args);
icculus@1190
   265
#endif
slouken@1895
   266
    if (ret >= 0) {
slouken@1895
   267
        /* Wait for the thread function to use arguments */
slouken@1895
   268
        SDL_SemWait(args->wait);
slouken@1895
   269
    } else {
slouken@1895
   270
        /* Oops, failed.  Gotta free everything */
slouken@1895
   271
        SDL_DelThread(thread);
slouken@1895
   272
        SDL_free(thread);
slouken@1895
   273
        thread = NULL;
slouken@1895
   274
    }
slouken@1895
   275
    SDL_DestroySemaphore(args->wait);
slouken@1895
   276
    SDL_free(args);
slouken@0
   277
slouken@1895
   278
    /* Everything is running now */
slouken@1895
   279
    return (thread);
slouken@0
   280
}
slouken@0
   281
slouken@1895
   282
void
slouken@1895
   283
SDL_WaitThread(SDL_Thread * thread, int *status)
slouken@0
   284
{
slouken@1895
   285
    if (thread) {
slouken@1895
   286
        SDL_SYS_WaitThread(thread);
slouken@1895
   287
        if (status) {
slouken@1895
   288
            *status = thread->status;
slouken@1895
   289
        }
slouken@1895
   290
        SDL_DelThread(thread);
slouken@1895
   291
        SDL_free(thread);
slouken@1895
   292
    }
slouken@0
   293
}
slouken@0
   294
slouken@3578
   295
SDL_threadID
slouken@1895
   296
SDL_GetThreadID(SDL_Thread * thread)
slouken@0
   297
{
slouken@3578
   298
    SDL_threadID id;
slouken@0
   299
slouken@1895
   300
    if (thread) {
slouken@1895
   301
        id = thread->threadid;
slouken@1895
   302
    } else {
slouken@1895
   303
        id = SDL_ThreadID();
slouken@1895
   304
    }
slouken@3578
   305
    return id;
slouken@0
   306
}
slouken@0
   307
slouken@1895
   308
void
slouken@1895
   309
SDL_KillThread(SDL_Thread * thread)
slouken@0
   310
{
slouken@2779
   311
    /* This is a no-op in SDL 1.3 and later. */
slouken@0
   312
}
slouken@0
   313
slouken@1895
   314
/* vi: set ts=4 sw=4 expandtab: */