blob: 83bb436bd0ad426c6f4f7fdfd964c0ad39fd301f [file] [log] [blame]
Craig Tiller8af4c332015-09-22 12:32:31 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tiller8af4c332015-09-22 12:32:31 -07004 * 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 Tiller9533d042016-03-25 17:11:06 -070034#include "src/core/lib/iomgr/exec_ctx.h"
Craig Tiller8af4c332015-09-22 12:32:31 -070035
Craig Tiller258f8de2015-09-23 12:32:40 -070036#include <grpc/support/log.h>
Craig Tilleref1bf872016-02-28 17:37:33 -080037#include <grpc/support/sync.h>
38#include <grpc/support/thd.h>
Craig Tiller258f8de2015-09-23 12:32:40 -070039
Craig Tillerdfd3a8f2016-08-24 09:43:45 -070040#include "src/core/lib/iomgr/combiner.h"
Craig Tiller099eb6e2016-06-30 15:10:12 -070041#include "src/core/lib/iomgr/workqueue.h"
Craig Tiller9533d042016-03-25 17:11:06 -070042#include "src/core/lib/profiling/timers.h"
Craig Tiller86253ca2015-10-08 13:31:02 -070043
Craig Tiller56aa0232016-05-11 12:37:04 -070044bool grpc_exec_ctx_ready_to_finish(grpc_exec_ctx *exec_ctx) {
Craig Tiller7c70b6c2017-01-23 07:48:42 -080045 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 Tiller56aa0232016-05-11 12:37:04 -070054 }
Craig Tiller56aa0232016-05-11 12:37:04 -070055}
56
57bool grpc_never_ready_to_finish(grpc_exec_ctx *exec_ctx, void *arg_ignored) {
58 return false;
59}
60
61bool grpc_always_ready_to_finish(grpc_exec_ctx *exec_ctx, void *arg_ignored) {
62 return true;
63}
64
Craig Tiller6c396862016-01-28 13:53:40 -080065bool grpc_exec_ctx_flush(grpc_exec_ctx *exec_ctx) {
66 bool did_something = 0;
Craig Tiller0ba432d2015-10-09 16:57:11 -070067 GPR_TIMER_BEGIN("grpc_exec_ctx_flush", 0);
Craig Tillerdfd3a8f2016-08-24 09:43:45 -070068 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 Tiller061ef742016-12-29 10:54:09 -080074 grpc_error *error = c->error_data.error;
Craig Tillerdfd3a8f2016-08-24 09:43:45 -070075 did_something = true;
Craig Tiller061ef742016-12-29 10:54:09 -080076 c->cb(exec_ctx, c->cb_arg, error);
77 GRPC_ERROR_UNREF(error);
Craig Tillerdfd3a8f2016-08-24 09:43:45 -070078 c = next;
79 }
Craig Tiller686d7a72016-10-10 14:02:29 -070080 } else if (!grpc_combiner_continue_exec_ctx(exec_ctx)) {
81 break;
Craig Tiller8af4c332015-09-22 12:32:31 -070082 }
Craig Tillera82950e2015-09-22 12:33:20 -070083 }
Craig Tillerdfd3a8f2016-08-24 09:43:45 -070084 GPR_ASSERT(exec_ctx->active_combiner == NULL);
Craig Tiller0ba432d2015-10-09 16:57:11 -070085 GPR_TIMER_END("grpc_exec_ctx_flush", 0);
Craig Tiller40864742015-09-30 08:35:31 -070086 return did_something;
Craig Tiller8af4c332015-09-22 12:32:31 -070087}
88
89void grpc_exec_ctx_finish(grpc_exec_ctx *exec_ctx) {
Craig Tiller7c70b6c2017-01-23 07:48:42 -080090 exec_ctx->flags |= GRPC_EXEC_CTX_FLAG_IS_FINISHED;
Craig Tiller8af4c332015-09-22 12:32:31 -070091 grpc_exec_ctx_flush(exec_ctx);
92}
93
Craig Tiller91031da2016-12-28 15:44:25 -080094static 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 Tiller8af4c332015-09-22 12:32:31 -070098}
99
Craig Tiller91031da2016-12-28 15:44:25 -0800100static 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 Tiller8af4c332015-09-22 12:32:31 -0700103}
Craig Tilleref1bf872016-02-28 17:37:33 -0800104
105void grpc_exec_ctx_global_init(void) {}
106void grpc_exec_ctx_global_shutdown(void) {}
Craig Tilleref1bf872016-02-28 17:37:33 -0800107
Craig Tiller91031da2016-12-28 15:44:25 -0800108static const grpc_closure_scheduler_vtable exec_ctx_scheduler_vtable = {
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800109 exec_ctx_run, exec_ctx_sched, "exec_ctx"};
Craig Tiller91031da2016-12-28 15:44:25 -0800110static grpc_closure_scheduler exec_ctx_scheduler = {&exec_ctx_scheduler_vtable};
111grpc_closure_scheduler *grpc_schedule_on_exec_ctx = &exec_ctx_scheduler;