David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 19 | #ifndef GRPC_CORE_LIB_IOMGR_EXECUTOR_H |
| 20 | #define GRPC_CORE_LIB_IOMGR_EXECUTOR_H |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 21 | |
Alexander Polcyn | db3e898 | 2018-02-21 16:59:24 -0800 | [diff] [blame] | 22 | #include <grpc/support/port_platform.h> |
| 23 | |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 24 | #include "src/core/lib/gpr/spinlock.h" |
| 25 | #include "src/core/lib/gprpp/thd.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 26 | #include "src/core/lib/iomgr/closure.h" |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 27 | |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 28 | typedef 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 Tiller | 7a82afd | 2017-07-18 09:40:40 -0700 | [diff] [blame] | 39 | typedef enum { |
| 40 | GRPC_EXECUTOR_SHORT, |
| 41 | GRPC_EXECUTOR_LONG |
| 42 | } grpc_executor_job_length; |
| 43 | |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 44 | class 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 Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 75 | void grpc_executor_init(); |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 76 | |
Sree Kuchibhotla | 7e9d525 | 2018-07-09 14:53:54 -0700 | [diff] [blame^] | 77 | grpc_closure_scheduler* grpc_executor_scheduler( |
| 78 | grpc_executor_job_length length); |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 79 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 80 | void grpc_executor_shutdown(); |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 81 | |
Craig Tiller | 5e56f00 | 2017-05-16 15:02:50 -0700 | [diff] [blame] | 82 | bool grpc_executor_is_threaded(); |
| 83 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 84 | void grpc_executor_set_threading(bool enable); |
Craig Tiller | 5e56f00 | 2017-05-16 15:02:50 -0700 | [diff] [blame] | 85 | |
Yash Tibrewal | 12fc6d4 | 2017-10-09 16:43:34 -0700 | [diff] [blame] | 86 | #endif /* GRPC_CORE_LIB_IOMGR_EXECUTOR_H */ |