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