node_bindings_linux.cc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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(bool is_browser)
  8. : NodeBindings(is_browser),
  9. epoll_(epoll_create(1)) {
  10. int backend_fd = uv_backend_fd(uv_loop_);
  11. struct epoll_event ev = { 0 };
  12. ev.events = EPOLLIN;
  13. ev.data.fd = backend_fd;
  14. epoll_ctl(epoll_, EPOLL_CTL_ADD, backend_fd, &ev);
  15. }
  16. NodeBindingsLinux::~NodeBindingsLinux() {
  17. }
  18. void NodeBindingsLinux::RunMessageLoop() {
  19. // Get notified when libuv's watcher queue changes.
  20. uv_loop_->data = this;
  21. uv_loop_->on_watcher_queue_updated = OnWatcherQueueChanged;
  22. NodeBindings::RunMessageLoop();
  23. }
  24. // static
  25. void NodeBindingsLinux::OnWatcherQueueChanged(uv_loop_t* loop) {
  26. NodeBindingsLinux* self = static_cast<NodeBindingsLinux*>(loop->data);
  27. // We need to break the io polling in the epoll thread when loop's watcher
  28. // queue changes, otherwise new events cannot be notified.
  29. self->WakeupEmbedThread();
  30. }
  31. void NodeBindingsLinux::PollEvents() {
  32. int timeout = uv_backend_timeout(uv_loop_);
  33. // Wait for new libuv events.
  34. int r;
  35. do {
  36. struct epoll_event ev;
  37. r = epoll_wait(epoll_, &ev, 1, timeout);
  38. } while (r == -1 && errno == EINTR);
  39. }
  40. // static
  41. NodeBindings* NodeBindings::Create(bool is_browser) {
  42. return new NodeBindingsLinux(is_browser);
  43. }
  44. } // namespace atom