暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SemaphoreImpl.h 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #pragma once
  2. #if (IL2CPP_THREADS_PTHREAD || IL2CPP_THREADS_WIN32)
  3. #include "os/Generic/WaitObject.h"
  4. #include "os/ErrorCodes.h"
  5. #include "os/WaitStatus.h"
  6. #include <stdint.h>
  7. namespace il2cpp
  8. {
  9. namespace os
  10. {
  11. class SemaphoreImpl : public WaitObject
  12. {
  13. public:
  14. SemaphoreImpl(int32_t initialValue, int32_t maximumValue)
  15. : WaitObject(kSemaphore)
  16. , m_MaximumValue(maximumValue)
  17. {
  18. m_Count = initialValue;
  19. }
  20. ~SemaphoreImpl()
  21. {
  22. }
  23. bool Post(int32_t releaseCount, int32_t* previousCount)
  24. {
  25. uint32_t oldCount;
  26. {
  27. WaitObject::ReleaseOnDestroy lock(m_Mutex);
  28. oldCount = m_Count;
  29. // Make sure we stay within range. Account for 32bit overflow.
  30. if (static_cast<uint64_t>(oldCount) + releaseCount > m_MaximumValue)
  31. return false;
  32. m_Count += releaseCount;
  33. WakeupOneThread();
  34. }
  35. if (previousCount)
  36. *previousCount = oldCount;
  37. return true;
  38. }
  39. protected:
  40. uint32_t m_MaximumValue;
  41. };
  42. }
  43. }
  44. #endif