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