processor.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * Copyright (c) 2020 Paul-Louis Ageneau
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef RTC_PROCESSOR_H
  19. #define RTC_PROCESSOR_H
  20. #include "include.hpp"
  21. #include "init.hpp"
  22. #include "threadpool.hpp"
  23. #include <condition_variable>
  24. #include <future>
  25. #include <memory>
  26. #include <mutex>
  27. #include <queue>
  28. namespace rtc {
  29. // Processed tasks in order by delegating them to the thread pool
  30. class Processor final {
  31. public:
  32. Processor() = default;
  33. ~Processor();
  34. Processor(const Processor &) = delete;
  35. Processor &operator=(const Processor &) = delete;
  36. Processor(Processor &&) = delete;
  37. Processor &operator=(Processor &&) = delete;
  38. void join();
  39. template <class F, class... Args>
  40. auto enqueue(F &&f, Args &&... args) -> invoke_future_t<F, Args...>;
  41. protected:
  42. void schedule();
  43. // Keep an init token
  44. const init_token mInitToken = Init::Token();
  45. std::queue<std::function<void()>> mTasks;
  46. bool mPending = false; // true iff a task is pending in the thread pool
  47. mutable std::mutex mMutex;
  48. std::condition_variable mCondition;
  49. };
  50. template <class F, class... Args>
  51. auto Processor::enqueue(F &&f, Args &&... args) -> invoke_future_t<F, Args...> {
  52. std::unique_lock lock(mMutex);
  53. using R = std::invoke_result_t<std::decay_t<F>, std::decay_t<Args>...>;
  54. auto task = std::make_shared<std::packaged_task<R()>>(
  55. std::bind(std::forward<F>(f), std::forward<Args>(args)...));
  56. std::future<R> result = task->get_future();
  57. auto bundle = [this, task = std::move(task)]() {
  58. try {
  59. (*task)();
  60. } catch (const std::exception &e) {
  61. PLOG_WARNING << "Unhandled exception in task: " << e.what();
  62. }
  63. schedule(); // chain the next task
  64. };
  65. if (!mPending) {
  66. ThreadPool::Instance().enqueue(std::move(bundle));
  67. mPending = true;
  68. } else {
  69. mTasks.emplace(std::move(bundle));
  70. }
  71. return result;
  72. }
  73. } // namespace rtc
  74. #endif