Semaphore.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2023 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/Core/Semaphore.h>
  6. #ifdef JPH_PLATFORM_WINDOWS
  7. JPH_SUPPRESS_WARNING_PUSH
  8. JPH_MSVC_SUPPRESS_WARNING(5039) // winbase.h(13179): warning C5039: 'TpSetCallbackCleanupGroup': pointer or reference to potentially throwing function passed to 'extern "C"' function under -EHc. Undefined behavior may occur if this function throws an exception.
  9. #ifndef WIN32_LEAN_AND_MEAN
  10. #define WIN32_LEAN_AND_MEAN
  11. #endif
  12. #ifndef JPH_COMPILER_MINGW
  13. #include <Windows.h>
  14. #else
  15. #include <windows.h>
  16. #endif
  17. JPH_SUPPRESS_WARNING_POP
  18. #endif
  19. JPH_NAMESPACE_BEGIN
  20. Semaphore::Semaphore()
  21. {
  22. #ifdef JPH_PLATFORM_WINDOWS
  23. mSemaphore = CreateSemaphore(nullptr, 0, INT_MAX, nullptr);
  24. #endif
  25. }
  26. Semaphore::~Semaphore()
  27. {
  28. #ifdef JPH_PLATFORM_WINDOWS
  29. CloseHandle(mSemaphore);
  30. #endif
  31. }
  32. void Semaphore::Release(uint inNumber)
  33. {
  34. JPH_ASSERT(inNumber > 0);
  35. #ifdef JPH_PLATFORM_WINDOWS
  36. int old_value = mCount.fetch_add(inNumber);
  37. if (old_value < 0)
  38. {
  39. int new_value = old_value + (int)inNumber;
  40. int num_to_release = min(new_value, 0) - old_value;
  41. ::ReleaseSemaphore(mSemaphore, num_to_release, nullptr);
  42. }
  43. #else
  44. std::lock_guard lock(mLock);
  45. mCount.fetch_add(inNumber, std::memory_order_relaxed);
  46. if (inNumber > 1)
  47. mWaitVariable.notify_all();
  48. else
  49. mWaitVariable.notify_one();
  50. #endif
  51. }
  52. void Semaphore::Acquire(uint inNumber)
  53. {
  54. JPH_ASSERT(inNumber > 0);
  55. #ifdef JPH_PLATFORM_WINDOWS
  56. int old_value = mCount.fetch_sub(inNumber);
  57. int new_value = old_value - (int)inNumber;
  58. if (new_value < 0)
  59. {
  60. int num_to_acquire = min(old_value, 0) - new_value;
  61. for (int i = 0; i < num_to_acquire; ++i)
  62. WaitForSingleObject(mSemaphore, INFINITE);
  63. }
  64. #else
  65. std::unique_lock lock(mLock);
  66. mCount.fetch_sub(inNumber, std::memory_order_relaxed);
  67. mWaitVariable.wait(lock, [this]() { return mCount >= 0; });
  68. #endif
  69. }
  70. JPH_NAMESPACE_END