2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-07-22 08:05:35 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/node_bindings_win.h"
|
2013-07-22 08:05:35 +00:00
|
|
|
|
2013-07-22 08:43:58 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include "base/logging.h"
|
2019-01-10 18:14:04 +00:00
|
|
|
#include "base/system/sys_info.h"
|
2013-07-23 11:19:11 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2013-07-22 08:05:35 +00:00
|
|
|
|
2017-03-08 08:33:44 +00:00
|
|
|
NodeBindingsWin::NodeBindingsWin(BrowserEnvironment browser_env)
|
2018-12-11 02:07:35 +00:00
|
|
|
: NodeBindings(browser_env) {
|
2023-09-07 23:25:17 +00:00
|
|
|
auto* const event_loop = uv_loop();
|
|
|
|
|
2018-12-11 02:07:35 +00:00
|
|
|
// on single-core the io comp port NumberOfConcurrentThreads needs to be 2
|
|
|
|
// to avoid cpu pegging likely caused by a busy loop in PollEvents
|
|
|
|
if (base::SysInfo::NumberOfProcessors() == 1) {
|
2023-09-07 23:25:17 +00:00
|
|
|
// the expectation is the event_loop has just been initialized
|
2018-12-11 02:07:35 +00:00
|
|
|
// which makes iocp replacement safe
|
2023-09-07 23:25:17 +00:00
|
|
|
CHECK_EQ(0u, event_loop->active_handles);
|
|
|
|
CHECK_EQ(0u, event_loop->active_reqs.count);
|
2018-12-11 02:07:35 +00:00
|
|
|
|
2023-09-07 23:25:17 +00:00
|
|
|
if (event_loop->iocp && event_loop->iocp != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle(event_loop->iocp);
|
2023-10-03 19:26:35 +00:00
|
|
|
event_loop->iocp =
|
|
|
|
CreateIoCompletionPort(INVALID_HANDLE_VALUE, nullptr, 0, 2);
|
2018-12-11 02:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-22 08:05:35 +00:00
|
|
|
|
|
|
|
void NodeBindingsWin::PollEvents() {
|
2023-09-07 23:25:17 +00:00
|
|
|
auto* const event_loop = uv_loop();
|
|
|
|
|
2015-08-21 01:59:58 +00:00
|
|
|
// If there are other kinds of events pending, uv_backend_timeout will
|
|
|
|
// instruct us not to wait.
|
|
|
|
DWORD bytes, timeout;
|
|
|
|
ULONG_PTR key;
|
|
|
|
OVERLAPPED* overlapped;
|
|
|
|
|
2023-09-07 23:25:17 +00:00
|
|
|
timeout = uv_backend_timeout(event_loop);
|
2015-08-21 01:59:58 +00:00
|
|
|
|
2023-09-07 23:25:17 +00:00
|
|
|
GetQueuedCompletionStatus(event_loop->iocp, &bytes, &key, &overlapped,
|
|
|
|
timeout);
|
2015-08-21 01:59:58 +00:00
|
|
|
|
|
|
|
// Give the event back so libuv can deal with it.
|
2023-10-03 19:26:35 +00:00
|
|
|
if (overlapped != nullptr)
|
2023-09-07 23:25:17 +00:00
|
|
|
PostQueuedCompletionStatus(event_loop->iocp, bytes, key, overlapped);
|
2013-07-22 08:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2017-03-08 08:33:44 +00:00
|
|
|
NodeBindings* NodeBindings::Create(BrowserEnvironment browser_env) {
|
|
|
|
return new NodeBindingsWin(browser_env);
|
2013-07-22 08:05:35 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|