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 | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 19 | #include "src/core/lib/iomgr/executor.h" |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 20 | |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include <grpc/support/alloc.h> |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 24 | #include <grpc/support/cpu.h> |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 25 | #include <grpc/support/log.h> |
| 26 | #include <grpc/support/sync.h> |
| 27 | #include <grpc/support/thd.h> |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 28 | #include <grpc/support/tls.h> |
| 29 | #include <grpc/support/useful.h> |
| 30 | |
Craig Tiller | 57bb9a9 | 2017-08-31 16:44:15 -0700 | [diff] [blame] | 31 | #include "src/core/lib/debug/stats.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 32 | #include "src/core/lib/iomgr/exec_ctx.h" |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 33 | #include "src/core/lib/support/spinlock.h" |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 34 | |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame^] | 35 | #define MAX_DEPTH 2 |
| 36 | |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 37 | typedef struct { |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 38 | gpr_mu mu; |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 39 | gpr_cv cv; |
| 40 | grpc_closure_list elems; |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame^] | 41 | size_t depth; |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 42 | bool shutdown; |
Craig Tiller | c2fb83e | 2017-07-18 12:38:25 -0700 | [diff] [blame] | 43 | bool queued_long_job; |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 44 | gpr_thd_id id; |
| 45 | } thread_state; |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 46 | |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 47 | static thread_state *g_thread_state; |
| 48 | static size_t g_max_threads; |
| 49 | static gpr_atm g_cur_threads; |
| 50 | static gpr_spinlock g_adding_thread_lock = GPR_SPINLOCK_STATIC_INITIALIZER; |
| 51 | |
| 52 | GPR_TLS_DECL(g_this_thread_state); |
| 53 | |
Craig Tiller | af723b0 | 2017-07-17 17:56:28 -0700 | [diff] [blame] | 54 | static grpc_tracer_flag executor_trace = |
| 55 | GRPC_TRACER_INITIALIZER(false, "executor"); |
| 56 | |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 57 | static void executor_thread(void *arg); |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 58 | |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame^] | 59 | static size_t run_closures(grpc_exec_ctx *exec_ctx, grpc_closure_list list) { |
| 60 | size_t n = 0; |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 61 | |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame^] | 62 | grpc_closure *c = list.head; |
| 63 | while (c != NULL) { |
| 64 | grpc_closure *next = c->next_data.next; |
| 65 | grpc_error *error = c->error_data.error; |
| 66 | if (GRPC_TRACER_ON(executor_trace)) { |
Craig Tiller | af723b0 | 2017-07-17 17:56:28 -0700 | [diff] [blame] | 67 | #ifndef NDEBUG |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame^] | 68 | gpr_log(GPR_DEBUG, "EXECUTOR: run %p [created by %s:%d]", c, |
| 69 | c->file_created, c->line_created); |
Craig Tiller | af723b0 | 2017-07-17 17:56:28 -0700 | [diff] [blame] | 70 | #else |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame^] | 71 | gpr_log(GPR_DEBUG, "EXECUTOR: run %p", c); |
Craig Tiller | af723b0 | 2017-07-17 17:56:28 -0700 | [diff] [blame] | 72 | #endif |
Craig Tiller | fb0262b | 2017-09-13 15:19:19 -0700 | [diff] [blame] | 73 | } |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame^] | 74 | #ifndef NDEBUG |
| 75 | c->scheduled = false; |
| 76 | #endif |
| 77 | c->cb(exec_ctx, c->cb_arg, error); |
| 78 | GRPC_ERROR_UNREF(error); |
| 79 | c = next; |
| 80 | n++; |
| 81 | grpc_exec_ctx_flush(exec_ctx); |
Craig Tiller | 061ef74 | 2016-12-29 10:54:09 -0800 | [diff] [blame] | 82 | } |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 83 | |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame^] | 84 | return n; |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Craig Tiller | 5e56f00 | 2017-05-16 15:02:50 -0700 | [diff] [blame] | 87 | bool grpc_executor_is_threaded() { |
| 88 | return gpr_atm_no_barrier_load(&g_cur_threads) > 0; |
| 89 | } |
| 90 | |
| 91 | void grpc_executor_set_threading(grpc_exec_ctx *exec_ctx, bool threading) { |
| 92 | gpr_atm cur_threads = gpr_atm_no_barrier_load(&g_cur_threads); |
| 93 | if (threading) { |
| 94 | if (cur_threads > 0) return; |
| 95 | g_max_threads = GPR_MAX(1, 2 * gpr_cpu_num_cores()); |
| 96 | gpr_atm_no_barrier_store(&g_cur_threads, 1); |
| 97 | gpr_tls_init(&g_this_thread_state); |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 98 | g_thread_state = |
| 99 | (thread_state *)gpr_zalloc(sizeof(thread_state) * g_max_threads); |
Craig Tiller | 5e56f00 | 2017-05-16 15:02:50 -0700 | [diff] [blame] | 100 | for (size_t i = 0; i < g_max_threads; i++) { |
| 101 | gpr_mu_init(&g_thread_state[i].mu); |
| 102 | gpr_cv_init(&g_thread_state[i].cv); |
| 103 | g_thread_state[i].elems = (grpc_closure_list)GRPC_CLOSURE_LIST_INIT; |
| 104 | } |
| 105 | |
| 106 | gpr_thd_options opt = gpr_thd_options_default(); |
| 107 | gpr_thd_options_set_joinable(&opt); |
| 108 | gpr_thd_new(&g_thread_state[0].id, executor_thread, &g_thread_state[0], |
| 109 | &opt); |
| 110 | } else { |
| 111 | if (cur_threads == 0) return; |
| 112 | for (size_t i = 0; i < g_max_threads; i++) { |
| 113 | gpr_mu_lock(&g_thread_state[i].mu); |
| 114 | g_thread_state[i].shutdown = true; |
| 115 | gpr_cv_signal(&g_thread_state[i].cv); |
| 116 | gpr_mu_unlock(&g_thread_state[i].mu); |
| 117 | } |
Craig Tiller | f7c8c9f | 2017-05-17 15:22:05 -0700 | [diff] [blame] | 118 | /* ensure no thread is adding a new thread... once this is past, then |
| 119 | no thread will try to add a new one either (since shutdown is true) */ |
| 120 | gpr_spinlock_lock(&g_adding_thread_lock); |
| 121 | gpr_spinlock_unlock(&g_adding_thread_lock); |
Craig Tiller | 5e56f00 | 2017-05-16 15:02:50 -0700 | [diff] [blame] | 122 | for (gpr_atm i = 0; i < g_cur_threads; i++) { |
| 123 | gpr_thd_join(g_thread_state[i].id); |
| 124 | } |
| 125 | gpr_atm_no_barrier_store(&g_cur_threads, 0); |
| 126 | for (size_t i = 0; i < g_max_threads; i++) { |
| 127 | gpr_mu_destroy(&g_thread_state[i].mu); |
| 128 | gpr_cv_destroy(&g_thread_state[i].cv); |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame^] | 129 | run_closures(exec_ctx, g_thread_state[i].elems); |
Craig Tiller | 5e56f00 | 2017-05-16 15:02:50 -0700 | [diff] [blame] | 130 | } |
| 131 | gpr_free(g_thread_state); |
| 132 | gpr_tls_destroy(&g_this_thread_state); |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 133 | } |
Craig Tiller | 5e56f00 | 2017-05-16 15:02:50 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void grpc_executor_init(grpc_exec_ctx *exec_ctx) { |
Craig Tiller | af723b0 | 2017-07-17 17:56:28 -0700 | [diff] [blame] | 137 | grpc_register_tracer(&executor_trace); |
Craig Tiller | b9b01ce | 2017-05-12 13:47:10 -0700 | [diff] [blame] | 138 | gpr_atm_no_barrier_store(&g_cur_threads, 0); |
Craig Tiller | 5e56f00 | 2017-05-16 15:02:50 -0700 | [diff] [blame] | 139 | grpc_executor_set_threading(exec_ctx, true); |
| 140 | } |
| 141 | |
| 142 | void grpc_executor_shutdown(grpc_exec_ctx *exec_ctx) { |
| 143 | grpc_executor_set_threading(exec_ctx, false); |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static void executor_thread(void *arg) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 147 | thread_state *ts = (thread_state *)arg; |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 148 | gpr_tls_set(&g_this_thread_state, (intptr_t)ts); |
| 149 | |
Craig Tiller | 8996208 | 2017-05-12 14:30:42 -0700 | [diff] [blame] | 150 | grpc_exec_ctx exec_ctx = |
| 151 | GRPC_EXEC_CTX_INITIALIZER(0, grpc_never_ready_to_finish, NULL); |
| 152 | |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame^] | 153 | size_t subtract_depth = 0; |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 154 | for (;;) { |
Craig Tiller | af723b0 | 2017-07-17 17:56:28 -0700 | [diff] [blame] | 155 | if (GRPC_TRACER_ON(executor_trace)) { |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame^] | 156 | gpr_log(GPR_DEBUG, "EXECUTOR[%d]: step (sub_depth=%" PRIdPTR ")", |
| 157 | (int)(ts - g_thread_state), subtract_depth); |
Craig Tiller | af723b0 | 2017-07-17 17:56:28 -0700 | [diff] [blame] | 158 | } |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 159 | gpr_mu_lock(&ts->mu); |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame^] | 160 | ts->depth -= subtract_depth; |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 161 | while (grpc_closure_list_empty(ts->elems) && !ts->shutdown) { |
Craig Tiller | 1ab56d8 | 2017-07-19 09:55:57 -0700 | [diff] [blame] | 162 | ts->queued_long_job = false; |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 163 | gpr_cv_wait(&ts->cv, &ts->mu, gpr_inf_future(GPR_CLOCK_REALTIME)); |
| 164 | } |
| 165 | if (ts->shutdown) { |
Craig Tiller | af723b0 | 2017-07-17 17:56:28 -0700 | [diff] [blame] | 166 | if (GRPC_TRACER_ON(executor_trace)) { |
Craig Tiller | 7d07994 | 2017-09-05 12:46:48 -0700 | [diff] [blame] | 167 | gpr_log(GPR_DEBUG, "EXECUTOR[%d]: shutdown", |
| 168 | (int)(ts - g_thread_state)); |
Craig Tiller | af723b0 | 2017-07-17 17:56:28 -0700 | [diff] [blame] | 169 | } |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 170 | gpr_mu_unlock(&ts->mu); |
| 171 | break; |
| 172 | } |
Craig Tiller | 57bb9a9 | 2017-08-31 16:44:15 -0700 | [diff] [blame] | 173 | GRPC_STATS_INC_EXECUTOR_QUEUE_DRAINED(&exec_ctx); |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame^] | 174 | grpc_closure_list exec = ts->elems; |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 175 | ts->elems = (grpc_closure_list)GRPC_CLOSURE_LIST_INIT; |
| 176 | gpr_mu_unlock(&ts->mu); |
Craig Tiller | af723b0 | 2017-07-17 17:56:28 -0700 | [diff] [blame] | 177 | if (GRPC_TRACER_ON(executor_trace)) { |
Craig Tiller | 7d07994 | 2017-09-05 12:46:48 -0700 | [diff] [blame] | 178 | gpr_log(GPR_DEBUG, "EXECUTOR[%d]: execute", (int)(ts - g_thread_state)); |
Craig Tiller | af723b0 | 2017-07-17 17:56:28 -0700 | [diff] [blame] | 179 | } |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 180 | |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame^] | 181 | subtract_depth = run_closures(&exec_ctx, exec); |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 182 | } |
Craig Tiller | 8996208 | 2017-05-12 14:30:42 -0700 | [diff] [blame] | 183 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static void executor_push(grpc_exec_ctx *exec_ctx, grpc_closure *closure, |
Craig Tiller | 7a82afd | 2017-07-18 09:40:40 -0700 | [diff] [blame] | 187 | grpc_error *error, bool is_short) { |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 188 | bool retry_push; |
Craig Tiller | 07d2fa7 | 2017-09-07 13:13:36 -0700 | [diff] [blame] | 189 | if (is_short) { |
| 190 | GRPC_STATS_INC_EXECUTOR_SCHEDULED_SHORT_ITEMS(exec_ctx); |
| 191 | } else { |
| 192 | GRPC_STATS_INC_EXECUTOR_SCHEDULED_LONG_ITEMS(exec_ctx); |
| 193 | } |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 194 | do { |
| 195 | retry_push = false; |
| 196 | size_t cur_thread_count = (size_t)gpr_atm_no_barrier_load(&g_cur_threads); |
| 197 | if (cur_thread_count == 0) { |
| 198 | if (GRPC_TRACER_ON(executor_trace)) { |
Craig Tiller | c2fb83e | 2017-07-18 12:38:25 -0700 | [diff] [blame] | 199 | #ifndef NDEBUG |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 200 | gpr_log(GPR_DEBUG, "EXECUTOR: schedule %p (created %s:%d) inline", |
| 201 | closure, closure->file_created, closure->line_created); |
Craig Tiller | c2fb83e | 2017-07-18 12:38:25 -0700 | [diff] [blame] | 202 | #else |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 203 | gpr_log(GPR_DEBUG, "EXECUTOR: schedule %p inline", closure); |
Craig Tiller | c2fb83e | 2017-07-18 12:38:25 -0700 | [diff] [blame] | 204 | #endif |
Craig Tiller | c2fb83e | 2017-07-18 12:38:25 -0700 | [diff] [blame] | 205 | } |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 206 | grpc_closure_list_append(&exec_ctx->closure_list, closure, error); |
| 207 | return; |
Craig Tiller | c2fb83e | 2017-07-18 12:38:25 -0700 | [diff] [blame] | 208 | } |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 209 | thread_state *ts = (thread_state *)gpr_tls_get(&g_this_thread_state); |
| 210 | if (ts == NULL) { |
| 211 | ts = &g_thread_state[GPR_HASH_POINTER(exec_ctx, cur_thread_count)]; |
Craig Tiller | 022ad3a | 2017-09-07 13:01:56 -0700 | [diff] [blame] | 212 | } else { |
| 213 | GRPC_STATS_INC_EXECUTOR_SCHEDULED_TO_SELF(exec_ctx); |
Craig Tiller | c2fb83e | 2017-07-18 12:38:25 -0700 | [diff] [blame] | 214 | } |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 215 | thread_state *orig_ts = ts; |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 216 | |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 217 | bool try_new_thread; |
| 218 | for (;;) { |
| 219 | if (GRPC_TRACER_ON(executor_trace)) { |
| 220 | #ifndef NDEBUG |
Craig Tiller | 8af33db | 2017-07-20 16:37:36 -0700 | [diff] [blame] | 221 | gpr_log( |
| 222 | GPR_DEBUG, |
| 223 | "EXECUTOR: try to schedule %p (%s) (created %s:%d) to thread %d", |
| 224 | closure, is_short ? "short" : "long", closure->file_created, |
| 225 | closure->line_created, (int)(ts - g_thread_state)); |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 226 | #else |
Craig Tiller | 8af33db | 2017-07-20 16:37:36 -0700 | [diff] [blame] | 227 | gpr_log(GPR_DEBUG, "EXECUTOR: try to schedule %p (%s) to thread %d", |
| 228 | closure, is_short ? "short" : "long", |
| 229 | (int)(ts - g_thread_state)); |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 230 | #endif |
| 231 | } |
| 232 | gpr_mu_lock(&ts->mu); |
| 233 | if (ts->queued_long_job) { |
Craig Tiller | b0ce25e | 2017-09-08 14:42:26 -0700 | [diff] [blame] | 234 | // if there's a long job queued, we never queue anything else to this |
| 235 | // queue (since long jobs can take 'infinite' time and we need to |
| 236 | // guarantee no starvation) |
| 237 | // ... spin through queues and try again |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 238 | gpr_mu_unlock(&ts->mu); |
Craig Tiller | a0d5185 | 2017-07-21 13:49:49 -0700 | [diff] [blame] | 239 | size_t idx = (size_t)(ts - g_thread_state); |
Craig Tiller | 1890883 | 2017-07-21 13:27:27 -0700 | [diff] [blame] | 240 | ts = &g_thread_state[(idx + 1) % cur_thread_count]; |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 241 | if (ts == orig_ts) { |
| 242 | retry_push = true; |
| 243 | try_new_thread = true; |
| 244 | break; |
| 245 | } |
| 246 | continue; |
| 247 | } |
| 248 | if (grpc_closure_list_empty(ts->elems)) { |
Craig Tiller | 022ad3a | 2017-09-07 13:01:56 -0700 | [diff] [blame] | 249 | GRPC_STATS_INC_EXECUTOR_WAKEUP_INITIATED(exec_ctx); |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 250 | gpr_cv_signal(&ts->cv); |
| 251 | } |
| 252 | grpc_closure_list_append(&ts->elems, closure, error); |
Craig Tiller | 2477cf3 | 2017-09-26 12:20:35 -0700 | [diff] [blame^] | 253 | ts->depth++; |
| 254 | try_new_thread = ts->depth > MAX_DEPTH && |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 255 | cur_thread_count < g_max_threads && !ts->shutdown; |
| 256 | if (!is_short) ts->queued_long_job = true; |
| 257 | gpr_mu_unlock(&ts->mu); |
| 258 | break; |
Craig Tiller | 3e9f98e | 2017-05-12 13:17:47 -0700 | [diff] [blame] | 259 | } |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 260 | if (try_new_thread && gpr_spinlock_trylock(&g_adding_thread_lock)) { |
| 261 | cur_thread_count = (size_t)gpr_atm_no_barrier_load(&g_cur_threads); |
| 262 | if (cur_thread_count < g_max_threads) { |
| 263 | gpr_atm_no_barrier_store(&g_cur_threads, cur_thread_count + 1); |
| 264 | |
| 265 | gpr_thd_options opt = gpr_thd_options_default(); |
| 266 | gpr_thd_options_set_joinable(&opt); |
| 267 | gpr_thd_new(&g_thread_state[cur_thread_count].id, executor_thread, |
| 268 | &g_thread_state[cur_thread_count], &opt); |
| 269 | } |
| 270 | gpr_spinlock_unlock(&g_adding_thread_lock); |
| 271 | } |
Craig Tiller | 07d2fa7 | 2017-09-07 13:13:36 -0700 | [diff] [blame] | 272 | if (retry_push) { |
| 273 | GRPC_STATS_INC_EXECUTOR_PUSH_RETRIES(exec_ctx); |
| 274 | } |
Craig Tiller | 2f767eb | 2017-07-20 12:06:14 -0700 | [diff] [blame] | 275 | } while (retry_push); |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 276 | } |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 277 | |
Craig Tiller | 7a82afd | 2017-07-18 09:40:40 -0700 | [diff] [blame] | 278 | static void executor_push_short(grpc_exec_ctx *exec_ctx, grpc_closure *closure, |
| 279 | grpc_error *error) { |
| 280 | executor_push(exec_ctx, closure, error, true); |
| 281 | } |
| 282 | |
| 283 | static void executor_push_long(grpc_exec_ctx *exec_ctx, grpc_closure *closure, |
| 284 | grpc_error *error) { |
| 285 | executor_push(exec_ctx, closure, error, false); |
| 286 | } |
| 287 | |
| 288 | static const grpc_closure_scheduler_vtable executor_vtable_short = { |
| 289 | executor_push_short, executor_push_short, "executor"}; |
| 290 | static grpc_closure_scheduler executor_scheduler_short = { |
| 291 | &executor_vtable_short}; |
| 292 | |
| 293 | static const grpc_closure_scheduler_vtable executor_vtable_long = { |
| 294 | executor_push_long, executor_push_long, "executor"}; |
| 295 | static grpc_closure_scheduler executor_scheduler_long = {&executor_vtable_long}; |
| 296 | |
| 297 | grpc_closure_scheduler *grpc_executor_scheduler( |
| 298 | grpc_executor_job_length length) { |
| 299 | return length == GRPC_EXECUTOR_SHORT ? &executor_scheduler_short |
| 300 | : &executor_scheduler_long; |
| 301 | } |