include/SDL_thread.h
author Sam Lantinga <slouken@libsdl.org>
Mon, 24 May 2010 22:56:36 -0700
changeset 4468 82f357a91d43
parent 3697 f7b03b6838cb
child 4866 f42e8db53f78
permissions -rw-r--r--
SDL_KillThread() is no longer supported - it was always dangerous! :)
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@251
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifndef _SDL_thread_h
slouken@0
    24
#define _SDL_thread_h
slouken@0
    25
slouken@1895
    26
/**
slouken@3407
    27
 *  \file SDL_thread.h
slouken@3407
    28
 *  
slouken@3407
    29
 *  Header for the SDL thread management routines.
slouken@1895
    30
 */
slouken@0
    31
slouken@1356
    32
#include "SDL_stdinc.h"
slouken@1358
    33
#include "SDL_error.h"
slouken@0
    34
slouken@0
    35
/* Thread synchronization primitives */
slouken@0
    36
#include "SDL_mutex.h"
slouken@0
    37
slouken@0
    38
#include "begin_code.h"
slouken@0
    39
/* Set up for C function definitions, even when using C++ */
slouken@0
    40
#ifdef __cplusplus
slouken@1895
    41
/* *INDENT-OFF* */
slouken@0
    42
extern "C" {
slouken@1895
    43
/* *INDENT-ON* */
slouken@0
    44
#endif
slouken@0
    45
slouken@0
    46
/* The SDL thread structure, defined in SDL_thread.c */
slouken@0
    47
struct SDL_Thread;
slouken@0
    48
typedef struct SDL_Thread SDL_Thread;
slouken@0
    49
slouken@3578
    50
/* The SDL thread ID */
slouken@3578
    51
typedef unsigned long SDL_threadID;
slouken@3578
    52
slouken@3269
    53
#if defined(__WIN32__) && !defined(HAVE_LIBC)
slouken@3407
    54
/**
slouken@3407
    55
 *  \file SDL_thread.h
slouken@3407
    56
 *  
slouken@3407
    57
 *  We compile SDL into a DLL. This means, that it's the DLL which
slouken@3407
    58
 *  creates a new thread for the calling process with the SDL_CreateThread()
slouken@3407
    59
 *  API. There is a problem with this, that only the RTL of the SDL.DLL will
slouken@3407
    60
 *  be initialized for those threads, and not the RTL of the calling 
slouken@3407
    61
 *  application!
slouken@3407
    62
 *  
slouken@3407
    63
 *  To solve this, we make a little hack here.
slouken@3407
    64
 *  
slouken@3407
    65
 *  We'll always use the caller's _beginthread() and _endthread() APIs to
slouken@3407
    66
 *  start a new thread. This way, if it's the SDL.DLL which uses this API,
slouken@3407
    67
 *  then the RTL of SDL.DLL will be used to create the new thread, and if it's
slouken@3407
    68
 *  the application, then the RTL of the application will be used.
slouken@3407
    69
 *  
slouken@3407
    70
 *  So, in short:
slouken@3407
    71
 *  Always use the _beginthread() and _endthread() of the calling runtime 
slouken@3407
    72
 *  library!
slouken@3407
    73
 */
slouken@1471
    74
#define SDL_PASSED_BEGINTHREAD_ENDTHREAD
slouken@1465
    75
#ifndef _WIN32_WCE
slouken@1895
    76
#include <process.h>            /* This has _beginthread() and _endthread() defined! */
icculus@1190
    77
#endif
icculus@1190
    78
slouken@3269
    79
#ifdef __GNUC__
slouken@1895
    80
typedef unsigned long (__cdecl * pfnSDL_CurrentBeginThread) (void *, unsigned,
slouken@1895
    81
                                                             unsigned
slouken@1895
    82
                                                             (__stdcall *
slouken@1895
    83
                                                              func) (void *),
slouken@1895
    84
                                                             void *arg,
slouken@1895
    85
                                                             unsigned,
slouken@1895
    86
                                                             unsigned
slouken@1895
    87
                                                             *threadID);
slouken@1895
    88
typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code);
slouken@1330
    89
