2015-06-21 17:33:46 +02:00
|
|
|
/*
|
|
|
|
|
Simple DirectMedia Layer
|
2023-01-09 09:41:41 -08:00
|
|
|
Copyright (C) 1997-2023 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
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
#include "SDL_systhread_c.h"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#include <system_error>
|
|
|
|
|
|
|
|
|
|
#include "SDL_sysmutex_c.h"
|
|
|
|
|
#include <Windows.h>
|
|
|
|
|
|
|
|
|
|
/* Create a mutex */
|
2023-04-28 07:31:12 -07:00
|
|
|
extern "C" SDL_Mutex *
|
2015-06-21 17:33:46 +02:00
|
|
|
SDL_CreateMutex(void)
|
|
|
|
|
{
|
|
|
|
|
/* Allocate and initialize the mutex */
|
|
|
|
|
try {
|
2023-04-28 07:31:12 -07:00
|
|
|
SDL_Mutex *mutex = new SDL_Mutex;
|
2015-06-21 17:33:46 +02:00
|
|
|
return mutex;
|
2022-11-30 12:51:59 -08:00
|
|
|
} catch (std::system_error &ex) {
|
2015-06-21 17:33:46 +02:00
|
|
|
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 */
|
2022-11-30 12:51:59 -08:00
|
|
|
extern "C" void
|
2023-04-28 07:31:12 -07:00
|
|
|
SDL_DestroyMutex(SDL_Mutex *mutex)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2022-11-29 17:30:03 +01:00
|
|
|
if (mutex != NULL) {
|
2015-06-21 17:33:46 +02:00
|
|
|
delete mutex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-13 14:03:40 -08:00
|
|
|
/* Lock the mutex */
|
2022-11-30 12:51:59 -08:00
|
|
|
extern "C" int
|
2023-04-28 07:31:12 -07:00
|
|
|
SDL_LockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
|
if (mutex == NULL) {
|
2022-12-13 14:03:40 -08:00
|
|
|
return 0;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
mutex->cpp_mutex.lock();
|
|
|
|
|
return 0;
|
2022-11-30 12:51:59 -08:00
|
|
|
} catch (std::system_error &ex) {
|
2022-01-17 16:26:02 +01:00
|
|
|
return SDL_SetError("unable to lock a C++ mutex: code=%d; %s", ex.code(), ex.what());
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* TryLock the mutex */
|
2023-04-28 07:31:12 -07:00
|
|
|
int SDL_TryLockMutex(SDL_Mutex *mutex)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
|
int retval = 0;
|
2022-12-13 14:03:40 -08:00
|
|
|
|
2015-06-21 17:33:46 +02:00
|
|
|
if (mutex == NULL) {
|
2022-12-13 14:03:40 -08:00
|
|
|
return 0;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mutex->cpp_mutex.try_lock() == false) {
|
|
|
|
|
retval = SDL_MUTEX_TIMEDOUT;
|
|
|
|
|
}
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Unlock the mutex */
|
2022-11-30 12:51:59 -08:00
|
|
|
extern "C" int
|
2023-04-28 07:31:12 -07:00
|
|
|
SDL_UnlockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
|
if (mutex == NULL) {
|
2022-12-13 14:03:40 -08:00
|
|
|
return 0;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mutex->cpp_mutex.unlock();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-04-24 01:07:59 -04:00
|
|
|
|