node_bindings_linux.cc 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright (c) 2014 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/common/node_bindings_linux.h"
  5. #include <sys/epoll.h>
  6. namespace atom {
  7. NodeBindingsLinux::NodeBindingsLinux(BrowserEnvironment browser_env)
  8. : NodeBindings(browser_env), epoll_(epoll_create(1)) {
  9. int backend_fd = uv_backend_fd(uv_loop_);
  10. struct epoll_event ev = {0};
  11. ev.events = EPOLLIN;
  12. ev.data.fd = backend_fd;
  13. epoll_ctl(epoll_, EPOLL_CTL_ADD, backend_fd, &ev);
  14. }
  15. NodeBindingsLinux::~NodeBindingsLinux() {}
  16. void NodeBindingsLinux::RunMessageLoop() {
  17. // Get notified when libuv's watcher queue changes.
  18. uv_loop_->data = this;
  19. uv_loop_->on_watcher_queue_updated = OnWatcherQueueChanged;
  20. NodeBindings::RunMessageLoop();
  21. }
  22. // static
  23. void NodeBindingsLinux::OnWatcherQueueChanged(uv_loop_t* loop) {
  24. NodeBindingsLinux* self = static_cast<NodeBindingsLinux*>(loop->data);
  25. // We need to break the io polling in the epoll thread when loop's watcher
  26. // queue changes, otherwise new events cannot be notified.
  27. self->WakeupEmbedThread();
  28. }
  29. void NodeBindingsLinux::PollEvents() {
  30. int timeout = uv_backend_timeout(uv_loop_);
  31. // Wait for new libuv events.
  32. int r;
  33. do {
  34. struct epoll_event ev;
  35. r = epoll_wait(epoll_, &ev, 1, timeout);
  36. } while (r == -1 && errno == EINTR);
  37. }
  38. // static
  39. NodeBindings* NodeBindings::Create(BrowserEnvironment browser_env) {
  40. return new NodeBindingsLinux(browser_env);
  41. }
  42. } // namespace atom