SDL 3.0
SDL_thread.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2023 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 <SDL3/SDL_stdinc.h>
32#include <SDL3/SDL_error.h>
33
34/* Thread synchronization primitives */
35#include <SDL3/SDL_atomic.h>
36#include <SDL3/SDL_mutex.h>
37
38#if (defined(__WIN32__) || defined(__GDK__)) && !defined(__WINRT__)
39#include <process.h> /* _beginthreadex() and _endthreadex() */
40#endif
41
42#include <SDL3/SDL_begin_code.h>
43/* Set up for C function definitions, even when using C++ */
44#ifdef __cplusplus
45extern "C" {
46#endif
47
48/* The SDL thread structure, defined in SDL_thread.c */
49struct SDL_Thread;
50typedef struct SDL_Thread SDL_Thread;
51
52/* The SDL thread ID */
53typedef unsigned long SDL_threadID;
54
55/* Thread local storage ID, 0 is the invalid ID */
56typedef unsigned int SDL_TLSID;
57
58/**
59 * The SDL thread priority.
60 *
61 * SDL will make system changes as necessary in order to apply the thread priority.
62 * Code which attempts to control thread state related to priority should be aware
63 * that calling SDL_SetThreadPriority may alter such state.
64 * SDL_HINT_THREAD_PRIORITY_POLICY can be used to control aspects of this behavior.
65 *
66 * \note On many systems you require special privileges to set high or time critical priority.
67 */
68typedef enum {
74
75/**
76 * The function passed to SDL_CreateThread().
77 *
78 * \param data what was passed as `data` to SDL_CreateThread()
79 * \returns a value that can be reported through SDL_WaitThread().
80 */
81typedef int (SDLCALL * SDL_ThreadFunction) (void *data);
82
83
84#if (defined(__WIN32__) || defined(__GDK__)) && !defined(__WINRT__)
85/**
86 * \file SDL_thread.h
87 *
88 * We compile SDL into a DLL. This means, that it's the DLL which
89 * creates a new thread for the calling process with the SDL_CreateThread()
90 * API. There is a problem with this, that only the RTL of the SDL3.DLL will
91 * be initialized for those threads, and not the RTL of the calling
92 * application!
93 *
94 * To solve this, we make a little hack here.
95 *
96 * We'll always use the caller's _beginthread() and _endthread() APIs to
97 * start a new thread. This way, if it's the SDL3.DLL which uses this API,
98 * then the RTL of SDL3.DLL will be used to create the new thread, and if it's
99 * the application, then the RTL of the application will be used.
100 *
101 * So, in short:
102 * Always use the _beginthread() and _endthread() of the calling runtime
103 * library!
104 */
105#define SDL_PASSED_BEGINTHREAD_ENDTHREAD
106
107typedef uintptr_t (__cdecl * pfnSDL_CurrentBeginThread)
108 (void *, unsigned, unsigned (__stdcall *func)(void *),
109 void * /*arg*/, unsigned, unsigned * /* threadID */);
110typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code);
111
112#ifndef SDL_beginthread
113#define SDL_beginthread _beginthreadex
114#endif
115#ifndef SDL_endthread
116#define SDL_endthread _endthreadex
117#endif
118
119
120/*
121 * Create a SDL Thread
122 *
123 * \param fn Thread function
124 * \param name name
125 * \param data some data
126 * \param pfnSDL_CurrentBeginThread begin function
127 * \param pfnSDL_CurrentEndThread end function
128 *
129 * \returns SDL_Thread pointer
130 *
131 * \since This function is available since SDL 3.0.0.
132 */
133extern DECLSPEC SDL_Thread *SDLCALL
134SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data,
135 pfnSDL_CurrentBeginThread pfnBeginThread,
136 pfnSDL_CurrentEndThread pfnEndThread);
137
138/*
139 * Create a SDL Thread, with explicit stack size
140 *
141 * \param fn Thread function
142 * \param name name
143 * \param stacksize stack size
144 * \param data some data
145 * \param pfnSDL_CurrentBeginThread begin function
146 * \param pfnSDL_CurrentEndThread end function
147 *
148 * \returns SDL_Thread pointer
149 *
150 * \since This function is available since SDL 3.0.0.
151 */
152extern DECLSPEC SDL_Thread *SDLCALL
154 const char *name, const size_t stacksize, void *data,
155 pfnSDL_CurrentBeginThread pfnBeginThread,
156 pfnSDL_CurrentEndThread pfnEndThread);
157
158
159#if defined(SDL_CreateThread) && SDL_DYNAMIC_API
160#undef SDL_CreateThread
161#define SDL_CreateThread(fn, name, data) SDL_CreateThread_REAL(fn, name, data, (pfnSDL_CurrentBeginThread)SDL_beginthread, (pfnSDL_CurrentEndThread)SDL_endthread)
162#undef SDL_CreateThreadWithStackSize
163#define SDL_CreateThreadWithStackSize(fn, name, stacksize, data) SDL_CreateThreadWithStackSize_REAL(fn, name, stacksize, data, (pfnSDL_CurrentBeginThread)SDL_beginthread, (pfnSDL_CurrentEndThread)SDL_endthread)
164#else
165#define SDL_CreateThread(fn, name, data) SDL_CreateThread(fn, name, data, (pfnSDL_CurrentBeginThread)SDL_beginthread, (pfnSDL_CurrentEndThread)SDL_endthread)
166#define SDL_CreateThreadWithStackSize(fn, name, stacksize, data) SDL_CreateThreadWithStackSize(fn, name, stacksize, data, (pfnSDL_CurrentBeginThread)SDL_beginthread, (pfnSDL_CurrentEndThread)SDL_endthread)
167#endif
168
169#else
170
171/**
172 * Create a new thread with a default stack size.
173 *
174 * This is equivalent to calling:
175 *
176 * ```c
177 * SDL_CreateThreadWithStackSize(fn, name, 0, data);
178 * ```
179 *
180 * \param fn the SDL_ThreadFunction function to call in the new thread
181 * \param name the name of the thread
182 * \param data a pointer that is passed to `fn`
183 * \returns an opaque pointer to the new thread object on success, NULL if the
184 * new thread could not be created; call SDL_GetError() for more
185 * information.
186 *
187 * \since This function is available since SDL 3.0.0.
188 *
189 * \sa SDL_CreateThreadWithStackSize
190 * \sa SDL_WaitThread
191 */
192extern DECLSPEC SDL_Thread *SDLCALL
194
195/**
196 * Create a new thread with a specific stack size.
197 *
198 * SDL makes an attempt to report `name` to the system, so that debuggers can
199 * display it. Not all platforms support this.
200 *
201 * Thread naming is a little complicated: Most systems have very small limits
202 * for the string length (Haiku has 32 bytes, Linux currently has 16, Visual
203 * C++ 6.0 has _nine_!), and possibly other arbitrary rules. You'll have to
204 * see what happens with your system's debugger. The name should be UTF-8 (but
205 * using the naming limits of C identifiers is a better bet). There are no
206 * requirements for thread naming conventions, so long as the string is
207 * null-terminated UTF-8, but these guidelines are helpful in choosing a name:
208 *
209 * https://stackoverflow.com/questions/149932/naming-conventions-for-threads
210 *
211 * If a system imposes requirements, SDL will try to munge the string for it
212 * (truncate, etc), but the original string contents will be available from
213 * SDL_GetThreadName().
214 *
215 * The size (in bytes) of the new stack can be specified. Zero means "use the
216 * system default" which might be wildly different between platforms. x86
217 * Linux generally defaults to eight megabytes, an embedded device might be a
218 * few kilobytes instead. You generally need to specify a stack that is a
219 * multiple of the system's page size (in many cases, this is 4 kilobytes, but
220 * check your system documentation).
221 *
222 * In SDL 2.1, stack size will be folded into the original SDL_CreateThread
223 * function, but for backwards compatibility, this is currently a separate
224 * function.
225 *
226 * \param fn the SDL_ThreadFunction function to call in the new thread
227 * \param name the name of the thread
228 * \param stacksize the size, in bytes, to allocate for the new thread stack.
229 * \param data a pointer that is passed to `fn`
230 * \returns an opaque pointer to the new thread object on success, NULL if the
231 * new thread could not be created; call SDL_GetError() for more
232 * information.
233 *
234 * \since This function is available since SDL 3.0.0.
235 *
236 * \sa SDL_WaitThread
237 */
238extern DECLSPEC SDL_Thread *SDLCALL
239SDL_CreateThreadWithStackSize(SDL_ThreadFunction fn, const char *name, const size_t stacksize, void *data);
240
241#endif
242
243/**
244 * Get the thread name as it was specified in SDL_CreateThread().
245 *
246 * This is internal memory, not to be freed by the caller, and remains valid
247 * until the specified thread is cleaned up by SDL_WaitThread().
248 *
249 * \param thread the thread to query
250 * \returns a pointer to a UTF-8 string that names the specified thread, or
251 * NULL if it doesn't have a name.
252 *
253 * \since This function is available since SDL 3.0.0.
254 *
255 * \sa SDL_CreateThread
256 */
257extern DECLSPEC const char *SDLCALL SDL_GetThreadName(SDL_Thread *thread);
258
259/**
260 * Get the thread identifier for the current thread.
261 *
262 * This thread identifier is as reported by the underlying operating system.
263 * If SDL is running on a platform that does not support threads the return
264 * value will always be zero.
265 *
266 * This function also returns a valid thread ID when called from the main
267 * thread.
268 *
269 * \returns the ID of the current thread.
270 *
271 * \since This function is available since SDL 3.0.0.
272 *
273 * \sa SDL_GetThreadID
274 */
275extern DECLSPEC SDL_threadID SDLCALL SDL_ThreadID(void);
276
277/**
278 * Get the thread identifier for the specified thread.
279 *
280 * This thread identifier is as reported by the underlying operating system.
281 * If SDL is running on a platform that does not support threads the return
282 * value will always be zero.
283 *
284 * \param thread the thread to query
285 * \returns the ID of the specified thread, or the ID of the current thread if
286 * `thread` is NULL.
287 *
288 * \since This function is available since SDL 3.0.0.
289 *
290 * \sa SDL_ThreadID
291 */
292extern DECLSPEC SDL_threadID SDLCALL SDL_GetThreadID(SDL_Thread * thread);
293
294/**
295 * Set the priority for the current thread.
296 *
297 * Note that some platforms will not let you alter the priority (or at least,
298 * promote the thread to a higher priority) at all, and some require you to be
299 * an administrator account. Be prepared for this to fail.
300 *
301 * \param priority the SDL_ThreadPriority to set
302 * \returns 0 on success or a negative error code on failure; call
303 * SDL_GetError() for more information.
304 *
305 * \since This function is available since SDL 3.0.0.
306 */
307extern DECLSPEC int SDLCALL SDL_SetThreadPriority(SDL_ThreadPriority priority);
308
309/**
310 * Wait for a thread to finish.
311 *
312 * Threads that haven't been detached will remain (as a "zombie") until this
313 * function cleans them up. Not doing so is a resource leak.
314 *
315 * Once a thread has been cleaned up through this function, the SDL_Thread
316 * that references it becomes invalid and should not be referenced again. As
317 * such, only one thread may call SDL_WaitThread() on another.
318 *
319 * The return code for the thread function is placed in the area pointed to by
320 * `status`, if `status` is not NULL.
321 *
322 * You may not wait on a thread that has been used in a call to
323 * SDL_DetachThread(). Use either that function or this one, but not both, or
324 * behavior is undefined.
325 *
326 * It is safe to pass a NULL thread to this function; it is a no-op.
327 *
328 * Note that the thread pointer is freed by this function and is not valid
329 * afterward.
330 *
331 * \param thread the SDL_Thread pointer that was returned from the
332 * SDL_CreateThread() call that started this thread
333 * \param status pointer to an integer that will receive the value returned
334 * from the thread function by its 'return', or NULL to not
335 * receive such value back.
336 *
337 * \since This function is available since SDL 3.0.0.
338 *
339 * \sa SDL_CreateThread
340 * \sa SDL_DetachThread
341 */
342extern DECLSPEC void SDLCALL SDL_WaitThread(SDL_Thread * thread, int *status);
343
344/**
345 * Let a thread clean up on exit without intervention.
346 *
347 * A thread may be "detached" to signify that it should not remain until
348 * another thread has called SDL_WaitThread() on it. Detaching a thread is
349 * useful for long-running threads that nothing needs to synchronize with or
350 * further manage. When a detached thread is done, it simply goes away.
351 *
352 * There is no way to recover the return code of a detached thread. If you
353 * need this, don't detach the thread and instead use SDL_WaitThread().
354 *
355 * Once a thread is detached, you should usually assume the SDL_Thread isn't
356 * safe to reference again, as it will become invalid immediately upon the
357 * detached thread's exit, instead of remaining until someone has called
358 * SDL_WaitThread() to finally clean it up. As such, don't detach the same
359 * thread more than once.
360 *
361 * If a thread has already exited when passed to SDL_DetachThread(), it will
362 * stop waiting for a call to SDL_WaitThread() and clean up immediately. It is
363 * not safe to detach a thread that might be used with SDL_WaitThread().
364 *
365 * You may not call SDL_WaitThread() on a thread that has been detached. Use
366 * either that function or this one, but not both, or behavior is undefined.
367 *
368 * It is safe to pass NULL to this function; it is a no-op.
369 *
370 * \param thread the SDL_Thread pointer that was returned from the
371 * SDL_CreateThread() call that started this thread
372 *
373 * \since This function is available since SDL 3.0.0.
374 *
375 * \sa SDL_CreateThread
376 * \sa SDL_WaitThread
377 */
378extern DECLSPEC void SDLCALL SDL_DetachThread(SDL_Thread * thread);
379
380/**
381 * Create a piece of thread-local storage.
382 *
383 * This creates an identifier that is globally visible to all threads but
384 * refers to data that is thread-specific.
385 *
386 * \returns the newly created thread local storage identifier or 0 on error.
387 *
388 * \since This function is available since SDL 3.0.0.
389 *
390 * \sa SDL_TLSGet
391 * \sa SDL_TLSSet
392 */
393extern DECLSPEC SDL_TLSID SDLCALL SDL_TLSCreate(void);
394
395/**
396 * Get the current thread's value associated with a thread local storage ID.
397 *
398 * \param id the thread local storage ID
399 * \returns the value associated with the ID for the current thread or NULL if
400 * no value has been set; call SDL_GetError() for more information.
401 *
402 * \since This function is available since SDL 3.0.0.
403 *
404 * \sa SDL_TLSCreate
405 * \sa SDL_TLSSet
406 */
407extern DECLSPEC void * SDLCALL SDL_TLSGet(SDL_TLSID id);
408
409/**
410 * Set the current thread's value associated with a thread local storage ID.
411 *
412 * The function prototype for `destructor` is:
413 *
414 * ```c
415 * void destructor(void *value)
416 * ```
417 *
418 * where its parameter `value` is what was passed as `value` to SDL_TLSSet().
419 *
420 * \param id the thread local storage ID
421 * \param value the value to associate with the ID for the current thread
422 * \param destructor a function called when the thread exits, to free the
423 * value
424 * \returns 0 on success or a negative error code on failure; call
425 * SDL_GetError() for more information.
426 *
427 * \since This function is available since SDL 3.0.0.
428 *
429 * \sa SDL_TLSCreate
430 * \sa SDL_TLSGet
431 */
432extern DECLSPEC int SDLCALL SDL_TLSSet(SDL_TLSID id, const void *value, void (SDLCALL *destructor)(void*));
433
434/**
435 * Cleanup all TLS data for this thread.
436 *
437 * \since This function is available since SDL 3.0.0.
438 */
439extern DECLSPEC void SDLCALL SDL_TLSCleanup(void);
440
441/* Ends C function definitions when using C++ */
442#ifdef __cplusplus
443}
444#endif
445#include <SDL3/SDL_close_code.h>
446
447#endif /* SDL_thread_h_ */
unsigned long uintptr_t
typedef void(APIENTRYP PFNGLDRAWRANGEELEMENTSPROC)(GLenum mode
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1967
GLenum func
GLuint const GLchar * name
GLsizei const GLfloat * value
void * SDL_TLSGet(SDL_TLSID id)
unsigned int SDL_TLSID
Definition: SDL_thread.h:56
void SDL_DetachThread(SDL_Thread *thread)
SDL_threadID SDL_GetThreadID(SDL_Thread *thread)
const char * SDL_GetThreadName(SDL_Thread *thread)
SDL_TLSID SDL_TLSCreate(void)
SDL_Thread * SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data)
void SDL_TLSCleanup(void)
struct SDL_Thread SDL_Thread
Definition: SDL_thread.h:50
int SDL_SetThreadPriority(SDL_ThreadPriority priority)
void SDL_WaitThread(SDL_Thread *thread, int *status)
unsigned long SDL_threadID
Definition: SDL_thread.h:53
int SDL_TLSSet(SDL_TLSID id, const void *value, void(*destructor)(void *))
SDL_ThreadPriority
Definition: SDL_thread.h:68
@ SDL_THREAD_PRIORITY_TIME_CRITICAL
Definition: SDL_thread.h:72
@ SDL_THREAD_PRIORITY_LOW
Definition: SDL_thread.h:69
@ SDL_THREAD_PRIORITY_HIGH
Definition: SDL_thread.h:71
@ SDL_THREAD_PRIORITY_NORMAL
Definition: SDL_thread.h:70
int(* SDL_ThreadFunction)(void *data)
Definition: SDL_thread.h:81
SDL_threadID SDL_ThreadID(void)
SDL_Thread * SDL_CreateThreadWithStackSize(SDL_ThreadFunction fn, const char *name, const size_t stacksize, void *data)