system_context_thread.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #pragma once
  2. //
  3. // Copyright (c) 2019-2022 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 "rotor/thread/export.h"
  11. #include <chrono>
  12. #include <condition_variable>
  13. #include <list>
  14. #include <mutex>
  15. #include <thread>
  16. #if defined(_MSC_VER)
  17. #pragma warning(push)
  18. #pragma warning(disable : 4251)
  19. #endif
  20. namespace rotor {
  21. namespace thread {
  22. struct supervisor_thread_t;
  23. /** \brief intrusive pointer for thread supervisor */
  24. using supervisor_ptr_t = intrusive_ptr_t<supervisor_thread_t>;
  25. /** \struct system_context_thread_t
  26. * \brief The thread system context, for blocking operations
  27. *
  28. */
  29. struct ROTOR_THREAD_API system_context_thread_t : public system_context_t {
  30. /** \brief constructs thread system context */
  31. system_context_thread_t() noexcept;
  32. /** \brief invokes blocking execution of the supervisor
  33. *
  34. * It blocks until root supervisor shuts down.
  35. *
  36. */
  37. virtual void run() noexcept;
  38. /** \brief checks for messages from external threads and fires expired timers */
  39. void check() noexcept;
  40. protected:
  41. /** \brief an alias for monotonic clock */
  42. using clock_t = std::chrono::steady_clock;
  43. /** \struct deadline_info_t
  44. * \brief struct to keep timer handlers
  45. */
  46. struct deadline_info_t {
  47. /** \brief non-owning pointer to timer handler */
  48. timer_handler_base_t *handler;
  49. /** \brief time point, after which the timer is considered expired */
  50. clock_t::time_point deadline;
  51. };
  52. /** \brief ordered list of deadline infos (type) */
  53. using list_t = std::list<deadline_info_t>;
  54. /** \brief fires handlers for expired timers */
  55. void update_time() noexcept;
  56. /** \brief start timer implementation */
  57. void start_timer(const pt::time_duration &interval, timer_handler_base_t &handler) noexcept;
  58. /** \brief cancel timer implementation */
  59. void cancel_timer(request_id_t timer_id) noexcept;
  60. /** \brief mutex for inbound queue */
  61. std::mutex mutex;
  62. /** \brief cv for notifying about pushing messages into inbound queue */
  63. std::condition_variable cv;
  64. /** \brief current time */
  65. clock_t::time_point now;
  66. /** \brief ordered list of deadline infos */
  67. list_t timer_nodes;
  68. /** \brief whether the context is intercepting blocking (I/O) handler */
  69. bool intercepting = false;
  70. friend struct supervisor_thread_t;
  71. };
  72. /** \brief intrusive pointer type for system context thread context */
  73. using system_context_ptr_t = rotor::intrusive_ptr_t<system_context_thread_t>;
  74. } // namespace thread
  75. } // namespace rotor
  76. #if defined(_MSC_VER)
  77. #pragma warning(pop)
  78. #endif