From 45c448d7107e58d5e4d2c8d234a4449ae351ff4b Mon Sep 17 00:00:00 2001
From: Avi Kivity <avi@cloudius-systems.com>
Date: Thu, 7 Feb 2013 11:39:58 +0200
Subject: [PATCH] sched: when a timer expires, also remove it from the list

Also remove other timers that expired simultaneously.
---
 core/sched.cc | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/core/sched.cc b/core/sched.cc
index e0023f797..3e317b710 100644
--- a/core/sched.cc
+++ b/core/sched.cc
@@ -6,6 +6,7 @@
 #include "drivers/clockevent.hh"
 #include "irqlock.hh"
 #include "align.hh"
+#include "drivers/clock.hh"
 
 namespace sched {
 
@@ -197,9 +198,17 @@ timer_list::timer_list()
 
 void timer_list::fired()
 {
-    timer& tmr = *_list.begin();
-    tmr._expired = true;
-    tmr._t.wake();
+    auto now = clock::get()->time();
+    auto i = _list.begin();
+    while (i != _list.end() && i->_time < now) {
+        auto j = i++;
+        j->_expired = true;
+        j->_t.wake();
+        _list.erase(j);
+    }
+    if (!_list.empty()) {
+        clock_event->set(_list.begin()->_time);
+    }
 }
 
 timer::timer(thread& t)
-- 
GitLab