1 // Copyright 2021 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_PROMISE_EVENT_ENGINE_WAKEUP_SCHEDULER_H 16 #define GRPC_SRC_CORE_LIB_PROMISE_EVENT_ENGINE_WAKEUP_SCHEDULER_H 17 18 #include <grpc/event_engine/event_engine.h> 19 #include <grpc/support/port_platform.h> 20 21 #include <memory> 22 #include <utility> 23 24 #include "absl/log/check.h" 25 #include "src/core/lib/iomgr/exec_ctx.h" 26 27 namespace grpc_core { 28 29 // A callback scheduler for activities that works by scheduling callbacks on the 30 // exec ctx. 31 class EventEngineWakeupScheduler { 32 public: EventEngineWakeupScheduler(std::shared_ptr<grpc_event_engine::experimental::EventEngine> event_engine)33 explicit EventEngineWakeupScheduler( 34 std::shared_ptr<grpc_event_engine::experimental::EventEngine> 35 event_engine) 36 : event_engine_(std::move(event_engine)) { 37 CHECK_NE(event_engine_, nullptr); 38 } 39 40 template <typename ActivityType> 41 class BoundScheduler 42 : public grpc_event_engine::experimental::EventEngine::Closure { 43 protected: BoundScheduler(EventEngineWakeupScheduler scheduler)44 explicit BoundScheduler(EventEngineWakeupScheduler scheduler) 45 : event_engine_(std::move(scheduler.event_engine_)) {} 46 BoundScheduler(const BoundScheduler&) = delete; 47 BoundScheduler& operator=(const BoundScheduler&) = delete; ScheduleWakeup()48 void ScheduleWakeup() { event_engine_->Run(this); } Run()49 void Run() final { 50 ApplicationCallbackExecCtx app_exec_ctx; 51 ExecCtx exec_ctx; 52 static_cast<ActivityType*>(this)->RunScheduledWakeup(); 53 } 54 55 private: 56 std::shared_ptr<grpc_event_engine::experimental::EventEngine> event_engine_; 57 }; 58 59 private: 60 std::shared_ptr<grpc_event_engine::experimental::EventEngine> event_engine_; 61 }; 62 63 } // namespace grpc_core 64 65 #endif // GRPC_SRC_CORE_LIB_PROMISE_EVENT_ENGINE_WAKEUP_SCHEDULER_H 66