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