blob: b563537f357c6436b74cfe209c6179065553d991 [file] [log] [blame]
Craig Tiller178edfa2016-02-17 20:54:46 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2016 gRPC authors.
Craig Tiller178edfa2016-02-17 20:54:46 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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 Tiller178edfa2016-02-17 20:54:46 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller178edfa2016-02-17 20:54:46 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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 Tiller178edfa2016-02-17 20:54:46 -080016 *
17 */
18
Craig Tiller9533d042016-03-25 17:11:06 -070019#include "src/core/lib/surface/channel_init.h"
Craig Tiller178edfa2016-02-17 20:54:46 -080020
21#include <grpc/support/alloc.h>
22#include <grpc/support/useful.h>
23
24typedef struct stage_slot {
25 grpc_channel_init_stage fn;
Craig Tillerbaa14a92017-11-03 09:09:36 -070026 void* arg;
Craig Tiller178edfa2016-02-17 20:54:46 -080027 int priority;
28 size_t insertion_order;
29} stage_slot;
30
31typedef struct stage_slots {
Craig Tillerbaa14a92017-11-03 09:09:36 -070032 stage_slot* slots;
Craig Tiller178edfa2016-02-17 20:54:46 -080033 size_t num_slots;
34 size_t cap_slots;
35} stage_slots;
36
37static stage_slots g_slots[GRPC_NUM_CHANNEL_STACK_TYPES];
38static bool g_finalized;
39
40void grpc_channel_init_init(void) {
41 for (int i = 0; i < GRPC_NUM_CHANNEL_STACK_TYPES; i++) {
Craig Tiller4782d922017-11-10 09:53:21 -080042 g_slots[i].slots = nullptr;
Craig Tiller178edfa2016-02-17 20:54:46 -080043 g_slots[i].num_slots = 0;
44 g_slots[i].cap_slots = 0;
45 }
46 g_finalized = false;
47}
48
49void grpc_channel_init_register_stage(grpc_channel_stack_type type,
50 int priority,
51 grpc_channel_init_stage stage,
Craig Tillerbaa14a92017-11-03 09:09:36 -070052 void* stage_arg) {
Craig Tiller178edfa2016-02-17 20:54:46 -080053 GPR_ASSERT(!g_finalized);
54 if (g_slots[type].cap_slots == g_slots[type].num_slots) {
55 g_slots[type].cap_slots = GPR_MAX(8, 3 * g_slots[type].cap_slots / 2);
Craig Tillerbaa14a92017-11-03 09:09:36 -070056 g_slots[type].slots = (stage_slot*)gpr_realloc(
Yash Tibrewalca3c1c02017-09-07 22:47:16 -070057 g_slots[type].slots,
58 g_slots[type].cap_slots * sizeof(*g_slots[type].slots));
Craig Tiller178edfa2016-02-17 20:54:46 -080059 }
Craig Tillerbaa14a92017-11-03 09:09:36 -070060 stage_slot* s = &g_slots[type].slots[g_slots[type].num_slots++];
Craig Tiller178edfa2016-02-17 20:54:46 -080061 s->insertion_order = g_slots[type].num_slots;
62 s->priority = priority;
63 s->fn = stage;
64 s->arg = stage_arg;
65}
66
Craig Tillerbaa14a92017-11-03 09:09:36 -070067static int compare_slots(const void* a, const void* b) {
68 const stage_slot* sa = (const stage_slot*)a;
69 const stage_slot* sb = (const stage_slot*)b;
Craig Tiller178edfa2016-02-17 20:54:46 -080070
71 int c = GPR_ICMP(sa->priority, sb->priority);
72 if (c != 0) return c;
73 return GPR_ICMP(sa->insertion_order, sb->insertion_order);
74}
75
76void grpc_channel_init_finalize(void) {
77 GPR_ASSERT(!g_finalized);
78 for (int i = 0; i < GRPC_NUM_CHANNEL_STACK_TYPES; i++) {
79 qsort(g_slots[i].slots, g_slots[i].num_slots, sizeof(*g_slots[i].slots),
80 compare_slots);
81 }
82 g_finalized = true;
83}
84
85void grpc_channel_init_shutdown(void) {
86 for (int i = 0; i < GRPC_NUM_CHANNEL_STACK_TYPES; i++) {
87 gpr_free(g_slots[i].slots);
Craig Tillerbaa14a92017-11-03 09:09:36 -070088 g_slots[i].slots = (stage_slot*)(void*)(uintptr_t)0xdeadbeef;
Craig Tiller178edfa2016-02-17 20:54:46 -080089 }
90}
91
Craig Tillerbaa14a92017-11-03 09:09:36 -070092bool grpc_channel_init_create_stack(grpc_exec_ctx* exec_ctx,
93 grpc_channel_stack_builder* builder,
Craig Tiller839bebe2016-04-06 08:07:11 -070094 grpc_channel_stack_type type) {
Craig Tiller178edfa2016-02-17 20:54:46 -080095 GPR_ASSERT(g_finalized);
96
Craig Tiller4f89b0b2017-04-03 10:00:17 -070097 grpc_channel_stack_builder_set_name(builder,
98 grpc_channel_stack_type_string(type));
Craig Tiller178edfa2016-02-17 20:54:46 -080099
100 for (size_t i = 0; i < g_slots[type].num_slots; i++) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700101 const stage_slot* slot = &g_slots[type].slots[i];
Craig Tiller87a7e1f2016-11-09 09:42:19 -0800102 if (!slot->fn(exec_ctx, builder, slot->arg)) {
Craig Tiller839bebe2016-04-06 08:07:11 -0700103 return false;
Craig Tiller178edfa2016-02-17 20:54:46 -0800104 }
105 }
106
Craig Tiller839bebe2016-04-06 08:07:11 -0700107 return true;
Craig Tiller178edfa2016-02-17 20:54:46 -0800108}