crl_dispatch_semaphore.cpp 1013 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #include <crl/dispatch/crl_dispatch_semaphore.h>
  8. #ifdef CRL_USE_DISPATCH
  9. #include <dispatch/dispatch.h>
  10. #include <exception>
  11. namespace crl {
  12. namespace {
  13. dispatch_semaphore_t Unwrap(void *value) {
  14. return static_cast<dispatch_semaphore_t>(value);
  15. }
  16. } // namespace
  17. auto semaphore::implementation::create() -> pointer {
  18. auto result = dispatch_semaphore_create(0);
  19. if (!result) {
  20. std::terminate();
  21. }
  22. return result;
  23. }
  24. void semaphore::implementation::operator()(pointer value) {
  25. if (value) {
  26. dispatch_release(Unwrap(value));
  27. }
  28. };
  29. void semaphore::acquire() {
  30. dispatch_semaphore_wait(Unwrap(_handle.get()), DISPATCH_TIME_FOREVER);
  31. }
  32. void semaphore::release() {
  33. dispatch_semaphore_signal(Unwrap(_handle.get()));
  34. }
  35. } // namespace crl
  36. #endif // CRL_USE_DISPATCH