mirror of
https://github.com/encounter/SDL.git
synced 2025-12-21 02:39:10 +00:00
WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d4ce48ff30d1)
This commit is contained in:
@@ -36,6 +36,8 @@
|
||||
#include "windows/SDL_systhread_c.h"
|
||||
#elif SDL_THREAD_PSP
|
||||
#include "psp/SDL_systhread_c.h"
|
||||
#elif SDL_THREAD_STDCPP
|
||||
#include "stdcpp/SDL_systhread_c.h"
|
||||
#else
|
||||
#error Need thread implementation for this platform
|
||||
#include "generic/SDL_systhread_c.h"
|
||||
|
||||
183
src/thread/stdcpp/SDL_syscond.cpp
Normal file
183
src/thread/stdcpp/SDL_syscond.cpp
Normal file
@@ -0,0 +1,183 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2012 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"
|
||||
|
||||
extern "C" {
|
||||
#include "SDL_thread.h"
|
||||
}
|
||||
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <exception>
|
||||
#include <ratio>
|
||||
|
||||
#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::exception & ex) {
|
||||
SDL_SetError("unable to create C++ condition variable: %s", ex.what());
|
||||
return NULL;
|
||||
} catch (...) {
|
||||
SDL_SetError("unable to create C++ condition variable due to an unknown exception");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* Destroy a condition variable */
|
||||
extern "C"
|
||||
void
|
||||
SDL_DestroyCond(SDL_cond * cond)
|
||||
{
|
||||
if (cond) {
|
||||
try {
|
||||
delete cond;
|
||||
} catch (...) {
|
||||
// catch any and all exceptions, just in case something happens
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
try {
|
||||
cond->cpp_cond.notify_one();
|
||||
return 0;
|
||||
} catch (...) {
|
||||
// catch any and all exceptions, just in case something happens
|
||||
SDL_SetError("unable to signal C++ condition variable due to an unknown exception");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
try {
|
||||
cond->cpp_cond.notify_all();
|
||||
return 0;
|
||||
} catch (...) {
|
||||
// catch any and all exceptions, just in case something happens
|
||||
SDL_SetError("unable to broadcast C++ condition variable due to an unknown exception");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* 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::defer_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::exception & ex) {
|
||||
SDL_SetError("unable to wait on C++ condition variable: %s", ex.what());
|
||||
return -1;
|
||||
} catch (...) {
|
||||
SDL_SetError("unable to lock wait on C++ condition variable due to an unknown exception");
|
||||
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: */
|
||||
113
src/thread/stdcpp/SDL_sysmutex.cpp
Normal file
113
src/thread/stdcpp/SDL_sysmutex.cpp
Normal file
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2012 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"
|
||||
|
||||
extern "C" {
|
||||
#include "SDL_thread.h"
|
||||
#include "SDL_systhread_c.h"
|
||||
#include "SDL_log.h"
|
||||
}
|
||||
|
||||
#include <exception>
|
||||
|
||||
#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::exception & ex) {
|
||||
SDL_SetError("unable to create C++ mutex: %s", ex.what());
|
||||
return NULL;
|
||||
} catch (...) {
|
||||
SDL_SetError("unable to create C++ mutex due to an unknown exception");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* Free the mutex */
|
||||
extern "C"
|
||||
void
|
||||
SDL_DestroyMutex(SDL_mutex * mutex)
|
||||
{
|
||||
if (mutex) {
|
||||
try {
|
||||
delete mutex;
|
||||
} catch (...) {
|
||||
// catch any and all exceptions, just in case something happens
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Lock the semaphore */
|
||||
extern "C"
|
||||
int
|
||||
SDL_mutexP(SDL_mutex * mutex)
|
||||
{
|
||||
SDL_threadID threadID = SDL_ThreadID();
|
||||
DWORD realThreadID = GetCurrentThreadId();
|
||||
if (mutex == NULL) {
|
||||
SDL_SetError("Passed a NULL mutex");
|
||||
return -1;
|
||||
}
|
||||
|
||||
try {
|
||||
mutex->cpp_mutex.lock();
|
||||
return 0;
|
||||
} catch (std::exception & ex) {
|
||||
SDL_SetError("unable to lock C++ mutex: %s", ex.what());
|
||||
return -1;
|
||||
} catch (...) {
|
||||
SDL_SetError("unable to lock C++ mutex due to an unknown exception");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Unlock the mutex */
|
||||
extern "C"
|
||||
int
|
||||
SDL_mutexV(SDL_mutex * mutex)
|
||||
{
|
||||
SDL_threadID threadID = SDL_ThreadID();
|
||||
DWORD realThreadID = GetCurrentThreadId();
|
||||
if (mutex == NULL) {
|
||||
SDL_SetError("Passed a NULL mutex");
|
||||
return -1;
|
||||
}
|
||||
|
||||
try {
|
||||
mutex->cpp_mutex.unlock();
|
||||
return 0;
|
||||
} catch (...) {
|
||||
// catch any and all exceptions, just in case something happens.
|
||||
SDL_SetError("unable to unlock C++ mutex due to an unknown exception");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
30
src/thread/stdcpp/SDL_sysmutex_c.h
Normal file
30
src/thread/stdcpp/SDL_sysmutex_c.h
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2012 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: */
|
||||
124
src/thread/stdcpp/SDL_systhread.cpp
Normal file
124
src/thread/stdcpp/SDL_systhread.cpp
Normal file
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2012 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"
|
||||
|
||||
/* Thread management routines for SDL */
|
||||
|
||||
extern "C" {
|
||||
#include "SDL_thread.h"
|
||||
#include "../SDL_systhread.h"
|
||||
#include "../SDL_thread_c.h"
|
||||
#include "SDL_log.h"
|
||||
}
|
||||
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
// HACK: Mimic C++11's thread_local keyword on Visual C++ 2012 (aka. VC++ 11)
|
||||
// TODO: make sure this hack doesn't get used if and when Visual C++ supports
|
||||
// the official, 'thread_local' keyword.
|
||||
#ifdef _MSC_VER
|
||||
#define thread_local __declspec(thread)
|
||||
// Documentation for __declspec(thread) can be found online at:
|
||||
// http://msdn.microsoft.com/en-us/library/2s9wt68x.aspx
|
||||
#endif
|
||||
|
||||
static void
|
||||
RunThread(void *args)
|
||||
{
|
||||
SDL_RunThread(args);
|
||||
}
|
||||
|
||||
extern "C"
|
||||
int
|
||||
SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
|
||||
{
|
||||
try {
|
||||
std::thread cpp_thread(RunThread, args);
|
||||
thread->handle = (void *) new std::thread(std::move(cpp_thread));
|
||||
return 0;
|
||||
} catch (std::exception & ex) {
|
||||
SDL_SetError("unable to create a C++ thread: %s", ex.what());
|
||||
return -1;
|
||||
} catch (...) {
|
||||
SDL_SetError("unable to create a C++ thread due to an unknown exception");
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
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.
|
||||
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 (...) {
|
||||
// Catch any exceptions, just in case.
|
||||
// Report nothing, as SDL_WaitThread does not seem to offer a means
|
||||
// to report errors to its callers.
|
||||
}
|
||||
}
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
26
src/thread/stdcpp/SDL_systhread_c.h
Normal file
26
src/thread/stdcpp/SDL_systhread_c.h
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2012 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: */
|
||||
Reference in New Issue
Block a user