2
0

WrappedSemaphore.cpp 649 B

12345678910111213141516171819202122232425262728293031
  1. #include "WrappedSemaphore.h"
  2. using namespace bell;
  3. WrappedSemaphore::WrappedSemaphore(int count)
  4. {
  5. semaphoreHandle = dispatch_semaphore_create(0);
  6. }
  7. WrappedSemaphore::~WrappedSemaphore()
  8. {
  9. dispatch_release(semaphoreHandle);
  10. }
  11. int WrappedSemaphore::wait()
  12. {
  13. return dispatch_semaphore_wait(semaphoreHandle, DISPATCH_TIME_FOREVER);
  14. }
  15. int WrappedSemaphore::twait(long milliseconds)
  16. {
  17. dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, (NSEC_PER_SEC / 1000) * milliseconds);
  18. return dispatch_semaphore_wait(semaphoreHandle, timeout);
  19. }
  20. void WrappedSemaphore::give()
  21. {
  22. dispatch_semaphore_signal(semaphoreHandle);
  23. }