Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | a93a25f | 2016-01-28 13:55:49 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include "src/core/iomgr/exec_ctx.h" |
| 35 | |
Craig Tiller | 258f8de | 2015-09-23 12:32:40 -0700 | [diff] [blame] | 36 | #include <grpc/support/log.h> |
Craig Tiller | ef1bf87 | 2016-02-28 17:37:33 -0800 | [diff] [blame] | 37 | #include <grpc/support/sync.h> |
| 38 | #include <grpc/support/thd.h> |
Craig Tiller | 258f8de | 2015-09-23 12:32:40 -0700 | [diff] [blame] | 39 | |
Craig Tiller | 86253ca | 2015-10-08 13:31:02 -0700 | [diff] [blame] | 40 | #include "src/core/profiling/timers.h" |
| 41 | |
Craig Tiller | ef1bf87 | 2016-02-28 17:37:33 -0800 | [diff] [blame] | 42 | #ifndef GRPC_EXECUTION_CONTEXT_SANITIZER |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 43 | bool grpc_exec_ctx_flush(grpc_exec_ctx *exec_ctx) { |
| 44 | bool did_something = 0; |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 45 | GPR_TIMER_BEGIN("grpc_exec_ctx_flush", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 46 | while (!grpc_closure_list_empty(exec_ctx->closure_list)) { |
Craig Tiller | b8b1a46 | 2015-09-22 12:50:03 -0700 | [diff] [blame] | 47 | grpc_closure *c = exec_ctx->closure_list.head; |
| 48 | exec_ctx->closure_list.head = exec_ctx->closure_list.tail = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 49 | while (c != NULL) { |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 50 | bool success = (bool)(c->final_data & 1); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 51 | grpc_closure *next = (grpc_closure *)(c->final_data & ~(uintptr_t)1); |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 52 | did_something = true; |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 53 | GPR_TIMER_BEGIN("grpc_exec_ctx_flush.cb", 0); |
Craig Tiller | e8b5f62 | 2015-11-02 14:15:03 -0800 | [diff] [blame] | 54 | c->cb(exec_ctx, c->cb_arg, success); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 55 | GPR_TIMER_END("grpc_exec_ctx_flush.cb", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 56 | c = next; |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 57 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 58 | } |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 59 | GPR_TIMER_END("grpc_exec_ctx_flush", 0); |
Craig Tiller | 4086474 | 2015-09-30 08:35:31 -0700 | [diff] [blame] | 60 | return did_something; |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void grpc_exec_ctx_finish(grpc_exec_ctx *exec_ctx) { |
| 64 | grpc_exec_ctx_flush(exec_ctx); |
| 65 | } |
| 66 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 67 | void grpc_exec_ctx_enqueue(grpc_exec_ctx *exec_ctx, grpc_closure *closure, |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 68 | bool success, |
| 69 | grpc_workqueue *offload_target_or_null) { |
| 70 | GPR_ASSERT(offload_target_or_null == NULL); |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 71 | grpc_closure_list_add(&exec_ctx->closure_list, closure, success); |
| 72 | } |
| 73 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 74 | void grpc_exec_ctx_enqueue_list(grpc_exec_ctx *exec_ctx, |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 75 | grpc_closure_list *list, |
| 76 | grpc_workqueue *offload_target_or_null) { |
| 77 | GPR_ASSERT(offload_target_or_null == NULL); |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 78 | grpc_closure_list_move(list, &exec_ctx->closure_list); |
| 79 | } |
Craig Tiller | ef1bf87 | 2016-02-28 17:37:33 -0800 | [diff] [blame] | 80 | |
| 81 | void grpc_exec_ctx_global_init(void) {} |
| 82 | void grpc_exec_ctx_global_shutdown(void) {} |
| 83 | #else |
| 84 | static gpr_mu g_mu; |
| 85 | static gpr_cv g_cv; |
| 86 | static int g_threads = 0; |
| 87 | |
| 88 | static void run_closure(void *arg) { |
| 89 | grpc_closure *closure = arg; |
| 90 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 91 | closure->cb(&exec_ctx, closure->cb_arg, (closure->final_data & 1) != 0); |
| 92 | grpc_exec_ctx_finish(&exec_ctx); |
| 93 | gpr_mu_lock(&g_mu); |
| 94 | if (--g_threads == 0) { |
| 95 | gpr_cv_signal(&g_cv); |
| 96 | } |
| 97 | gpr_mu_unlock(&g_mu); |
| 98 | } |
| 99 | |
| 100 | static void start_closure(grpc_closure *closure) { |
| 101 | gpr_thd_id id; |
| 102 | gpr_mu_lock(&g_mu); |
| 103 | g_threads++; |
| 104 | gpr_mu_unlock(&g_mu); |
| 105 | gpr_thd_new(&id, run_closure, closure, NULL); |
| 106 | } |
| 107 | |
Craig Tiller | 3af3f74 | 2016-02-28 21:53:13 -0800 | [diff] [blame] | 108 | bool grpc_exec_ctx_flush(grpc_exec_ctx *exec_ctx) { return false; } |
Craig Tiller | ef1bf87 | 2016-02-28 17:37:33 -0800 | [diff] [blame] | 109 | |
Craig Tiller | 3af3f74 | 2016-02-28 21:53:13 -0800 | [diff] [blame] | 110 | void grpc_exec_ctx_finish(grpc_exec_ctx *exec_ctx) {} |
Craig Tiller | ef1bf87 | 2016-02-28 17:37:33 -0800 | [diff] [blame] | 111 | |
| 112 | void grpc_exec_ctx_enqueue(grpc_exec_ctx *exec_ctx, grpc_closure *closure, |
| 113 | bool success, |
| 114 | grpc_workqueue *offload_target_or_null) { |
| 115 | GPR_ASSERT(offload_target_or_null == NULL); |
Craig Tiller | 3af3f74 | 2016-02-28 21:53:13 -0800 | [diff] [blame] | 116 | if (closure == NULL) return; |
Craig Tiller | ef1bf87 | 2016-02-28 17:37:33 -0800 | [diff] [blame] | 117 | closure->final_data = success; |
| 118 | start_closure(closure); |
| 119 | } |
| 120 | |
| 121 | void grpc_exec_ctx_enqueue_list(grpc_exec_ctx *exec_ctx, |
| 122 | grpc_closure_list *list, |
| 123 | grpc_workqueue *offload_target_or_null) { |
Craig Tiller | 3af3f74 | 2016-02-28 21:53:13 -0800 | [diff] [blame] | 124 | GPR_ASSERT(offload_target_or_null == NULL); |
| 125 | if (list == NULL) return; |
Craig Tiller | ef1bf87 | 2016-02-28 17:37:33 -0800 | [diff] [blame] | 126 | grpc_closure *p = list->head; |
| 127 | while (p) { |
| 128 | grpc_closure *start = p; |
| 129 | p = grpc_closure_next(start); |
| 130 | start_closure(start); |
| 131 | } |
| 132 | grpc_closure_list r = GRPC_CLOSURE_LIST_INIT; |
| 133 | *list = r; |
| 134 | } |
| 135 | |
| 136 | void grpc_exec_ctx_global_init(void) { |
| 137 | gpr_mu_init(&g_mu); |
| 138 | gpr_cv_init(&g_cv); |
| 139 | } |
| 140 | |
| 141 | void grpc_exec_ctx_global_shutdown(void) { |
| 142 | gpr_mu_lock(&g_mu); |
| 143 | while (g_threads != 0) { |
| 144 | gpr_cv_wait(&g_cv, &g_mu, gpr_inf_future(GPR_CLOCK_REALTIME)); |
| 145 | } |
| 146 | gpr_mu_unlock(&g_mu); |
| 147 | |
| 148 | gpr_mu_destroy(&g_mu); |
| 149 | gpr_cv_destroy(&g_cv); |
| 150 | } |
| 151 | #endif |