Make alarm initialization take a timespec now.

This will allow combining of gpr_now() calls, and make it easier to test the
alarm code in the future.
	Change on 2014/12/16 by ctiller <ctiller@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=82246768
pull/1/merge
ctiller 10 years ago committed by Nicolas Noble
parent 5447c5461c
commit 769b70ba9d
  1. 6
      src/core/channel/client_setup.c
  2. 3
      src/core/iomgr/alarm.h
  3. 5
      src/core/iomgr/iomgr_libevent.c
  4. 2
      src/core/surface/call.c
  5. 5
      test/core/iomgr/alarm_test.c

@ -212,11 +212,11 @@ void grpc_client_setup_request_finish(grpc_client_setup_request *r,
/* TODO(klempner): Replace these values with further consideration. 2x is
probably too aggressive of a backoff. */
gpr_timespec max_backoff = gpr_time_from_minutes(2);
gpr_timespec deadline =
gpr_time_add(s->current_backoff_interval, gpr_now());
gpr_timespec now = gpr_now();
gpr_timespec deadline = gpr_time_add(s->current_backoff_interval, now);
GPR_ASSERT(!s->in_alarm);
s->in_alarm = 1;
grpc_alarm_init(&s->backoff_alarm, deadline, backoff_alarm_done, s);
grpc_alarm_init(&s->backoff_alarm, deadline, backoff_alarm_done, s, now);
s->current_backoff_interval =
gpr_time_add(s->current_backoff_interval, s->current_backoff_interval);
if (gpr_time_cmp(s->current_backoff_interval, max_backoff) > 0) {

@ -53,7 +53,8 @@ typedef struct grpc_alarm grpc_alarm;
information about when to free up any user-level state.
Returns 1 on success, 0 on failure. */
int grpc_alarm_init(grpc_alarm *alarm, gpr_timespec deadline,
grpc_iomgr_cb_func alarm_cb, void *alarm_cb_arg);
grpc_iomgr_cb_func alarm_cb, void *alarm_cb_arg,
gpr_timespec now);
/* Note that there is no alarm destroy function. This is because the
alarm is a one-time occurrence with a guarantee that the callback will

@ -363,10 +363,11 @@ static void libevent_alarm_cb(int fd, short what, void *arg /*=alarm*/) {
}
int grpc_alarm_init(grpc_alarm *alarm, gpr_timespec deadline,
grpc_iomgr_cb_func alarm_cb, void *alarm_cb_arg) {
grpc_iomgr_cb_func alarm_cb, void *alarm_cb_arg,
gpr_timespec now) {
grpc_libevent_activation_data *adata =
&alarm->task.activation[GRPC_EM_TA_ONLY];
gpr_timespec delay_timespec = gpr_time_sub(deadline, gpr_now());
gpr_timespec delay_timespec = gpr_time_sub(deadline, now);
struct timeval delay = gpr_timeval_from_timespec(delay_timespec);
alarm->task.type = GRPC_EM_TASK_ALARM;
gpr_atm_rel_store(&alarm->triggered, ALARM_TRIGGER_INIT);

@ -877,5 +877,5 @@ void grpc_call_set_deadline(grpc_call_element *elem, gpr_timespec deadline) {
}
grpc_call_internal_ref(call);
call->have_alarm = 1;
grpc_alarm_init(&call->alarm, deadline, call_alarm, call);
grpc_alarm_init(&call->alarm, deadline, call_alarm, call, gpr_now());
}

@ -116,7 +116,8 @@ static void test_grpc_alarm() {
gpr_cv_init(&arg.cv);
gpr_event_init(&arg.fcb_arg);
grpc_alarm_init(&alarm, gpr_time_add(tv0, gpr_now()), alarm_cb, &arg);
grpc_alarm_init(&alarm, gpr_time_add(tv0, gpr_now()), alarm_cb, &arg,
gpr_now());
alarm_deadline = gpr_time_add(gpr_now(), tv1);
gpr_mu_lock(&arg.mu);
@ -162,7 +163,7 @@ static void test_grpc_alarm() {
gpr_event_init(&arg2.fcb_arg);
grpc_alarm_init(&alarm_to_cancel, gpr_time_add(tv2, gpr_now()), alarm_cb,
&arg2);
&arg2, gpr_now());
grpc_alarm_cancel(&alarm_to_cancel);
alarm_deadline = gpr_time_add(gpr_now(), tv1);

Loading…
Cancel
Save