Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, 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 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 34 | #include "src/core/lib/iomgr/exec_ctx.h" |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 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 | dfd3a8f | 2016-08-24 09:43:45 -0700 | [diff] [blame] | 40 | #include "src/core/lib/iomgr/combiner.h" |
Craig Tiller | 099eb6e | 2016-06-30 15:10:12 -0700 | [diff] [blame] | 41 | #include "src/core/lib/iomgr/workqueue.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 42 | #include "src/core/lib/profiling/timers.h" |
Craig Tiller | 86253ca | 2015-10-08 13:31:02 -0700 | [diff] [blame] | 43 | |
Craig Tiller | 56aa023 | 2016-05-11 12:37:04 -0700 | [diff] [blame] | 44 | bool grpc_exec_ctx_ready_to_finish(grpc_exec_ctx *exec_ctx) { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 45 | if ((exec_ctx->flags & GRPC_EXEC_CTX_FLAG_IS_FINISHED) == 0) { |
| 46 | if (exec_ctx->check_ready_to_finish(exec_ctx, |
| 47 | exec_ctx->check_ready_to_finish_arg)) { |
| 48 | exec_ctx->flags |= GRPC_EXEC_CTX_FLAG_IS_FINISHED; |
| 49 | return true; |
| 50 | } |
| 51 | return false; |
| 52 | } else { |
| 53 | return true; |
Craig Tiller | 56aa023 | 2016-05-11 12:37:04 -0700 | [diff] [blame] | 54 | } |
Craig Tiller | 56aa023 | 2016-05-11 12:37:04 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | bool grpc_never_ready_to_finish(grpc_exec_ctx *exec_ctx, void *arg_ignored) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | bool grpc_always_ready_to_finish(grpc_exec_ctx *exec_ctx, void *arg_ignored) { |
| 62 | return true; |
| 63 | } |
| 64 | |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 65 | bool grpc_exec_ctx_flush(grpc_exec_ctx *exec_ctx) { |
| 66 | bool did_something = 0; |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 67 | GPR_TIMER_BEGIN("grpc_exec_ctx_flush", 0); |
Craig Tiller | dfd3a8f | 2016-08-24 09:43:45 -0700 | [diff] [blame] | 68 | for (;;) { |
| 69 | if (!grpc_closure_list_empty(exec_ctx->closure_list)) { |
| 70 | grpc_closure *c = exec_ctx->closure_list.head; |
| 71 | exec_ctx->closure_list.head = exec_ctx->closure_list.tail = NULL; |
| 72 | while (c != NULL) { |
| 73 | grpc_closure *next = c->next_data.next; |
Craig Tiller | 061ef74 | 2016-12-29 10:54:09 -0800 | [diff] [blame] | 74 | grpc_error *error = c->error_data.error; |
Craig Tiller | dfd3a8f | 2016-08-24 09:43:45 -0700 | [diff] [blame] | 75 | did_something = true; |
Craig Tiller | 061ef74 | 2016-12-29 10:54:09 -0800 | [diff] [blame] | 76 | c->cb(exec_ctx, c->cb_arg, error); |
| 77 | GRPC_ERROR_UNREF(error); |
Craig Tiller | dfd3a8f | 2016-08-24 09:43:45 -0700 | [diff] [blame] | 78 | c = next; |
| 79 | } |
Craig Tiller | 686d7a7 | 2016-10-10 14:02:29 -0700 | [diff] [blame] | 80 | } else if (!grpc_combiner_continue_exec_ctx(exec_ctx)) { |
| 81 | break; |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 82 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 83 | } |
Craig Tiller | dfd3a8f | 2016-08-24 09:43:45 -0700 | [diff] [blame] | 84 | GPR_ASSERT(exec_ctx->active_combiner == NULL); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 85 | GPR_TIMER_END("grpc_exec_ctx_flush", 0); |
Craig Tiller | 4086474 | 2015-09-30 08:35:31 -0700 | [diff] [blame] | 86 | return did_something; |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void grpc_exec_ctx_finish(grpc_exec_ctx *exec_ctx) { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 90 | exec_ctx->flags |= GRPC_EXEC_CTX_FLAG_IS_FINISHED; |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 91 | grpc_exec_ctx_flush(exec_ctx); |
| 92 | } |
| 93 | |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 94 | static void exec_ctx_run(grpc_exec_ctx *exec_ctx, grpc_closure *closure, |
| 95 | grpc_error *error) { |
| 96 | closure->cb(exec_ctx, closure->cb_arg, error); |
| 97 | GRPC_ERROR_UNREF(error); |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 100 | static void exec_ctx_sched(grpc_exec_ctx *exec_ctx, grpc_closure *closure, |
| 101 | grpc_error *error) { |
| 102 | grpc_closure_list_append(&exec_ctx->closure_list, closure, error); |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 103 | } |
Craig Tiller | ef1bf87 | 2016-02-28 17:37:33 -0800 | [diff] [blame] | 104 | |
| 105 | void grpc_exec_ctx_global_init(void) {} |
| 106 | void grpc_exec_ctx_global_shutdown(void) {} |
Craig Tiller | ef1bf87 | 2016-02-28 17:37:33 -0800 | [diff] [blame] | 107 | |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 108 | static const grpc_closure_scheduler_vtable exec_ctx_scheduler_vtable = { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 109 | exec_ctx_run, exec_ctx_sched, "exec_ctx"}; |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 110 | static grpc_closure_scheduler exec_ctx_scheduler = {&exec_ctx_scheduler_vtable}; |
| 111 | grpc_closure_scheduler *grpc_schedule_on_exec_ctx = &exec_ctx_scheduler; |