node_bindings_win.cc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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/system/sys_info.h"
  7. namespace electron {
  8. NodeBindingsWin::NodeBindingsWin(BrowserEnvironment browser_env)
  9. : NodeBindings(browser_env) {
  10. auto* const event_loop = uv_loop();
  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 event_loop has just been initialized
  15. // which makes iocp replacement safe
  16. CHECK_EQ(0u, event_loop->active_handles);
  17. CHECK_EQ(0u, event_loop->active_reqs.count);
  18. if (event_loop->iocp && event_loop->iocp != INVALID_HANDLE_VALUE)
  19. CloseHandle(event_loop->iocp);
  20. event_loop->iocp =
  21. CreateIoCompletionPort(INVALID_HANDLE_VALUE, nullptr, 0, 2);
  22. }
  23. }
  24. void NodeBindingsWin::PollEvents() {
  25. auto* const event_loop = uv_loop();
  26. // If there are other kinds of events pending, uv_backend_timeout will
  27. // instruct us not to wait.
  28. DWORD bytes, timeout;
  29. ULONG_PTR key;
  30. OVERLAPPED* overlapped;
  31. timeout = uv_backend_timeout(event_loop);
  32. GetQueuedCompletionStatus(event_loop->iocp, &bytes, &key, &overlapped,
  33. timeout);
  34. // Give the event back so libuv can deal with it.
  35. if (overlapped != nullptr)
  36. PostQueuedCompletionStatus(event_loop->iocp, bytes, key, overlapped);
  37. }
  38. // static
  39. std::unique_ptr<NodeBindings> NodeBindings::Create(BrowserEnvironment env) {
  40. return std::make_unique<NodeBindingsWin>(env);
  41. }
  42. } // namespace electron