system_context_thread.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #pragma once
  2. //
  3. // Copyright (c) 2019-2021 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  4. //
  5. // Distributed under the MIT Software License
  6. //
  7. #include "rotor/arc.hpp"
  8. #include "rotor/system_context.h"
  9. #include "rotor/timer_handler.hpp"
  10. #include <chrono>
  11. #include <condition_variable>
  12. #include <list>
  13. #include <mutex>
  14. #include <thread>
  15. namespace rotor {
  16. namespace thread {
  17. struct supervisor_thread_t;
  18. /** \brief intrusive pointer for thread supervisor */
  19. using supervisor_ptr_t = intrusive_ptr_t<supervisor_thread_t>;
  20. /** \struct system_context_thread_t
  21. * \brief The thread system context, for blocking operations
  22. *
  23. */
  24. struct system_context_thread_t : public system_context_t {
  25. /** \brief constructs thread system context
  26. *
  27. * \param queue_size defines pre-allocated ibound queue size for messages from other threads
  28. *
  29. * \param poll_time how much time it will spend in polling inbound queue before switching into
  30. * notification mode (i.e. cv + mutex)
  31. */
  32. system_context_thread_t() noexcept;
  33. /** \brief invokes blocking execution of the supervisor
  34. *
  35. * It blocks until root supervisor shuts down.
  36. *
  37. */
  38. virtual void run() noexcept;
  39. /** \brief checks for messages from external threads and fires expired timers */
  40. void check() noexcept;
  41. protected:
  42. /** \brief an alias for monotonic clock */
  43. using clock_t = std::chrono::steady_clock;
  44. /** \struct deadline_info_t
  45. * \brief struct to keep timer handlers
  46. */
  47. struct deadline_info_t {
  48. /** \brief non-owning pointer to timer handler */
  49. timer_handler_base_t *handler;
  50. /** \brief time point, after which the timer is considered expired */
  51. clock_t::time_point deadline;
  52. };
  53. /** \brief ordered list of deadline infos (type) */
  54. using list_t = std::list<deadline_info_t>;
  55. /** \brief fires handlers for expired timers */
  56. void update_time() noexcept;
  57. /** \brief start timer implementation */
  58. void start_timer(const pt::time_duration &interval, timer_handler_base_t &handler) noexcept;
  59. /** \brief cancel timer implementation */
  60. void cancel_timer(request_id_t timer_id) noexcept;
  61. /** \brief mutex for inbound queue */
  62. std::mutex mutex;
  63. /** \brief cv for notifying about pushing messages into inbound queue */
  64. std::condition_variable cv;
  65. /** \brief current time */
  66. clock_t::time_point now;
  67. /** \brief ordered list of deadline infos */
  68. list_t timer_nodes;
  69. /** \brief whether the context is intercepting blocking (I/O) handler */
  70. bool intercepting = false;
  71. friend struct supervisor_thread_t;
  72. };
  73. /** \brief intrusive pointer type for system context thread context */
  74. using system_context_ptr_t = rotor::intrusive_ptr_t<system_context_thread_t>;
  75. } // namespace thread
  76. } // namespace rotor