Semaphore.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #pragma once
  2. #include "os/ErrorCodes.h"
  3. #include "os/WaitStatus.h"
  4. #include "os/Handle.h"
  5. #include "utils/NonCopyable.h"
  6. namespace il2cpp
  7. {
  8. namespace os
  9. {
  10. class SemaphoreImpl;
  11. class Semaphore : public il2cpp::utils::NonCopyable
  12. {
  13. public:
  14. Semaphore(int32_t initialValue = 0, int32_t maximumValue = 1);
  15. ~Semaphore();
  16. bool Post(int32_t releaseCount = 1, int32_t* previousCount = NULL);
  17. WaitStatus Wait(bool interruptible = false);
  18. WaitStatus Wait(uint32_t ms, bool interruptible = false);
  19. private:
  20. SemaphoreImpl* m_Semaphore;
  21. };
  22. class SemaphoreHandle : public Handle
  23. {
  24. public:
  25. SemaphoreHandle(Semaphore* semaphore) : m_Semaphore(semaphore) {}
  26. virtual ~SemaphoreHandle() { delete m_Semaphore; }
  27. virtual bool Wait() { m_Semaphore->Wait(true); return true; }
  28. virtual bool Wait(uint32_t ms) { return m_Semaphore->Wait(ms, true) != kWaitStatusTimeout; }
  29. virtual WaitStatus Wait(bool interruptible) { return m_Semaphore->Wait(interruptible); }
  30. virtual WaitStatus Wait(uint32_t ms, bool interruptible) { return m_Semaphore->Wait(ms, interruptible); }
  31. virtual void Signal() { m_Semaphore->Post(1, NULL); }
  32. Semaphore& Get() { return *m_Semaphore; }
  33. private:
  34. Semaphore* m_Semaphore;
  35. };
  36. }
  37. }