TimerQueue.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Function.h>
  27. #include <AK/NonnullOwnPtr.h>
  28. #include <AK/OwnPtr.h>
  29. #include <Kernel/Scheduler.h>
  30. #include <Kernel/Time/TimeManagement.h>
  31. #include <Kernel/TimerQueue.h>
  32. namespace Kernel {
  33. static TimerQueue* s_the;
  34. TimerQueue& TimerQueue::the()
  35. {
  36. if (!s_the)
  37. s_the = new TimerQueue;
  38. return *s_the;
  39. }
  40. TimerQueue::TimerQueue()
  41. {
  42. m_ticks_per_second = TimeManagement::the().ticks_per_second();
  43. }
  44. TimerId TimerQueue::add_timer(NonnullOwnPtr<Timer>&& timer)
  45. {
  46. u64 timer_expiration = timer->expires;
  47. ASSERT(timer_expiration >= g_uptime);
  48. timer->id = ++m_timer_id_count;
  49. if (m_timer_queue.is_empty()) {
  50. m_timer_queue.append(move(timer));
  51. m_next_timer_due = timer_expiration;
  52. } else {
  53. auto following_timer = m_timer_queue.find([&timer_expiration](auto& other) { return other->expires > timer_expiration; });
  54. if (following_timer.is_end()) {
  55. m_timer_queue.append(move(timer));
  56. } else {
  57. auto next_timer_needs_update = following_timer.is_begin();
  58. m_timer_queue.insert_before(following_timer, move(timer));
  59. if (next_timer_needs_update)
  60. m_next_timer_due = timer_expiration;
  61. }
  62. }
  63. return m_timer_id_count;
  64. }
  65. TimerId TimerQueue::add_timer(timeval& deadline, Function<void()>&& callback)
  66. {
  67. NonnullOwnPtr timer = make<Timer>();
  68. timer->expires = g_uptime + seconds_to_ticks(deadline.tv_sec) + microseconds_to_ticks(deadline.tv_usec);
  69. timer->callback = move(callback);
  70. return add_timer(move(timer));
  71. }
  72. bool TimerQueue::cancel_timer(TimerId id)
  73. {
  74. auto it = m_timer_queue.find([id](auto& timer) { return timer->id == id; });
  75. if (it.is_end())
  76. return false;
  77. auto was_next_timer = it.is_begin();
  78. m_timer_queue.remove(it);
  79. if (was_next_timer)
  80. update_next_timer_due();
  81. return true;
  82. }
  83. void TimerQueue::fire()
  84. {
  85. if (m_timer_queue.is_empty())
  86. return;
  87. ASSERT(m_next_timer_due == m_timer_queue.first()->expires);
  88. while (!m_timer_queue.is_empty() && g_uptime > m_timer_queue.first()->expires) {
  89. auto timer = m_timer_queue.take_first();
  90. timer->callback();
  91. }
  92. update_next_timer_due();
  93. }
  94. void TimerQueue::update_next_timer_due()
  95. {
  96. if (m_timer_queue.is_empty())
  97. m_next_timer_due = 0;
  98. else
  99. m_next_timer_due = m_timer_queue.first()->expires;
  100. }
  101. }