node_bindings_mac.cc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright (c) 2013 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_mac.h"
  5. #include <errno.h>
  6. #include <sys/select.h>
  7. #include <sys/sysctl.h>
  8. #include <sys/time.h>
  9. #include <sys/types.h>
  10. #include "atom/common/node_includes.h"
  11. namespace atom {
  12. NodeBindingsMac::NodeBindingsMac(BrowserEnvironment browser_env)
  13. : NodeBindings(browser_env) {}
  14. NodeBindingsMac::~NodeBindingsMac() {}
  15. void NodeBindingsMac::RunMessageLoop() {
  16. // Get notified when libuv's watcher queue changes.
  17. uv_loop_->data = this;
  18. uv_loop_->on_watcher_queue_updated = OnWatcherQueueChanged;
  19. NodeBindings::RunMessageLoop();
  20. }
  21. // static
  22. void NodeBindingsMac::OnWatcherQueueChanged(uv_loop_t* loop) {
  23. NodeBindingsMac* self = static_cast<NodeBindingsMac*>(loop->data);
  24. // We need to break the io polling in the kqueue thread when loop's watcher
  25. // queue changes, otherwise new events cannot be notified.
  26. self->WakeupEmbedThread();
  27. }
  28. void NodeBindingsMac::PollEvents() {
  29. struct timeval tv;
  30. int timeout = uv_backend_timeout(uv_loop_);
  31. if (timeout != -1) {
  32. tv.tv_sec = timeout / 1000;
  33. tv.tv_usec = (timeout % 1000) * 1000;
  34. }
  35. fd_set readset;
  36. int fd = uv_backend_fd(uv_loop_);
  37. FD_ZERO(&readset);
  38. FD_SET(fd, &readset);
  39. // Wait for new libuv events.
  40. int r;
  41. do {
  42. r = select(fd + 1, &readset, nullptr, nullptr,
  43. timeout == -1 ? nullptr : &tv);
  44. } while (r == -1 && errno == EINTR);
  45. }
  46. // static
  47. NodeBindings* NodeBindings::Create(BrowserEnvironment browser_env) {
  48. return new NodeBindingsMac(browser_env);
  49. }
  50. } // namespace atom