include/SDL_thread.h
author Sam Lantinga <slouken@libsdl.org>
Sat, 31 Dec 2011 09:28:07 -0500
changeset 6138 4c64952a58fb
parent 5969 3a041d215edc
child 6430 48d519500f7e
permissions -rw-r--r--
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@0
    21
slouken@0
    22
#ifndef _SDL_thread_h
slouken@0
    23
#define _SDL_thread_h
slouken@0
    24
slouken@1895
    25
/**
slouken@3407
    26
 *  \file SDL_thread.h
slouken@3407
    27
 *  
slouken@3407
    28
 *  Header for the SDL thread management routines.
slouken@1895
    29
 */
slouken@0
    30
slouken@1356
    31
#include "SDL_stdinc.h"
slouken@1358
    32
#include "SDL_error.h"
slouken@0
    33
slouken@0
    34
/* Thread synchronization primitives */
slouken@0
    35
#include "SDL_mutex.h"
slouken@0
    36
slouken@0
    37
#include "begin_code.h"
slouken@0
    38
/* Set up for C function definitions, even when using C++ */
slouken@0
    39
#ifdef __cplusplus
slouken@1895
    40
/* *INDENT-OFF* */
slouken@0
    41
extern "C" {
slouken@1895
    42
/* *INDENT-ON* */
slouken@0
    43
#endif
slouken@0
    44
slouken@0
    45
/* The SDL thread structure, defined in SDL_thread.c */
slouken@0
    46
struct SDL_Thread;
slouken@0
    47
typedef struct SDL_Thread SDL_Thread;
slouken@0
    48
slouken@3578
    49
/* The SDL thread ID */
slouken@3578
    50
typedef unsigned long SDL_threadID;
slouken@3578
    51
slouken@5506
    52
/* The SDL thread priority
slouken@5506
    53
 *
slouken@5506
    54
 * Note: On many systems you require special privileges to set high priority.
slouken@5506
    55
 */
slouken@5506
    56
typedef enum {
slouken@5506
    57
    SDL_THREAD_PRIORITY_LOW,
slouken@5506
    58
    SDL_THREAD_PRIORITY_NORMAL,
slouken@5506
    59
    SDL_THREAD_PRIORITY_HIGH
slouken@5506
    60
} SDL_ThreadPriority;
slouken@5506
    61
slouken@4866
    62
/* The function passed to SDL_CreateThread()
slouken@4866
    63
   It is passed a void* user context parameter and returns an int.
slouken@4866
    64
 */
slouken@4866
    65
typedef int (SDLCALL * SDL_ThreadFunction) (void *data);
slouken@4866
    66
slouken@5086
    67
#if defined(__WIN32__) && !defined(HAVE_LIBC)
slouken@3407
    68
/**
slouken@3407
    69
 *  \file SDL_thread.h
slouken@3407
    70
 *  
slouken@3407
    71
 *  We compile SDL into a DLL. This means, that it's the DLL which
slouken@3407
    72
 *  creates a new thread for the calling process with the SDL_CreateThread()
slouken@3407
    73
 *  API. There is a problem with this, that only the RTL of the SDL.DLL will
slouken@3407
    74
 *  be initialized for those threads, and not the RTL of the calling 
slouken@3407
    75
 *  application!
slouken@3407
    76
 *  
slouken@3407
    77
 *  To solve this, we make a little hack here.
slouken@3407
    78
 *  
slouken@3407
    79
 *  We'll always use the caller's _beginthread() and _endthread() APIs to
slouken@3407
    80
 *  start a new thread. This way, if it's the SDL.DLL which uses this API,
slouken@3407
    81
 *  then the RTL of SDL.DLL will be used to create the new thread, and if it's
slouken@3407
    82
 *  the application, then the RTL of the application will be used.
slouken@3407
    83
 *  
slouken@3407
    84
 *  So, in short:
slouken@3407
    85
 *  Always use the _beginthread() and _endthread() of the calling runtime 
slouken@3407
    86
 *  library!
slouken@3407
    87
 */
slouken@1471
    88
#define SDL_PASSED_BEGINTHREAD_ENDTHREAD
slouken@1465
    89
#ifndef _WIN32_WCE
slouken@1895
    90
#include <process.h>            /* This has _beginthread() and _endthread() defined! */
icculus@1190
    91
#endif
icculus@1190
    92
slouken@1895
    93
typedef uintptr_t(__cdecl * pfnSDL_CurrentBeginThread) (void *, unsigned,
slouken@1895
    94
                                                        unsigned (__stdcall *
slouken@1895
    95
                                                                  func) (void
slouken@1895
    96
                                                                         *),
slouken@1895
    97
                                                        void *arg, unsigned,
slouken@1895
    98
                                                        unsigned *threadID);
slouken@1895
    99
typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code);
icculus@1190
   100
slouken@3407
   101
/**
slouken@3407
   102
 *  Create a thread.
slouken@3407
   103
 */
slouken@2060
   104
extern DECLSPEC SDL_Thread *SDLCALL
icculus@5969
   105
SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data,
slouken@2060
   106
                 pfnSDL_CurrentBeginThread pfnBeginThread,
