node_bindings_win.cc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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/system/sys_info.h"
  8. namespace atom {
  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. NodeBindingsWin::~NodeBindingsWin() {}
  24. void NodeBindingsWin::PollEvents() {
  25. // If there are other kinds of events pending, uv_backend_timeout will
  26. // instruct us not to wait.
  27. DWORD bytes, timeout;
  28. ULONG_PTR key;
  29. OVERLAPPED* overlapped;
  30. timeout = uv_backend_timeout(uv_loop_);
  31. GetQueuedCompletionStatus(uv_loop_->iocp, &bytes, &key, &overlapped, timeout);
  32. // Give the event back so libuv can deal with it.
  33. if (overlapped != NULL)
  34. PostQueuedCompletionStatus(uv_loop_->iocp, bytes, key, overlapped);
  35. }
  36. // static
  37. NodeBindings* NodeBindings::Create(BrowserEnvironment browser_env) {
  38. return new NodeBindingsWin(browser_env);
  39. }
  40. } // namespace atom