SDL_sysrwlock.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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. #include <shared_mutex>
  20. #include <system_error>
  21. #include <Windows.h>
  22. struct SDL_rwlock
  23. {
  24. std::shared_mutex cpp_mutex;
  25. SDL_threadID write_owner;
  26. };
  27. /* Create a rwlock */
  28. extern "C" SDL_rwlock *SDL_CreateRWLock(void)
  29. {
  30. /* Allocate and initialize the rwlock */
  31. try {
  32. SDL_rwlock *rwlock = new SDL_rwlock;
  33. return rwlock;
  34. } catch (std::system_error &ex) {
  35. SDL_SetError("unable to create a C++ rwlock: code=%d; %s", ex.code(), ex.what());
  36. return NULL;
  37. } catch (std::bad_alloc &) {
  38. SDL_OutOfMemory();
  39. return NULL;
  40. }
  41. }
  42. /* Free the rwlock */
  43. extern "C" void SDL_DestroyRWLock(SDL_rwlock *rwlock)
  44. {
  45. if (rwlock != NULL) {
  46. delete rwlock;
  47. }
  48. }
  49. /* Lock the rwlock */
  50. extern "C" int SDL_LockRWLockForReading(SDL_rwlock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
  51. {
  52. if (!rwlock) {
  53. return SDL_InvalidParamError("rwlock");
  54. }
  55. try {
  56. rwlock->cpp_mutex.lock_shared();
  57. return 0;
  58. } catch (std::system_error &ex) {
  59. return SDL_SetError("unable to lock a C++ rwlock: code=%d; %s", ex.code(), ex.what());
  60. }
  61. }
  62. /* Lock the rwlock for writing */
  63. extern "C" int SDL_LockRWLockForWriting(SDL_rwlock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
  64. {
  65. if (!rwlock) {
  66. return SDL_InvalidParamError("rwlock");
  67. }
  68. try {
  69. rwlock->cpp_mutex.lock();
  70. rwlock->write_owner = SDL_ThreadID();
  71. return 0;
  72. } catch (std::system_error &ex) {
  73. return SDL_SetError("unable to lock a C++ rwlock: code=%d; %s", ex.code(), ex.what());
  74. }
  75. }
  76. /* TryLock the rwlock for reading */
  77. int SDL_TryLockRWLockForReading(SDL_rwlock *rwlock)
  78. {
  79. int retval = 0;
  80. if (!rwlock) {
  81. retval = SDL_InvalidParamError("rwlock");
  82. } else if (rwlock->cpp_mutex.try_lock_shared() == false) {
  83. retval = SDL_RWLOCK_TIMEDOUT;
  84. }
  85. return retval;
  86. }
  87. /* TryLock the rwlock for writing */
  88. int SDL_TryLockRWLockForWriting(SDL_rwlock *rwlock)
  89. {
  90. int retval = 0;
  91. if (!rwlock) {
  92. retval = SDL_InvalidParamError("rwlock");
  93. } else if (rwlock->cpp_mutex.try_lock() == false) {
  94. retval = SDL_RWLOCK_TIMEDOUT;
  95. } else {
  96. rwlock->write_owner = SDL_ThreadID();
  97. }
  98. return retval;
  99. }
  100. /* Unlock the rwlock */
  101. extern "C" int
  102. SDL_UnlockRWLock(SDL_rwlock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
  103. {
  104. if (!rwlock) {
  105. return SDL_InvalidParamError("rwlock");
  106. } else if (rwlock->write_owner == SDL_ThreadID()) {
  107. rwlock->write_owner = 0;
  108. rwlock->cpp_mutex.unlock();
  109. } else {
  110. rwlock->cpp_mutex.unlock_shared();
  111. }
  112. return 0;
  113. }