node_bindings_linux.cc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 "shell/common/node_bindings_linux.h"
  5. #include <sys/epoll.h>
  6. namespace electron {
  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() = default;
  16. void NodeBindingsLinux::PrepareMessageLoop() {
  17. int handle = uv_backend_fd(uv_loop_);
  18. // If the backend fd hasn't changed, don't proceed.
  19. if (handle == handle_)
  20. return;
  21. NodeBindings::PrepareMessageLoop();
  22. }
  23. void NodeBindingsLinux::RunMessageLoop() {
  24. int handle = uv_backend_fd(uv_loop_);
  25. // If the backend fd hasn't changed, don't proceed.
  26. if (handle == handle_)
  27. return;
  28. handle_ = handle;
  29. // Get notified when libuv's watcher queue changes.
  30. uv_loop_->data = this;
  31. uv_loop_->on_watcher_queue_updated = OnWatcherQueueChanged;
  32. NodeBindings::RunMessageLoop();
  33. }
  34. // static
  35. void NodeBindingsLinux::OnWatcherQueueChanged(uv_loop_t* loop) {
  36. NodeBindingsLinux* self = static_cast<NodeBindingsLinux*>(loop->data);
  37. // We need to break the io polling in the epoll thread when loop's watcher
  38. // queue changes, otherwise new events cannot be notified.
  39. self->WakeupEmbedThread();
  40. }
  41. void NodeBindingsLinux::PollEvents() {
  42. int timeout = uv_backend_timeout(uv_loop_);
  43. // Wait for new libuv events.
  44. int r;
  45. do {
  46. struct epoll_event ev;
  47. r = epoll_wait(epoll_, &ev, 1, timeout);
  48. } while (r == -1 && errno == EINTR);
  49. }
  50. // static
  51. NodeBindings* NodeBindings::Create(BrowserEnvironment browser_env) {
  52. return new NodeBindingsLinux(browser_env);
  53. }
  54. } // namespace electron