2 Simple DirectMedia Layer
3 Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
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.
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:
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.
28 * Header for the SDL thread management routines.
31 #include "SDL_stdinc.h"
32 #include "SDL_error.h"
34 /* Thread synchronization primitives */
35 #include "SDL_atomic.h"
36 #include "SDL_mutex.h"
38 #include "begin_code.h"
39 /* Set up for C function definitions, even when using C++ */
44 /* The SDL thread structure, defined in SDL_thread.c */
46 typedef struct SDL_Thread SDL_Thread
;
48 /* The SDL thread ID */
49 typedef unsigned long SDL_threadID
;
51 /* Thread local storage ID, 0 is the invalid ID */
52 typedef unsigned int SDL_TLSID
;
54 /* The SDL thread priority
56 * Note: On many systems you require special privileges to set high priority.
59 SDL_THREAD_PRIORITY_LOW
,
60 SDL_THREAD_PRIORITY_NORMAL
,
61 SDL_THREAD_PRIORITY_HIGH
64 /* The function passed to SDL_CreateThread()
65 It is passed a void* user context parameter and returns an int.
67 typedef int (SDLCALL
* SDL_ThreadFunction
) (void *data
);
69 #if defined(__WIN32__) && !defined(HAVE_LIBC)
73 * We compile SDL into a DLL. This means, that it's the DLL which
74 * creates a new thread for the calling process with the SDL_CreateThread()
75 * API. There is a problem with this, that only the RTL of the SDL.DLL will
76 * be initialized for those threads, and not the RTL of the calling
79 * To solve this, we make a little hack here.
81 * We'll always use the caller's _beginthread() and _endthread() APIs to
82 * start a new thread. This way, if it's the SDL.DLL which uses this API,
83 * then the RTL of SDL.DLL will be used to create the new thread, and if it's
84 * the application, then the RTL of the application will be used.
87 * Always use the _beginthread() and _endthread() of the calling runtime
90 #define SDL_PASSED_BEGINTHREAD_ENDTHREAD
91 #include <process.h> /* This has _beginthread() and _endthread() defined! */
93 typedef uintptr_t(__cdecl
* pfnSDL_CurrentBeginThread
) (void *, unsigned,
99 typedef void (__cdecl
* pfnSDL_CurrentEndThread
) (unsigned code
);
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
);
112 #define SDL_CreateThread(fn, name, data) SDL_CreateThread(fn, name, data, (pfnSDL_CurrentBeginThread)_beginthreadex, (pfnSDL_CurrentEndThread)_endthreadex)
119 * Thread naming is a little complicated: Most systems have very small
120 * limits for the string length (BeOS has 32 bytes, Linux currently has 16,
121 * Visual C++ 6.0 has nine!), and possibly other arbitrary rules. You'll
122 * have to see what happens with your system's debugger. The name should be
123 * UTF-8 (but using the naming limits of C identifiers is a better bet).
124 * There are no requirements for thread naming conventions, so long as the
125 * string is null-terminated UTF-8, but these guidelines are helpful in
128 * http://stackoverflow.com/questions/149932/naming-conventions-for-threads
130 * If a system imposes requirements, SDL will try to munge the string for
131 * it (truncate, etc), but the original string contents will be available
132 * from SDL_GetThreadName().
134 extern DECLSPEC SDL_Thread
*SDLCALL
135 SDL_CreateThread(SDL_ThreadFunction fn
, const char *name
, void *data
);
140 * Get the thread name, as it was specified in SDL_CreateThread().
141 * This function returns a pointer to a UTF-8 string that names the
142 * specified thread, or NULL if it doesn't have a name. This is internal
143 * memory, not to be free()'d by the caller, and remains valid until the
144 * specified thread is cleaned up by SDL_WaitThread().
146 extern DECLSPEC
const char *SDLCALL
SDL_GetThreadName(SDL_Thread
*thread
);
149 * Get the thread identifier for the current thread.
151 extern DECLSPEC SDL_threadID SDLCALL
SDL_ThreadID(void);
154 * Get the thread identifier for the specified thread.
156 * Equivalent to SDL_ThreadID() if the specified thread is NULL.
158 extern DECLSPEC SDL_threadID SDLCALL
SDL_GetThreadID(SDL_Thread
* thread
);
161 * Set the priority for the current thread
163 extern DECLSPEC
int SDLCALL
SDL_SetThreadPriority(SDL_ThreadPriority priority
);
166 * Wait for a thread to finish.
168 * The return code for the thread function is placed in the area
169 * pointed to by \c status, if \c status is not NULL.
171 extern DECLSPEC
void SDLCALL
SDL_WaitThread(SDL_Thread
* thread
, int *status
);
174 * \brief Create an identifier that is globally visible to all threads but refers to data that is thread-specific.
176 * \return The newly created thread local storage identifier, or 0 on error
179 * static SDL_SpinLock tls_lock;
180 * static SDL_TLSID thread_local_storage;
182 * void SetMyThreadData(void *value)
184 * if (!thread_local_storage) {
185 * SDL_AtomicLock(&tls_lock);
186 * if (!thread_local_storage) {
187 * thread_local_storage = SDL_TLSCreate();
189 * SDL_AtomicUnLock(&tls_lock);
191 * SDL_TLSSet(thread_local_storage, value);
194 * void *GetMyThreadData(void)
196 * return SDL_TLSGet(thread_local_storage);
203 extern DECLSPEC SDL_TLSID SDLCALL
SDL_TLSCreate(void);
206 * \brief Get the value associated with a thread local storage ID for the current thread.
208 * \param id The thread local storage ID
210 * \return The value associated with the ID for the current thread, or NULL if no value has been set.
212 * \sa SDL_TLSCreate()
215 extern DECLSPEC
void * SDLCALL
SDL_TLSGet(SDL_TLSID id
);
218 * \brief Set the value associated with a thread local storage ID for the current thread.
220 * \param id The thread local storage ID
221 * \param value The value to associate with the ID for the current thread
222 * \param destructor A function called when the thread exits, to free the value.
224 * \return 0 on success, -1 on error
226 * \sa SDL_TLSCreate()
229 extern DECLSPEC
int SDLCALL
SDL_TLSSet(SDL_TLSID id
, const void *value
, void (*destructor
)(void*));
232 /* Ends C function definitions when using C++ */
236 #include "close_code.h"
238 #endif /* _SDL_thread_h */
240 /* vi: set ts=4 sw=4 expandtab: */