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