node_bindings_win.cc 1.6 KB

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