1 // 2 // 3 // Copyright 2016 gRPC authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 // 17 // 18 19 #ifndef GRPC_SRC_CORE_LIB_IOMGR_COMBINER_H 20 #define GRPC_SRC_CORE_LIB_IOMGR_COMBINER_H 21 22 #include <grpc/support/atm.h> 23 #include <grpc/support/port_platform.h> 24 #include <stddef.h> 25 26 #include "src/core/lib/debug/trace.h" 27 #include "src/core/lib/iomgr/exec_ctx.h" 28 29 namespace grpc_core { 30 // TODO(yashkt) : Remove this class and replace it with a class that does not 31 // use ExecCtx 32 class Combiner { 33 public: 34 void Run(grpc_closure* closure, grpc_error_handle error); 35 // TODO(yashkt) : Remove this method 36 void FinallyRun(grpc_closure* closure, grpc_error_handle error); 37 // Force the next combiner execution to be offloaded 38 void ForceOffload(); 39 Combiner* next_combiner_on_this_exec_ctx = nullptr; 40 MultiProducerSingleConsumerQueue queue; 41 // either: 42 // a pointer to the initiating exec ctx if that is the only exec_ctx that has 43 // ever queued to this combiner, or NULL. If this is non-null, it's not 44 // dereferenceable (since the initiating exec_ctx may have gone out of scope) 45 gpr_atm initiating_exec_ctx_or_null; 46 // state is: 47 // lower bit - zero if orphaned (STATE_UNORPHANED) 48 // other bits - number of items queued on the lock (STATE_ELEM_COUNT_LOW_BIT) 49 gpr_atm state; 50 bool time_to_execute_final_list = false; 51 grpc_closure_list final_list; 52 // TODO(ctiller): delete this when the combiner_offload_to_event_engine 53 // experiment is removed. 54 grpc_closure offload; 55 gpr_refcount refs; 56 std::shared_ptr<grpc_event_engine::experimental::EventEngine> event_engine; 57 }; 58 } // namespace grpc_core 59 60 // Provides serialized access to some resource. 61 // Each action queued on a combiner is executed serially in a borrowed thread. 62 // The actual thread executing actions may change over time (but there will only 63 // ever be one at a time). 64 65 // Initialize the lock 66 grpc_core::Combiner* grpc_combiner_create( 67 std::shared_ptr<grpc_event_engine::experimental::EventEngine> event_engine); 68 69 #ifndef NDEBUG 70 #define GRPC_COMBINER_DEBUG_ARGS \ 71 , const char *file, int line, const char *reason 72 #define GRPC_COMBINER_REF(combiner, reason) \ 73 grpc_combiner_ref((combiner), __FILE__, __LINE__, (reason)) 74 #define GRPC_COMBINER_UNREF(combiner, reason) \ 75 grpc_combiner_unref((combiner), __FILE__, __LINE__, (reason)) 76 #else 77 #define GRPC_COMBINER_DEBUG_ARGS 78 #define GRPC_COMBINER_REF(combiner, reason) grpc_combiner_ref((combiner)) 79 #define GRPC_COMBINER_UNREF(combiner, reason) grpc_combiner_unref((combiner)) 80 #endif 81 82 // Ref/unref the lock, for when we're sharing the lock ownership 83 // Prefer to use the macros above 84 grpc_core::Combiner* grpc_combiner_ref( 85 grpc_core::Combiner* lock GRPC_COMBINER_DEBUG_ARGS); 86 void grpc_combiner_unref(grpc_core::Combiner* lock GRPC_COMBINER_DEBUG_ARGS); 87 88 bool grpc_combiner_continue_exec_ctx(); 89 90 #endif // GRPC_SRC_CORE_LIB_IOMGR_COMBINER_H 91