SDL_sysmutex.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. extern "C" {
  20. #include "SDL_systhread_c.h"
  21. }
  22. #include <system_error>
  23. #include "SDL_sysmutex_c.h"
  24. #include <windows.h>
  25. extern "C" SDL_Mutex * SDL_CreateMutex(void)
  26. {
  27. // Allocate and initialize the mutex
  28. try {
  29. SDL_Mutex *mutex = new SDL_Mutex;
  30. return mutex;
  31. } catch (std::system_error &ex) {
  32. SDL_SetError("unable to create a C++ mutex: code=%d; %s", ex.code(), ex.what());
  33. } catch (std::bad_alloc &) {
  34. SDL_OutOfMemory();
  35. }
  36. return NULL;
  37. }
  38. extern "C" void SDL_DestroyMutex(SDL_Mutex *mutex)
  39. {
  40. if (mutex != NULL) {
  41. delete mutex;
  42. }
  43. }
  44. extern "C" void SDL_LockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
  45. {
  46. if (mutex != NULL) {
  47. try {
  48. mutex->cpp_mutex.lock();
  49. } catch (std::system_error &ex) {
  50. SDL_assert(!"Error trying to lock mutex"); // assume we're in a lot of trouble if this assert fails.
  51. //return SDL_SetError("unable to lock a C++ mutex: code=%d; %s", ex.code(), ex.what());
  52. }
  53. }
  54. }
  55. extern "C" int SDL_TryLockMutex(SDL_Mutex *mutex)
  56. {
  57. return ((mutex == NULL) || mutex->cpp_mutex.try_lock()) ? 0 : SDL_MUTEX_TIMEDOUT;
  58. }
  59. /* Unlock the mutex */
  60. extern "C" void SDL_UnlockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
  61. {
  62. if (mutex != NULL) {
  63. mutex->cpp_mutex.unlock();
  64. }
  65. }