slouken@2060
   107
                 pfnSDL_CurrentEndThread pfnEndThread);
icculus@1190
   108
slouken@3269
   109
#if defined(_WIN32_WCE)
slouken@3407
   110
slouken@3407
   111
/**
slouken@3407
   112
 *  Create a thread.
slouken@3407
   113
 */
icculus@5969
   114
#define SDL_CreateThread(fn, name, data) SDL_CreateThread(fn, name, data, NULL, NULL)
slouken@3407
   115
slouken@1330
   116
#else
slouken@3407
   117
slouken@3407
   118
/**
slouken@3407
   119
 *  Create a thread.
slouken@3407
   120
 */
icculus@5969
   121
#define SDL_CreateThread(fn, name, data) SDL_CreateThread(fn, name, data, _beginthreadex, _endthreadex)
slouken@3407
   122
slouken@1330
   123
#endif
icculus@1190
   124
#else
slouken@3407
   125
slouken@3407
   126
/**
slouken@3407
   127
 *  Create a thread.
icculus@5969
   128
 *
icculus@5969
   129
 *   Thread naming is a little complicated: Most systems have very small
icculus@5969
   130
 *    limits for the string length (BeOS has 32 bytes, Linux currently has 16,
icculus@5969
   131
 *    Visual C++ 6.0 has nine!), and possibly other arbitrary rules. You'll
icculus@5969
   132
 *    have to see what happens with your system's debugger. The name should be
icculus@5969
   133
 *    UTF-8 (but using the naming limits of C identifiers is a better bet).
icculus@5969
   134
 *   There are no requirements for thread naming conventions, so long as the
icculus@5969
   135
 *    string is null-terminated UTF-8, but these guidelines are helpful in
icculus@5969
   136
 *    choosing a name:
icculus@5969
   137
 *
icculus@5969
   138
 *    http://stackoverflow.com/questions/149932/naming-conventions-for-threads
icculus@5969
   139
 *
icculus@5969
   140
 *   If a system imposes requirements, SDL will try to munge the string for
icculus@5969
   141
 *    it (truncate, etc), but the original string contents will be available
icculus@5969
   142
 *    from SDL_GetThreadName().
slouken@3407
   143
 */
slouken@1895
   144
extern DECLSPEC SDL_Thread *SDLCALL
icculus@5969
   145
SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data);
slouken@3407
   146
icculus@1190
   147
#endif
slouken@0
   148
slouken@3407
   149
/**
icculus@5969
   150
 * Get the thread name, as it was specified in SDL_CreateThread().
icculus@5969
   151
 *  This function returns a pointer to a UTF-8 string that names the
icculus@5969
   152
 *  specified thread, or NULL if it doesn't have a name. This is internal
icculus@5969
   153
 *  memory, not to be free()'d by the caller, and remains valid until the
icculus@5969
   154
 *  specified thread is cleaned up by SDL_WaitThread().
icculus@5969
   155
 */
icculus@5969
   156
extern DECLSPEC const char *SDLCALL SDL_GetThreadName(SDL_Thread *thread);
icculus@5969
   157
icculus@5969
   158
/**
slouken@3578
   159
 *  Get the thread identifier for the current thread.
slouken@3407
   160
 */
slouken@3578
   161
extern DECLSPEC SDL_threadID SDLCALL SDL_ThreadID(void);
slouken@0
   162
slouken@3407
   163
/**
slouken@3578
   164
 *  Get the thread identifier for the specified thread.
slouken@3407
   165
 *  
slouken@3407
   166
 *  Equivalent to SDL_ThreadID() if the specified thread is NULL.
slouken@0
   167
 */
slouken@3578
   168
extern DECLSPEC SDL_threadID SDLCALL SDL_GetThreadID(SDL_Thread * thread);
slouken@0
   169
slouken@3407
   170
/**
slouken@5509
   171
 *  Set the priority for the current thread
slouken@5506
   172
 */
slouken@5509
   173
extern DECLSPEC int SDLCALL SDL_SetThreadPriority(SDL_ThreadPriority priority);
slouken@5506
   174
slouken@5506
   175
/**
slouken@3407
   176
 *  Wait for a thread to finish.
slouken@3407
   177
 *  
slouken@3407
   178
 *  The return code for the thread function is placed in the area
slouken@3407
   179
 *  pointed to by \c status, if \c status is not NULL.
slouken@0
   180
 */
slouken@1895
   181
extern DECLSPEC void SDLCALL SDL_WaitThread(SDL_Thread * thread, int *status);
slouken@0
   182
slouken@0
   183
slouken@0
   184
/* Ends C function definitions when using C++ */
slouken@0
   185
#ifdef __cplusplus
slouken@1895
   186
/* *INDENT-OFF* */
slouken@0
   187
}
slouken@1895
   188
/* *INDENT-ON* */
slouken@0
   189
#endif
slouken@0
   190
#include "close_code.h"
slouken@0
   191
slouken@0
   192
#endif /* _SDL_thread_h */
slouken@1895
   193
slouken@1895
   194
/* vi: set ts=4 sw=4 expandtab: */