crl_dispatch_queue.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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_queue.h>
  8. #if defined CRL_USE_DISPATCH && !defined CRL_FORCE_COMMON_QUEUE
  9. #include <dispatch/dispatch.h>
  10. #include <exception>
  11. namespace crl {
  12. namespace {
  13. dispatch_queue_t Unwrap(void *value) {
  14. return static_cast<dispatch_queue_t>(value);
  15. }
  16. } // namespace
  17. auto queue::implementation::create() -> pointer {
  18. auto result = dispatch_queue_create(nullptr, DISPATCH_QUEUE_SERIAL);
  19. if (!result) {
  20. std::terminate();
  21. }
  22. return result;
  23. }
  24. void queue::implementation::operator()(pointer value) {
  25. if (value) {
  26. dispatch_release(Unwrap(value));
  27. }
  28. };
  29. queue::queue() : _handle(implementation::create()) {
  30. }
  31. void queue::async_plain(void (*callable)(void*), void *argument) {
  32. dispatch_async_f(
  33. Unwrap(_handle.get()),
  34. argument,
  35. callable);
  36. }
  37. void queue::sync_plain(void (*callable)(void*), void *argument) {
  38. dispatch_sync_f(
  39. Unwrap(_handle.get()),
  40. argument,
  41. callable);
  42. }
  43. } // namespace crl
  44. #endif // CRL_USE_DISPATCH && !CRL_FORCE_COMMON_QUEUE