#else
slouken@1895
    90
typedef uintptr_t(__cdecl * pfnSDL_CurrentBeginThread) (void *, unsigned,
slouken@1895
    91
                                                        unsigned (__stdcall *
slouken@1895
    92
                                                                  func) (void
slouken@1895
    93
                                                                         *),
slouken@1895
    94
                                                        void *arg, unsigned,
slouken@1895
    95
                                                        unsigned *threadID);
slouken@1895
    96
typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code);
slouken@1330
    97
#endif
icculus@1190
    98
slouken@3407
    99
/**
slouken@3407
   100
 *  Create a thread.
slouken@3407
   101
 */
slouken@2060
   102
extern DECLSPEC SDL_Thread *SDLCALL
slouken@2060
   103
SDL_CreateThread(int (SDLCALL * f) (void *), void *data,
slouken@2060
   104
                 pfnSDL_CurrentBeginThread pfnBeginThread,
slouken@2060
   105
                 pfnSDL_CurrentEndThread pfnEndThread);
icculus@1190
   106
slouken@3269
   107
#if defined(_WIN32_WCE)
slouken@3407
   108
slouken@3407
   109
/**
slouken@3407
   110
 *  Create a thread.
slouken@3407
   111
 */
slouken@1330
   112
#define SDL_CreateThread(fn, data) SDL_CreateThread(fn, data, NULL, NULL)
slouken@3407
   113
slouken@1330
   114
#else
slouken@3407
   115
slouken@3407
   116
/**
slouken@3407
   117
 *  Create a thread.
slouken@3407
   118
 */
slouken@1330
   119
#define SDL_CreateThread(fn, data) SDL_CreateThread(fn, data, _beginthreadex, _endthreadex)
slouken@3407
   120
slouken@1330
   121
#endif
icculus@1190
   122
#else
slouken@3407
   123
slouken@3407
   124
/**
slouken@3407
   125
 *  Create a thread.
slouken@3407
   126
 */
slouken@1895
   127
extern DECLSPEC SDL_Thread *SDLCALL
slouken@1895
   128
SDL_CreateThread(int (SDLCALL * fn) (void *), void *data);
slouken@3407
   129
icculus@1190
   130
#endif
slouken@0
   131
slouken@3407
   132
/**
slouken@3578
   133
 *  Get the thread identifier for the current thread.
slouken@3407
   134
 */
slouken@3578
   135
extern DECLSPEC SDL_threadID SDLCALL SDL_ThreadID(void);
slouken@0
   136
slouken@3407
   137
/**
slouken@3578
   138
 *  Get the thread identifier for the specified thread.
slouken@3407
   139
 *  
slouken@3407
   140
 *  Equivalent to SDL_ThreadID() if the specified thread is NULL.
slouken@0
   141
 */
slouken@3578
   142
extern DECLSPEC SDL_threadID SDLCALL SDL_GetThreadID(SDL_Thread * thread);
slouken@0
   143
slouken@3407
   144
/**
slouken@3407
   145
 *  Wait for a thread to finish.
slouken@3407
   146
 *  
slouken@3407
   147
 *  The return code for the thread function is placed in the area
slouken@3407
   148
 *  pointed to by \c status, if \c status is not NULL.
slouken@0
   149
 */
slouken@1895
   150
extern DECLSPEC void SDLCALL SDL_WaitThread(SDL_Thread * thread, int *status);
slouken@0
   151
slouken@0
   152
slouken@0
   153
/* Ends C function definitions when using C++ */
slouken@0
   154
#ifdef __cplusplus
slouken@1895
   155
/* *INDENT-OFF* */
slouken@0
   156
}
slouken@1895
   157
/* *INDENT-ON* */
slouken@0
   158
#endif
slouken@0
   159
#include "close_code.h"
slouken@0
   160
slouken@0
   161
#endif /* _SDL_thread_h */
slouken@1895
   162
slouken@1895
   163
/* vi: set ts=4 sw=4 expandtab: */