1 // Copyright 2022 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_POLLER_H 15 #define GRPC_SRC_CORE_LIB_EVENT_ENGINE_POLLER_H 16 17 #include <grpc/event_engine/event_engine.h> 18 #include <grpc/support/port_platform.h> 19 20 #include "absl/functional/function_ref.h" 21 22 namespace grpc_event_engine { 23 namespace experimental { 24 25 // A generic cross-platform "poller" concept. 26 // Concrete implementations will likely manage a set of sockets/file 27 // descriptors/etc, allowing threads to drive polling and event processing via 28 // Work(...). 29 class Poller { 30 public: 31 enum class WorkResult { kOk, kDeadlineExceeded, kKicked }; 32 33 virtual ~Poller() = default; 34 // Poll once for events and process received events. The callback function 35 // "schedule_poll_again" is expected to be run synchronously prior to 36 // processing received events. The callback's responsibility primarily is to 37 // schedule Poller::Work asynchronously again. This would ensure that the next 38 // polling cycle would run as quickly as possible to ensure continuous 39 // polling. 40 // 41 // Returns: 42 // * Poller::WorkResult::kKicked if it was Kicked. A poller that was Kicked 43 // may still process some events and if so, it may have run the 44 // schedule_poll_again callback function synchronously. When the poller 45 // returns Poller::WorkResult::kKicked it's up to the user to determine 46 // if the schedule_poll_again callback has run or not. 47 // * Poller::WorkResult::kDeadlineExceeded if timeout occurred. The 48 // schedule_poll_again callback is not run in this case. 49 // * Poller::WorkResult::kOk, otherwise indicating that the 50 // schedule_poll_again callback function was run synchronously before some 51 // events were processed. 52 virtual WorkResult Work(EventEngine::Duration timeout, 53 absl::FunctionRef<void()> schedule_poll_again) = 0; 54 // Trigger the threads executing Work(..) to break out as soon as possible. 55 virtual void Kick() = 0; 56 }; 57 58 } // namespace experimental 59 } // namespace grpc_event_engine 60 61 #endif // GRPC_SRC_CORE_LIB_EVENT_ENGINE_POLLER_H 62