blob: a1391ffe56002e3a561a19b3813b2bc4d6e4d2da [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;
26 void *arg;
27 int priority;
28 size_t insertion_order;
29} stage_slot;
30
31typedef struct stage_slots {
32 stage_slot *slots;
33 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++) {
42 g_slots[i].slots = NULL;
43 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,
52 void *stage_arg) {
53 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);
56 g_slots[type].slots =
57 gpr_realloc(g_slots[type].slots,
58 g_slots[type].cap_slots * sizeof(*g_slots[type].slots));
59 }
60 stage_slot *s = &g_slots[type].slots[g_slots[type].num_slots++];
61 s->insertion_order = g_slots[type].num_slots;
62 s->priority = priority;
63 s->fn = stage;
64 s->arg = stage_arg;
65}
66
67static int compare_slots(const void *a, const void *b) {
68 const stage_slot *sa = a;
69 const stage_slot *sb = b;
70
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 Tiller3d2686b2016-03-21 07:12:54 -070088 g_slots[i].slots = (void *)(uintptr_t)0xdeadbeef;
Craig Tiller178edfa2016-02-17 20:54:46 -080089 }
90}
91
Craig Tiller839bebe2016-04-06 08:07:11 -070092bool grpc_channel_init_create_stack(grpc_exec_ctx *exec_ctx,
93 grpc_channel_stack_builder *builder,
94 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++) {
101 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}