| 1 |
// Copyright 2022 The gRPC Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
#include <grpc/support/port_platform.h> |
| 15 |
|
| 16 |
#ifdef GPR_WINDOWS |
| 17 |
|
| 18 |
#include <memory> |
| 19 |
|
| 20 |
#include "absl/status/status.h" |
| 21 |
#include "absl/status/statusor.h" |
| 22 |
#include "absl/strings/string_view.h" |
| 23 |
|
| 24 |
#include <grpc/event_engine/endpoint_config.h> |
| 25 |
#include <grpc/event_engine/event_engine.h> |
| 26 |
#include <grpc/event_engine/memory_allocator.h> |
| 27 |
#include <grpc/event_engine/slice_buffer.h> |
| 28 |
|
| 29 |
#include "src/core/lib/event_engine/handle_containers.h" |
| 30 |
#include "src/core/lib/event_engine/posix_engine/timer_manager.h" |
| 31 |
#include "src/core/lib/event_engine/trace.h" |
| 32 |
#include "src/core/lib/event_engine/utils.h" |
| 33 |
#include "src/core/lib/event_engine/windows/iocp.h" |
| 34 |
#include "src/core/lib/event_engine/windows/windows_engine.h" |
| 35 |
#include "src/core/lib/gprpp/sync.h" |
| 36 |
#include "src/core/lib/gprpp/time.h" |
| 37 |
|
| 38 |
namespace grpc_event_engine { |
| 39 |
namespace experimental { |
| 40 |
|
| 41 |
// TODO(hork): The iomgr timer and execution engine can be reused. It should |
| 42 |
// be separated out from the posix_engine and instantiated as components. It is |
| 43 |
// effectively copied below. |
| 44 |
|
| 45 |
struct WindowsEventEngine::Closure final : public EventEngine::Closure { |
| 46 |
absl::AnyInvocable<void()> cb; |
| 47 |
posix_engine::Timer timer; |
| 48 |
WindowsEventEngine* engine; |
| 49 |
EventEngine::TaskHandle handle; |
| 50 |
|
| 51 |
void Run() override { |
| 52 |
GRPC_EVENT_ENGINE_TRACE("WindowsEventEngine:%p executing callback:%s", |
| 53 |
engine, HandleToString(handle).c_str()); |
| 54 |
{ |
| 55 |
grpc_core::MutexLock lock(&engine->mu_); |
| 56 |
engine->known_handles_.erase(handle); |
| 57 |
} |
| 58 |
cb(); |
| 59 |
delete this; |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
WindowsEventEngine::WindowsEventEngine() |
| 64 |
: executor_(std::make_shared<ThreadPool>()), |
| 65 |
iocp_(executor_.get()), |
| 66 |
timer_manager_(executor_) { |
| 67 |
WSADATA wsaData; |
| 68 |
int status = WSAStartup(MAKEWORD(2, 0), &wsaData); |
| 69 |
GPR_ASSERT(status == 0); |
| 70 |
} |
| 71 |
|
| 72 |
WindowsEventEngine::~WindowsEventEngine() { |
| 73 |
{ |
| 74 |
grpc_core::MutexLock lock(&mu_); |
| 75 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_event_engine_trace)) { |
| 76 |
for (auto handle : known_handles_) { |
| 77 |
gpr_log(GPR_ERROR, |
| 78 |
"WindowsEventEngine:%p uncleared TaskHandle at shutdown:%s", |
| 79 |
this, HandleToString(handle).c_str()); |
| 80 |
} |
| 81 |
} |
| 82 |
GPR_ASSERT(GPR_LIKELY(known_handles_.empty())); |
| 83 |
GPR_ASSERT(WSACleanup() == 0); |
| 84 |
timer_manager_.Shutdown(); |
| 85 |
} |
| 86 |
executor_->Quiesce(); |
| 87 |
} |
| 88 |
|
| 89 |
bool WindowsEventEngine::Cancel(EventEngine::TaskHandle handle) { |
| 90 |
grpc_core::MutexLock lock(&mu_); |
| 91 |
if (!known_handles_.contains(handle)) return false; |
| 92 |
auto* cd = reinterpret_cast<Closure*>(handle.keys[0]); |
| 93 |
bool r = timer_manager_.TimerCancel(&cd->timer); |
| 94 |
known_handles_.erase(handle); |
| 95 |
if (r) delete cd; |
| 96 |
return r; |
| 97 |
} |
| 98 |
|
| 99 |
EventEngine::TaskHandle WindowsEventEngine::RunAfter( |
| 100 |
Duration when, absl::AnyInvocable<void()> closure) { |
| 101 |
return RunAfterInternal(when, std::move(closure)); |
| 102 |
} |
| 103 |
|
| 104 |
EventEngine::TaskHandle WindowsEventEngine::RunAfter( |
| 105 |
Duration when, EventEngine::Closure* closure) { |
| 106 |
return RunAfterInternal(when, [closure]() { closure->Run(); }); |
| 107 |
} |
| 108 |
|
| 109 |
void WindowsEventEngine::Run(absl::AnyInvocable<void()> closure) { |
| 110 |
executor_->Run(std::move(closure)); |
| 111 |
} |
| 112 |
|
| 113 |
void WindowsEventEngine::Run(EventEngine::Closure* closure) { |
| 114 |
executor_->Run(closure); |
| 115 |
} |
| 116 |
|
| 117 |
EventEngine::TaskHandle WindowsEventEngine::RunAfterInternal( |
| 118 |
Duration when, absl::AnyInvocable<void()> cb) { |
| 119 |
auto when_ts = ToTimestamp(timer_manager_.Now(), when); |
| 120 |
auto* cd = new Closure; |
| 121 |
cd->cb = std::move(cb); |
| 122 |
cd->engine = this; |
| 123 |
EventEngine::TaskHandle handle{reinterpret_cast<intptr_t>(cd), |
| 124 |
aba_token_.fetch_add(1)}; |
| 125 |
grpc_core::MutexLock lock(&mu_); |
| 126 |
known_handles_.insert(handle); |
| 127 |
cd->handle = handle; |
| 128 |
GRPC_EVENT_ENGINE_TRACE("WindowsEventEngine:%p scheduling callback:%s", this, |
| 129 |
HandleToString(handle).c_str()); |
| 130 |
timer_manager_.TimerInit(&cd->timer, when_ts, cd); |
| 131 |
return handle; |
| 132 |
} |
| 133 |
|
| 134 |
std::unique_ptr<EventEngine::DNSResolver> WindowsEventEngine::GetDNSResolver( |
| 135 |
EventEngine::DNSResolver::ResolverOptions const& /*options*/) { |
| 136 |
GPR_ASSERT(false && "unimplemented"); |
| 137 |
} |
| 138 |
|
| 139 |
bool WindowsEventEngine::IsWorkerThread() { |
| 140 |
GPR_ASSERT(false && "unimplemented"); |
| 141 |
} |
| 142 |
|
| 143 |
bool WindowsEventEngine::CancelConnect(EventEngine::ConnectionHandle handle) { |
| 144 |
GPR_ASSERT(false && "unimplemented"); |
| 145 |
} |
| 146 |
|
| 147 |
EventEngine::ConnectionHandle WindowsEventEngine::Connect( |
| 148 |
OnConnectCallback on_connect, const ResolvedAddress& addr, |
| 149 |
const EndpointConfig& args, MemoryAllocator memory_allocator, |
| 150 |
Duration deadline) { |
| 151 |
GPR_ASSERT(false && "unimplemented"); |
| 152 |
} |
| 153 |
|
| 154 |
absl::StatusOr<std::unique_ptr<EventEngine::Listener>> |
| 155 |
WindowsEventEngine::CreateListener( |
| 156 |
Listener::AcceptCallback on_accept, |
| 157 |
absl::AnyInvocable<void(absl::Status)> on_shutdown, |
| 158 |
const EndpointConfig& config, |
| 159 |
std::unique_ptr<MemoryAllocatorFactory> memory_allocator_factory) { |
| 160 |
GPR_ASSERT(false && "unimplemented"); |
| 161 |
} |
| 162 |
|
| 163 |
} // namespace experimental |
| 164 |
} // namespace grpc_event_engine |
| 165 |
#endif // GPR_WINDOWS |