include/SDL_thread.h
author Ryan C. Gordon <icculus@icculus.org>
Mon, 12 Sep 2011 13:36:38 -0400
changeset 5904 a21d78fc998c
parent 5535 96594ac5fd1a
child 5969 3a041d215edc
permissions -rw-r--r--
Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 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
slouken@4866
   105
SDL_CreateThread(SDL_ThreadFunction fn, 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
 */
slouken@1330
   114
#define SDL_CreateThread(fn, data) SDL_CreateThread(fn, 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
 */
slouken@1330
   121
#define SDL_CreateThread(fn, data) SDL_CreateThread(fn, 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.
slouken@3407
   128
 */
slouken@1895
   129
extern DECLSPEC SDL_Thread *SDLCALL
slouken@4866
   130
SDL_CreateThread(SDL_ThreadFunction fn, void *data);
slouken@3407
   131
icculus@1190
   132
#endif
slouken@0
   133
slouken@3407
   134
/**
slouken@3578
   135
 *  Get the thread identifier for the current thread.
slouken@3407
   136
 */
slouken@3578
   137
extern DECLSPEC SDL_threadID SDLCALL SDL_ThreadID(void);
slouken@0
   138
slouken@3407
   139
/**
slouken@3578
   140
 *  Get the thread identifier for the specified thread.
slouken@3407
   141
 *  
slouken@3407
   142
 *  Equivalent to SDL_ThreadID() if the specified thread is NULL.
slouken@0
   143
 */
slouken@3578
   144
extern DECLSPEC SDL_threadID SDLCALL SDL_GetThreadID(SDL_Thread * thread);
slouken@0
   145
slouken@3407
   146
/**
slouken@5509
   147
 *  Set the priority for the current thread
slouken@5506
   148
 */
slouken@5509
   149
extern DECLSPEC int SDLCALL SDL_SetThreadPriority(SDL_ThreadPriority priority);
slouken@5506
   150
slouken@5506
   151
/**
slouken@3407
   152
 *  Wait for a thread to finish.
slouken@3407
   153
 *  
slouken@3407
   154
 *  The return code for the thread function is placed in the area
slouken@3407
   155
 *  pointed to by \c status, if \c status is not NULL.
slouken@0
   156
 */
slouken@1895
   157
extern DECLSPEC void SDLCALL SDL_WaitThread(SDL_Thread * thread, int *status);
slouken@0
   158
slouken@0
   159
slouken@0
   160
/* Ends C function definitions when using C++ */
slouken@0
   161
#ifdef __cplusplus
slouken@1895
   162
/* *INDENT-OFF* */
slouken@0
   163
}
slouken@1895
   164
/* *INDENT-ON* */
slouken@0
   165
#endif
slouken@0
   166
#include "close_code.h"
slouken@0
   167
slouken@0
   168
#endif /* _SDL_thread_h */
slouken@1895
   169
slouken@1895
   170
/* vi: set ts=4 sw=4 expandtab: */