exec_ctx.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. *
  3. * Copyright 2015 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/iomgr/exec_ctx.h"
  20. #include <grpc/support/log.h>
  21. #include <grpc/support/sync.h>
  22. #include "src/core/lib/gprpp/thd.h"
  23. #include "src/core/lib/iomgr/combiner.h"
  24. #include "src/core/lib/profiling/timers.h"
  25. static void exec_ctx_run(grpc_closure* closure, grpc_error* error) {
  26. #ifndef NDEBUG
  27. closure->scheduled = false;
  28. if (grpc_trace_closure.enabled()) {
  29. gpr_log(GPR_DEBUG, "running closure %p: created [%s:%d]: %s [%s:%d]",
  30. closure, closure->file_created, closure->line_created,
  31. closure->run ? "run" : "scheduled", closure->file_initiated,
  32. closure->line_initiated);
  33. }
  34. #endif
  35. closure->cb(closure->cb_arg, error);
  36. #ifndef NDEBUG
  37. if (grpc_trace_closure.enabled()) {
  38. gpr_log(GPR_DEBUG, "closure %p finished", closure);
  39. }
  40. #endif
  41. GRPC_ERROR_UNREF(error);
  42. }
  43. static void exec_ctx_sched(grpc_closure* closure, grpc_error* error) {
  44. grpc_closure_list_append(grpc_core::ExecCtx::Get()->closure_list(), closure,
  45. error);
  46. }
  47. static gpr_timespec g_start_time;
  48. static grpc_millis timespec_to_millis_round_down(gpr_timespec ts) {
  49. ts = gpr_time_sub(ts, g_start_time);
  50. double x = GPR_MS_PER_SEC * static_cast<double>(ts.tv_sec) +
  51. static_cast<double>(ts.tv_nsec) / GPR_NS_PER_MS;
  52. if (x < 0) return 0;
  53. if (x > GRPC_MILLIS_INF_FUTURE) return GRPC_MILLIS_INF_FUTURE;
  54. return static_cast<grpc_millis>(x);
  55. }
  56. static grpc_millis timespec_to_millis_round_up(gpr_timespec ts) {
  57. ts = gpr_time_sub(ts, g_start_time);
  58. double x = GPR_MS_PER_SEC * static_cast<double>(ts.tv_sec) +
  59. static_cast<double>(ts.tv_nsec) / GPR_NS_PER_MS +
  60. static_cast<double>(GPR_NS_PER_SEC - 1) /
  61. static_cast<double>(GPR_NS_PER_SEC);
  62. if (x < 0) return 0;
  63. if (x > GRPC_MILLIS_INF_FUTURE) return GRPC_MILLIS_INF_FUTURE;
  64. return static_cast<grpc_millis>(x);
  65. }
  66. gpr_timespec grpc_millis_to_timespec(grpc_millis millis,
  67. gpr_clock_type clock_type) {
  68. // special-case infinities as grpc_millis can be 32bit on some platforms
  69. // while gpr_time_from_millis always takes an int64_t.
  70. if (millis == GRPC_MILLIS_INF_FUTURE) {
  71. return gpr_inf_future(clock_type);
  72. }
  73. if (millis == GRPC_MILLIS_INF_PAST) {
  74. return gpr_inf_past(clock_type);
  75. }
  76. if (clock_type == GPR_TIMESPAN) {
  77. return gpr_time_from_millis(millis, GPR_TIMESPAN);
  78. }
  79. return gpr_time_add(gpr_convert_clock_type(g_start_time, clock_type),
  80. gpr_time_from_millis(millis, GPR_TIMESPAN));
  81. }
  82. grpc_millis grpc_timespec_to_millis_round_down(gpr_timespec ts) {
  83. return timespec_to_millis_round_down(
  84. gpr_convert_clock_type(ts, g_start_time.clock_type));
  85. }
  86. grpc_millis grpc_timespec_to_millis_round_up(gpr_timespec ts) {
  87. return timespec_to_millis_round_up(
  88. gpr_convert_clock_type(ts, g_start_time.clock_type));
  89. }
  90. static const grpc_closure_scheduler_vtable exec_ctx_scheduler_vtable = {
  91. exec_ctx_run, exec_ctx_sched, "exec_ctx"};
  92. static grpc_closure_scheduler exec_ctx_scheduler = {&exec_ctx_scheduler_vtable};
  93. grpc_closure_scheduler* grpc_schedule_on_exec_ctx = &exec_ctx_scheduler;
  94. namespace grpc_core {
  95. GPR_TLS_CLASS_DEF(ExecCtx::exec_ctx_);
  96. void ExecCtx::GlobalInit(void) {
  97. g_start_time = gpr_now(GPR_CLOCK_MONOTONIC);
  98. gpr_tls_init(&exec_ctx_);
  99. }
  100. bool ExecCtx::Flush() {
  101. bool did_something = 0;
  102. GPR_TIMER_SCOPE("grpc_exec_ctx_flush", 0);
  103. for (;;) {
  104. if (!grpc_closure_list_empty(closure_list_)) {
  105. grpc_closure* c = closure_list_.head;
  106. closure_list_.head = closure_list_.tail = nullptr;
  107. while (c != nullptr) {
  108. grpc_closure* next = c->next_data.next;
  109. grpc_error* error = c->error_data.error;
  110. did_something = true;
  111. exec_ctx_run(c, error);
  112. c = next;
  113. }
  114. } else if (!grpc_combiner_continue_exec_ctx()) {
  115. break;
  116. }
  117. }
  118. GPR_ASSERT(combiner_data_.active_combiner == nullptr);
  119. return did_something;
  120. }
  121. grpc_millis ExecCtx::Now() {
  122. if (!now_is_valid_) {
  123. now_ = timespec_to_millis_round_down(gpr_now(GPR_CLOCK_MONOTONIC));
  124. now_is_valid_ = true;
  125. }
  126. return now_;
  127. }
  128. } // namespace grpc_core