Fix grpc_millis type (timers are broken on 32-bit systems otherwise)

pull/14894/head
Sree Kuchibhotla 7 years ago
parent 22054d4a22
commit c069af240b
  1. 18
      src/core/lib/iomgr/exec_ctx.cc
  2. 6
      src/core/lib/iomgr/exec_ctx.h
  3. 2
      src/core/lib/iomgr/timer.h
  4. 5
      src/core/lib/iomgr/timer_generic.cc

@ -53,24 +53,24 @@ static void exec_ctx_sched(grpc_closure* closure, grpc_error* error) {
static gpr_timespec g_start_time; static gpr_timespec g_start_time;
static gpr_atm timespec_to_atm_round_down(gpr_timespec ts) { static grpc_millis timespec_to_millis_round_down(gpr_timespec ts) {
ts = gpr_time_sub(ts, g_start_time); ts = gpr_time_sub(ts, g_start_time);
double x = GPR_MS_PER_SEC * static_cast<double>(ts.tv_sec) + double x = GPR_MS_PER_SEC * static_cast<double>(ts.tv_sec) +
static_cast<double>(ts.tv_nsec) / GPR_NS_PER_MS; static_cast<double>(ts.tv_nsec) / GPR_NS_PER_MS;
if (x < 0) return 0; if (x < 0) return 0;
if (x > GPR_ATM_MAX) return GPR_ATM_MAX; if (x > GRPC_MILLIS_INF_FUTURE) return GRPC_MILLIS_INF_FUTURE;
return static_cast<gpr_atm>(x); return static_cast<grpc_millis>(x);
} }
static gpr_atm timespec_to_atm_round_up(gpr_timespec ts) { static grpc_millis timespec_to_millis_round_up(gpr_timespec ts) {
ts = gpr_time_sub(ts, g_start_time); ts = gpr_time_sub(ts, g_start_time);
double x = GPR_MS_PER_SEC * static_cast<double>(ts.tv_sec) + double x = GPR_MS_PER_SEC * static_cast<double>(ts.tv_sec) +
static_cast<double>(ts.tv_nsec) / GPR_NS_PER_MS + static_cast<double>(ts.tv_nsec) / GPR_NS_PER_MS +
static_cast<double>(GPR_NS_PER_SEC - 1) / static_cast<double>(GPR_NS_PER_SEC - 1) /
static_cast<double>(GPR_NS_PER_SEC); static_cast<double>(GPR_NS_PER_SEC);
if (x < 0) return 0; if (x < 0) return 0;
if (x > GPR_ATM_MAX) return GPR_ATM_MAX; if (x > GRPC_MILLIS_INF_FUTURE) return GRPC_MILLIS_INF_FUTURE;
return static_cast<gpr_atm>(x); return static_cast<grpc_millis>(x);
} }
gpr_timespec grpc_millis_to_timespec(grpc_millis millis, gpr_timespec grpc_millis_to_timespec(grpc_millis millis,
@ -92,12 +92,12 @@ gpr_timespec grpc_millis_to_timespec(grpc_millis millis,
} }
grpc_millis grpc_timespec_to_millis_round_down(gpr_timespec ts) { grpc_millis grpc_timespec_to_millis_round_down(gpr_timespec ts) {
return timespec_to_atm_round_down( return timespec_to_millis_round_down(
gpr_convert_clock_type(ts, g_start_time.clock_type)); gpr_convert_clock_type(ts, g_start_time.clock_type));
} }
grpc_millis grpc_timespec_to_millis_round_up(gpr_timespec ts) { grpc_millis grpc_timespec_to_millis_round_up(gpr_timespec ts) {
return timespec_to_atm_round_up( return timespec_to_millis_round_up(
gpr_convert_clock_type(ts, g_start_time.clock_type)); gpr_convert_clock_type(ts, g_start_time.clock_type));
} }
@ -138,7 +138,7 @@ bool ExecCtx::Flush() {
grpc_millis ExecCtx::Now() { grpc_millis ExecCtx::Now() {
if (!now_is_valid_) { if (!now_is_valid_) {
now_ = timespec_to_atm_round_down(gpr_now(GPR_CLOCK_MONOTONIC)); now_ = timespec_to_millis_round_down(gpr_now(GPR_CLOCK_MONOTONIC));
now_is_valid_ = true; now_is_valid_ = true;
} }
return now_; return now_;

@ -28,10 +28,10 @@
#include "src/core/lib/gpr/tls.h" #include "src/core/lib/gpr/tls.h"
#include "src/core/lib/iomgr/closure.h" #include "src/core/lib/iomgr/closure.h"
typedef gpr_atm grpc_millis; typedef int64_t grpc_millis;
#define GRPC_MILLIS_INF_FUTURE GPR_ATM_MAX #define GRPC_MILLIS_INF_FUTURE INT64_MAX
#define GRPC_MILLIS_INF_PAST GPR_ATM_MIN #define GRPC_MILLIS_INF_PAST INT64_MIN
/** A workqueue represents a list of work to be executed asynchronously. /** A workqueue represents a list of work to be executed asynchronously.
Forward declared here to avoid a circular dependency with workqueue.h. */ Forward declared here to avoid a circular dependency with workqueue.h. */

@ -28,7 +28,7 @@
#include "src/core/lib/iomgr/iomgr.h" #include "src/core/lib/iomgr/iomgr.h"
typedef struct grpc_timer { typedef struct grpc_timer {
gpr_atm deadline; grpc_millis deadline;
uint32_t heap_index; /* INVALID_HEAP_INDEX if not in heap */ uint32_t heap_index; /* INVALID_HEAP_INDEX if not in heap */
bool pending; bool pending;
struct grpc_timer* next; struct grpc_timer* next;

@ -30,6 +30,7 @@
#include <grpc/support/string_util.h> #include <grpc/support/string_util.h>
#include <grpc/support/sync.h> #include <grpc/support/sync.h>
#include "src/core/lib/iomgr/exec_ctx.h"
#include "src/core/lib/debug/trace.h" #include "src/core/lib/debug/trace.h"
#include "src/core/lib/gpr/spinlock.h" #include "src/core/lib/gpr/spinlock.h"
#include "src/core/lib/gpr/tls.h" #include "src/core/lib/gpr/tls.h"
@ -337,7 +338,7 @@ static void timer_init(grpc_timer* timer, grpc_millis deadline,
if (grpc_timer_trace.enabled()) { if (grpc_timer_trace.enabled()) {
gpr_log(GPR_DEBUG, gpr_log(GPR_DEBUG,
"TIMER %p: SET %" PRIdPTR " now %" PRIdPTR " call %p[%p]", timer, "TIMER %p: SET %" PRIdPTR " now %" PRId64 " call %p[%p]", timer,
deadline, grpc_core::ExecCtx::Get()->Now(), closure, closure->cb); deadline, grpc_core::ExecCtx::Get()->Now(), closure, closure->cb);
} }
@ -650,7 +651,7 @@ static grpc_timer_check_result timer_check(grpc_millis* next) {
if (next == nullptr) { if (next == nullptr) {
next_str = gpr_strdup("NULL"); next_str = gpr_strdup("NULL");
} else { } else {
gpr_asprintf(&next_str, "%" PRIdPTR, *next); gpr_asprintf(&next_str, "%" PRId64, *next);
} }
gpr_log(GPR_DEBUG, "TIMER CHECK END: r=%d; next=%s", r, next_str); gpr_log(GPR_DEBUG, "TIMER CHECK END: r=%d; next=%s", r, next_str);
gpr_free(next_str); gpr_free(next_str);

Loading…
Cancel
Save