crl_dispatch_async.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #pragma once
  8. #include <crl/common/crl_common_config.h>
  9. #ifdef CRL_USE_DISPATCH
  10. #include <crl/common/crl_common_utils.h>
  11. #include <type_traits>
  12. namespace crl::details {
  13. void *background_queue_dispatch();
  14. void *main_queue_dispatch();
  15. void on_queue_async(void *queue, void (*callable)(void*), void *argument);
  16. void on_queue_sync(void *queue, void (*callable)(void*), void *argument);
  17. template <
  18. typename Wrapper,
  19. typename Invoker,
  20. typename Callable,
  21. typename Return = decltype(std::declval<Callable>()())>
  22. inline void on_queue_invoke(
  23. void *queue,
  24. Invoker invoker,
  25. Callable &&callable) {
  26. using Function = std::decay_t<Callable>;
  27. if constexpr (details::is_plain_function_v<Function, Return>) {
  28. using Plain = Return(*)();
  29. static_assert(sizeof(Plain) <= sizeof(void*));
  30. const auto copy = static_cast<Plain>(callable);
  31. invoker(queue, [](void *passed) {
  32. Wrapper::Invoke([](void *passed) {
  33. const auto callable = reinterpret_cast<Plain>(passed);
  34. (*callable)();
  35. }, passed);
  36. }, reinterpret_cast<void*>(copy));
  37. } else {
  38. const auto copy = new Function(std::forward<Callable>(callable));
  39. invoker(queue, [](void *passed) {
  40. Wrapper::Invoke([](void *passed) {
  41. const auto callable = static_cast<Function*>(passed);
  42. const auto guard = details::finally([=] { delete callable; });
  43. (*callable)();
  44. }, passed);
  45. }, static_cast<void*>(copy));
  46. }
  47. }
  48. struct EmptyWrapper {
  49. template <typename Callable>
  50. static inline void Invoke(Callable &&callable, void *argument) {
  51. callable(argument);
  52. }
  53. };
  54. inline void async_plain(void (*callable)(void*), void *argument) {
  55. return on_queue_async(
  56. background_queue_dispatch(),
  57. callable,
  58. argument);
  59. }
  60. } // namespace crl::details
  61. namespace crl {
  62. template <typename Callable>
  63. inline void async(Callable &&callable) {
  64. return details::on_queue_invoke<details::EmptyWrapper>(
  65. details::background_queue_dispatch(),
  66. details::on_queue_async,
  67. std::forward<Callable>(callable));
  68. }
  69. template <typename Callable>
  70. inline void sync(Callable &&callable) {
  71. return details::on_queue_invoke<details::EmptyWrapper>(
  72. details::background_queue_dispatch(),
  73. details::on_queue_sync,
  74. std::forward<Callable>(callable));
  75. }
  76. } // namespace crl
  77. #endif // CRL_USE_DISPATCH