SDL standard library

Dependents:   H261_encoder

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SDL_thread.h Source File

SDL_thread.h

Go to the documentation of this file.
00001 /*
00002   Simple DirectMedia Layer
00003   Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
00004 
00005   This software is provided 'as-is', without any express or implied
00006   warranty.  In no event will the authors be held liable for any damages
00007   arising from the use of this software.
00008 
00009   Permission is granted to anyone to use this software for any purpose,
00010   including commercial applications, and to alter it and redistribute it
00011   freely, subject to the following restrictions:
00012 
00013   1. The origin of this software must not be misrepresented; you must not
00014      claim that you wrote the original software. If you use this software
00015      in a product, an acknowledgment in the product documentation would be
00016      appreciated but is not required.
00017   2. Altered source versions must be plainly marked as such, and must not be
00018      misrepresented as being the original software.
00019   3. This notice may not be removed or altered from any source distribution.
00020 */
00021 
00022 #ifndef _SDL_thread_h
00023 #define _SDL_thread_h
00024 
00025 /**
00026  *  \file SDL_thread.h
00027  *
00028  *  Header for the SDL thread management routines.
00029  */
00030 
00031 #include "SDL_stdinc.h"
00032 #include "SDL_error.h"
00033 
00034 /* Thread synchronization primitives */
00035 #include "SDL_atomic.h"
00036 #include "SDL_mutex.h"
00037 
00038 #include "begin_code.h"
00039 /* Set up for C function definitions, even when using C++ */
00040 #ifdef __cplusplus
00041 extern "C" {
00042 #endif
00043 
00044 /* The SDL thread structure, defined in SDL_thread.c */
00045 struct SDL_Thread;
00046 typedef struct SDL_Thread SDL_Thread;
00047 
00048 /* The SDL thread ID */
00049 typedef unsigned long SDL_threadID;
00050 
00051 /* Thread local storage ID, 0 is the invalid ID */
00052 typedef unsigned int SDL_TLSID;
00053 
00054 /**
00055  *  The SDL thread priority.
00056  *
00057  *  \note On many systems you require special privileges to set high priority.
00058  */
00059 typedef enum {
00060     SDL_THREAD_PRIORITY_LOW,
00061     SDL_THREAD_PRIORITY_NORMAL,
00062     SDL_THREAD_PRIORITY_HIGH
00063 } SDL_ThreadPriority;
00064 
00065 /**
00066  *  The function passed to SDL_CreateThread().
00067  *  It is passed a void* user context parameter and returns an int.
00068  */
00069 typedef int (SDLCALL * SDL_ThreadFunction) (void *data);
00070 
00071 #if defined(__WIN32__) && !defined(HAVE_LIBC)
00072 /**
00073  *  \file SDL_thread.h
00074  *
00075  *  We compile SDL into a DLL. This means, that it's the DLL which
00076  *  creates a new thread for the calling process with the SDL_CreateThread()
00077  *  API. There is a problem with this, that only the RTL of the SDL.DLL will
00078  *  be initialized for those threads, and not the RTL of the calling
00079  *  application!
00080  *
00081  *  To solve this, we make a little hack here.
00082  *
00083  *  We'll always use the caller's _beginthread() and _endthread() APIs to
00084  *  start a new thread. This way, if it's the SDL.DLL which uses this API,
00085  *  then the RTL of SDL.DLL will be used to create the new thread, and if it's
00086  *  the application, then the RTL of the application will be used.
00087  *
00088  *  So, in short:
00089  *  Always use the _beginthread() and _endthread() of the calling runtime
00090  *  library!
00091  */
00092 #define SDL_PASSED_BEGINTHREAD_ENDTHREAD
00093 #include <process.h>            /* This has _beginthread() and _endthread() defined! */
00094 
00095 typedef uintptr_t(__cdecl * pfnSDL_CurrentBeginThread) (void *, unsigned,
00096                                                         unsigned (__stdcall *
00097                                                                   func) (void
00098                                                                          *),
00099                                                         void *arg, unsigned,
00100                                                         unsigned *threadID);
00101 typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code);
00102 
00103 /**
00104  *  Create a thread.
00105  */
00106 extern DECLSPEC SDL_Thread *SDLCALL
00107 SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data,
00108                  pfnSDL_CurrentBeginThread pfnBeginThread,
00109                  pfnSDL_CurrentEndThread pfnEndThread);
00110 
00111 /**
00112  *  Create a thread.
00113  */
00114 #if defined(SDL_CreateThread) && SDL_DYNAMIC_API
00115 #undef SDL_CreateThread
00116 #define SDL_CreateThread(fn, name, data) SDL_CreateThread_REAL(fn, name, data, (pfnSDL_CurrentBeginThread)_beginthreadex, (pfnSDL_CurrentEndThread)_endthreadex)
00117 #else
00118 #define SDL_CreateThread(fn, name, data) SDL_CreateThread(fn, name, data, (pfnSDL_CurrentBeginThread)_beginthreadex, (pfnSDL_CurrentEndThread)_endthreadex)
00119 #endif
00120 
00121 #else
00122 
00123 /**
00124  *  Create a thread.
00125  *
00126  *   Thread naming is a little complicated: Most systems have very small
00127  *    limits for the string length (Haiku has 32 bytes, Linux currently has 16,
00128  *    Visual C++ 6.0 has nine!), and possibly other arbitrary rules. You'll
00129  *    have to see what happens with your system's debugger. The name should be
00130  *    UTF-8 (but using the naming limits of C identifiers is a better bet).
00131  *   There are no requirements for thread naming conventions, so long as the
00132  *    string is null-terminated UTF-8, but these guidelines are helpful in
00133  *    choosing a name:
00134  *
00135  *    http://stackoverflow.com/questions/149932/naming-conventions-for-threads
00136  *
00137  *   If a system imposes requirements, SDL will try to munge the string for
00138  *    it (truncate, etc), but the original string contents will be available
00139  *    from SDL_GetThreadName().
00140  */
00141 extern DECLSPEC SDL_Thread *SDLCALL
00142 SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data);
00143 
00144 #endif
00145 
00146 /**
00147  * Get the thread name, as it was specified in SDL_CreateThread().
00148  *  This function returns a pointer to a UTF-8 string that names the
00149  *  specified thread, or NULL if it doesn't have a name. This is internal
00150  *  memory, not to be free()'d by the caller, and remains valid until the
00151  *  specified thread is cleaned up by SDL_WaitThread().
00152  */
00153 extern DECLSPEC const char *SDLCALL SDL_GetThreadName(SDL_Thread *thread);
00154 
00155 /**
00156  *  Get the thread identifier for the current thread.
00157  */
00158 extern DECLSPEC SDL_threadID SDLCALL SDL_ThreadID(void);
00159 
00160 /**
00161  *  Get the thread identifier for the specified thread.
00162  *
00163  *  Equivalent to SDL_ThreadID() if the specified thread is NULL.
00164  */
00165 extern DECLSPEC SDL_threadID SDLCALL SDL_GetThreadID(SDL_Thread * thread);
00166 
00167 /**
00168  *  Set the priority for the current thread
00169  */
00170 extern DECLSPEC int SDLCALL SDL_SetThreadPriority(SDL_ThreadPriority priority);
00171 
00172 /**
00173  *  Wait for a thread to finish. Threads that haven't been detached will
00174  *  remain (as a "zombie") until this function cleans them up. Not doing so
00175  *  is a resource leak.
00176  *
00177  *  Once a thread has been cleaned up through this function, the SDL_Thread
00178  *  that references it becomes invalid and should not be referenced again.
00179  *  As such, only one thread may call SDL_WaitThread() on another.
00180  *
00181  *  The return code for the thread function is placed in the area
00182  *  pointed to by \c status, if \c status is not NULL.
00183  *
00184  *  You may not wait on a thread that has been used in a call to
00185  *  SDL_DetachThread(). Use either that function or this one, but not
00186  *  both, or behavior is undefined.
00187  *
00188  *  It is safe to pass NULL to this function; it is a no-op.
00189  */
00190 extern DECLSPEC void SDLCALL SDL_WaitThread(SDL_Thread * thread, int *status);
00191 
00192 /**
00193  *  A thread may be "detached" to signify that it should not remain until
00194  *  another thread has called SDL_WaitThread() on it. Detaching a thread
00195  *  is useful for long-running threads that nothing needs to synchronize
00196  *  with or further manage. When a detached thread is done, it simply
00197  *  goes away.
00198  *
00199  *  There is no way to recover the return code of a detached thread. If you
00200  *  need this, don't detach the thread and instead use SDL_WaitThread().
00201  *
00202  *  Once a thread is detached, you should usually assume the SDL_Thread isn't
00203  *  safe to reference again, as it will become invalid immediately upon
00204  *  the detached thread's exit, instead of remaining until someone has called
00205  *  SDL_WaitThread() to finally clean it up. As such, don't detach the same
00206  *  thread more than once.
00207  *
00208  *  If a thread has already exited when passed to SDL_DetachThread(), it will
00209  *  stop waiting for a call to SDL_WaitThread() and clean up immediately.
00210  *  It is not safe to detach a thread that might be used with SDL_WaitThread().
00211  *
00212  *  You may not call SDL_WaitThread() on a thread that has been detached.
00213  *  Use either that function or this one, but not both, or behavior is
00214  *  undefined.
00215  *
00216  *  It is safe to pass NULL to this function; it is a no-op.
00217  */
00218 extern DECLSPEC void SDLCALL SDL_DetachThread(SDL_Thread * thread);
00219 
00220 /**
00221  *  \brief Create an identifier that is globally visible to all threads but refers to data that is thread-specific.
00222  *
00223  *  \return The newly created thread local storage identifier, or 0 on error
00224  *
00225  *  \code
00226  *  static SDL_SpinLock tls_lock;
00227  *  static SDL_TLSID thread_local_storage;
00228  * 
00229  *  void SetMyThreadData(void *value)
00230  *  {
00231  *      if (!thread_local_storage) {
00232  *          SDL_AtomicLock(&tls_lock);
00233  *          if (!thread_local_storage) {
00234  *              thread_local_storage = SDL_TLSCreate();
00235  *          }
00236  *          SDL_AtomicUnLock(&tls_lock);
00237  *      }
00238  *      SDL_TLSSet(thread_local_storage, value);
00239  *  }
00240  *  
00241  *  void *GetMyThreadData(void)
00242  *  {
00243  *      return SDL_TLSGet(thread_local_storage);
00244  *  }
00245  *  \endcode
00246  *
00247  *  \sa SDL_TLSGet()
00248  *  \sa SDL_TLSSet()
00249  */
00250 extern DECLSPEC SDL_TLSID SDLCALL SDL_TLSCreate(void);
00251 
00252 /**
00253  *  \brief Get the value associated with a thread local storage ID for the current thread.
00254  *
00255  *  \param id The thread local storage ID
00256  *
00257  *  \return The value associated with the ID for the current thread, or NULL if no value has been set.
00258  *
00259  *  \sa SDL_TLSCreate()
00260  *  \sa SDL_TLSSet()
00261  */
00262 extern DECLSPEC void * SDLCALL SDL_TLSGet(SDL_TLSID id);
00263 
00264 /**
00265  *  \brief Set the value associated with a thread local storage ID for the current thread.
00266  *
00267  *  \param id The thread local storage ID
00268  *  \param value The value to associate with the ID for the current thread
00269  *  \param destructor A function called when the thread exits, to free the value.
00270  *
00271  *  \return 0 on success, -1 on error
00272  *
00273  *  \sa SDL_TLSCreate()
00274  *  \sa SDL_TLSGet()
00275  */
00276 extern DECLSPEC int SDLCALL SDL_TLSSet(SDL_TLSID id, const void *value, void (*destructor)(void*));
00277 
00278 
00279 /* Ends C function definitions when using C++ */
00280 #ifdef __cplusplus
00281 }
00282 #endif
00283 #include "close_code.h"
00284 
00285 #endif /* _SDL_thread_h */
00286 
00287 /* vi: set ts=4 sw=4 expandtab: */