2015-06-21 17:33:46 +02:00
|
|
|
/*
|
|
|
|
|
Simple DirectMedia Layer
|
2024-01-01 13:15:26 -08:00
|
|
|
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
|
freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
|
appreciated but is not required.
|
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
|
misrepresented as being the original software.
|
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
|
*/
|
2022-11-29 18:34:15 -08:00
|
|
|
#include "SDL_internal.h"
|
2015-06-21 17:33:46 +02:00
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// Thread management routines for SDL
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
#include "../SDL_thread_c.h"
|
|
|
|
|
#include "../SDL_systhread.h"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#include <thread>
|
|
|
|
|
#include <system_error>
|
|
|
|
|
|
2024-01-24 02:40:51 +01:00
|
|
|
#ifdef SDL_PLATFORM_WINRT
|
2022-11-23 10:41:43 -08:00
|
|
|
#include <Windows.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
static void RunThread(void *args)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2022-11-30 12:51:59 -08:00
|
|
|
SDL_RunThread((SDL_Thread *)args);
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
extern "C"
|
|
|
|
|
bool SDL_SYS_CreateThread(SDL_Thread *thread,
|
2024-05-21 01:46:48 -04:00
|
|
|
SDL_FunctionPointer pfnBeginThread,
|
|
|
|
|
SDL_FunctionPointer pfnEndThread)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
|
try {
|
2016-04-12 14:38:50 -04:00
|
|
|
// !!! FIXME: no way to set a thread stack size here.
|
2024-06-24 22:31:32 +02:00
|
|
|
thread->handle = (void *)new std::thread(RunThread, thread);
|
2024-08-22 17:33:49 -07:00
|
|
|
return true;
|
2022-11-30 12:51:59 -08:00
|
|
|
} catch (std::system_error &ex) {
|
2024-06-24 22:31:32 +02:00
|
|
|
return SDL_SetError("unable to start a C++ thread: code=%d; %s", ex.code().value(), ex.what());
|
2015-06-21 17:33:46 +02:00
|
|
|
} catch (std::bad_alloc &) {
|
2022-01-17 17:22:30 +01:00
|
|
|
return SDL_OutOfMemory();
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
extern "C"
|
|
|
|
|
void SDL_SYS_SetupThread(const char *name)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// Do nothing.
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
extern "C"
|
|
|
|
|
SDL_ThreadID SDL_GetCurrentThreadID(void)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2024-06-24 22:31:32 +02:00
|
|
|
static_assert(sizeof(std::thread::id) <= sizeof(SDL_ThreadID), "std::thread::id must not be bigger than SDL_ThreadID");
|
|
|
|
|
SDL_ThreadID thread_id{};
|
|
|
|
|
const auto cpp_thread_id = std::this_thread::get_id();
|
|
|
|
|
SDL_memcpy(&thread_id, &cpp_thread_id, sizeof(std::thread::id));
|
|
|
|
|
return thread_id;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
extern "C"
|
|
|
|
|
bool SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2024-01-24 02:40:51 +01:00
|
|
|
#ifdef SDL_PLATFORM_WINRT
|
2022-11-23 10:41:43 -08:00
|
|
|
int value;
|
|
|
|
|
|
|
|
|
|
if (priority == SDL_THREAD_PRIORITY_LOW) {
|
|
|
|
|
value = THREAD_PRIORITY_LOWEST;
|
2022-11-27 17:38:43 +01:00
|
|
|
} else if (priority == SDL_THREAD_PRIORITY_HIGH) {
|
2022-11-23 10:41:43 -08:00
|
|
|
value = THREAD_PRIORITY_HIGHEST;
|
2022-11-27 17:38:43 +01:00
|
|
|
} else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
|
2022-11-23 10:41:43 -08:00
|
|
|
// FIXME: WinRT does not support TIME_CRITICAL! -flibit
|
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_SYSTEM, "TIME_CRITICAL unsupported, falling back to HIGHEST");
|
|
|
|
|
value = THREAD_PRIORITY_HIGHEST;
|
2022-11-27 17:38:43 +01:00
|
|
|
} else {
|
2022-11-23 10:41:43 -08:00
|
|
|
value = THREAD_PRIORITY_NORMAL;
|
|
|
|
|
}
|
|
|
|
|
if (!SetThreadPriority(GetCurrentThread(), value)) {
|
|
|
|
|
return WIN_SetError("SetThreadPriority()");
|
|
|
|
|
}
|
2024-08-22 17:33:49 -07:00
|
|
|
return true;
|
2022-11-23 10:41:43 -08:00
|
|
|
#else
|
2020-03-01 12:50:42 -08:00
|
|
|
return SDL_Unsupported();
|
2022-11-23 10:41:43 -08:00
|
|
|
#endif
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
extern "C"
|
|
|
|
|
void SDL_SYS_WaitThread(SDL_Thread *thread)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2022-11-30 12:51:59 -08:00
|
|
|
if (!thread) {
|
2015-06-21 17:33:46 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2022-11-30 12:51:59 -08:00
|
|
|
std::thread *cpp_thread = (std::thread *)thread->handle;
|
2024-06-24 19:45:37 +02:00
|
|
|
if (cpp_thread) {
|
|
|
|
|
if (cpp_thread->joinable()) {
|
|
|
|
|
cpp_thread->join();
|
|
|
|
|
}
|
|
|
|
|
delete cpp_thread;
|
|
|
|
|
thread->handle = nullptr;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
} catch (std::system_error &) {
|
|
|
|
|
// An error occurred when joining the thread. SDL_WaitThread does not,
|
|
|
|
|
// however, seem to provide a means to report errors to its callers
|
|
|
|
|
// though!
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
extern "C"
|
|
|
|
|
void SDL_SYS_DetachThread(SDL_Thread *thread)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2022-11-30 12:51:59 -08:00
|
|
|
if (!thread) {
|
2015-06-21 17:33:46 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2022-11-30 12:51:59 -08:00
|
|
|
std::thread *cpp_thread = (std::thread *)thread->handle;
|
2024-06-24 19:45:37 +02:00
|
|
|
if (cpp_thread) {
|
|
|
|
|
if (cpp_thread->joinable()) {
|
|
|
|
|
cpp_thread->detach();
|
|
|
|
|
}
|
|
|
|
|
delete cpp_thread;
|
|
|
|
|
thread->handle = nullptr;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
} catch (std::system_error &) {
|
|
|
|
|
// An error occurred when detaching the thread. SDL_DetachThread does not,
|
|
|
|
|
// however, seem to provide a means to report errors to its callers
|
|
|
|
|
// though!
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-11 10:38:40 -07:00
|
|
|
static thread_local SDL_TLSData *thread_local_storage;
|
|
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
|
void SDL_SYS_InitTLSData(void)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-11 10:38:40 -07:00
|
|
|
extern "C"
|
|
|
|
|
SDL_TLSData * SDL_SYS_GetTLSData(void)
|
|
|
|
|
{
|
|
|
|
|
return thread_local_storage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern "C"
|
2024-08-22 17:33:49 -07:00
|
|
|
bool SDL_SYS_SetTLSData(SDL_TLSData *data)
|
2024-07-11 10:38:40 -07:00
|
|
|
{
|
|
|
|
|
thread_local_storage = data;
|
2024-08-22 17:33:49 -07:00
|
|
|
return true;
|
2024-07-11 10:38:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
|
void SDL_SYS_QuitTLSData(void)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
|
}
|