Craig Tiller | 1948244 | 2016-01-25 09:59:20 -0800 | [diff] [blame] | 1 | // |
| 2 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | // Copyright 2016 gRPC authors. |
Craig Tiller | 1948244 | 2016-01-25 09:59:20 -0800 | [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 |
Craig Tiller | 1948244 | 2016-01-25 09:59:20 -0800 | [diff] [blame] | 8 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | 1948244 | 2016-01-25 09:59:20 -0800 | [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. |
Craig Tiller | 1948244 | 2016-01-25 09:59:20 -0800 | [diff] [blame] | 16 | // |
| 17 | // |
Craig Tiller | 694cf8b | 2016-01-15 21:13:25 -0800 | [diff] [blame] | 18 | |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 19 | #include "src/core/ext/filters/client_channel/subchannel_index.h" |
Craig Tiller | 7391f13 | 2016-01-22 06:39:54 -0800 | [diff] [blame] | 20 | |
Craig Tiller | 8cdba66 | 2016-01-22 20:01:55 -0800 | [diff] [blame] | 21 | #include <stdbool.h> |
Craig Tiller | 7391f13 | 2016-01-22 06:39:54 -0800 | [diff] [blame] | 22 | #include <string.h> |
| 23 | |
| 24 | #include <grpc/support/alloc.h> |
| 25 | #include <grpc/support/avl.h> |
Mark D. Roth | 2d7387c | 2016-08-17 13:51:05 -0700 | [diff] [blame] | 26 | #include <grpc/support/string_util.h> |
Craig Tiller | 7391f13 | 2016-01-22 06:39:54 -0800 | [diff] [blame] | 27 | #include <grpc/support/tls.h> |
| 28 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 29 | #include "src/core/lib/channel/channel_args.h" |
Craig Tiller | 7391f13 | 2016-01-22 06:39:54 -0800 | [diff] [blame] | 30 | |
Craig Tiller | 1948244 | 2016-01-25 09:59:20 -0800 | [diff] [blame] | 31 | // a map of subchannel_key --> subchannel, used for detecting connections |
| 32 | // to the same destination in order to share them |
Craig Tiller | 694cf8b | 2016-01-15 21:13:25 -0800 | [diff] [blame] | 33 | static gpr_avl g_subchannel_index; |
| 34 | |
| 35 | static gpr_mu g_mu; |
| 36 | |
Juanli Shen | 6502ecc | 2017-09-13 13:10:54 -0700 | [diff] [blame] | 37 | static gpr_refcount g_refcount; |
| 38 | |
Craig Tiller | 7391f13 | 2016-01-22 06:39:54 -0800 | [diff] [blame] | 39 | struct grpc_subchannel_key { |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 40 | grpc_subchannel_args args; |
Craig Tiller | 694cf8b | 2016-01-15 21:13:25 -0800 | [diff] [blame] | 41 | }; |
| 42 | |
David Garcia Quintas | 9a4ed68 | 2017-07-13 16:12:30 -0700 | [diff] [blame] | 43 | static bool g_force_creation = false; |
| 44 | |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 45 | static grpc_subchannel_key *create_key( |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 46 | const grpc_subchannel_args *args, |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 47 | grpc_channel_args *(*copy_channel_args)(const grpc_channel_args *args)) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 48 | grpc_subchannel_key *k = (grpc_subchannel_key *)gpr_malloc(sizeof(*k)); |
Craig Tiller | 7391f13 | 2016-01-22 06:39:54 -0800 | [diff] [blame] | 49 | k->args.filter_count = args->filter_count; |
Craig Tiller | 525654a | 2016-05-03 22:38:41 -0700 | [diff] [blame] | 50 | if (k->args.filter_count > 0) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 51 | k->args.filters = (const grpc_channel_filter **)gpr_malloc( |
| 52 | sizeof(*k->args.filters) * k->args.filter_count); |
Craig Tiller | 525654a | 2016-05-03 22:38:41 -0700 | [diff] [blame] | 53 | memcpy((grpc_channel_filter *)k->args.filters, args->filters, |
| 54 | sizeof(*k->args.filters) * k->args.filter_count); |
| 55 | } else { |
| 56 | k->args.filters = NULL; |
| 57 | } |
Craig Tiller | 7391f13 | 2016-01-22 06:39:54 -0800 | [diff] [blame] | 58 | k->args.args = copy_channel_args(args->args); |
Craig Tiller | 694cf8b | 2016-01-15 21:13:25 -0800 | [diff] [blame] | 59 | return k; |
| 60 | } |
| 61 | |
Mark D. Roth | e3a2100 | 2016-10-24 13:29:05 -0700 | [diff] [blame] | 62 | grpc_subchannel_key *grpc_subchannel_key_create( |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 63 | const grpc_subchannel_args *args) { |
| 64 | return create_key(args, grpc_channel_args_normalize); |
Craig Tiller | 694cf8b | 2016-01-15 21:13:25 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Craig Tiller | 7391f13 | 2016-01-22 06:39:54 -0800 | [diff] [blame] | 67 | static grpc_subchannel_key *subchannel_key_copy(grpc_subchannel_key *k) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 68 | return create_key(&k->args, grpc_channel_args_copy); |
Craig Tiller | 694cf8b | 2016-01-15 21:13:25 -0800 | [diff] [blame] | 69 | } |
| 70 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 71 | int grpc_subchannel_key_compare(const grpc_subchannel_key *a, |
| 72 | const grpc_subchannel_key *b) { |
David Garcia Quintas | 9a4ed68 | 2017-07-13 16:12:30 -0700 | [diff] [blame] | 73 | if (g_force_creation) return false; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 74 | int c = GPR_ICMP(a->args.filter_count, b->args.filter_count); |
Craig Tiller | 7391f13 | 2016-01-22 06:39:54 -0800 | [diff] [blame] | 75 | if (c != 0) return c; |
Craig Tiller | 525654a | 2016-05-03 22:38:41 -0700 | [diff] [blame] | 76 | if (a->args.filter_count > 0) { |
| 77 | c = memcmp(a->args.filters, b->args.filters, |
| 78 | a->args.filter_count * sizeof(*a->args.filters)); |
| 79 | if (c != 0) return c; |
| 80 | } |
Craig Tiller | 7391f13 | 2016-01-22 06:39:54 -0800 | [diff] [blame] | 81 | return grpc_channel_args_compare(a->args.args, b->args.args); |
| 82 | } |
| 83 | |
Craig Tiller | 1948244 | 2016-01-25 09:59:20 -0800 | [diff] [blame] | 84 | void grpc_subchannel_key_destroy(grpc_exec_ctx *exec_ctx, |
| 85 | grpc_subchannel_key *k) { |
Craig Tiller | fdb603f | 2016-01-25 11:56:24 -0800 | [diff] [blame] | 86 | gpr_free((grpc_channel_args *)k->args.filters); |
Craig Tiller | 4cc1c35 | 2016-12-27 08:48:01 -0800 | [diff] [blame] | 87 | grpc_channel_args_destroy(exec_ctx, (grpc_channel_args *)k->args.args); |
Craig Tiller | 694cf8b | 2016-01-15 21:13:25 -0800 | [diff] [blame] | 88 | gpr_free(k); |
| 89 | } |
| 90 | |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 91 | static void sck_avl_destroy(void *p, void *user_data) { |
| 92 | grpc_exec_ctx *exec_ctx = (grpc_exec_ctx *)user_data; |
Yash Tibrewal | bc130da | 2017-09-12 22:44:08 -0700 | [diff] [blame] | 93 | grpc_subchannel_key_destroy(exec_ctx, (grpc_subchannel_key *)p); |
Craig Tiller | 1948244 | 2016-01-25 09:59:20 -0800 | [diff] [blame] | 94 | } |
Craig Tiller | 694cf8b | 2016-01-15 21:13:25 -0800 | [diff] [blame] | 95 | |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 96 | static void *sck_avl_copy(void *p, void *unused) { |
Yash Tibrewal | bc130da | 2017-09-12 22:44:08 -0700 | [diff] [blame] | 97 | return subchannel_key_copy((grpc_subchannel_key *)p); |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 98 | } |
Craig Tiller | 694cf8b | 2016-01-15 21:13:25 -0800 | [diff] [blame] | 99 | |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 100 | static long sck_avl_compare(void *a, void *b, void *unused) { |
Yash Tibrewal | bc130da | 2017-09-12 22:44:08 -0700 | [diff] [blame] | 101 | return grpc_subchannel_key_compare((grpc_subchannel_key *)a, |
| 102 | (grpc_subchannel_key *)b); |
Craig Tiller | 694cf8b | 2016-01-15 21:13:25 -0800 | [diff] [blame] | 103 | } |
| 104 | |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 105 | static void scv_avl_destroy(void *p, void *user_data) { |
| 106 | grpc_exec_ctx *exec_ctx = (grpc_exec_ctx *)user_data; |
Yash Tibrewal | bc130da | 2017-09-12 22:44:08 -0700 | [diff] [blame] | 107 | GRPC_SUBCHANNEL_WEAK_UNREF(exec_ctx, (grpc_subchannel *)p, |
| 108 | "subchannel_index"); |
Craig Tiller | 694cf8b | 2016-01-15 21:13:25 -0800 | [diff] [blame] | 109 | } |
| 110 | |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 111 | static void *scv_avl_copy(void *p, void *unused) { |
Yash Tibrewal | bc130da | 2017-09-12 22:44:08 -0700 | [diff] [blame] | 112 | GRPC_SUBCHANNEL_WEAK_REF((grpc_subchannel *)p, "subchannel_index"); |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 113 | return p; |
Craig Tiller | 694cf8b | 2016-01-15 21:13:25 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static const gpr_avl_vtable subchannel_avl_vtable = { |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 117 | .destroy_key = sck_avl_destroy, |
| 118 | .copy_key = sck_avl_copy, |
| 119 | .compare_keys = sck_avl_compare, |
| 120 | .destroy_value = scv_avl_destroy, |
| 121 | .copy_value = scv_avl_copy}; |
Craig Tiller | 694cf8b | 2016-01-15 21:13:25 -0800 | [diff] [blame] | 122 | |
Craig Tiller | 8cdba66 | 2016-01-22 20:01:55 -0800 | [diff] [blame] | 123 | void grpc_subchannel_index_init(void) { |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 124 | g_subchannel_index = gpr_avl_create(&subchannel_avl_vtable); |
| 125 | gpr_mu_init(&g_mu); |
Juanli Shen | 6502ecc | 2017-09-13 13:10:54 -0700 | [diff] [blame] | 126 | gpr_ref_init(&g_refcount, 1); |
Craig Tiller | 8cdba66 | 2016-01-22 20:01:55 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void grpc_subchannel_index_shutdown(void) { |
Juanli Shen | 6502ecc | 2017-09-13 13:10:54 -0700 | [diff] [blame] | 130 | // TODO(juanlishen): This refcounting mechanism may lead to memory leackage. |
| 131 | // To solve that, we should force polling to flush any pending callbacks, then |
| 132 | // shutdown safely. |
| 133 | grpc_subchannel_index_unref(); |
Craig Tiller | 8cdba66 | 2016-01-22 20:01:55 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Juanli Shen | 6502ecc | 2017-09-13 13:10:54 -0700 | [diff] [blame] | 136 | void grpc_subchannel_index_unref(void) { |
| 137 | if (gpr_unref(&g_refcount)) { |
| 138 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 139 | gpr_mu_destroy(&g_mu); |
| 140 | gpr_avl_unref(g_subchannel_index, &exec_ctx); |
| 141 | grpc_exec_ctx_finish(&exec_ctx); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void grpc_subchannel_index_ref(void) { gpr_ref_non_zero(&g_refcount); } |
| 146 | |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 147 | grpc_subchannel *grpc_subchannel_index_find(grpc_exec_ctx *exec_ctx, |
| 148 | grpc_subchannel_key *key) { |
Craig Tiller | 1948244 | 2016-01-25 09:59:20 -0800 | [diff] [blame] | 149 | // Lock, and take a reference to the subchannel index. |
| 150 | // We don't need to do the search under a lock as avl's are immutable. |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 151 | gpr_mu_lock(&g_mu); |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 152 | gpr_avl index = gpr_avl_ref(g_subchannel_index, exec_ctx); |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 153 | gpr_mu_unlock(&g_mu); |
Craig Tiller | 694cf8b | 2016-01-15 21:13:25 -0800 | [diff] [blame] | 154 | |
Yash Tibrewal | 7cdd99c | 2017-09-08 16:04:12 -0700 | [diff] [blame] | 155 | grpc_subchannel *c = GRPC_SUBCHANNEL_REF_FROM_WEAK_REF( |
| 156 | (grpc_subchannel *)gpr_avl_get(index, key, exec_ctx), "index_find"); |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 157 | gpr_avl_unref(index, exec_ctx); |
Craig Tiller | 694cf8b | 2016-01-15 21:13:25 -0800 | [diff] [blame] | 158 | |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 159 | return c; |
Craig Tiller | 694cf8b | 2016-01-15 21:13:25 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 162 | grpc_subchannel *grpc_subchannel_index_register(grpc_exec_ctx *exec_ctx, |
| 163 | grpc_subchannel_key *key, |
| 164 | grpc_subchannel *constructed) { |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 165 | grpc_subchannel *c = NULL; |
David Garcia Quintas | 6d8ca69 | 2017-05-11 14:29:34 -0700 | [diff] [blame] | 166 | bool need_to_unref_constructed; |
Craig Tiller | 694cf8b | 2016-01-15 21:13:25 -0800 | [diff] [blame] | 167 | |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 168 | while (c == NULL) { |
David Garcia Quintas | 6d8ca69 | 2017-05-11 14:29:34 -0700 | [diff] [blame] | 169 | need_to_unref_constructed = false; |
| 170 | |
Craig Tiller | 1948244 | 2016-01-25 09:59:20 -0800 | [diff] [blame] | 171 | // Compare and swap loop: |
| 172 | // - take a reference to the current index |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 173 | gpr_mu_lock(&g_mu); |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 174 | gpr_avl index = gpr_avl_ref(g_subchannel_index, exec_ctx); |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 175 | gpr_mu_unlock(&g_mu); |
Craig Tiller | 694cf8b | 2016-01-15 21:13:25 -0800 | [diff] [blame] | 176 | |
Craig Tiller | 1948244 | 2016-01-25 09:59:20 -0800 | [diff] [blame] | 177 | // - Check to see if a subchannel already exists |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 178 | c = (grpc_subchannel *)gpr_avl_get(index, key, exec_ctx); |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 179 | if (c != NULL) { |
David Garcia Quintas | 6d8ca69 | 2017-05-11 14:29:34 -0700 | [diff] [blame] | 180 | c = GRPC_SUBCHANNEL_REF_FROM_WEAK_REF(c, "index_register"); |
| 181 | } |
| 182 | if (c != NULL) { |
Craig Tiller | 1948244 | 2016-01-25 09:59:20 -0800 | [diff] [blame] | 183 | // yes -> we're done |
David Garcia Quintas | 6d8ca69 | 2017-05-11 14:29:34 -0700 | [diff] [blame] | 184 | need_to_unref_constructed = true; |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 185 | } else { |
Craig Tiller | 1948244 | 2016-01-25 09:59:20 -0800 | [diff] [blame] | 186 | // no -> update the avl and compare/swap |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 187 | gpr_avl updated = gpr_avl_add( |
| 188 | gpr_avl_ref(index, exec_ctx), subchannel_key_copy(key), |
| 189 | GRPC_SUBCHANNEL_WEAK_REF(constructed, "index_register"), exec_ctx); |
Craig Tiller | 694cf8b | 2016-01-15 21:13:25 -0800 | [diff] [blame] | 190 | |
Craig Tiller | 1948244 | 2016-01-25 09:59:20 -0800 | [diff] [blame] | 191 | // it may happen (but it's expected to be unlikely) |
| 192 | // that some other thread has changed the index: |
| 193 | // compare/swap here to check that, and retry as necessary |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 194 | gpr_mu_lock(&g_mu); |
| 195 | if (index.root == g_subchannel_index.root) { |
| 196 | GPR_SWAP(gpr_avl, updated, g_subchannel_index); |
| 197 | c = constructed; |
| 198 | } |
| 199 | gpr_mu_unlock(&g_mu); |
Craig Tiller | 1948244 | 2016-01-25 09:59:20 -0800 | [diff] [blame] | 200 | |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 201 | gpr_avl_unref(updated, exec_ctx); |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 202 | } |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 203 | gpr_avl_unref(index, exec_ctx); |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 204 | } |
Craig Tiller | 694cf8b | 2016-01-15 21:13:25 -0800 | [diff] [blame] | 205 | |
David Garcia Quintas | 6d8ca69 | 2017-05-11 14:29:34 -0700 | [diff] [blame] | 206 | if (need_to_unref_constructed) { |
| 207 | GRPC_SUBCHANNEL_UNREF(exec_ctx, constructed, "index_register"); |
| 208 | } |
| 209 | |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 210 | return c; |
Craig Tiller | 694cf8b | 2016-01-15 21:13:25 -0800 | [diff] [blame] | 211 | } |
Craig Tiller | 8cdba66 | 2016-01-22 20:01:55 -0800 | [diff] [blame] | 212 | |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 213 | void grpc_subchannel_index_unregister(grpc_exec_ctx *exec_ctx, |
| 214 | grpc_subchannel_key *key, |
| 215 | grpc_subchannel *constructed) { |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 216 | bool done = false; |
| 217 | while (!done) { |
Craig Tiller | 1948244 | 2016-01-25 09:59:20 -0800 | [diff] [blame] | 218 | // Compare and swap loop: |
| 219 | // - take a reference to the current index |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 220 | gpr_mu_lock(&g_mu); |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 221 | gpr_avl index = gpr_avl_ref(g_subchannel_index, exec_ctx); |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 222 | gpr_mu_unlock(&g_mu); |
Craig Tiller | 8cdba66 | 2016-01-22 20:01:55 -0800 | [diff] [blame] | 223 | |
Craig Tiller | 1948244 | 2016-01-25 09:59:20 -0800 | [diff] [blame] | 224 | // Check to see if this key still refers to the previously |
| 225 | // registered subchannel |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 226 | grpc_subchannel *c = (grpc_subchannel *)gpr_avl_get(index, key, exec_ctx); |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 227 | if (c != constructed) { |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 228 | gpr_avl_unref(index, exec_ctx); |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 229 | break; |
| 230 | } |
Craig Tiller | 8cdba66 | 2016-01-22 20:01:55 -0800 | [diff] [blame] | 231 | |
Craig Tiller | 1948244 | 2016-01-25 09:59:20 -0800 | [diff] [blame] | 232 | // compare and swap the update (some other thread may have |
| 233 | // mutated the index behind us) |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 234 | gpr_avl updated = |
| 235 | gpr_avl_remove(gpr_avl_ref(index, exec_ctx), key, exec_ctx); |
Craig Tiller | 8cdba66 | 2016-01-22 20:01:55 -0800 | [diff] [blame] | 236 | |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 237 | gpr_mu_lock(&g_mu); |
| 238 | if (index.root == g_subchannel_index.root) { |
| 239 | GPR_SWAP(gpr_avl, updated, g_subchannel_index); |
| 240 | done = true; |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 241 | } |
| 242 | gpr_mu_unlock(&g_mu); |
Craig Tiller | 8cdba66 | 2016-01-22 20:01:55 -0800 | [diff] [blame] | 243 | |
yang-g | 59ef648 | 2017-07-17 13:52:24 -0700 | [diff] [blame] | 244 | gpr_avl_unref(updated, exec_ctx); |
| 245 | gpr_avl_unref(index, exec_ctx); |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 246 | } |
Craig Tiller | 8cdba66 | 2016-01-22 20:01:55 -0800 | [diff] [blame] | 247 | } |
David Garcia Quintas | 9a4ed68 | 2017-07-13 16:12:30 -0700 | [diff] [blame] | 248 | |
| 249 | void grpc_subchannel_index_test_only_set_force_creation(bool force_creation) { |
| 250 | g_force_creation = force_creation; |
| 251 | } |