]> khamidou — GitHub mirrors - electron.git/commitdiff
linux: Implement libuv message loop polling.
authorCheng Zhao <zcbenz@gmail.com>
Fri, 21 Feb 2014 05:21:02 +0000 (13:21 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 21 Feb 2014 05:21:02 +0000 (13:21 +0800)
common/node_bindings_linux.cc
common/node_bindings_linux.h

index 4f8ad9903a31e75eb523fba51ad7c18355483f0f..035441c8dc377125b231dc95c461964f2ce22406 100644 (file)
@@ -4,16 +4,49 @@
 
 #include "common/node_bindings_linux.h"
 
+#include <sys/epoll.h>
+
 namespace atom {
 
 NodeBindingsLinux::NodeBindingsLinux(bool is_browser)
-    : NodeBindings(is_browser) {
+    : NodeBindings(is_browser),
+      epoll_(epoll_create(1)) {
+  int backend_fd = uv_backend_fd(uv_loop_);
+  struct epoll_event ev = { 0 };
+  ev.events = EPOLLIN;
+  ev.data.fd = backend_fd;
+  epoll_ctl(epoll_, EPOLL_CTL_ADD, backend_fd, &ev);
 }
 
 NodeBindingsLinux::~NodeBindingsLinux() {
 }
 
+void NodeBindingsLinux::RunMessageLoop() {
+  // Get notified when libuv's watcher queue changes.
+  uv_loop_->data = this;
+  uv_loop_->on_watcher_queue_updated = OnWatcherQueueChanged;
+
+  NodeBindings::RunMessageLoop();
+}
+
+// static
+void NodeBindingsLinux::OnWatcherQueueChanged(uv_loop_t* loop) {
+  NodeBindingsLinux* self = static_cast<NodeBindingsLinux*>(loop->data);
+
+  // We need to break the io polling in the epoll thread when loop's watcher
+  // queue changes, otherwise new events cannot be notified.
+  self->WakeupEmbedThread();
+}
+
 void NodeBindingsLinux::PollEvents() {
+  int timeout = uv_backend_timeout(uv_loop_);
+
+  // Wait for new libuv events.
+  int r;
+  do {
+    struct epoll_event ev;
+    r = epoll_wait(epoll_, &ev, 1, timeout);
+  } while (r == -1 && errno == EINTR);
 }
 
 // static
index b4a294144c4f56cd278c13ec2265bfe764ce9a03..83711543d80f517bdee636d3da1781f4ff8be477 100644 (file)
@@ -15,9 +15,17 @@ class NodeBindingsLinux : public NodeBindings {
   explicit NodeBindingsLinux(bool is_browser);
   virtual ~NodeBindingsLinux();
 
+  virtual void RunMessageLoop() OVERRIDE;
+
  private:
+  // Called when uv's watcher queue changes.
+  static void OnWatcherQueueChanged(uv_loop_t* loop);
+
   virtual void PollEvents() OVERRIDE;
 
+  // Epoll to poll for uv's backend fd.
+  int epoll_;
+
   DISALLOW_COPY_AND_ASSIGN(NodeBindingsLinux);
 };