TimerQueue.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Function.h>
  8. #include <AK/IntrusiveList.h>
  9. #include <AK/NonnullRefPtr.h>
  10. #include <AK/OwnPtr.h>
  11. #include <AK/RefCounted.h>
  12. #include <AK/Time.h>
  13. #include <Kernel/Time/TimeManagement.h>
  14. namespace Kernel {
  15. TYPEDEF_DISTINCT_ORDERED_ID(u64, TimerId);
  16. class Timer : public RefCounted<Timer> {
  17. friend class TimerQueue;
  18. public:
  19. void setup(clockid_t clock_id, Time expires, Function<void()>&& callback)
  20. {
  21. VERIFY(!is_queued());
  22. m_clock_id = clock_id;
  23. m_expires = expires;
  24. m_callback = move(callback);
  25. }
  26. ~Timer()
  27. {
  28. VERIFY(!is_queued());
  29. }
  30. Time remaining() const;
  31. private:
  32. TimerId m_id;
  33. clockid_t m_clock_id;
  34. Time m_expires;
  35. Time m_remaining {};
  36. Function<void()> m_callback;
  37. Atomic<bool> m_cancelled { false };
  38. Atomic<bool> m_callback_finished { false };
  39. Atomic<bool> m_in_use { false };
  40. bool operator<(const Timer& rhs) const
  41. {
  42. return m_expires < rhs.m_expires;
  43. }
  44. bool operator>(const Timer& rhs) const
  45. {
  46. return m_expires > rhs.m_expires;
  47. }
  48. bool operator==(const Timer& rhs) const
  49. {
  50. return m_id == rhs.m_id;
  51. }
  52. void clear_cancelled() { return m_cancelled.store(false, AK::memory_order_release); }
  53. bool set_cancelled() { return m_cancelled.exchange(true, AK::memory_order_acq_rel); }
  54. bool is_in_use() { return m_in_use.load(AK::memory_order_acquire); };
  55. void set_in_use() { m_in_use.store(true, AK::memory_order_release); }
  56. void clear_in_use() { return m_in_use.store(false, AK::memory_order_release); }
  57. bool is_callback_finished() const { return m_callback_finished.load(AK::memory_order_acquire); }
  58. void clear_callback_finished() { m_callback_finished.store(false, AK::memory_order_release); }
  59. void set_callback_finished() { m_callback_finished.store(true, AK::memory_order_release); }
  60. Time now(bool) const;
  61. bool is_queued() const { return m_list_node.is_in_list(); }
  62. public:
  63. IntrusiveListNode<Timer> m_list_node;
  64. using List = IntrusiveList<Timer, RawPtr<Timer>, &Timer::m_list_node>;
  65. };
  66. class TimerQueue {
  67. friend class Timer;
  68. public:
  69. TimerQueue();
  70. static TimerQueue& the();
  71. TimerId add_timer(NonnullRefPtr<Timer>&&);
  72. bool add_timer_without_id(NonnullRefPtr<Timer>, clockid_t, const Time&, Function<void()>&&);
  73. TimerId add_timer(clockid_t, const Time& timeout, Function<void()>&& callback);
  74. bool cancel_timer(TimerId id);
  75. bool cancel_timer(Timer& timer, bool* was_in_use = nullptr);
  76. bool cancel_timer(NonnullRefPtr<Timer>&& timer)
  77. {
  78. return cancel_timer(*move(timer));
  79. }
  80. void fire();
  81. private:
  82. struct Queue {
  83. Timer::List list;
  84. Time next_timer_due {};
  85. };
  86. void remove_timer_locked(Queue&, Timer&);
  87. void update_next_timer_due(Queue&);
  88. void add_timer_locked(NonnullRefPtr<Timer>);
  89. Queue& queue_for_timer(Timer& timer)
  90. {
  91. switch (timer.m_clock_id) {
  92. case CLOCK_MONOTONIC:
  93. case CLOCK_MONOTONIC_COARSE:
  94. case CLOCK_MONOTONIC_RAW:
  95. return m_timer_queue_monotonic;
  96. case CLOCK_REALTIME:
  97. case CLOCK_REALTIME_COARSE:
  98. return m_timer_queue_realtime;
  99. default:
  100. VERIFY_NOT_REACHED();
  101. }
  102. }
  103. u64 m_timer_id_count { 0 };
  104. u64 m_ticks_per_second { 0 };
  105. Queue m_timer_queue_monotonic;
  106. Queue m_timer_queue_realtime;
  107. Timer::List m_timers_executing;
  108. };
  109. }