diff options
Diffstat (limited to 'source/3rd-party/SDL2/src/thread/stdcpp')
5 files changed, 499 insertions, 0 deletions
diff --git a/source/3rd-party/SDL2/src/thread/stdcpp/SDL_syscond.cpp b/source/3rd-party/SDL2/src/thread/stdcpp/SDL_syscond.cpp new file mode 100644 index 0000000..32c7c4b --- /dev/null +++ b/source/3rd-party/SDL2/src/thread/stdcpp/SDL_syscond.cpp @@ -0,0 +1,164 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org> + + 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. +*/ +#include "../../SDL_internal.h" + +extern "C" { +#include "SDL_thread.h" +} + +#include <chrono> +#include <condition_variable> +#include <ratio> +#include <system_error> + +#include "SDL_sysmutex_c.h" + +struct SDL_cond +{ + std::condition_variable_any cpp_cond; +}; + +/* Create a condition variable */ +extern "C" +SDL_cond * +SDL_CreateCond(void) +{ + /* Allocate and initialize the condition variable */ + try { + SDL_cond * cond = new SDL_cond; + return cond; + } catch (std::system_error & ex) { + SDL_SetError("unable to create a C++ condition variable: code=%d; %s", ex.code(), ex.what()); + return NULL; + } catch (std::bad_alloc &) { + SDL_OutOfMemory(); + return NULL; + } +} + +/* Destroy a condition variable */ +extern "C" +void +SDL_DestroyCond(SDL_cond * cond) +{ + if (cond) { + delete cond; + } +} + +/* Restart one of the threads that are waiting on the condition variable */ +extern "C" +int +SDL_CondSignal(SDL_cond * cond) +{ + if (!cond) { + SDL_SetError("Passed a NULL condition variable"); + return -1; + } + + cond->cpp_cond.notify_one(); + return 0; +} + +/* Restart all threads that are waiting on the condition variable */ +extern "C" +int +SDL_CondBroadcast(SDL_cond * cond) +{ + if (!cond) { + SDL_SetError("Passed a NULL condition variable"); + return -1; + } + + cond->cpp_cond.notify_all(); + return 0; +} + +/* Wait on the condition variable for at most 'ms' milliseconds. + The mutex must be locked before entering this function! + The mutex is unlocked during the wait, and locked again after the wait. + +Typical use: + +Thread A: + SDL_LockMutex(lock); + while ( ! condition ) { + SDL_CondWait(cond, lock); + } + SDL_UnlockMutex(lock); + +Thread B: + SDL_LockMutex(lock); + ... + condition = true; + ... + SDL_CondSignal(cond); + SDL_UnlockMutex(lock); + */ +extern "C" +int +SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms) +{ + if (!cond) { + SDL_SetError("Passed a NULL condition variable"); + return -1; + } + + if (!mutex) { + SDL_SetError("Passed a NULL mutex variable"); + return -1; + } + + try { + std::unique_lock<std::recursive_mutex> cpp_lock(mutex->cpp_mutex, std::adopt_lock_t()); + if (ms == SDL_MUTEX_MAXWAIT) { + cond->cpp_cond.wait( + cpp_lock + ); + cpp_lock.release(); + return 0; + } else { + auto wait_result = cond->cpp_cond.wait_for( + cpp_lock, + std::chrono::duration<Uint32, std::milli>(ms) + ); + cpp_lock.release(); + if (wait_result == std::cv_status::timeout) { + return SDL_MUTEX_TIMEDOUT; + } else { + return 0; + } + } + } catch (std::system_error & ex) { + SDL_SetError("unable to wait on a C++ condition variable: code=%d; %s", ex.code(), ex.what()); + return -1; + } +} + +/* Wait on the condition variable forever */ +extern "C" +int +SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex) +{ + return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT); +} + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/source/3rd-party/SDL2/src/thread/stdcpp/SDL_sysmutex.cpp b/source/3rd-party/SDL2/src/thread/stdcpp/SDL_sysmutex.cpp new file mode 100644 index 0000000..667d36b --- /dev/null +++ b/source/3rd-party/SDL2/src/thread/stdcpp/SDL_sysmutex.cpp @@ -0,0 +1,111 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org> + + 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. +*/ +#include "../../SDL_internal.h" + +extern "C" { +#include "SDL_thread.h" +#include "SDL_systhread_c.h" +#include "SDL_log.h" +} + +#include <system_error> + +#include "SDL_sysmutex_c.h" +#include <Windows.h> + + +/* Create a mutex */ +extern "C" +SDL_mutex * +SDL_CreateMutex(void) +{ + /* Allocate and initialize the mutex */ + try { + SDL_mutex * mutex = new SDL_mutex; + return mutex; + } catch (std::system_error & ex) { + SDL_SetError("unable to create a C++ mutex: code=%d; %s", ex.code(), ex.what()); + return NULL; + } catch (std::bad_alloc &) { + SDL_OutOfMemory(); + return NULL; + } +} + +/* Free the mutex */ +extern "C" +void +SDL_DestroyMutex(SDL_mutex * mutex) +{ + if (mutex) { + delete mutex; + } +} + +/* Lock the semaphore */ +extern "C" +int +SDL_mutexP(SDL_mutex * mutex) +{ + if (mutex == NULL) { + SDL_SetError("Passed a NULL mutex"); + return -1; + } + + try { + mutex->cpp_mutex.lock(); + return 0; + } catch (std::system_error & ex) { + SDL_SetError("unable to lock a C++ mutex: code=%d; %s", ex.code(), ex.what()); + return -1; + } +} + +/* TryLock the mutex */ +int +SDL_TryLockMutex(SDL_mutex * mutex) +{ + int retval = 0; + if (mutex == NULL) { + return SDL_SetError("Passed a NULL mutex"); + } + + if (mutex->cpp_mutex.try_lock() == false) { + retval = SDL_MUTEX_TIMEDOUT; + } + return retval; +} + +/* Unlock the mutex */ +extern "C" +int +SDL_mutexV(SDL_mutex * mutex) +{ + if (mutex == NULL) { + SDL_SetError("Passed a NULL mutex"); + return -1; + } + + mutex->cpp_mutex.unlock(); + return 0; +} + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/source/3rd-party/SDL2/src/thread/stdcpp/SDL_sysmutex_c.h b/source/3rd-party/SDL2/src/thread/stdcpp/SDL_sysmutex_c.h new file mode 100644 index 0000000..000288f --- /dev/null +++ b/source/3rd-party/SDL2/src/thread/stdcpp/SDL_sysmutex_c.h @@ -0,0 +1,30 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org> + + 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. +*/ +#include "SDL_config.h" + +#include <mutex> + +struct SDL_mutex +{ + std::recursive_mutex cpp_mutex; +}; + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/source/3rd-party/SDL2/src/thread/stdcpp/SDL_systhread.cpp b/source/3rd-party/SDL2/src/thread/stdcpp/SDL_systhread.cpp new file mode 100644 index 0000000..3020f1c --- /dev/null +++ b/source/3rd-party/SDL2/src/thread/stdcpp/SDL_systhread.cpp @@ -0,0 +1,168 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org> + + 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. +*/ +#include "../../SDL_internal.h" + +/* Thread management routines for SDL */ + +extern "C" { +#include "SDL_thread.h" +#include "../SDL_thread_c.h" +#include "../SDL_systhread.h" +#include "SDL_log.h" +} + +#include <mutex> +#include <thread> +#include <system_error> + +#ifdef __WINRT__ +#include <Windows.h> +#endif + +static void +RunThread(void *args) +{ + SDL_RunThread(args); +} + +extern "C" +int +SDL_SYS_CreateThread(SDL_Thread * thread, void *args) +{ + try { + // !!! FIXME: no way to set a thread stack size here. + std::thread cpp_thread(RunThread, args); + thread->handle = (void *) new std::thread(std::move(cpp_thread)); + return 0; + } catch (std::system_error & ex) { + SDL_SetError("unable to start a C++ thread: code=%d; %s", ex.code(), ex.what()); + return -1; + } catch (std::bad_alloc &) { + SDL_OutOfMemory(); + return -1; + } +} + +extern "C" +void +SDL_SYS_SetupThread(const char *name) +{ + // Make sure a thread ID gets assigned ASAP, for debugging purposes: + SDL_ThreadID(); + return; +} + +extern "C" +SDL_threadID +SDL_ThreadID(void) +{ +#ifdef __WINRT__ + return GetCurrentThreadId(); +#else + // HACK: Mimick a thread ID, if one isn't otherwise available. + static thread_local SDL_threadID current_thread_id = 0; + static SDL_threadID next_thread_id = 1; + 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; +#endif +} + +extern "C" +int +SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority) +{ + // Thread priorities do not look to be settable via C++11's thread + // interface, at least as of this writing (Nov 2012). std::thread does + // provide access to the OS' native handle, however, and some form of + // priority-setting could, in theory, be done through this interface. + // + // WinRT: UPDATE (Aug 20, 2013): thread priorities cannot be changed + // on WinRT, at least not for any thread that's already been created. + // WinRT threads appear to be based off of the WinRT class, + // ThreadPool, more info on which can be found at: + // http://msdn.microsoft.com/en-us/library/windows/apps/windows.system.threading.threadpool.aspx + // + // For compatibility sake, 0 will be returned here. + return (0); +} + +extern "C" +void +SDL_SYS_WaitThread(SDL_Thread * thread) +{ + if ( ! thread) { + return; + } + + try { + std::thread * cpp_thread = (std::thread *) thread->handle; + 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! + } +} + +extern "C" +void +SDL_SYS_DetachThread(SDL_Thread * thread) +{ + if ( ! thread) { + return; + } + + try { + std::thread * cpp_thread = (std::thread *) thread->handle; + 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! + } +} + +extern "C" +SDL_TLSData * +SDL_SYS_GetTLSData(void) +{ + return SDL_Generic_GetTLSData(); +} + +extern "C" +int +SDL_SYS_SetTLSData(SDL_TLSData *data) +{ + return SDL_Generic_SetTLSData(data); +} + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/source/3rd-party/SDL2/src/thread/stdcpp/SDL_systhread_c.h b/source/3rd-party/SDL2/src/thread/stdcpp/SDL_systhread_c.h new file mode 100644 index 0000000..ee4764d --- /dev/null +++ b/source/3rd-party/SDL2/src/thread/stdcpp/SDL_systhread_c.h @@ -0,0 +1,26 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org> + + 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. +*/ +#include "SDL_config.h" + +/* For a thread handle, use a void pointer to a std::thread */ +typedef void * SYS_ThreadHandle; + +/* vi: set ts=4 sw=4 expandtab: */ |