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 15 #ifndef GRPC_SRC_CORE_LIB_EVENT_ENGINE_POSIX_ENGINE_EV_POLL_POSIX_H 16 #define GRPC_SRC_CORE_LIB_EVENT_ENGINE_POSIX_ENGINE_EV_POLL_POSIX_H 17 18 #include <grpc/event_engine/event_engine.h> 19 #include <grpc/support/port_platform.h> 20 21 #include <memory> 22 #include <string> 23 24 #include "absl/base/thread_annotations.h" 25 #include "absl/functional/function_ref.h" 26 #include "absl/strings/string_view.h" 27 #include "src/core/lib/event_engine/poller.h" 28 #include "src/core/lib/event_engine/posix_engine/event_poller.h" 29 #include "src/core/lib/event_engine/posix_engine/wakeup_fd_posix.h" 30 #include "src/core/util/sync.h" 31 32 namespace grpc_event_engine { 33 namespace experimental { 34 35 class PollEventHandle; 36 37 // Definition of poll based poller. 38 class PollPoller : public PosixEventPoller, 39 public std::enable_shared_from_this<PollPoller> { 40 public: 41 explicit PollPoller(Scheduler* scheduler); 42 PollPoller(Scheduler* scheduler, bool use_phony_poll); 43 EventHandle* CreateHandle(int fd, absl::string_view name, 44 bool track_err) override; 45 Poller::WorkResult Work( 46 grpc_event_engine::experimental::EventEngine::Duration timeout, 47 absl::FunctionRef<void()> schedule_poll_again) override; Name()48 std::string Name() override { return "poll"; } 49 void Kick() override; GetScheduler()50 Scheduler* GetScheduler() { return scheduler_; } 51 void Shutdown() override; CanTrackErrors()52 bool CanTrackErrors() const override { return false; } 53 ~PollPoller() override; 54 55 // Forkable 56 void PrepareFork() override; 57 void PostforkParent() override; 58 void PostforkChild() override; 59 60 void Close(); 61 62 private: 63 void KickExternal(bool ext); 64 void PollerHandlesListAddHandle(PollEventHandle* handle) 65 ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_); 66 void PollerHandlesListRemoveHandle(PollEventHandle* handle) 67 ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_); 68 friend class PollEventHandle; 69 class HandlesList { 70 public: HandlesList(PollEventHandle * handle)71 explicit HandlesList(PollEventHandle* handle) : handle(handle) {} 72 PollEventHandle* handle; 73 PollEventHandle* next = nullptr; 74 PollEventHandle* prev = nullptr; 75 }; 76 grpc_core::Mutex mu_; 77 Scheduler* scheduler_; 78 bool use_phony_poll_; 79 bool was_kicked_ ABSL_GUARDED_BY(mu_); 80 bool was_kicked_ext_ ABSL_GUARDED_BY(mu_); 81 int num_poll_handles_ ABSL_GUARDED_BY(mu_); 82 PollEventHandle* poll_handles_list_head_ ABSL_GUARDED_BY(mu_) = nullptr; 83 std::unique_ptr<WakeupFd> wakeup_fd_; 84 bool closed_ ABSL_GUARDED_BY(mu_); 85 }; 86 87 // Return an instance of a poll based poller tied to the specified scheduler. 88 // It use_phony_poll is true, it implies that the poller is declared 89 // non-polling and any attempt to schedule a blocking poll will result in a 90 // crash failure. 91 std::shared_ptr<PollPoller> MakePollPoller(Scheduler* scheduler, 92 bool use_phony_poll); 93 94 } // namespace experimental 95 } // namespace grpc_event_engine 96 97 #endif // GRPC_SRC_CORE_LIB_EVENT_ENGINE_POSIX_ENGINE_EV_POLL_POSIX_H 98