blob: 8829138c5fa71e3baa1ed5c36fe160e7361a3e4d [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;
Sree Kuchibhotla00476fd2018-07-16 18:09:27 -070030 size_t id; // For debugging purposes
31 const char* name; // Thread state name
Sree Kuchibhotla7e9d5252018-07-09 14:53:54 -070032 gpr_cv cv;
33 grpc_closure_list elems;
34 size_t depth; // Number of closures in the closure list
35 bool shutdown;
36 bool queued_long_job;
37 grpc_core::Thread thd;
Sree Kuchibhotla83d0bfa2018-07-10 11:29:43 -070038} ThreadState;
Sree Kuchibhotla7e9d5252018-07-09 14:53:54 -070039
Sree Kuchibhotla37d8bbc2018-07-10 13:30:57 -070040typedef enum {
41 GRPC_EXECUTOR_SHORT = 0,
42 GRPC_EXECUTOR_LONG,
43 GRPC_NUM_EXECUTOR_JOB_TYPES // Add new values above this
44} GrpcExecutorJobType;
Craig Tiller7a82afd2017-07-18 09:40:40 -070045
Sree Kuchibhotla7e9d5252018-07-09 14:53:54 -070046class GrpcExecutor {
47 public:
48 GrpcExecutor(const char* executor_name);
Sree Kuchibhotlaf0ed1a22018-07-11 17:16:03 -070049
Sree Kuchibhotla7e9d5252018-07-09 14:53:54 -070050 void Init();
51
52 /** Is the executor multi-threaded? */
Sree Kuchibhotla83d0bfa2018-07-10 11:29:43 -070053 bool IsThreaded() const;
Sree Kuchibhotla7e9d5252018-07-09 14:53:54 -070054
55 /* Enable/disable threading - must be called after Init and Shutdown() */
56 void SetThreading(bool threading);
57
58 /** Shutdown the executor, running all pending work as part of the call */
59 void Shutdown();
60
61 /** Enqueue the closure onto the executor. is_short is true if the closure is
62 * a short job (i.e expected to not block and complete quickly) */
63 void Enqueue(grpc_closure* closure, grpc_error* error, bool is_short);
64
65 private:
Sree Kuchibhotla00476fd2018-07-16 18:09:27 -070066 static size_t RunClosures(const char* executor_name, grpc_closure_list list);
Sree Kuchibhotla7e9d5252018-07-09 14:53:54 -070067 static void ThreadMain(void* arg);
68
Sree Kuchibhotla83d0bfa2018-07-10 11:29:43 -070069 const char* name_;
70 ThreadState* thd_state_;
71 size_t max_threads_;
72 gpr_atm num_threads_;
73 gpr_spinlock adding_thread_lock_;
Sree Kuchibhotla7e9d5252018-07-09 14:53:54 -070074};
75
76// == Global executor functions ==
77
Sree Kuchibhotla37d8bbc2018-07-10 13:30:57 -070078typedef enum {
79 GRPC_DEFAULT_EXECUTOR = 0,
80 GRPC_RESOLVER_EXECUTOR,
81
82 GRPC_NUM_EXECUTORS // Add new values above this
83} GrpcExecutorType;
84
85// TODO(sreek): Currently we have two executors (available globally): The
86// default executor and the resolver executor.
87//
88// Some of the functions below operate on the DEFAULT executor only while some
89// operate of ALL the executors. This is a bit confusing and should be cleaned
90// up in future (where we make all the following functions take executor_type
91// and/or job_type)
92
93// Initialize ALL the executors
Yash Tibrewal8cf14702017-12-06 09:47:54 -080094void grpc_executor_init();
David Garcia Quintas4bc34632015-10-07 16:12:35 -070095
Sree Kuchibhotla37d8bbc2018-07-10 13:30:57 -070096// Shutdown ALL the executors
Yash Tibrewal8cf14702017-12-06 09:47:54 -080097void grpc_executor_shutdown();
David Garcia Quintas4bc34632015-10-07 16:12:35 -070098
Sree Kuchibhotla37d8bbc2018-07-10 13:30:57 -070099// Set the threading mode for ALL the executors
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800100void grpc_executor_set_threading(bool enable);
Craig Tiller5e56f002017-05-16 15:02:50 -0700101
Sree Kuchibhotla37d8bbc2018-07-10 13:30:57 -0700102// Get the DEFAULT executor scheduler for the given job_type
103grpc_closure_scheduler* grpc_executor_scheduler(GrpcExecutorJobType job_type);
104
105// Get the executor scheduler for a given executor_type and a job_type
106grpc_closure_scheduler* grpc_executor_scheduler(GrpcExecutorType executor_type,
107 GrpcExecutorJobType job_type);
108
109// Return if a given executor is running in threaded mode (i.e if
110// grpc_executor_set_threading(true) was called previously on that executor)
111bool grpc_executor_is_threaded(GrpcExecutorType executor_type);
112
113// Return if the DEFAULT executor is threaded
114bool grpc_executor_is_threaded();
115
Yash Tibrewal12fc6d42017-10-09 16:43:34 -0700116#endif /* GRPC_CORE_LIB_IOMGR_EXECUTOR_H */