blob: cafe47decb99f98751b189e6b931c8863c54040e [file] [log] [blame]
David Garcia Quintas4bc34632015-10-07 16:12:35 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
David Garcia Quintas4bc34632015-10-07 16:12:35 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
David Garcia Quintas4bc34632015-10-07 16:12:35 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
David Garcia Quintas4bc34632015-10-07 16:12:35 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
David Garcia Quintas4bc34632015-10-07 16:12:35 -070016 *
17 */
18
Craig Tiller9a4dddd2016-03-25 17:08:13 -070019#ifndef GRPC_CORE_LIB_IOMGR_EXECUTOR_H
20#define GRPC_CORE_LIB_IOMGR_EXECUTOR_H
David Garcia Quintas4bc34632015-10-07 16:12:35 -070021
Alexander Polcyndb3e8982018-02-21 16:59:24 -080022#include <grpc/support/port_platform.h>
23
Sree Kuchibhotla7e9d5252018-07-09 14:53:54 -070024#include "src/core/lib/gpr/spinlock.h"
25#include "src/core/lib/gprpp/thd.h"
Craig Tiller9533d042016-03-25 17:11:06 -070026#include "src/core/lib/iomgr/closure.h"
David Garcia Quintas4bc34632015-10-07 16:12:35 -070027
Sree Kuchibhotla7e9d5252018-07-09 14:53:54 -070028typedef struct {
29 gpr_mu mu;
30 size_t id; // For debugging purposes
31 gpr_cv cv;
32 grpc_closure_list elems;
33 size_t depth; // Number of closures in the closure list
34 bool shutdown;
35 bool queued_long_job;
36 grpc_core::Thread thd;
37} thread_state;
38
Craig Tiller7a82afd2017-07-18 09:40:40 -070039typedef enum {
40 GRPC_EXECUTOR_SHORT,
41 GRPC_EXECUTOR_LONG
42} grpc_executor_job_length;
43
Sree Kuchibhotla7e9d5252018-07-09 14:53:54 -070044class GrpcExecutor {
45 public:
46 GrpcExecutor(const char* executor_name);
47 void Init();
48
49 /** Is the executor multi-threaded? */
50 bool IsThreaded();
51
52 /* Enable/disable threading - must be called after Init and Shutdown() */
53 void SetThreading(bool threading);
54
55 /** Shutdown the executor, running all pending work as part of the call */
56 void Shutdown();
57
58 /** Enqueue the closure onto the executor. is_short is true if the closure is
59 * a short job (i.e expected to not block and complete quickly) */
60 void Enqueue(grpc_closure* closure, grpc_error* error, bool is_short);
61
62 private:
63 static size_t RunClosures(grpc_closure_list list);
64 static void ThreadMain(void* arg);
65
66 const char* name;
67 thread_state* thd_state;
68 size_t max_threads;
69 gpr_atm num_threads;
70 gpr_spinlock adding_thread_lock;
71};
72
73// == Global executor functions ==
74
Yash Tibrewal8cf14702017-12-06 09:47:54 -080075void grpc_executor_init();
David Garcia Quintas4bc34632015-10-07 16:12:35 -070076
Sree Kuchibhotla7e9d5252018-07-09 14:53:54 -070077grpc_closure_scheduler* grpc_executor_scheduler(
78 grpc_executor_job_length length);
David Garcia Quintas4bc34632015-10-07 16:12:35 -070079
Yash Tibrewal8cf14702017-12-06 09:47:54 -080080void grpc_executor_shutdown();
David Garcia Quintas4bc34632015-10-07 16:12:35 -070081
Craig Tiller5e56f002017-05-16 15:02:50 -070082bool grpc_executor_is_threaded();
83
Yash Tibrewal8cf14702017-12-06 09:47:54 -080084void grpc_executor_set_threading(bool enable);
Craig Tiller5e56f002017-05-16 15:02:50 -070085
Yash Tibrewal12fc6d42017-10-09 16:43:34 -070086#endif /* GRPC_CORE_LIB_IOMGR_EXECUTOR_H */