src/thread/SDL_thread.c
author Holmes Futrell <hfutrell@umail.ucsb.edu>
Mon, 18 Aug 2008 19:39:08 +0000
branchgsoc2008_iphone
changeset 2467 058e1f140ff3
parent 1895 c121d94672cb
child 2779 4436464c4f51
permissions -rw-r--r--
Fixed problem where creating the view passed NULL as the context for setting current context ... needed to actually pass the context.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 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@1895
   143
    if (SDL_Threads == NULL) {
slouken@1895
   144
        SDL_ThreadsQuit();
slouken@1895
   145
    }
slouken@0
   146
}
slouken@0
   147
slouken@0
   148
/* The default (non-thread-safe) global error variable */
slouken@0
   149
static SDL_error SDL_global_error;
slouken@0
   150
slouken@0
   151
/* Routine to get the thread-specific error variable */
slouken@1895
   152
SDL_error *
slouken@1895
   153
SDL_GetErrBuf(void)
slouken@0
   154
{
slouken@1895
   155
    SDL_error *errbuf;
slouken@0
   156
slouken@1895
   157
    errbuf = &SDL_global_error;
slouken@1895
   158
    if (SDL_Threads) {
slouken@1895
   159
        int i;
slouken@1895
   160
        Uint32 this_thread;
slouken@0
   161
slouken@1895
   162
        this_thread = SDL_ThreadID();
slouken@1895
   163
        SDL_mutexP(thread_lock);
slouken@1895
   164
        for (i = 0; i < SDL_numthreads; ++i) {
slouken@1895
   165
            if (this_thread == SDL_Threads[i]->threadid) {
slouken@1895
   166
                errbuf = &SDL_Threads[i]->errbuf;
slouken@1895
   167
                break;
slouken@1895
   168
            }
slouken@1895
   169
        }
slouken@1895
   170
        SDL_mutexV(thread_lock);
slouken@1895
   171
    }
slouken@1895
   172
    return (errbuf);
slouken@0
   173
}
slouken@0
   174
slouken@0
   175
slouken@0
   176
/* Arguments and callback to setup and run the user thread function */
slouken@1895
   177
typedef struct
slouken@1895
   178
{
slouken@1895
   179
    int (SDLCALL * func) (void *);
slouken@1895
   180
    void *data;
slouken@1895
   181
    SDL_Thread *info;
slouken@1895
   182
    SDL_sem *wait;
slouken@0
   183
} thread_args;
slouken@0
   184
slouken@1895
   185
void
slouken@1895
   186
SDL_RunThread(void *data)
slouken@0
   187
{
slouken@1895
   188
    thread_args *args;
slouken@1895
   189
    int (SDLCALL * userfunc) (void *);
slouken@1895
   190
    void *userdata;
slouken@1895
   191
    int *statusloc;
slouken@0
   192
slouken@1895
   193
    /* Perform any system-dependent setup
slouken@1895
   194
       - this function cannot fail, and cannot use SDL_SetError()
slouken@1895
   195
     */
slouken@1895
   196
    SDL_SYS_SetupThread();
slouken@0
   197
slouken@1895
   198
    /* Get the thread id */
slouken@1895
   199
    args = (thread_args *) data;
slouken@1895
   200
    args->info->threadid = SDL_ThreadID();
slouken@0
   201
slouken@1895
   202
    /* Figure out what function to run */
slouken@1895
   203
    userfunc = args->func;
slouken@1895
   204
    userdata = args->data;
slouken@1895
   205
    statusloc = &args->info->status;
slouken@0
   206
slouken@1895
   207
    /* Wake up the parent thread */
slouken@1895
   208
    SDL_SemPost(args->wait);
slouken@0
   209
slouken@1895
   210
    /* Run the function */
slouken@1895
   211
    *statusloc = userfunc(userdata);
slouken@0
   212
}
slouken@0
   213
slouken@1471
   214
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
slouken@1330
   215
#undef SDL_CreateThread
slouken@1895
   216
DECLSPEC SDL_Thread *SDLCALL
slouken@1895
   217
