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
|
|
|
|
|
|
|
|
/* Thread management routines for SDL */
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
#include "../SDL_thread_c.h"
|
|
|
|
|
#include "../SDL_systhread.h"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
|
#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
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
extern "C" int
|
2024-05-21 01:46:48 -04:00
|
|
|
SDL_SYS_CreateThread(SDL_Thread *thread,
|
|
|
|
|
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.
|
2020-03-26 22:14:59 -04:00
|
|
|
std::thread cpp_thread(RunThread, thread);
|
2022-11-30 12:51:59 -08:00
|
|
|
thread->handle = (void *)new std::thread(std::move(cpp_thread));
|
2015-06-21 17:33:46 +02:00
|
|
|
return 0;
|
2022-11-30 12:51:59 -08:00
|
|
|
} catch (std::system_error &ex) {
|
2022-01-17 17:22:30 +01:00
|
|
|
return SDL_SetError("unable to start a C++ thread: code=%d; %s", ex.code(), 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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
extern "C" void
|
2015-06-21 17:33:46 +02:00
|
|
|
SDL_SYS_SetupThread(const char *name)
|
|
|
|
|
{
|
|
|
|
|
// Make sure a thread ID gets assigned ASAP, for debugging purposes:
|
2024-01-18 04:57:12 -08:00
|
|
|
SDL_GetCurrentThreadID();
|
2015-06-21 17:33:46 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 04:57:12 -08:00
|
|
|
extern "C" SDL_ThreadID
|
|
|
|
|
SDL_GetCurrentThreadID(void)
|
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
|
|
|
return GetCurrentThreadId();
|
|
|
|
|
#else
|
Fix remaining typos (#7921)
* Fix remaining typos
Found via `codespell -q 3 -S *.hex,*.pdf,./src/libm,./src/hidapi,./src/stdlib/SDL_malloc.c,./src/video/x11/edid.h -L caf,currenty,datas,einstance,fo,hda,lod,mata,parm,parms,pevent,pevents,pixelx,requestor,ser,statics,te,texturers,thid,uscaled,windowz`
2023-07-03 15:46:47 -04:00
|
|
|
// HACK: Mimic a thread ID, if one isn't otherwise available.
|
2024-01-18 04:57:12 -08:00
|
|
|
static thread_local SDL_ThreadID current_thread_id = 0;
|
|
|
|
|
static SDL_ThreadID next_thread_id = 1;
|
2015-06-21 17:33:46 +02:00
|
|
|
static std::mutex next_thread_id_mutex;
|
|
|
|
|
|
|
|
|
|
if (current_thread_id == 0) {
|
|
|
|
|
std::lock_guard<std::mutex> lock(next_thread_id_mutex);
|
|
|
|
|
current_thread_id = next_thread_id;
|
|
|
|
|
++next_thread_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return current_thread_id;
|
2022-11-23 10:41:43 -08:00
|
|
|
#endif
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
extern "C" int
|
2015-06-21 17:33:46 +02:00
|
|
|
SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
|
|
|
|
|
{
|
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()");
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
#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
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08: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;
|
2015-06-21 17:33:46 +02:00
|
|
|
if (cpp_thread->joinable()) {
|
|
|
|
|
cpp_thread->join();
|
|
|
|
|
}
|
|
|
|
|
} 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!
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08: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;
|
2015-06-21 17:33:46 +02:00
|
|
|
if (cpp_thread->joinable()) {
|
|
|
|
|
cpp_thread->detach();
|
|
|
|
|
}
|
|
|
|
|
} 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!
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
extern "C" SDL_TLSData *
|
2016-11-16 22:08:51 +01:00
|
|
|
SDL_SYS_GetTLSData(void)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
|
return SDL_Generic_GetTLSData();
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 12:51:59 -08:00
|
|
|
extern "C" int
|
2015-06-21 17:33:46 +02:00
|
|
|
SDL_SYS_SetTLSData(SDL_TLSData *data)
|
|
|
|
|
{
|
|
|
|
|
return SDL_Generic_SetTLSData(data);
|
|
|
|
|
}
|