Semaphore.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. void* GetOSHandle();
  20. private:
  21. SemaphoreImpl* m_Semaphore;
  22. };
  23. class SemaphoreHandle : public Handle
  24. {
  25. public:
  26. SemaphoreHandle(Semaphore* semaphore) : m_Semaphore(semaphore) {}
  27. virtual ~SemaphoreHandle() { delete m_Semaphore; }
  28. virtual bool Wait() { m_Semaphore->Wait(true); return true; }
  29. virtual bool Wait(uint32_t ms) { return m_Semaphore->Wait(ms, true) != kWaitStatusTimeout; }
  30. virtual WaitStatus Wait(bool interruptible) { return m_Semaphore->Wait(interruptible); }
  31. virtual WaitStatus Wait(uint32_t ms, bool interruptible) { return m_Semaphore->Wait(ms, interruptible); }
  32. virtual void Signal() { m_Semaphore->Post(1, NULL); }
  33. virtual void* GetOSHandle() { return m_Semaphore->GetOSHandle(); }
  34. Semaphore& Get() { return *m_Semaphore; }
  35. private:
  36. Semaphore* m_Semaphore;
  37. };
  38. }
  39. }