SDL_CreateThread(int (SDLCALL * fn) (void *), void *data,
slouken@1895
   218
                 pfnSDL_CurrentBeginThread pfnBeginThread,
slouken@1895
   219
                 pfnSDL_CurrentEndThread pfnEndThread)
icculus@1190
   220
#else
slouken@1895
   221
DECLSPEC SDL_Thread *SDLCALL
slouken@1895
   222
SDL_CreateThread(int (SDLCALL * fn) (void *), 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 */
slouken@1895
   239
    args = (thread_args *) SDL_malloc(sizeof(*args));
slouken@1895
   240
    if (args == NULL) {
slouken@1895
   241
        SDL_OutOfMemory();
slouken@1895
   242
        SDL_free(thread);
slouken@1895
   243
        return (NULL);
slouken@1895
   244
    }
slouken@1895
   245
    args->func = fn;
slouken@1895
   246
    args->data = data;
slouken@1895
   247
    args->info = thread;
slouken@1895
   248
    args->wait = SDL_CreateSemaphore(0);
slouken@1895
   249
    if (args->wait == NULL) {
slouken@1895
   250
        SDL_free(thread);
slouken@1895
   251
        SDL_free(args);
slouken@1895
   252
        return (NULL);
slouken@1895
   253
    }
slouken@0
   254
slouken@1895
   255
    /* Add the thread to the list of available threads */
slouken@1895
   256
    SDL_AddThread(thread);
slouken@0
   257
slouken@1895
   258
    /* Create the thread and go! */
slouken@1471
   259
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
slouken@1895
   260
    ret = SDL_SYS_CreateThread(thread, args, pfnBeginThread, pfnEndThread);
icculus@1190
   261
#else
slouken@1895
   262
    ret = SDL_SYS_CreateThread(thread, args);
icculus@1190
   263
#endif
slouken@1895
   264
    if (ret >= 0) {
slouken@1895
   265
        /* Wait for the thread function to use arguments */
slouken@1895
   266
        SDL_SemWait(args->wait);
slouken@1895
   267
    } else {
slouken@1895
   268
        /* Oops, failed.  Gotta free everything */
slouken@1895
   269
        SDL_DelThread(thread);
slouken@1895
   270
        SDL_free(thread);
slouken@1895
   271
        thread = NULL;
slouken@1895
   272
    }
slouken@1895
   273
    SDL_DestroySemaphore(args->wait);
slouken@1895
   274
    SDL_free(args);
slouken@0
   275
slouken@1895
   276
    /* Everything is running now */
slouken@1895
   277
    return (thread);
slouken@0
   278
}
slouken@0
   279
slouken@1895
   280
void
slouken@1895
   281
SDL_WaitThread(SDL_Thread * thread, int *status)
slouken@0
   282
{
slouken@1895
   283
    if (thread) {
slouken@1895
   284
        SDL_SYS_WaitThread(thread);
slouken@1895
   285
        if (status) {
slouken@1895
   286
            *status = thread->status;
slouken@1895
   287
        }
slouken@1895
   288
        SDL_DelThread(thread);
slouken@1895
   289
        SDL_free(thread);
slouken@1895
   290
    }
slouken@0
   291
}
slouken@0
   292
slouken@1895
   293
Uint32
slouken@1895
   294
SDL_GetThreadID(SDL_Thread * thread)
slouken@0
   295
{
slouken@1895
   296
    Uint32 id;
slouken@0
   297
slouken@1895
   298
    if (thread) {
slouken@1895
   299
        id = thread->threadid;
slouken@1895
   300
    } else {
slouken@1895
   301
        id = SDL_ThreadID();
slouken@1895
   302
    }
slouken@1895
   303
    return (id);
slouken@0
   304
}
slouken@0
   305
slouken@1895
   306
void
slouken@1895
   307
SDL_KillThread(SDL_Thread * thread)
slouken@0
   308
{
slouken@1895
   309
    if (thread) {
slouken@1895
   310
        SDL_SYS_KillThread(thread);
slouken@1895
   311
        SDL_WaitThread(thread, NULL);
slouken@1895
   312
    }
slouken@0
   313
}
slouken@0
   314
slouken@1895
   315
/* vi: set ts=4 sw=4 expandtab: */