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 #ifndef GRPC_SRC_CORE_LIB_EVENT_ENGINE_WINDOWS_IOCP_H
15 #define GRPC_SRC_CORE_LIB_EVENT_ENGINE_WINDOWS_IOCP_H
16 
17 #include <grpc/support/port_platform.h>
18 
19 #ifdef GPR_WINDOWS
20 
21 #include "absl/status/status.h"
22 
23 #include <grpc/event_engine/event_engine.h>
24 
25 #include "src/core/lib/event_engine/poller.h"
26 #include "src/core/lib/event_engine/thread_pool/thread_pool.h"
27 #include "src/core/lib/event_engine/windows/win_socket.h"
28 
29 namespace grpc_event_engine {
30 namespace experimental {
31 
32 class IOCP final : public Poller {
33  public:
34   explicit IOCP(ThreadPool* thread_pool) noexcept;
35   ~IOCP();
36   // Not copyable
37   IOCP(const IOCP&) = delete;
38   IOCP& operator=(const IOCP&) = delete;
39   // Not moveable
40   IOCP(IOCP&& other) = delete;
41   IOCP& operator=(IOCP&& other) = delete;
42 
43   // interface methods
44   void Shutdown();
45   WorkResult Work(EventEngine::Duration timeout,
46                   absl::FunctionRef<void()> schedule_poll_again) override;
47   void Kick() override;
48 
49   std::unique_ptr<WinSocket> Watch(SOCKET socket);
50   // Return the set of default flags
51   static DWORD GetDefaultSocketFlags();
52 
53  private:
54   // Initialize default flags via checking platform support
55   static DWORD WSASocketFlagsInit();
56 
57   ThreadPool* thread_pool_;
58   HANDLE iocp_handle_;
59   OVERLAPPED kick_overlap_;
60   ULONG kick_token_;
61   std::atomic<int> outstanding_kicks_{0};
62 };
63 
64 }  // namespace experimental
65 }  // namespace grpc_event_engine
66 
67 #endif
68 
69 #endif  // GRPC_SRC_CORE_LIB_EVENT_ENGINE_WINDOWS_IOCP_H
70