node_bindings_mac.cc 1.6 KB

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