Semaphore.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #define WIN32_LEAN_AND_MEAN
  10. #ifndef JPH_COMPILER_MINGW
  11. #include <Windows.h>
  12. #else
  13. #include <windows.h>
  14. #endif
  15. JPH_SUPPRESS_WARNING_POP
  16. #endif
  17. JPH_NAMESPACE_BEGIN
  18. Semaphore::Semaphore()
  19. {
  20. #ifdef JPH_PLATFORM_WINDOWS
  21. mSemaphore = CreateSemaphore(nullptr, 0, INT_MAX, nullptr);
  22. #endif
  23. }
  24. Semaphore::~Semaphore()
  25. {
  26. #ifdef JPH_PLATFORM_WINDOWS
  27. CloseHandle(mSemaphore);
  28. #endif
  29. }
  30. void Semaphore::Release(uint inNumber)
  31. {
  32. JPH_ASSERT(inNumber > 0);
  33. #ifdef JPH_PLATFORM_WINDOWS
  34. int old_value = mCount.fetch_add(inNumber);
  35. if (old_value < 0)
  36. {
  37. int new_value = old_value + (int)inNumber;
  38. int num_to_release = min(new_value, 0) - old_value;
  39. ::ReleaseSemaphore(mSemaphore, num_to_release, nullptr);
  40. }
  41. #else
  42. std::lock_guard lock(mLock);
  43. mCount += (int)inNumber;
  44. if (inNumber > 1)
  45. mWaitVariable.notify_all();
  46. else
  47. mWaitVariable.notify_one();
  48. #endif
  49. }
  50. void Semaphore::Acquire(uint inNumber)
  51. {
  52. JPH_ASSERT(inNumber > 0);
  53. #ifdef JPH_PLATFORM_WINDOWS
  54. int old_value = mCount.fetch_sub(inNumber);
  55. int new_value = old_value - (int)inNumber;
  56. if (new_value < 0)
  57. {
  58. int num_to_acquire = min(old_value, 0) - new_value;
  59. for (int i = 0; i < num_to_acquire; ++i)
  60. WaitForSingleObject(mSemaphore, INFINITE);
  61. }
  62. #else
  63. std::unique_lock lock(mLock);
  64. mCount -= (int)inNumber;
  65. mWaitVariable.wait(lock, [this]() { return mCount >= 0; });
  66. #endif
  67. }
  68. JPH_NAMESPACE_END