Explorar o código

CEventLoop: Consolidate gettimeofday() syscalls.

Andreas Kling %!s(int64=6) %!d(string=hai) anos
pai
achega
8e0611201e
Modificáronse 2 ficheiros con 13 adicións e 10 borrados
  1. 11 8
      LibCore/CEventLoop.cpp
  2. 2 2
      LibCore/CEventLoop.h

+ 11 - 8
LibCore/CEventLoop.cpp

@@ -170,16 +170,19 @@ void CEventLoop::wait_for_event()
         ASSERT_NOT_REACHED();
         ASSERT_NOT_REACHED();
     }
     }
 
 
+    timeval now;
+    gettimeofday(&now, nullptr);
+
     for (auto& it : *s_timers) {
     for (auto& it : *s_timers) {
         auto& timer = *it.value;
         auto& timer = *it.value;
-        if (!timer.has_expired())
+        if (!timer.has_expired(now))
             continue;
             continue;
 #ifdef CEVENTLOOP_DEBUG
 #ifdef CEVENTLOOP_DEBUG
         dbgprintf("CEventLoop: Timer %d has expired, sending CTimerEvent to %p\n", timer.timer_id, timer.owner);
         dbgprintf("CEventLoop: Timer %d has expired, sending CTimerEvent to %p\n", timer.timer_id, timer.owner);
 #endif
 #endif
         post_event(*timer.owner, make<CTimerEvent>(timer.timer_id));
         post_event(*timer.owner, make<CTimerEvent>(timer.timer_id));
         if (timer.should_reload) {
         if (timer.should_reload) {
-            timer.reload();
+            timer.reload(now);
         } else {
         } else {
             // FIXME: Support removing expired timers that don't want to reload.
             // FIXME: Support removing expired timers that don't want to reload.
             ASSERT_NOT_REACHED();
             ASSERT_NOT_REACHED();
@@ -200,16 +203,14 @@ void CEventLoop::wait_for_event()
     process_file_descriptors_after_select(rfds);
     process_file_descriptors_after_select(rfds);
 }
 }
 
 
-bool CEventLoop::EventLoopTimer::has_expired() const
+bool CEventLoop::EventLoopTimer::has_expired(const timeval& now) const
 {
 {
-    timeval now;
-    gettimeofday(&now, nullptr);
     return now.tv_sec > fire_time.tv_sec || (now.tv_sec == fire_time.tv_sec && now.tv_usec >= fire_time.tv_usec);
     return now.tv_sec > fire_time.tv_sec || (now.tv_sec == fire_time.tv_sec && now.tv_usec >= fire_time.tv_usec);
 }
 }
 
 
-void CEventLoop::EventLoopTimer::reload()
+void CEventLoop::EventLoopTimer::reload(const timeval& now)
 {
 {
-    gettimeofday(&fire_time, nullptr);
+    fire_time = now;
     fire_time.tv_sec += interval / 1000;
     fire_time.tv_sec += interval / 1000;
     fire_time.tv_usec += (interval % 1000) * 1000;
     fire_time.tv_usec += (interval % 1000) * 1000;
 }
 }
@@ -232,7 +233,9 @@ int CEventLoop::register_timer(CObject& object, int milliseconds, bool should_re
     auto timer = make<EventLoopTimer>();
     auto timer = make<EventLoopTimer>();
     timer->owner = object.make_weak_ptr();
     timer->owner = object.make_weak_ptr();
     timer->interval = milliseconds;
     timer->interval = milliseconds;
-    timer->reload();
+    timeval now;
+    gettimeofday(&now, nullptr);
+    timer->reload(now);
     timer->should_reload = should_reload;
     timer->should_reload = should_reload;
     int timer_id = ++s_next_timer_id;  // FIXME: This will eventually wrap around.
     int timer_id = ++s_next_timer_id;  // FIXME: This will eventually wrap around.
     ASSERT(timer_id); // FIXME: Aforementioned wraparound.
     ASSERT(timer_id); // FIXME: Aforementioned wraparound.

+ 2 - 2
LibCore/CEventLoop.h

@@ -65,8 +65,8 @@ private:
         bool should_reload { false };
         bool should_reload { false };
         WeakPtr<CObject> owner;
         WeakPtr<CObject> owner;
 
 
-        void reload();
-        bool has_expired() const;
+        void reload(const timeval& now);
+        bool has_expired(const timeval& now) const;
     };
     };
 
 
     static HashMap<int, OwnPtr<EventLoopTimer>>* s_timers;
     static HashMap<int, OwnPtr<EventLoopTimer>>* s_timers;