SDL_sysmutex.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
  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_thread.h"
  21. #include "SDL_systhread_c.h"
  22. #include "SDL_log.h"
  23. }
  24. #include <system_error>
  25. #include "SDL_sysmutex_c.h"
  26. #include <Windows.h>
  27. /* Create a mutex */
  28. extern "C"
  29. SDL_mutex *
  30. SDL_CreateMutex(void)
  31. {
  32. /* Allocate and initialize the mutex */
  33. try {
  34. SDL_mutex * mutex = new SDL_mutex;
  35. return mutex;
  36. } catch (std::system_error & ex) {
  37. SDL_SetError("unable to create a C++ mutex: code=%d; %s", ex.code(), ex.what());
  38. return NULL;
  39. } catch (std::bad_alloc &) {
  40. SDL_OutOfMemory();
  41. return NULL;
  42. }
  43. }
  44. /* Free the mutex */
  45. extern "C"
  46. void
  47. SDL_DestroyMutex(SDL_mutex * mutex)
  48. {
  49. if (mutex) {
  50. delete mutex;
  51. }
  52. }
  53. /* Lock the semaphore */
  54. extern "C"
  55. int
  56. SDL_mutexP(SDL_mutex * mutex)
  57. {
  58. if (mutex == NULL) {
  59. SDL_SetError("Passed a NULL mutex");
  60. return -1;
  61. }
  62. try {
  63. mutex->cpp_mutex.lock();
  64. return 0;
  65. } catch (std::system_error & ex) {
  66. SDL_SetError("unable to lock a C++ mutex: code=%d; %s", ex.code(), ex.what());
  67. return -1;
  68. }
  69. }
  70. /* TryLock the mutex */
  71. int
  72. SDL_TryLockMutex(SDL_mutex * mutex)
  73. {
  74. int retval = 0;
  75. if (mutex == NULL) {
  76. return SDL_SetError("Passed a NULL mutex");
  77. }
  78. if (mutex->cpp_mutex.try_lock() == false) {
  79. retval = SDL_MUTEX_TIMEDOUT;
  80. }
  81. return retval;
  82. }
  83. /* Unlock the mutex */
  84. extern "C"
  85. int
  86. SDL_mutexV(SDL_mutex * mutex)
  87. {
  88. if (mutex == NULL) {
  89. SDL_SetError("Passed a NULL mutex");
  90. return -1;
  91. }
  92. mutex->cpp_mutex.unlock();
  93. return 0;
  94. }
  95. /* vi: set ts=4 sw=4 expandtab: */