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