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
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 
    22 #ifndef _SDL_thread_h
    23 #define _SDL_thread_h
    24 
    25 /**
    26  *  \file SDL_thread.h
    27  *
    28  *  Header for the SDL thread management routines.
    29  */
    30 
    31 #include "SDL_stdinc.h"
    32 #include "SDL_error.h"
    33 
    34 /* Thread synchronization primitives */
    35 #include "SDL_mutex.h"
    36 
    37 #include "begin_code.h"
    38 /* Set up for C function definitions, even when using C++ */
    39 #ifdef __cplusplus
    40 extern "C" {
    41 #endif
    42 
    43 /* The SDL thread structure, defined in SDL_thread.c */
    44 struct SDL_Thread;
    45 typedef struct SDL_Thread SDL_Thread;
    46 
    47 /* The SDL thread ID */
    48 typedef unsigned long SDL_threadID;
    49 
    50 /* The SDL thread priority
    51  *
    52  * Note: On many systems you require special privileges to set high priority.
    53  */
    54 typedef enum {
    55     SDL_THREAD_PRIORITY_LOW,
    56     SDL_THREAD_PRIORITY_NORMAL,
    57     SDL_THREAD_PRIORITY_HIGH
    58 } SDL_ThreadPriority;
    59 
    60 /* The function passed to SDL_CreateThread()
    61    It is passed a void* user context parameter and returns an int.
    62  */
    63 typedef int (SDLCALL * SDL_ThreadFunction) (void *data);
    64 
    65 #if defined(__WIN32__) && !defined(HAVE_LIBC)
    66 /**
    67  *  \file SDL_thread.h
    68  *
    69  *  We compile SDL into a DLL. This means, that it's the DLL which
    70  *  creates a new thread for the calling process with the SDL_CreateThread()
    71  *  API. There is a problem with this, that only the RTL of the SDL.DLL will
    72  *  be initialized for those threads, and not the RTL of the calling
    73  *  application!
    74  *
    75  *  To solve this, we make a little hack here.
    76  *
    77  *  We'll always use the caller's _beginthread() and _endthread() APIs to
    78  *  start a new thread. This way, if it's the SDL.DLL which uses this API,
    79  *  then the RTL of SDL.DLL will be used to create the new thread, and if it's
    80  *  the application, then the RTL of the application will be used.
    81  *
    82  *  So, in short:
    83  *  Always use the _beginthread() and _endthread() of the calling runtime
    84  *  library!
    85  */
    86 #define SDL_PASSED_BEGINTHREAD_ENDTHREAD
    87 #include <process.h>            /* This has _beginthread() and _endthread() defined! */
    88 
    89 typedef uintptr_t(__cdecl * pfnSDL_CurrentBeginThread) (void *, unsigned,
    90                                                         unsigned (__stdcall *
    91                                                                   func) (void
    92                                                                          *),
    93                                                         void *arg, unsigned,
    94                                                         unsigned *threadID);
    95 typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code);
    96 
    97 /**
    98  *  Create a thread.
    99  */
   100 extern DECLSPEC SDL_Thread *SDLCALL
   101 SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data,
   102                  pfnSDL_CurrentBeginThread pfnBeginThread,
   103                  pfnSDL_CurrentEndThread pfnEndThread);
   104 
   105 /**
   106  *  Create a thread.
   107  */
   108 #define SDL_CreateThread(fn, name, data) SDL_CreateThread(fn, name, data, _beginthreadex, _endthreadex)
   109 
   110 #else
   111 
   112 /**
   113  *  Create a thread.
   114  *
   115  *   Thread naming is a little complicated: Most systems have very small
   116  *    limits for the string length (BeOS has 32 bytes, Linux currently has 16,
   117  *    Visual C++ 6.0 has nine!), and possibly other arbitrary rules. You'll
   118  *    have to see what happens with your system's debugger. The name should be
   119  *    UTF-8 (but using the naming limits of C identifiers is a better bet).
   120  *   There are no requirements for thread naming conventions, so long as the
   121  *    string is null-terminated UTF-8, but these guidelines are helpful in
   122  *    choosing a name:
   123  *
   124  *    http://stackoverflow.com/questions/149932/naming-conventions-for-threads
   125  *
   126  *   If a system imposes requirements, SDL will try to munge the string for
   127  *    it (truncate, etc), but the original string contents will be available
   128  *    from SDL_GetThreadName().
   129  */
   130 extern DECLSPEC SDL_Thread *SDLCALL
   131 SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data);
   132 
   133 #endif
   134 
   135 /**
   136  * Get the thread name, as it was specified in SDL_CreateThread().
   137  *  This function returns a pointer to a UTF-8 string that names the
   138  *  specified thread, or NULL if it doesn't have a name. This is internal
   139  *  memory, not to be free()'d by the caller, and remains valid until the
   140  *  specified thread is cleaned up by SDL_WaitThread().
   141  */
   142 extern DECLSPEC const char *SDLCALL SDL_GetThreadName(SDL_Thread *thread);
   143 
   144 /**
   145  *  Get the thread identifier for the current thread.
   146  */
   147 extern DECLSPEC SDL_threadID SDLCALL SDL_ThreadID(void);
   148 
   149 /**
   150  *  Get the thread identifier for the specified thread.
   151  *
   152  *  Equivalent to SDL_ThreadID() if the specified thread is NULL.
   153  */
   154 extern DECLSPEC SDL_threadID SDLCALL SDL_GetThreadID(SDL_Thread * thread);
   155 
   156 /**
   157  *  Set the priority for the current thread
   158  */
   159 extern DECLSPEC int SDLCALL SDL_SetThreadPriority(SDL_ThreadPriority priority);
   160 
   161 /**
   162  *  Wait for a thread to finish.
   163  *
   164  *  The return code for the thread function is placed in the area
   165  *  pointed to by \c status, if \c status is not NULL.
   166  */
   167 extern DECLSPEC void SDLCALL SDL_WaitThread(SDL_Thread * thread, int *status);
   168 
   169 
   170 /* Ends C function definitions when using C++ */
   171 #ifdef __cplusplus
   172 }
   173 #endif
   174 #include "close_code.h"
   175 
   176 #endif /* _SDL_thread_h */
   177 
   178 /* vi: set ts=4 sw=4 expandtab: */