node_bindings_win.cc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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_win.h"
  5. #include <windows.h>
  6. #include "base/logging.h"
  7. #include "base/sys_info.h"
  8. extern "C" {
  9. #include "vendor/node/deps/uv/src/win/internal.h"
  10. }
  11. namespace atom {
  12. NodeBindingsWin::NodeBindingsWin(BrowserEnvironment browser_env)
  13. : NodeBindings(browser_env) {
  14. // on single-core the io comp port NumberOfConcurrentThreads needs to be 2
  15. // to avoid cpu pegging likely caused by a busy loop in PollEvents
  16. if (base::SysInfo::NumberOfProcessors() == 1) {
  17. // the expectation is the uv_loop_ has just been initialized
  18. // which makes iocp replacement safe
  19. CHECK_EQ(0u, uv_loop_->active_handles);
  20. CHECK_EQ(0u, uv_loop_->active_reqs.count);
  21. if (uv_loop_->iocp && uv_loop_->iocp != INVALID_HANDLE_VALUE)
  22. CloseHandle(uv_loop_->iocp);
  23. uv_loop_->iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 2);
  24. }
  25. }
  26. NodeBindingsWin::~NodeBindingsWin() {}
  27. void NodeBindingsWin::PollEvents() {
  28. // If there are other kinds of events pending, uv_backend_timeout will
  29. // instruct us not to wait.
  30. DWORD bytes, timeout;
  31. ULONG_PTR key;
  32. OVERLAPPED* overlapped;
  33. timeout = uv_backend_timeout(uv_loop_);
  34. GetQueuedCompletionStatus(uv_loop_->iocp, &bytes, &key, &overlapped, timeout);
  35. // Give the event back so libuv can deal with it.
  36. if (overlapped != NULL)
  37. PostQueuedCompletionStatus(uv_loop_->iocp, bytes, key, overlapped);
  38. }
  39. // static
  40. NodeBindings* NodeBindings::Create(BrowserEnvironment browser_env) {
  41. return new NodeBindingsWin(browser_env);
  42. }
  43. } // namespace atom