channel_init.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. *
  3. * Copyright 2016 gRPC authors.
  4. *
  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
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  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.
  16. *
  17. */
  18. #include <grpc/support/port_platform.h>
  19. #include "src/core/lib/surface/channel_init.h"
  20. #include <grpc/support/alloc.h>
  21. typedef struct stage_slot {
  22. grpc_channel_init_stage fn;
  23. void* arg;
  24. int priority;
  25. size_t insertion_order;
  26. } stage_slot;
  27. typedef struct stage_slots {
  28. stage_slot* slots;
  29. size_t num_slots;
  30. size_t cap_slots;
  31. } stage_slots;
  32. static stage_slots g_slots[GRPC_NUM_CHANNEL_STACK_TYPES];
  33. static bool g_finalized;
  34. void grpc_channel_init_init(void) {
  35. for (int i = 0; i < GRPC_NUM_CHANNEL_STACK_TYPES; i++) {
  36. g_slots[i].slots = nullptr;
  37. g_slots[i].num_slots = 0;
  38. g_slots[i].cap_slots = 0;
  39. }
  40. g_finalized = false;
  41. }
  42. void grpc_channel_init_register_stage(grpc_channel_stack_type type,
  43. int priority,
  44. grpc_channel_init_stage stage,
  45. void* stage_arg) {
  46. GPR_ASSERT(!g_finalized);
  47. if (g_slots[type].cap_slots == g_slots[type].num_slots) {
  48. g_slots[type].cap_slots = GPR_MAX(8, 3 * g_slots[type].cap_slots / 2);
  49. g_slots[type].slots = static_cast<stage_slot*>(
  50. gpr_realloc(g_slots[type].slots,
  51. g_slots[type].cap_slots * sizeof(*g_slots[type].slots)));
  52. }
  53. stage_slot* s = &g_slots[type].slots[g_slots[type].num_slots++];
  54. s->insertion_order = g_slots[type].num_slots;
  55. s->priority = priority;
  56. s->fn = stage;
  57. s->arg = stage_arg;
  58. }
  59. static int compare_slots(const void* a, const void* b) {
  60. const stage_slot* sa = static_cast<const stage_slot*>(a);
  61. const stage_slot* sb = static_cast<const stage_slot*>(b);
  62. int c = GPR_ICMP(sa->priority, sb->priority);
  63. if (c != 0) return c;
  64. return GPR_ICMP(sa->insertion_order, sb->insertion_order);
  65. }
  66. void grpc_channel_init_finalize(void) {
  67. GPR_ASSERT(!g_finalized);
  68. for (int i = 0; i < GRPC_NUM_CHANNEL_STACK_TYPES; i++) {
  69. qsort(g_slots[i].slots, g_slots[i].num_slots, sizeof(*g_slots[i].slots),
  70. compare_slots);
  71. }
  72. g_finalized = true;
  73. }
  74. void grpc_channel_init_shutdown(void) {
  75. for (int i = 0; i < GRPC_NUM_CHANNEL_STACK_TYPES; i++) {
  76. gpr_free(g_slots[i].slots);
  77. g_slots[i].slots =
  78. static_cast<stage_slot*>((void*)static_cast<uintptr_t>(0xdeadbeef));
  79. }
  80. }
  81. bool grpc_channel_init_create_stack(grpc_channel_stack_builder* builder,
  82. grpc_channel_stack_type type) {
  83. GPR_ASSERT(g_finalized);
  84. grpc_channel_stack_builder_set_name(builder,
  85. grpc_channel_stack_type_string(type));
  86. for (size_t i = 0; i < g_slots[type].num_slots; i++) {
  87. const stage_slot* slot = &g_slots[type].slots[i];
  88. if (!slot->fn(builder, slot->arg)) {
  89. return false;
  90. }
  91. }
  92. return true;
  93. }