You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2014/12/25 22:05:57 UTC

[06/12] mesos git commit: Introduce libevent clock implementation.

Introduce libevent clock implementation.

Review: https://reviews.apache.org/r/28322


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/a027dd13
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/a027dd13
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/a027dd13

Branch: refs/heads/master
Commit: a027dd1360325ac489961f6317ba8c385dcf995c
Parents: 25d068f
Author: Joris Van Remoortere <jo...@gmail.com>
Authored: Sat Dec 20 12:35:57 2014 -0800
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Thu Dec 25 11:54:12 2014 -0800

----------------------------------------------------------------------
 3rdparty/libprocess/src/libevent.cpp | 72 ++++++++++++++++++++++++++++---
 1 file changed, 66 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/a027dd13/3rdparty/libprocess/src/libevent.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/libevent.cpp b/3rdparty/libprocess/src/libevent.cpp
index 524d811..28c2cf7 100644
--- a/3rdparty/libprocess/src/libevent.cpp
+++ b/3rdparty/libprocess/src/libevent.cpp
@@ -7,21 +7,81 @@
 
 #include "event_loop.hpp"
 #include "libevent.hpp"
+#include "synchronized.hpp"
 
 namespace process {
 
 struct event_base* base = NULL;
 
+
 void* EventLoop::run(void*)
 {
-  int result = event_base_loop(base, 0);
-  if (result < 0) {
-    LOG(FATAL) << "Failed to run event loop";
-  } else if (result == 1) {
-    VLOG(1) << "Finished running event loop due to lack of events";
+  do {
+    int result = event_base_loop(base, EVLOOP_ONCE);
+    if (result < 0) {
+      LOG(FATAL) << "Failed to run event loop";
+    } else if (result == 1) {
+      VLOG(1) << "All events handled, continuing event loop";
+      continue;
+    } else if (event_base_got_break(base)) {
+      break;
+    } else if (event_base_got_exit(base)) {
+      break;
+    }
+  } while (true);
+  return NULL;
+}
+
+
+namespace internal {
+
+struct Delay
+{
+  void(*function)(void);
+  event* timer;
+};
+
+void handle_delay(int, short, void* arg)
+{
+  Delay* delay = reinterpret_cast<Delay*>(arg);
+  delay->function();
+  delete delay;
+}
+
+}  // namespace internal {
+
+
+void EventLoop::delay(const Duration& duration, void(*function)(void))
+{
+  internal::Delay* delay = new internal::Delay();
+  delay->timer = evtimer_new(base, &internal::handle_delay, delay);
+  if (delay->timer == NULL) {
+    LOG(FATAL) << "Failed to delay, evtimer_new";
   }
 
-  return NULL;
+  delay->function = function;
+
+  timeval t{0, 0};
+  if (duration > Seconds(0)) {
+    t = duration.timeval();
+  }
+
+  evtimer_add(delay->timer, &t);
+}
+
+
+double EventLoop::time()
+{
+  // Get the cached time if running the event loop, or call
+  // gettimeofday() to get the current time. Since a lot of logic in
+  // libprocess depends on time math, we want to log fatal rather than
+  // cause logic errors if the time fails.
+  timeval t;
+  if (event_base_gettimeofday_cached(base, &t) < 0) {
+    LOG(FATAL) << "Failed to get time, event_base_gettimeofday_cached";
+  }
+
+  return Duration(t).secs();
 }