include/SDL_thread.h
author Sam Lantinga <slouken@libsdl.org>
Mon, 24 Jan 2011 15:46:11 -0800
changeset 5086 c2539ff054c8
parent 5062 e8916fe9cfc8
child 5262 b530ef003506
permissions -rw-r--r--
Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
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@4866
    53
/* The function passed to SDL_CreateThread()
slouken@4866
    54
   It is passed a void* user context parameter and returns an int.
slouken@4866
    55
 */
slouken@4866
    56
typedef int (SDLCALL * SDL_ThreadFunction) (void *data);
slouken@4866
    57
slouken@5086
    58
#if defined(__WIN32__) && !defined(HAVE_LIBC)
slouken@3407
    59
/**
slouken@3407
    60
 *  \file SDL_thread.h
slouken@3407
    61
 *  
slouken@3407
    62
 *  We compile SDL into a DLL. This means, that it's the DLL which
slouken@3407
    63
 *  creates a new thread for the calling process with the SDL_CreateThread()
slouken@3407
    64
 *  API. There is a problem with this, that only the RTL of the SDL.DLL will
slouken@3407
    65
 *  be initialized for those threads, and not the RTL of the calling 
slouken@3407
    66
 *  application!
slouken@3407
    67
 *  
slouken@3407
    68
 *  To solve this, we make a little hack here.
slouken@3407
    69
 *  
slouken@3407
    70
 *  We'll always use the caller's _beginthread() and _endthread() APIs to
slouken@3407
    71
 *  start a new thread. This way, if it's the SDL.DLL which uses this API,
slouken@3407
    72
 *  then the RTL of SDL.DLL will be used to create the new thread, and if it's
slouken@3407
    73
 *  the application, then the RTL of the application will be used.
slouken@3407
    74
 *  
slouken@3407
    75
 *  So, in short:
slouken@3407
    76
 *  Always use the _beginthread() and _endthread() of the calling runtime 
slouken@3407
    77
 *  library!
slouken@3407
    78
 */
slouken@1471
    79
#define SDL_PASSED_BEGINTHREAD_ENDTHREAD
slouken@1465
    80
#ifndef _WIN32_WCE
slouken@1895
    81
#include <process.h>            /* This has _beginthread() and _endthread() defined! */
icculus@1190
    82
#endif
icculus@1190
    83
slouken@3269
    84
#ifdef __GNUC__
slouken@1895
    85
typedef unsigned long (__cdecl * pfnSDL_CurrentBeginThread) (void *, unsigned,
slouken@1895
    86
                                                             unsigned
slouken@1895
    87
                                                             (__stdcall *
slouken@1895
    88
                                                              func) (void *),
slouken@1895
    89
                                                             void *arg,
slouken@1895
    90
                                                             unsigned,
slouken@1895
    91
                                                             unsigned
slouken@1895
    92
                                                             *threadID);
slouken@1895
    93
typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code);
slouken@1330
    94
#else
slouken@1895
    95
typedef uintptr_t(__cdecl * pfnSDL_CurrentBeginThread) (void *, unsigned,
slouken@1895
    96
                                                        unsigned (__stdcall *
slouken@1895
    97
                                                                  func) (void
slouken@1895
    98
                                                                         *),
slouken@1895
    99
                                                        void *arg, unsigned,
slouken@1895
   100
                                                        unsigned *threadID);
slouken@1895
   101
typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code);
slouken@1330
   102
#endif
icculus@1190
   103
slouken@3407
   104
/**
slouken@3407
   105
 *  Create a thread.
slouken@3407
   106
 */
slouken@2060
   107
extern DECLSPEC SDL_Thread *SDLCALL
slouken@4866
   108
SDL_CreateThread(SDL_ThreadFunction fn, void *data,
slouken@2060
   109
                 pfnSDL_CurrentBeginThread pfnBeginThread,
slouken@2060
   110
                 pfnSDL_CurrentEndThread pfnEndThread);
icculus@1190
   111
slouken@3269
   112
#if defined(_WIN32_WCE)
slouken@3407
   113
slouken@3407
   114
/**
slouken@3407
   115
 *  Create a thread.
slouken@3407
   116
 */
slouken@1330
   117
#define SDL_CreateThread(fn, data) SDL_CreateThread(fn, data, NULL, NULL)
slouken@3407
   118
slouken@1330
   119
#else
slouken@3407
   120
slouken@3407
   121
/**
slouken@3407
   122
 *  Create a thread.
slouken@3407
   123
 */
slouken@1330
   124
#define SDL_CreateThread(fn, data) SDL_CreateThread(fn, data, _beginthreadex, _endthreadex)
slouken@3407
   125
slouken@1330
   126
#endif
icculus@1190
   127
#else
slouken@3407
   128
slouken@3407
   129
/**
slouken@3407
   130
 *  Create a thread.
slouken@3407
   131
 */
slouken@1895
   132
extern DECLSPEC SDL_Thread *SDLCALL
slouken@4866
   133
SDL_CreateThread(SDL_ThreadFunction fn, void *data);
slouken@3407
   134
icculus@1190
   135
#endif
slouken@0
   136
slouken@3407
   137
/**
slouken@3578
   138
 *  Get the thread identifier for the current thread.
slouken@3407
   139
 */
slouken@3578
   140
extern DECLSPEC SDL_threadID SDLCALL SDL_ThreadID(void);
slouken@0
   141
slouken@3407
   142
/**
slouken@3578
   143
 *  Get the thread identifier for the specified thread.
slouken@3407
   144
 *  
slouken@3407
   145
 *  Equivalent to SDL_ThreadID() if the specified thread is NULL.
slouken@0
   146
 */
slouken@3578
   147
extern DECLSPEC SDL_threadID SDLCALL SDL_GetThreadID(SDL_Thread * thread);
slouken@0
   148
slouken@3407
   149
/**
slouken@3407
   150
 *  Wait for a thread to finish.
slouken@3407
   151
 *  
slouken@3407
   152
 *  The return code for the thread function is placed in the area
slouken@3407
   153
 *  pointed to by \c status, if \c status is not NULL.
slouken@0
   154
 */
slouken@1895
   155
extern DECLSPEC void SDLCALL SDL_WaitThread(SDL_Thread * thread, int *status);
slouken@0
   156
slouken@0
   157
slouken@0
   158
/* Ends C function definitions when using C++ */
slouken@0
   159
#ifdef __cplusplus
slouken@1895
   160
/* *INDENT-OFF* */
slouken@0
   161
}
slouken@1895
   162
/* *INDENT-ON* */
slouken@0
   163
#endif
slouken@0
   164
#include "close_code.h"
slouken@0
   165
slouken@0
   166
#endif /* _SDL_thread_h */
slouken@1895
   167
slouken@1895
   168
/* vi: set ts=4 sw=4 expandtab: */