Craig Tiller | 298751c | 2015-09-22 09:41:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Craig Tiller | 298751c | 2015-09-22 09:41:05 -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 | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 34 | #ifndef GRPC_CORE_LIB_IOMGR_CLOSURE_H |
| 35 | #define GRPC_CORE_LIB_IOMGR_CLOSURE_H |
Craig Tiller | 298751c | 2015-09-22 09:41:05 -0700 | [diff] [blame] | 36 | |
Craig Tiller | 48db18f | 2015-11-02 14:14:51 -0800 | [diff] [blame] | 37 | #include <grpc/support/port_platform.h> |
Craig Tiller | 7cdad96 | 2016-11-10 08:37:21 -0800 | [diff] [blame] | 38 | |
| 39 | #include <grpc/impl/codegen/exec_ctx_fwd.h> |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 40 | #include <stdbool.h> |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 41 | #include "src/core/lib/iomgr/error.h" |
Craig Tiller | f7cade1 | 2016-07-07 21:41:10 -0700 | [diff] [blame] | 42 | #include "src/core/lib/support/mpscq.h" |
Craig Tiller | 298751c | 2015-09-22 09:41:05 -0700 | [diff] [blame] | 43 | |
| 44 | struct grpc_closure; |
| 45 | typedef struct grpc_closure grpc_closure; |
| 46 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 47 | typedef struct grpc_closure_list { |
Craig Tiller | 298751c | 2015-09-22 09:41:05 -0700 | [diff] [blame] | 48 | grpc_closure *head; |
| 49 | grpc_closure *tail; |
| 50 | } grpc_closure_list; |
| 51 | |
| 52 | /** gRPC Callback definition. |
| 53 | * |
| 54 | * \param arg Arbitrary input. |
Craig Tiller | 081f1a3 | 2016-05-24 13:16:42 -0700 | [diff] [blame] | 55 | * \param error GRPC_ERROR_NONE if no error occurred, otherwise some grpc_error |
| 56 | * describing what went wrong */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 57 | typedef void (*grpc_iomgr_cb_func)(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 58 | grpc_error *error); |
Craig Tiller | 298751c | 2015-09-22 09:41:05 -0700 | [diff] [blame] | 59 | |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 60 | typedef struct grpc_closure_scheduler grpc_closure_scheduler; |
| 61 | |
| 62 | typedef struct grpc_closure_scheduler_vtable { |
| 63 | /* NOTE: for all these functions, closure->scheduler == the scheduler that was |
| 64 | used to find this vtable */ |
| 65 | void (*run)(grpc_exec_ctx *exec_ctx, grpc_closure *closure, |
| 66 | grpc_error *error); |
| 67 | void (*sched)(grpc_exec_ctx *exec_ctx, grpc_closure *closure, |
| 68 | grpc_error *error); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 69 | const char *name; |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 70 | } grpc_closure_scheduler_vtable; |
| 71 | |
| 72 | /** Abstract type that can schedule closures for execution */ |
| 73 | struct grpc_closure_scheduler { |
| 74 | const grpc_closure_scheduler_vtable *vtable; |
| 75 | }; |
| 76 | |
Craig Tiller | 298751c | 2015-09-22 09:41:05 -0700 | [diff] [blame] | 77 | /** A closure over a grpc_iomgr_cb_func. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 78 | struct grpc_closure { |
Craig Tiller | f7cade1 | 2016-07-07 21:41:10 -0700 | [diff] [blame] | 79 | /** Once queued, next indicates the next queued closure; before then, scratch |
| 80 | * space */ |
| 81 | union { |
| 82 | grpc_closure *next; |
| 83 | gpr_mpscq_node atm_next; |
| 84 | uintptr_t scratch; |
| 85 | } next_data; |
| 86 | |
Craig Tiller | 298751c | 2015-09-22 09:41:05 -0700 | [diff] [blame] | 87 | /** Bound callback. */ |
| 88 | grpc_iomgr_cb_func cb; |
| 89 | |
| 90 | /** Arguments to be passed to "cb". */ |
| 91 | void *cb_arg; |
| 92 | |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 93 | /** Scheduler to schedule against: NULL to schedule against current execution |
| 94 | context */ |
| 95 | grpc_closure_scheduler *scheduler; |
| 96 | |
Craig Tiller | 081f1a3 | 2016-05-24 13:16:42 -0700 | [diff] [blame] | 97 | /** Once queued, the result of the closure. Before then: scratch space */ |
Craig Tiller | a7cd41c | 2016-08-31 12:59:24 -0700 | [diff] [blame] | 98 | union { |
| 99 | grpc_error *error; |
| 100 | uintptr_t scratch; |
| 101 | } error_data; |
Craig Tiller | 298751c | 2015-09-22 09:41:05 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 104 | /** Initializes \a closure with \a cb and \a cb_arg. Returns \a closure. */ |
| 105 | grpc_closure *grpc_closure_init(grpc_closure *closure, grpc_iomgr_cb_func cb, |
| 106 | void *cb_arg, |
| 107 | grpc_closure_scheduler *scheduler); |
Craig Tiller | 298751c | 2015-09-22 09:41:05 -0700 | [diff] [blame] | 108 | |
Craig Tiller | 3ff551b | 2015-10-06 09:11:37 -0700 | [diff] [blame] | 109 | /* Create a heap allocated closure: try to avoid except for very rare events */ |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 110 | grpc_closure *grpc_closure_create(grpc_iomgr_cb_func cb, void *cb_arg, |
| 111 | grpc_closure_scheduler *scheduler); |
Craig Tiller | 3ff551b | 2015-10-06 09:11:37 -0700 | [diff] [blame] | 112 | |
Craig Tiller | 298751c | 2015-09-22 09:41:05 -0700 | [diff] [blame] | 113 | #define GRPC_CLOSURE_LIST_INIT \ |
| 114 | { NULL, NULL } |
| 115 | |
Craig Tiller | a36857d | 2016-07-08 16:57:42 -0700 | [diff] [blame] | 116 | void grpc_closure_list_init(grpc_closure_list *list); |
| 117 | |
Craig Tiller | 081f1a3 | 2016-05-24 13:16:42 -0700 | [diff] [blame] | 118 | /** add \a closure to the end of \a list |
| 119 | and set \a closure's result to \a error */ |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 120 | void grpc_closure_list_append(grpc_closure_list *list, grpc_closure *closure, |
| 121 | grpc_error *error); |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 122 | |
Craig Tiller | 0ede545 | 2016-04-23 12:21:45 -0700 | [diff] [blame] | 123 | /** force all success bits in \a list to false */ |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 124 | void grpc_closure_list_fail_all(grpc_closure_list *list, |
| 125 | grpc_error *forced_failure); |
Craig Tiller | 0ede545 | 2016-04-23 12:21:45 -0700 | [diff] [blame] | 126 | |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 127 | /** append all closures from \a src to \a dst and empty \a src. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 128 | void grpc_closure_list_move(grpc_closure_list *src, grpc_closure_list *dst); |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 129 | |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 130 | /** return whether \a list is empty. */ |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 131 | bool grpc_closure_list_empty(grpc_closure_list list); |
Craig Tiller | 298751c | 2015-09-22 09:41:05 -0700 | [diff] [blame] | 132 | |
Craig Tiller | de2c41c | 2016-09-01 15:08:08 -0700 | [diff] [blame] | 133 | /** Run a closure directly. Caller ensures that no locks are being held above. |
| 134 | * Note that calling this at the end of a closure callback function itself is |
| 135 | * by definition safe. */ |
Craig Tiller | 44b12f9 | 2016-09-08 10:06:14 -0700 | [diff] [blame] | 136 | void grpc_closure_run(grpc_exec_ctx *exec_ctx, grpc_closure *closure, |
| 137 | grpc_error *error); |
Craig Tiller | de2c41c | 2016-09-01 15:08:08 -0700 | [diff] [blame] | 138 | |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 139 | /** Schedule a closure to be run. Does not need to be run from a safe point. */ |
| 140 | void grpc_closure_sched(grpc_exec_ctx *exec_ctx, grpc_closure *closure, |
| 141 | grpc_error *error); |
| 142 | |
| 143 | /** Schedule all closures in a list to be run. Does not need to be run from a |
| 144 | * safe point. */ |
| 145 | void grpc_closure_list_sched(grpc_exec_ctx *exec_ctx, |
| 146 | grpc_closure_list *closure_list); |
| 147 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 148 | #endif /* GRPC_CORE_LIB_IOMGR_CLOSURE_H */ |