WrappedSemaphore.cpp 643 B

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