From 7e9d52530d0145fe8202d2fd35621407745e91ab Mon Sep 17 00:00:00 2001 From: Sree Kuchibhotla Date: Mon, 9 Jul 2018 14:53:54 -0700 Subject: Move executor implementation into GrpcExecutor class --- src/core/lib/iomgr/executor.h | 56 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 10 deletions(-) (limited to 'src/core/lib/iomgr/executor.h') diff --git a/src/core/lib/iomgr/executor.h b/src/core/lib/iomgr/executor.h index 68d540af55..cafe47decb 100644 --- a/src/core/lib/iomgr/executor.h +++ b/src/core/lib/iomgr/executor.h @@ -21,30 +21,66 @@ #include +#include "src/core/lib/gpr/spinlock.h" +#include "src/core/lib/gprpp/thd.h" #include "src/core/lib/iomgr/closure.h" +typedef struct { + gpr_mu mu; + size_t id; // For debugging purposes + gpr_cv cv; + grpc_closure_list elems; + size_t depth; // Number of closures in the closure list + bool shutdown; + bool queued_long_job; + grpc_core::Thread thd; +} thread_state; + typedef enum { GRPC_EXECUTOR_SHORT, GRPC_EXECUTOR_LONG } grpc_executor_job_length; -/** Initialize the global executor. - * - * This mechanism is meant to outsource work (grpc_closure instances) to a - * thread, for those cases where blocking isn't an option but there isn't a - * non-blocking solution available. */ +class GrpcExecutor { + public: + GrpcExecutor(const char* executor_name); + void Init(); + + /** Is the executor multi-threaded? */ + bool IsThreaded(); + + /* Enable/disable threading - must be called after Init and Shutdown() */ + void SetThreading(bool threading); + + /** Shutdown the executor, running all pending work as part of the call */ + void Shutdown(); + + /** Enqueue the closure onto the executor. is_short is true if the closure is + * a short job (i.e expected to not block and complete quickly) */ + void Enqueue(grpc_closure* closure, grpc_error* error, bool is_short); + + private: + static size_t RunClosures(grpc_closure_list list); + static void ThreadMain(void* arg); + + const char* name; + thread_state* thd_state; + size_t max_threads; + gpr_atm num_threads; + gpr_spinlock adding_thread_lock; +}; + +// == Global executor functions == + void grpc_executor_init(); -grpc_closure_scheduler* grpc_executor_scheduler(grpc_executor_job_length); +grpc_closure_scheduler* grpc_executor_scheduler( + grpc_executor_job_length length); -/** Shutdown the executor, running all pending work as part of the call */ void grpc_executor_shutdown(); -/** Is the executor multi-threaded? */ bool grpc_executor_is_threaded(); -/* enable/disable threading - must be called after grpc_executor_init and before - grpc_executor_shutdown */ void grpc_executor_set_threading(bool enable); #endif /* GRPC_CORE_LIB_IOMGR_EXECUTOR_H */ -- cgit v1.2.3