You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

104 lines
2.2 KiB

  1. #pragma once
  2. #include <set>
  3. #include <memory>
  4. #include "misc.h"
  5. namespace asyncpp {
  6. namespace timer {
  7. struct delay;
  8. struct timer;
  9. struct registration
  10. {
  11. public:
  12. struct inner
  13. {
  14. timer& owner;
  15. time_point timeout;
  16. /**
  17. * @brief Constructor.
  18. */
  19. inline inner(timer& p_owner, time_point p_timeout);
  20. };
  21. using inner_ptr_w = std::weak_ptr<inner>;
  22. public:
  23. delay& owner;
  24. inner_ptr_w ptr;
  25. /**
  26. * @brief Constructor.
  27. */
  28. inline registration(delay& p_owner);
  29. };
  30. struct timer
  31. {
  32. private:
  33. using inner_ptr_s = std::shared_ptr<registration::inner>;
  34. struct inner_less_compare
  35. { constexpr bool operator()(const inner_ptr_s& lhs, const inner_ptr_s& rhs) const; };
  36. using inner_set = std::set<inner_ptr_s, inner_less_compare>;
  37. private:
  38. inner_set _registrations;
  39. public:
  40. /**
  41. * @brief Get the number of resources assigned to this timer.
  42. */
  43. inline size_t resource_count() const;
  44. /**
  45. * @brief Set the thread local current timer.
  46. */
  47. inline void make_current();
  48. /**
  49. * @brief Set the thread local current timer.
  50. */
  51. inline void clear_current();
  52. public:
  53. /**
  54. * @brief Get the thread local timer instance.
  55. */
  56. static inline timer* current();
  57. /**
  58. * @brief Register a new resource within this timer.
  59. */
  60. static inline void register_resource(registration& p_value);
  61. /**
  62. * @brief Register a new resource within this timer.
  63. */
  64. static inline void unregister_resource(registration& p_value);
  65. private:
  66. /**
  67. * @brief Add a new resource to the timer.
  68. */
  69. inline inner_ptr_s make_inner(registration& p_value);
  70. private:
  71. struct storage
  72. {
  73. timer* current { nullptr };
  74. };
  75. /**
  76. * @brief Get the thread local storage.
  77. */
  78. static inline storage& local_storage();
  79. };
  80. } }