aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core
diff options
context:
space:
mode:
authorGravatar David Garcia Quintas <dgq@google.com>2015-10-04 23:09:47 -0700
committerGravatar David Garcia Quintas <dgq@google.com>2015-10-04 23:09:47 -0700
commitf747bbc04327c9ef02690cd7466ec6786967c4fe (patch)
treeb031152a4387370999e91d9922fef90efc86f3ac /src/core
parentcb954cfc2858829fe7bff785b1a5d5204bfd338f (diff)
s/grpc_alarm/grpc_timer && s/grpc_cq_alarm/grpc_alarm
Diffstat (limited to 'src/core')
-rw-r--r--src/core/client_config/subchannel.c8
-rw-r--r--src/core/iomgr/iocp_windows.c2
-rw-r--r--src/core/iomgr/iomgr.c8
-rw-r--r--src/core/iomgr/pollset_posix.c4
-rw-r--r--src/core/iomgr/pollset_windows.c4
-rw-r--r--src/core/iomgr/tcp_client_posix.c8
-rw-r--r--src/core/iomgr/tcp_client_windows.c8
-rw-r--r--src/core/iomgr/tcp_windows.c2
-rw-r--r--src/core/iomgr/timer.c (renamed from src/core/iomgr/alarm.c)172
-rw-r--r--src/core/iomgr/timer.h (renamed from src/core/iomgr/alarm.h)50
-rw-r--r--src/core/iomgr/timer_heap.c (renamed from src/core/iomgr/alarm_heap.c)82
-rw-r--r--src/core/iomgr/timer_heap.h (renamed from src/core/iomgr/alarm_heap.h)32
-rw-r--r--src/core/iomgr/timer_internal.h (renamed from src/core/iomgr/alarm_internal.h)24
-rw-r--r--src/core/surface/alarm.c83
-rw-r--r--src/core/surface/call.c10
-rw-r--r--src/core/surface/channel_connectivity.c8
-rw-r--r--src/core/surface/completion_queue.c47
-rw-r--r--src/core/surface/completion_queue.h26
18 files changed, 304 insertions, 274 deletions
diff --git a/src/core/client_config/subchannel.c b/src/core/client_config/subchannel.c
index a2c521a20d..d9472025da 100644
--- a/src/core/client_config/subchannel.c
+++ b/src/core/client_config/subchannel.c
@@ -40,7 +40,7 @@
#include "src/core/channel/channel_args.h"
#include "src/core/channel/client_channel.h"
#include "src/core/channel/connected_channel.h"
-#include "src/core/iomgr/alarm.h"
+#include "src/core/iomgr/timer.h"
#include "src/core/transport/connectivity_state.h"
#include "src/core/surface/channel.h"
@@ -130,7 +130,7 @@ struct grpc_subchannel {
/** do we have an active alarm? */
int have_alarm;
/** our alarm */
- grpc_alarm alarm;
+ grpc_timer alarm;
/** current random value */
gpr_uint32 random;
};
@@ -459,7 +459,7 @@ void grpc_subchannel_process_transport_op(grpc_exec_ctx *exec_ctx,
}
if (cancel_alarm) {
- grpc_alarm_cancel(exec_ctx, &c->alarm);
+ grpc_timer_cancel(exec_ctx, &c->alarm);
}
if (op->disconnect) {
@@ -690,7 +690,7 @@ static void subchannel_connected(grpc_exec_ctx *exec_ctx, void *arg,
GPR_ASSERT(!c->have_alarm);
c->have_alarm = 1;
connectivity_state_changed_locked(exec_ctx, c, "connect_failed");
- grpc_alarm_init(exec_ctx, &c->alarm, c->next_attempt, on_alarm, c, now);
+ grpc_timer_init(exec_ctx, &c->alarm, c->next_attempt, on_alarm, c, now);
gpr_mu_unlock(&c->mu);
}
}
diff --git a/src/core/iomgr/iocp_windows.c b/src/core/iomgr/iocp_windows.c
index cebd863924..65da3a9d2d 100644
--- a/src/core/iomgr/iocp_windows.c
+++ b/src/core/iomgr/iocp_windows.c
@@ -42,7 +42,7 @@
#include <grpc/support/alloc.h>
#include <grpc/support/thd.h>
-#include "src/core/iomgr/alarm_internal.h"
+#include "src/core/iomgr/timer_internal.h"
#include "src/core/iomgr/iocp_windows.h"
#include "src/core/iomgr/iomgr_internal.h"
#include "src/core/iomgr/socket_windows.h"
diff --git a/src/core/iomgr/iomgr.c b/src/core/iomgr/iomgr.c
index e61fc32925..212ce5534d 100644
--- a/src/core/iomgr/iomgr.c
+++ b/src/core/iomgr/iomgr.c
@@ -43,7 +43,7 @@
#include <grpc/support/thd.h>
#include "src/core/iomgr/iomgr_internal.h"
-#include "src/core/iomgr/alarm_internal.h"
+#include "src/core/iomgr/timer_internal.h"
#include "src/core/support/string.h"
static gpr_mu g_mu;
@@ -55,7 +55,7 @@ void grpc_iomgr_init(void) {
g_shutdown = 0;
gpr_mu_init(&g_mu);
gpr_cv_init(&g_rcv);
- grpc_alarm_list_init(gpr_now(GPR_CLOCK_MONOTONIC));
+ grpc_timer_list_init(gpr_now(GPR_CLOCK_MONOTONIC));
g_root_object.next = g_root_object.prev = &g_root_object;
g_root_object.name = "root";
grpc_iomgr_platform_init();
@@ -98,7 +98,7 @@ void grpc_iomgr_shutdown(void) {
}
last_warning_time = gpr_now(GPR_CLOCK_REALTIME);
}
- if (grpc_alarm_check(&exec_ctx, gpr_inf_future(GPR_CLOCK_MONOTONIC),
+ if (grpc_timer_check(&exec_ctx, gpr_inf_future(GPR_CLOCK_MONOTONIC),
NULL)) {
gpr_mu_unlock(&g_mu);
grpc_exec_ctx_flush(&exec_ctx);
@@ -124,7 +124,7 @@ void grpc_iomgr_shutdown(void) {
}
gpr_mu_unlock(&g_mu);
- grpc_alarm_list_shutdown(&exec_ctx);
+ grpc_timer_list_shutdown(&exec_ctx);
grpc_exec_ctx_finish(&exec_ctx);
/* ensure all threads have left g_mu */
diff --git a/src/core/iomgr/pollset_posix.c b/src/core/iomgr/pollset_posix.c
index 464c1f6ae3..4ba1bda1a9 100644
--- a/src/core/iomgr/pollset_posix.c
+++ b/src/core/iomgr/pollset_posix.c
@@ -42,7 +42,7 @@
#include <string.h>
#include <unistd.h>
-#include "src/core/iomgr/alarm_internal.h"
+#include "src/core/iomgr/timer_internal.h"
#include "src/core/iomgr/fd_posix.h"
#include "src/core/iomgr/iomgr_internal.h"
#include "src/core/iomgr/socket_utils_posix.h"
@@ -204,7 +204,7 @@ void grpc_pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
grpc_exec_ctx_enqueue_list(exec_ctx, &pollset->idle_jobs);
goto done;
}
- if (grpc_alarm_check(exec_ctx, now, &deadline)) {
+ if (grpc_timer_check(exec_ctx, now, &deadline)) {
gpr_mu_unlock(&pollset->mu);
locked = 0;
goto done;
diff --git a/src/core/iomgr/pollset_windows.c b/src/core/iomgr/pollset_windows.c
index 96abaea0b3..9f74580273 100644
--- a/src/core/iomgr/pollset_windows.c
+++ b/src/core/iomgr/pollset_windows.c
@@ -37,7 +37,7 @@
#include <grpc/support/thd.h>
-#include "src/core/iomgr/alarm_internal.h"
+#include "src/core/iomgr/timer_internal.h"
#include "src/core/iomgr/iomgr_internal.h"
#include "src/core/iomgr/iocp_windows.h"
#include "src/core/iomgr/pollset.h"
@@ -136,7 +136,7 @@ void grpc_pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
worker->kicked = 0;
worker->pollset = pollset;
gpr_cv_init(&worker->cv);
- if (grpc_alarm_check(exec_ctx, now, &deadline)) {
+ if (grpc_timer_check(exec_ctx, now, &deadline)) {
goto done;
}
if (!pollset->kicked_without_pollers && !pollset->shutting_down) {
diff --git a/src/core/iomgr/tcp_client_posix.c b/src/core/iomgr/tcp_client_posix.c
index 346566866a..037964a97c 100644
--- a/src/core/iomgr/tcp_client_posix.c
+++ b/src/core/iomgr/tcp_client_posix.c
@@ -42,7 +42,7 @@
#include <string.h>
#include <unistd.h>
-#include "src/core/iomgr/alarm.h"
+#include "src/core/iomgr/timer.h"
#include "src/core/iomgr/iomgr_posix.h"
#include "src/core/iomgr/pollset_posix.h"
#include "src/core/iomgr/sockaddr_utils.h"
@@ -60,7 +60,7 @@ typedef struct {
gpr_mu mu;
grpc_fd *fd;
gpr_timespec deadline;
- grpc_alarm alarm;
+ grpc_timer alarm;
int refs;
grpc_closure write_closure;
grpc_pollset_set *interested_parties;
@@ -132,7 +132,7 @@ static void on_writable(grpc_exec_ctx *exec_ctx, void *acp, int success) {
ac->fd = NULL;
gpr_mu_unlock(&ac->mu);
- grpc_alarm_cancel(exec_ctx, &ac->alarm);
+ grpc_timer_cancel(exec_ctx, &ac->alarm);
gpr_mu_lock(&ac->mu);
if (success) {
@@ -284,7 +284,7 @@ void grpc_tcp_client_connect(grpc_exec_ctx *exec_ctx, grpc_closure *closure,
}
gpr_mu_lock(&ac->mu);
- grpc_alarm_init(exec_ctx, &ac->alarm,
+ grpc_timer_init(exec_ctx, &ac->alarm,
gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC),
tc_on_alarm, ac, gpr_now(GPR_CLOCK_MONOTONIC));
grpc_fd_notify_on_write(exec_ctx, ac->fd, &ac->write_closure);
diff --git a/src/core/iomgr/tcp_client_windows.c b/src/core/iomgr/tcp_client_windows.c
index 3540c55676..e5691b7e12 100644
--- a/src/core/iomgr/tcp_client_windows.c
+++ b/src/core/iomgr/tcp_client_windows.c
@@ -43,7 +43,7 @@
#include <grpc/support/slice_buffer.h>
#include <grpc/support/useful.h>
-#include "src/core/iomgr/alarm.h"
+#include "src/core/iomgr/timer.h"
#include "src/core/iomgr/iocp_windows.h"
#include "src/core/iomgr/tcp_client.h"
#include "src/core/iomgr/tcp_windows.h"
@@ -56,7 +56,7 @@ typedef struct {
gpr_mu mu;
grpc_winsocket *socket;
gpr_timespec deadline;
- grpc_alarm alarm;
+ grpc_timer alarm;
char *addr_name;
int refs;
grpc_closure on_connect;
@@ -91,7 +91,7 @@ static void on_connect(grpc_exec_ctx *exec_ctx, void *acp, int from_iocp) {
grpc_winsocket_callback_info *info = &ac->socket->write_info;
grpc_closure *on_done = ac->on_done;
- grpc_alarm_cancel(exec_ctx, &ac->alarm);
+ grpc_timer_cancel(exec_ctx, &ac->alarm);
gpr_mu_lock(&ac->mu);
@@ -201,7 +201,7 @@ void grpc_tcp_client_connect(grpc_exec_ctx *exec_ctx, grpc_closure *on_done,
ac->endpoint = endpoint;
grpc_closure_init(&ac->on_connect, on_connect, ac);
- grpc_alarm_init(exec_ctx, &ac->alarm, deadline, on_alarm, ac,
+ grpc_timer_init(exec_ctx, &ac->alarm, deadline, on_alarm, ac,
gpr_now(GPR_CLOCK_MONOTONIC));
grpc_socket_notify_on_write(exec_ctx, socket, &ac->on_connect);
return;
diff --git a/src/core/iomgr/tcp_windows.c b/src/core/iomgr/tcp_windows.c
index 9ceffca065..5ff78231bd 100644
--- a/src/core/iomgr/tcp_windows.c
+++ b/src/core/iomgr/tcp_windows.c
@@ -44,7 +44,7 @@
#include <grpc/support/string_util.h>
#include <grpc/support/useful.h>
-#include "src/core/iomgr/alarm.h"
+#include "src/core/iomgr/timer.h"
#include "src/core/iomgr/iocp_windows.h"
#include "src/core/iomgr/sockaddr.h"
#include "src/core/iomgr/sockaddr_utils.h"
diff --git a/src/core/iomgr/alarm.c b/src/core/iomgr/timer.c
index 0ba5361606..66fafe75ad 100644
--- a/src/core/iomgr/alarm.c
+++ b/src/core/iomgr/timer.c
@@ -31,10 +31,10 @@
*
*/
-#include "src/core/iomgr/alarm.h"
+#include "src/core/iomgr/timer.h"
-#include "src/core/iomgr/alarm_heap.h"
-#include "src/core/iomgr/alarm_internal.h"
+#include "src/core/iomgr/timer_heap.h"
+#include "src/core/iomgr/timer_internal.h"
#include "src/core/iomgr/time_averaged_stats.h"
#include <grpc/support/log.h>
#include <grpc/support/sync.h>
@@ -51,37 +51,37 @@
typedef struct {
gpr_mu mu;
grpc_time_averaged_stats stats;
- /* All and only alarms with deadlines <= this will be in the heap. */
+ /* All and only timers with deadlines <= this will be in the heap. */
gpr_timespec queue_deadline_cap;
gpr_timespec min_deadline;
/* Index in the g_shard_queue */
gpr_uint32 shard_queue_index;
- /* This holds all alarms with deadlines < queue_deadline_cap. Alarms in this
+ /* This holds all timers with deadlines < queue_deadline_cap. Timers in this
list have the top bit of their deadline set to 0. */
- grpc_alarm_heap heap;
- /* This holds alarms whose deadline is >= queue_deadline_cap. */
- grpc_alarm list;
+ grpc_timer_heap heap;
+ /* This holds timers whose deadline is >= queue_deadline_cap. */
+ grpc_timer list;
} shard_type;
/* Protects g_shard_queue */
static gpr_mu g_mu;
-/* Allow only one run_some_expired_alarms at once */
+/* Allow only one run_some_expired_timers at once */
static gpr_mu g_checker_mu;
static gpr_clock_type g_clock_type;
static shard_type g_shards[NUM_SHARDS];
/* Protected by g_mu */
static shard_type *g_shard_queue[NUM_SHARDS];
-static int run_some_expired_alarms(grpc_exec_ctx *exec_ctx, gpr_timespec now,
+static int run_some_expired_timers(grpc_exec_ctx *exec_ctx, gpr_timespec now,
gpr_timespec *next, int success);
static gpr_timespec compute_min_deadline(shard_type *shard) {
- return grpc_alarm_heap_is_empty(&shard->heap)
+ return grpc_timer_heap_is_empty(&shard->heap)
? shard->queue_deadline_cap
- : grpc_alarm_heap_top(&shard->heap)->deadline;
+ : grpc_timer_heap_top(&shard->heap)->deadline;
}
-void grpc_alarm_list_init(gpr_timespec now) {
+void grpc_timer_list_init(gpr_timespec now) {
gpr_uint32 i;
gpr_mu_init(&g_mu);
@@ -95,27 +95,27 @@ void grpc_alarm_list_init(gpr_timespec now) {
0.5);
shard->queue_deadline_cap = now;
shard->shard_queue_index = i;
- grpc_alarm_heap_init(&shard->heap);
+ grpc_timer_heap_init(&shard->heap);
shard->list.next = shard->list.prev = &shard->list;
shard->min_deadline = compute_min_deadline(shard);
g_shard_queue[i] = shard;
}
}
-void grpc_alarm_list_shutdown(grpc_exec_ctx *exec_ctx) {
+void grpc_timer_list_shutdown(grpc_exec_ctx *exec_ctx) {
int i;
- run_some_expired_alarms(exec_ctx, gpr_inf_future(g_clock_type), NULL, 0);
+ run_some_expired_timers(exec_ctx, gpr_inf_future(g_clock_type), NULL, 0);
for (i = 0; i < NUM_SHARDS; i++) {
shard_type *shard = &g_shards[i];
gpr_mu_destroy(&shard->mu);
- grpc_alarm_heap_destroy(&shard->heap);
+ grpc_timer_heap_destroy(&shard->heap);
}
gpr_mu_destroy(&g_mu);
gpr_mu_destroy(&g_checker_mu);
}
/* This is a cheap, but good enough, pointer hash for sharding the tasks: */
-static size_t shard_idx(const grpc_alarm *info) {
+static size_t shard_idx(const grpc_timer *info) {
size_t x = (size_t)info;
return ((x >> 4) ^ (x >> 9) ^ (x >> 14)) & (NUM_SHARDS - 1);
}
@@ -132,15 +132,15 @@ static gpr_timespec dbl_to_ts(double d) {
return ts;
}
-static void list_join(grpc_alarm *head, grpc_alarm *alarm) {
- alarm->next = head;
- alarm->prev = head->prev;
- alarm->next->prev = alarm->prev->next = alarm;
+static void list_join(grpc_timer *head, grpc_timer *timer) {
+ timer->next = head;
+ timer->prev = head->prev;
+ timer->next->prev = timer->prev->next = timer;
}
-static void list_remove(grpc_alarm *alarm) {
- alarm->next->prev = alarm->prev;
- alarm->prev->next = alarm->next;
+static void list_remove(grpc_timer *timer) {
+ timer->next->prev = timer->prev;
+ timer->prev->next = timer->next;
}
static void swap_adjacent_shards_in_queue(gpr_uint32 first_shard_queue_index) {
@@ -170,16 +170,16 @@ static void note_deadline_change(shard_type *shard) {
}
}
-void grpc_alarm_init(grpc_exec_ctx *exec_ctx, grpc_alarm *alarm,
- gpr_timespec deadline, grpc_iomgr_cb_func alarm_cb,
- void *alarm_cb_arg, gpr_timespec now) {
- int is_first_alarm = 0;
- shard_type *shard = &g_shards[shard_idx(alarm)];
+void grpc_timer_init(grpc_exec_ctx *exec_ctx, grpc_timer *timer,
+ gpr_timespec deadline, grpc_iomgr_cb_func timer_cb,
+ void *timer_cb_arg, gpr_timespec now) {
+ int is_first_timer = 0;
+ shard_type *shard = &g_shards[shard_idx(timer)];
GPR_ASSERT(deadline.clock_type == g_clock_type);
GPR_ASSERT(now.clock_type == g_clock_type);
- grpc_closure_init(&alarm->closure, alarm_cb, alarm_cb_arg);
- alarm->deadline = deadline;
- alarm->triggered = 0;
+ grpc_closure_init(&timer->closure, timer_cb, timer_cb_arg);
+ timer->deadline = deadline;
+ timer->triggered = 0;
/* TODO(ctiller): check deadline expired */
@@ -187,25 +187,25 @@ void grpc_alarm_init(grpc_exec_ctx *exec_ctx, grpc_alarm *alarm,
grpc_time_averaged_stats_add_sample(&shard->stats,
ts_to_dbl(gpr_time_sub(deadline, now)));
if (gpr_time_cmp(deadline, shard->queue_deadline_cap) < 0) {
- is_first_alarm = grpc_alarm_heap_add(&shard->heap, alarm);
+ is_first_timer = grpc_timer_heap_add(&shard->heap, timer);
} else {
- alarm->heap_index = INVALID_HEAP_INDEX;
- list_join(&shard->list, alarm);
+ timer->heap_index = INVALID_HEAP_INDEX;
+ list_join(&shard->list, timer);
}
gpr_mu_unlock(&shard->mu);
/* Deadline may have decreased, we need to adjust the master queue. Note
that there is a potential racy unlocked region here. There could be a
- reordering of multiple grpc_alarm_init calls, at this point, but the < test
+ reordering of multiple grpc_timer_init calls, at this point, but the < test
below should ensure that we err on the side of caution. There could
- also be a race with grpc_alarm_check, which might beat us to the lock. In
- that case, it is possible that the alarm that we added will have already
+ also be a race with grpc_timer_check, which might beat us to the lock. In
+ that case, it is possible that the timer that we added will have already
run by the time we hold the lock, but that too is a safe error.
- Finally, it's possible that the grpc_alarm_check that intervened failed to
- trigger the new alarm because the min_deadline hadn't yet been reduced.
- In that case, the alarm will simply have to wait for the next
- grpc_alarm_check. */
- if (is_first_alarm) {
+ Finally, it's possible that the grpc_timer_check that intervened failed to
+ trigger the new timer because the min_deadline hadn't yet been reduced.
+ In that case, the timer will simply have to wait for the next
+ grpc_timer_check. */
+ if (is_first_timer) {
gpr_mu_lock(&g_mu);
if (gpr_time_cmp(deadline, shard->min_deadline) < 0) {
gpr_timespec old_min_deadline = g_shard_queue[0]->min_deadline;
@@ -220,16 +220,16 @@ void grpc_alarm_init(grpc_exec_ctx *exec_ctx, grpc_alarm *alarm,
}
}
-void grpc_alarm_cancel(grpc_exec_ctx *exec_ctx, grpc_alarm *alarm) {
- shard_type *shard = &g_shards[shard_idx(alarm)];
+void grpc_timer_cancel(grpc_exec_ctx *exec_ctx, grpc_timer *timer) {
+ shard_type *shard = &g_shards[shard_idx(timer)];
gpr_mu_lock(&shard->mu);
- if (!alarm->triggered) {
- grpc_exec_ctx_enqueue(exec_ctx, &alarm->closure, 0);
- alarm->triggered = 1;
- if (alarm->heap_index == INVALID_HEAP_INDEX) {
- list_remove(alarm);
+ if (!timer->triggered) {
+ grpc_exec_ctx_enqueue(exec_ctx, &timer->closure, 0);
+ timer->triggered = 1;
+ if (timer->heap_index == INVALID_HEAP_INDEX) {
+ list_remove(timer);
} else {
- grpc_alarm_heap_remove(&shard->heap, alarm);
+ grpc_timer_heap_remove(&shard->heap, timer);
}
}
gpr_mu_unlock(&shard->mu);
@@ -237,7 +237,7 @@ void grpc_alarm_cancel(grpc_exec_ctx *exec_ctx, grpc_alarm *alarm) {
/* This is called when the queue is empty and "now" has reached the
queue_deadline_cap. We compute a new queue deadline and then scan the map
- for alarms that fall at or under it. Returns true if the queue is no
+ for timers that fall at or under it. Returns true if the queue is no
longer empty.
REQUIRES: shard->mu locked */
static int refill_queue(shard_type *shard, gpr_timespec now) {
@@ -248,49 +248,49 @@ static int refill_queue(shard_type *shard, gpr_timespec now) {
double deadline_delta =
GPR_CLAMP(computed_deadline_delta, MIN_QUEUE_WINDOW_DURATION,
MAX_QUEUE_WINDOW_DURATION);
- grpc_alarm *alarm, *next;
+ grpc_timer *timer, *next;
- /* Compute the new cap and put all alarms under it into the queue: */
+ /* Compute the new cap and put all timers under it into the queue: */
shard->queue_deadline_cap = gpr_time_add(
gpr_time_max(now, shard->queue_deadline_cap), dbl_to_ts(deadline_delta));
- for (alarm = shard->list.next; alarm != &shard->list; alarm = next) {
- next = alarm->next;
+ for (timer = shard->list.next; timer != &shard->list; timer = next) {
+ next = timer->next;
- if (gpr_time_cmp(alarm->deadline, shard->queue_deadline_cap) < 0) {
- list_remove(alarm);
- grpc_alarm_heap_add(&shard->heap, alarm);
+ if (gpr_time_cmp(timer->deadline, shard->queue_deadline_cap) < 0) {
+ list_remove(timer);
+ grpc_timer_heap_add(&shard->heap, timer);
}
}
- return !grpc_alarm_heap_is_empty(&shard->heap);
+ return !grpc_timer_heap_is_empty(&shard->heap);
}
-/* This pops the next non-cancelled alarm with deadline <= now from the queue,
+/* This pops the next non-cancelled timer with deadline <= now from the queue,
or returns NULL if there isn't one.
REQUIRES: shard->mu locked */
-static grpc_alarm *pop_one(shard_type *shard, gpr_timespec now) {
- grpc_alarm *alarm;
+static grpc_timer *pop_one(shard_type *shard, gpr_timespec now) {
+ grpc_timer *timer;
for (;;) {
- if (grpc_alarm_heap_is_empty(&shard->heap)) {
+ if (grpc_timer_heap_is_empty(&shard->heap)) {
if (gpr_time_cmp(now, shard->queue_deadline_cap) < 0) return NULL;
if (!refill_queue(shard, now)) return NULL;
}
- alarm = grpc_alarm_heap_top(&shard->heap);
- if (gpr_time_cmp(alarm->deadline, now) > 0) return NULL;
- alarm->triggered = 1;
- grpc_alarm_heap_pop(&shard->heap);
- return alarm;
+ timer = grpc_timer_heap_top(&shard->heap);
+ if (gpr_time_cmp(timer->deadline, now) > 0) return NULL;
+ timer->triggered = 1;
+ grpc_timer_heap_pop(&shard->heap);
+ return timer;
}
}
/* REQUIRES: shard->mu unlocked */
-static size_t pop_alarms(grpc_exec_ctx *exec_ctx, shard_type *shard,
+static size_t pop_timers(grpc_exec_ctx *exec_ctx, shard_type *shard,
gpr_timespec now, gpr_timespec *new_min_deadline,
int success) {
size_t n = 0;
- grpc_alarm *alarm;
+ grpc_timer *timer;
gpr_mu_lock(&shard->mu);
- while ((alarm = pop_one(shard, now))) {
- grpc_exec_ctx_enqueue(exec_ctx, &alarm->closure, success);
+ while ((timer = pop_one(shard, now))) {
+ grpc_exec_ctx_enqueue(exec_ctx, &timer->closure, success);
n++;
}
*new_min_deadline = compute_min_deadline(shard);
@@ -298,11 +298,11 @@ static size_t pop_alarms(grpc_exec_ctx *exec_ctx, shard_type *shard,
return n;
}
-static int run_some_expired_alarms(grpc_exec_ctx *exec_ctx, gpr_timespec now,
+static int run_some_expired_timers(grpc_exec_ctx *exec_ctx, gpr_timespec now,
gpr_timespec *next, int success) {
size_t n = 0;
- /* TODO(ctiller): verify that there are any alarms (atomically) here */
+ /* TODO(ctiller): verify that there are any timers (atomically) here */
if (gpr_mu_trylock(&g_checker_mu)) {
gpr_mu_lock(&g_mu);
@@ -310,16 +310,16 @@ static int run_some_expired_alarms(grpc_exec_ctx *exec_ctx, gpr_timespec now,
while (gpr_time_cmp(g_shard_queue[0]->min_deadline, now) < 0) {
gpr_timespec new_min_deadline;
- /* For efficiency, we pop as many available alarms as we can from the
- shard. This may violate perfect alarm deadline ordering, but that
+ /* For efficiency, we pop as many available timers as we can from the
+ shard. This may violate perfect timer deadline ordering, but that
shouldn't be a big deal because we don't make ordering guarantees. */
- n += pop_alarms(exec_ctx, g_shard_queue[0], now, &new_min_deadline,
+ n += pop_timers(exec_ctx, g_shard_queue[0], now, &new_min_deadline,
success);
- /* An grpc_alarm_init() on the shard could intervene here, adding a new
- alarm that is earlier than new_min_deadline. However,
- grpc_alarm_init() will block on the master_lock before it can call
- set_min_deadline, so this one will complete first and then the AddAlarm
+ /* An grpc_timer_init() on the shard could intervene here, adding a new
+ timer that is earlier than new_min_deadline. However,
+ grpc_timer_init() will block on the master_lock before it can call
+ set_min_deadline, so this one will complete first and then the Addtimer
will reduce the min_deadline (perhaps unnecessarily). */
g_shard_queue[0]->min_deadline = new_min_deadline;
note_deadline_change(g_shard_queue[0]);
@@ -336,15 +336,15 @@ static int run_some_expired_alarms(grpc_exec_ctx *exec_ctx, gpr_timespec now,
return (int)n;
}
-int grpc_alarm_check(grpc_exec_ctx *exec_ctx, gpr_timespec now,
+int grpc_timer_check(grpc_exec_ctx *exec_ctx, gpr_timespec now,
gpr_timespec *next) {
GPR_ASSERT(now.clock_type == g_clock_type);
- return run_some_expired_alarms(
+ return run_some_expired_timers(
exec_ctx, now, next,
gpr_time_cmp(now, gpr_inf_future(now.clock_type)) != 0);
}
-gpr_timespec grpc_alarm_list_next_timeout(void) {
+gpr_timespec grpc_timer_list_next_timeout(void) {
gpr_timespec out;
gpr_mu_lock(&g_mu);
out = g_shard_queue[0]->min_deadline;
diff --git a/src/core/iomgr/alarm.h b/src/core/iomgr/timer.h
index 94f9bc1355..9abe58133d 100644
--- a/src/core/iomgr/alarm.h
+++ b/src/core/iomgr/timer.h
@@ -31,59 +31,59 @@
*
*/
-#ifndef GRPC_INTERNAL_CORE_IOMGR_ALARM_H
-#define GRPC_INTERNAL_CORE_IOMGR_ALARM_H
+#ifndef GRPC_INTERNAL_CORE_IOMGR_TIMER_H
+#define GRPC_INTERNAL_CORE_IOMGR_TIMER_H
#include "src/core/iomgr/iomgr.h"
#include "src/core/iomgr/exec_ctx.h"
#include <grpc/support/port_platform.h>
#include <grpc/support/time.h>
-typedef struct grpc_alarm {
+typedef struct grpc_timer {
gpr_timespec deadline;
gpr_uint32 heap_index; /* INVALID_HEAP_INDEX if not in heap */
int triggered;
- struct grpc_alarm *next;
- struct grpc_alarm *prev;
+ struct grpc_timer *next;
+ struct grpc_timer *prev;
grpc_closure closure;
-} grpc_alarm;
+} grpc_timer;
-/* Initialize *alarm. When expired or canceled, alarm_cb will be called with
- *alarm_cb_arg and status to indicate if it expired (SUCCESS) or was
- canceled (CANCELLED). alarm_cb is guaranteed to be called exactly once,
+/* Initialize *timer. When expired or canceled, timer_cb will be called with
+ *timer_cb_arg and status to indicate if it expired (SUCCESS) or was
+ canceled (CANCELLED). timer_cb is guaranteed to be called exactly once,
and application code should check the status to determine how it was
invoked. The application callback is also responsible for maintaining
information about when to free up any user-level state. */
-void grpc_alarm_init(grpc_exec_ctx *exec_ctx, grpc_alarm *alarm,
- gpr_timespec deadline, grpc_iomgr_cb_func alarm_cb,
- void *alarm_cb_arg, gpr_timespec now);
+void grpc_timer_init(grpc_exec_ctx *exec_ctx, grpc_timer *timer,
+ gpr_timespec deadline, grpc_iomgr_cb_func timer_cb,
+ void *timer_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
+/* Note that there is no timer destroy function. This is because the
+ timer is a one-time occurrence with a guarantee that the callback will
be called exactly once, either at expiration or cancellation. Thus, all
- the internal alarm event management state is destroyed just before
+ the internal timer event management state is destroyed just before
that callback is invoked. If the user has additional state associated with
- the alarm, the user is responsible for determining when it is safe to
+ the timer, the user is responsible for determining when it is safe to
destroy that state. */
-/* Cancel an *alarm.
+/* Cancel an *timer.
There are three cases:
- 1. We normally cancel the alarm
- 2. The alarm has already run
- 3. We can't cancel the alarm because it is "in flight".
+ 1. We normally cancel the timer
+ 2. The timer has already run
+ 3. We can't cancel the timer because it is "in flight".
In all of these cases, the cancellation is still considered successful.
- They are essentially distinguished in that the alarm_cb will be run
+ They are essentially distinguished in that the timer_cb will be run
exactly once from either the cancellation (with status CANCELLED)
or from the activation (with status SUCCESS)
Note carefully that the callback function MAY occur in the same callstack
- as grpc_alarm_cancel. It's expected that most alarms will be cancelled (their
+ as grpc_timer_cancel. It's expected that most timers will be cancelled (their
primary use is to implement deadlines), and so this code is optimized such
that cancellation costs as little as possible. Making callbacks run inline
matches this aim.
- Requires: cancel() must happen after add() on a given alarm */
-void grpc_alarm_cancel(grpc_exec_ctx *exec_ctx, grpc_alarm *alarm);
+ Requires: cancel() must happen after add() on a given timer */
+void grpc_timer_cancel(grpc_exec_ctx *exec_ctx, grpc_timer *timer);
-#endif /* GRPC_INTERNAL_CORE_IOMGR_ALARM_H */
+#endif /* GRPC_INTERNAL_CORE_IOMGR_TIMER_H */
diff --git a/src/core/iomgr/alarm_heap.c b/src/core/iomgr/timer_heap.c
index 769142e425..31d41d6750 100644
--- a/src/core/iomgr/alarm_heap.c
+++ b/src/core/iomgr/timer_heap.c
@@ -31,7 +31,7 @@
*
*/
-#include "src/core/iomgr/alarm_heap.h"
+#include "src/core/iomgr/timer_heap.h"
#include <string.h>
@@ -43,7 +43,7 @@
position. This functor is called each time immediately after modifying a
value in the underlying container, with the offset of the modified element as
its argument. */
-static void adjust_upwards(grpc_alarm **first, gpr_uint32 i, grpc_alarm *t) {
+static void adjust_upwards(grpc_timer **first, gpr_uint32 i, grpc_timer *t) {
while (i > 0) {
gpr_uint32 parent = (gpr_uint32)(((int)i - 1) / 2);
if (gpr_time_cmp(first[parent]->deadline, t->deadline) >= 0) break;
@@ -58,8 +58,8 @@ static void adjust_upwards(grpc_alarm **first, gpr_uint32 i, grpc_alarm *t) {
/* Adjusts a heap so as to move a hole at position i farther away from the root,
until a suitable position is found for element t. Then, copies t into that
position. */
-static void adjust_downwards(grpc_alarm **first, gpr_uint32 i,
- gpr_uint32 length, grpc_alarm *t) {
+static void adjust_downwards(grpc_timer **first, gpr_uint32 i,
+ gpr_uint32 length, grpc_timer *t) {
for (;;) {
gpr_uint32 left_child = 1u + 2u * i;
gpr_uint32 right_child;
@@ -83,66 +83,66 @@ static void adjust_downwards(grpc_alarm **first, gpr_uint32 i,
#define SHRINK_MIN_ELEMS 8
#define SHRINK_FULLNESS_FACTOR 2
-static void maybe_shrink(grpc_alarm_heap *heap) {
- if (heap->alarm_count >= 8 &&
- heap->alarm_count <= heap->alarm_capacity / SHRINK_FULLNESS_FACTOR / 2) {
- heap->alarm_capacity = heap->alarm_count * SHRINK_FULLNESS_FACTOR;
- heap->alarms =
- gpr_realloc(heap->alarms, heap->alarm_capacity * sizeof(grpc_alarm *));
+static void maybe_shrink(grpc_timer_heap *heap) {
+ if (heap->timer_count >= 8 &&
+ heap->timer_count <= heap->timer_capacity / SHRINK_FULLNESS_FACTOR / 2) {
+ heap->timer_capacity = heap->timer_count * SHRINK_FULLNESS_FACTOR;
+ heap->timers =
+ gpr_realloc(heap->timers, heap->timer_capacity * sizeof(grpc_timer *));
}
}
-static void note_changed_priority(grpc_alarm_heap *heap, grpc_alarm *alarm) {
- gpr_uint32 i = alarm->heap_index;
+static void note_changed_priority(grpc_timer_heap *heap, grpc_timer *timer) {
+ gpr_uint32 i = timer->heap_index;
gpr_uint32 parent = (gpr_uint32)(((int)i - 1) / 2);
- if (gpr_time_cmp(heap->alarms[parent]->deadline, alarm->deadline) < 0) {
- adjust_upwards(heap->alarms, i, alarm);
+ if (gpr_time_cmp(heap->timers[parent]->deadline, timer->deadline) < 0) {
+ adjust_upwards(heap->timers, i, timer);
} else {
- adjust_downwards(heap->alarms, i, heap->alarm_count, alarm);
+ adjust_downwards(heap->timers, i, heap->timer_count, timer);
}
}
-void grpc_alarm_heap_init(grpc_alarm_heap *heap) {
+void grpc_timer_heap_init(grpc_timer_heap *heap) {
memset(heap, 0, sizeof(*heap));
}
-void grpc_alarm_heap_destroy(grpc_alarm_heap *heap) { gpr_free(heap->alarms); }
+void grpc_timer_heap_destroy(grpc_timer_heap *heap) { gpr_free(heap->timers); }
-int grpc_alarm_heap_add(grpc_alarm_heap *heap, grpc_alarm *alarm) {
- if (heap->alarm_count == heap->alarm_capacity) {
- heap->alarm_capacity =
- GPR_MAX(heap->alarm_capacity + 1, heap->alarm_capacity * 3 / 2);
- heap->alarms =
- gpr_realloc(heap->alarms, heap->alarm_capacity * sizeof(grpc_alarm *));
+int grpc_timer_heap_add(grpc_timer_heap *heap, grpc_timer *timer) {
+ if (heap->timer_count == heap->timer_capacity) {
+ heap->timer_capacity =
+ GPR_MAX(heap->timer_capacity + 1, heap->timer_capacity * 3 / 2);
+ heap->timers =
+ gpr_realloc(heap->timers, heap->timer_capacity * sizeof(grpc_timer *));
}
- alarm->heap_index = heap->alarm_count;
- adjust_upwards(heap->alarms, heap->alarm_count, alarm);
- heap->alarm_count++;
- return alarm->heap_index == 0;
+ timer->heap_index = heap->timer_count;
+ adjust_upwards(heap->timers, heap->timer_count, timer);
+ heap->timer_count++;
+ return timer->heap_index == 0;
}
-void grpc_alarm_heap_remove(grpc_alarm_heap *heap, grpc_alarm *alarm) {
- gpr_uint32 i = alarm->heap_index;
- if (i == heap->alarm_count - 1) {
- heap->alarm_count--;
+void grpc_timer_heap_remove(grpc_timer_heap *heap, grpc_timer *timer) {
+ gpr_uint32 i = timer->heap_index;
+ if (i == heap->timer_count - 1) {
+ heap->timer_count--;
maybe_shrink(heap);
return;
}
- heap->alarms[i] = heap->alarms[heap->alarm_count - 1];
- heap->alarms[i]->heap_index = i;
- heap->alarm_count--;
+ heap->timers[i] = heap->timers[heap->timer_count - 1];
+ heap->timers[i]->heap_index = i;
+ heap->timer_count--;
maybe_shrink(heap);
- note_changed_priority(heap, heap->alarms[i]);
+ note_changed_priority(heap, heap->timers[i]);
}
-int grpc_alarm_heap_is_empty(grpc_alarm_heap *heap) {
- return heap->alarm_count == 0;
+int grpc_timer_heap_is_empty(grpc_timer_heap *heap) {
+ return heap->timer_count == 0;
}
-grpc_alarm *grpc_alarm_heap_top(grpc_alarm_heap *heap) {
- return heap->alarms[0];
+grpc_timer *grpc_timer_heap_top(grpc_timer_heap *heap) {
+ return heap->timers[0];
}
-void grpc_alarm_heap_pop(grpc_alarm_heap *heap) {
- grpc_alarm_heap_remove(heap, grpc_alarm_heap_top(heap));
+void grpc_timer_heap_pop(grpc_timer_heap *heap) {
+ grpc_timer_heap_remove(heap, grpc_timer_heap_top(heap));
}
diff --git a/src/core/iomgr/alarm_heap.h b/src/core/iomgr/timer_heap.h
index 91d6ee3ca2..cd5258f93e 100644
--- a/src/core/iomgr/alarm_heap.h
+++ b/src/core/iomgr/timer_heap.h
@@ -31,27 +31,27 @@
*
*/
-#ifndef GRPC_INTERNAL_CORE_IOMGR_ALARM_HEAP_H
-#define GRPC_INTERNAL_CORE_IOMGR_ALARM_HEAP_H
+#ifndef GRPC_INTERNAL_CORE_IOMGR_TIMER_HEAP_H
+#define GRPC_INTERNAL_CORE_IOMGR_TIMER_HEAP_H
-#include "src/core/iomgr/alarm.h"
+#include "src/core/iomgr/timer.h"
typedef struct {
- grpc_alarm **alarms;
- gpr_uint32 alarm_count;
- gpr_uint32 alarm_capacity;
-} grpc_alarm_heap;
+ grpc_timer **timers;
+ gpr_uint32 timer_count;
+ gpr_uint32 timer_capacity;
+} grpc_timer_heap;
-/* return 1 if the new alarm is the first alarm in the heap */
-int grpc_alarm_heap_add(grpc_alarm_heap *heap, grpc_alarm *alarm);
+/* return 1 if the new timer is the first timer in the heap */
+int grpc_timer_heap_add(grpc_timer_heap *heap, grpc_timer *timer);
-void grpc_alarm_heap_init(grpc_alarm_heap *heap);
-void grpc_alarm_heap_destroy(grpc_alarm_heap *heap);
+void grpc_timer_heap_init(grpc_timer_heap *heap);
+void grpc_timer_heap_destroy(grpc_timer_heap *heap);
-void grpc_alarm_heap_remove(grpc_alarm_heap *heap, grpc_alarm *alarm);
-grpc_alarm *grpc_alarm_heap_top(grpc_alarm_heap *heap);
-void grpc_alarm_heap_pop(grpc_alarm_heap *heap);
+void grpc_timer_heap_remove(grpc_timer_heap *heap, grpc_timer *timer);
+grpc_timer *grpc_timer_heap_top(grpc_timer_heap *heap);
+void grpc_timer_heap_pop(grpc_timer_heap *heap);
-int grpc_alarm_heap_is_empty(grpc_alarm_heap *heap);
+int grpc_timer_heap_is_empty(grpc_timer_heap *heap);
-#endif /* GRPC_INTERNAL_CORE_IOMGR_ALARM_HEAP_H */
+#endif /* GRPC_INTERNAL_CORE_IOMGR_TIMER_HEAP_H */
diff --git a/src/core/iomgr/alarm_internal.h b/src/core/iomgr/timer_internal.h
index 31d840e6f9..f180eca36e 100644
--- a/src/core/iomgr/alarm_internal.h
+++ b/src/core/iomgr/timer_internal.h
@@ -31,33 +31,33 @@
*
*/
-#ifndef GRPC_INTERNAL_CORE_IOMGR_ALARM_INTERNAL_H
-#define GRPC_INTERNAL_CORE_IOMGR_ALARM_INTERNAL_H
+#ifndef GRPC_INTERNAL_CORE_IOMGR_TIMER_INTERNAL_H
+#define GRPC_INTERNAL_CORE_IOMGR_TIMER_INTERNAL_H
#include "src/core/iomgr/exec_ctx.h"
#include <grpc/support/sync.h>
#include <grpc/support/time.h>
-/* iomgr internal api for dealing with alarms */
+/* iomgr internal api for dealing with timers */
-/* Check for alarms to be run, and run them.
- Return non zero if alarm callbacks were executed.
+/* Check for timers to be run, and run them.
+ Return non zero if timer callbacks were executed.
Drops drop_mu if it is non-null before executing callbacks.
- If next is non-null, TRY to update *next with the next running alarm
- IF that alarm occurs before *next current value.
+ If next is non-null, TRY to update *next with the next running timer
+ IF that timer occurs before *next current value.
*next is never guaranteed to be updated on any given execution; however,
with high probability at least one thread in the system will see an update
at any time slice. */
-int grpc_alarm_check(grpc_exec_ctx* exec_ctx, gpr_timespec now,
+int grpc_timer_check(grpc_exec_ctx* exec_ctx, gpr_timespec now,
gpr_timespec* next);
-void grpc_alarm_list_init(gpr_timespec now);
-void grpc_alarm_list_shutdown(grpc_exec_ctx* exec_ctx);
+void grpc_timer_list_init(gpr_timespec now);
+void grpc_timer_list_shutdown(grpc_exec_ctx* exec_ctx);
-gpr_timespec grpc_alarm_list_next_timeout(void);
+gpr_timespec grpc_timer_list_next_timeout(void);
/* the following must be implemented by each iomgr implementation */
void grpc_kick_poller(void);
-#endif /* GRPC_INTERNAL_CORE_IOMGR_ALARM_INTERNAL_H */
+#endif /* GRPC_INTERNAL_CORE_IOMGR_TIMER_INTERNAL_H */
diff --git a/src/core/surface/alarm.c b/src/core/surface/alarm.c
new file mode 100644
index 0000000000..7c47dd56f8
--- /dev/null
+++ b/src/core/surface/alarm.c
@@ -0,0 +1,83 @@
+/*
+ *
+ * Copyright 2015, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "src/core/iomgr/timer.h"
+#include "src/core/surface/completion_queue.h"
+#include <grpc/grpc.h>
+#include <grpc/support/alloc.h>
+
+struct grpc_alarm {
+ grpc_timer alarm;
+ grpc_cq_completion completion;
+ /** completion queue where events about this alarm will be posted */
+ grpc_completion_queue *cq;
+ /** user supplied tag */
+ void *tag;
+};
+
+static void do_nothing_end_completion(grpc_exec_ctx *exec_ctx, void *arg,
+ grpc_cq_completion *c) {}
+
+static void alarm_cb(grpc_exec_ctx *exec_ctx, void *arg, int success) {
+ grpc_alarm *alarm = arg;
+ grpc_cq_end_op(exec_ctx, alarm->cq, alarm->tag, success,
+ do_nothing_end_completion, NULL, &alarm->completion);
+}
+
+grpc_alarm *grpc_alarm_create(grpc_completion_queue *cq, gpr_timespec deadline,
+ void *tag) {
+ grpc_alarm *alarm = gpr_malloc(sizeof(grpc_alarm));
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+
+ GRPC_CQ_INTERNAL_REF(cq, "alarm");
+ alarm->cq = cq;
+ alarm->tag = tag;
+
+ grpc_timer_init(&exec_ctx, &alarm->alarm, deadline, alarm_cb, alarm,
+ gpr_now(GPR_CLOCK_MONOTONIC));
+ grpc_cq_begin_op(cq);
+ grpc_exec_ctx_finish(&exec_ctx);
+ return alarm;
+}
+
+void grpc_alarm_cancel(grpc_alarm *alarm) {
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_timer_cancel(&exec_ctx, &alarm->alarm);
+ grpc_exec_ctx_finish(&exec_ctx);
+}
+
+void grpc_alarm_destroy(grpc_alarm *alarm) {
+ grpc_alarm_cancel(alarm);
+ GRPC_CQ_INTERNAL_UNREF(alarm->cq, "alarm");
+ gpr_free(alarm);
+}
diff --git a/src/core/surface/call.c b/src/core/surface/call.c
index 0b917f1561..4aa3ac3cd3 100644
--- a/src/core/surface/call.c
+++ b/src/core/surface/call.c
@@ -42,7 +42,7 @@
#include <grpc/support/useful.h>
#include "src/core/channel/channel_stack.h"
-#include "src/core/iomgr/alarm.h"
+#include "src/core/iomgr/timer.h"
#include "src/core/profiling/timers.h"
#include "src/core/support/string.h"
#include "src/core/surface/byte_buffer_queue.h"
@@ -236,7 +236,7 @@ struct grpc_call {
grpc_call_context_element context[GRPC_CONTEXT_COUNT];
/* Deadline alarm - if have_alarm is non-zero */
- grpc_alarm alarm;
+ grpc_timer alarm;
/* Call refcount - to keep the call alive during asynchronous operations */
gpr_refcount internal_refcount;
@@ -986,7 +986,7 @@ static void call_on_done_recv(grpc_exec_ctx *exec_ctx, void *pc, int success) {
GPR_ASSERT(call->read_state <= READ_STATE_STREAM_CLOSED);
call->read_state = READ_STATE_STREAM_CLOSED;
if (call->have_alarm) {
- grpc_alarm_cancel(exec_ctx, &call->alarm);
+ grpc_timer_cancel(exec_ctx, &call->alarm);
}
/* propagate cancellation to any interested children */
child_call = call->first_child;
@@ -1298,7 +1298,7 @@ void grpc_call_destroy(grpc_call *c) {
GPR_ASSERT(!c->destroy_called);
c->destroy_called = 1;
if (c->have_alarm) {
- grpc_alarm_cancel(&exec_ctx, &c->alarm);
+ grpc_timer_cancel(&exec_ctx, &c->alarm);
}
cancel = c->read_state != READ_STATE_STREAM_CLOSED;
unlock(&exec_ctx, c);
@@ -1417,7 +1417,7 @@ static void set_deadline_alarm(grpc_exec_ctx *exec_ctx, grpc_call *call,
GRPC_CALL_INTERNAL_REF(call, "alarm");
call->have_alarm = 1;
call->send_deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC);
- grpc_alarm_init(exec_ctx, &call->alarm, call->send_deadline, call_alarm, call,
+ grpc_timer_init(exec_ctx, &call->alarm, call->send_deadline, call_alarm, call,
gpr_now(GPR_CLOCK_MONOTONIC));
}
diff --git a/src/core/surface/channel_connectivity.c b/src/core/surface/channel_connectivity.c
index 47cbab154f..b992437d78 100644
--- a/src/core/surface/channel_connectivity.c
+++ b/src/core/surface/channel_connectivity.c
@@ -37,7 +37,7 @@
#include <grpc/support/log.h>
#include "src/core/channel/client_channel.h"
-#include "src/core/iomgr/alarm.h"
+#include "src/core/iomgr/timer.h"
#include "src/core/surface/completion_queue.h"
grpc_connectivity_state grpc_channel_check_connectivity_state(
@@ -74,7 +74,7 @@ typedef struct {
int success;
int removed;
grpc_closure on_complete;
- grpc_alarm alarm;
+ grpc_timer alarm;
grpc_connectivity_state state;
grpc_completion_queue *cq;
grpc_cq_completion completion_storage;
@@ -131,7 +131,7 @@ static void partly_done(grpc_exec_ctx *exec_ctx, state_watcher *w,
gpr_mu_lock(&w->mu);
w->success = 1;
gpr_mu_unlock(&w->mu);
- grpc_alarm_cancel(exec_ctx, &w->alarm);
+ grpc_timer_cancel(exec_ctx, &w->alarm);
}
gpr_mu_lock(&w->mu);
@@ -187,7 +187,7 @@ void grpc_channel_watch_connectivity_state(
w->tag = tag;
w->channel = channel;
- grpc_alarm_init(&exec_ctx, &w->alarm,
+ grpc_timer_init(&exec_ctx, &w->alarm,
gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC),
timeout_complete, w, gpr_now(GPR_CLOCK_MONOTONIC));
diff --git a/src/core/surface/completion_queue.c b/src/core/surface/completion_queue.c
index 31ec1b68da..d73e5a7b46 100644
--- a/src/core/surface/completion_queue.c
+++ b/src/core/surface/completion_queue.c
@@ -36,6 +36,7 @@
#include <stdio.h>
#include <string.h>
+#include "src/core/iomgr/timer.h"
#include "src/core/iomgr/pollset.h"
#include "src/core/support/string.h"
#include "src/core/surface/call.h"
@@ -71,6 +72,15 @@ struct grpc_completion_queue {
grpc_closure pollset_destroy_done;
};
+struct grpc_cq_alarm {
+ grpc_timer alarm;
+ grpc_cq_completion completion;
+ /** completion queue where events about this alarm will be posted */
+ grpc_completion_queue *cq;
+ /** user supplied tag */
+ void *tag;
+};
+
static void on_pollset_destroy_done(grpc_exec_ctx *exec_ctx, void *cc,
int success);
@@ -355,40 +365,3 @@ grpc_pollset *grpc_cq_pollset(grpc_completion_queue *cc) {
void grpc_cq_mark_server_cq(grpc_completion_queue *cc) { cc->is_server_cq = 1; }
int grpc_cq_is_server_cq(grpc_completion_queue *cc) { return cc->is_server_cq; }
-
-static void do_nothing_end_completion(grpc_exec_ctx *exec_ctx, void *arg,
- grpc_cq_completion *c) {}
-
-static void cq_alarm_cb(grpc_exec_ctx *exec_ctx, void *arg, int success) {
- grpc_cq_alarm *cq_alarm = arg;
- grpc_cq_end_op(exec_ctx, cq_alarm->cq, cq_alarm->tag, success,
- do_nothing_end_completion, NULL, &cq_alarm->completion);
-}
-
-grpc_cq_alarm *grpc_cq_alarm_create(grpc_completion_queue *cq,
- gpr_timespec deadline, void *tag) {
- grpc_cq_alarm *cq_alarm = gpr_malloc(sizeof(grpc_cq_alarm));
- grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
-
- GRPC_CQ_INTERNAL_REF(cq, "cq_alarm");
- cq_alarm->cq = cq;
- cq_alarm->tag = tag;
-
- grpc_alarm_init(&exec_ctx, &cq_alarm->alarm, deadline, cq_alarm_cb, cq_alarm,
- gpr_now(GPR_CLOCK_MONOTONIC));
- grpc_cq_begin_op(cq);
- grpc_exec_ctx_finish(&exec_ctx);
- return cq_alarm;
-}
-
-void grpc_cq_alarm_cancel(grpc_cq_alarm *cq_alarm) {
- grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- grpc_alarm_cancel(&exec_ctx, &cq_alarm->alarm);
- grpc_exec_ctx_finish(&exec_ctx);
-}
-
-void grpc_cq_alarm_destroy(grpc_cq_alarm *cq_alarm) {
- grpc_cq_alarm_cancel(cq_alarm);
- GRPC_CQ_INTERNAL_UNREF(cq_alarm->cq, "cq_alarm");
- gpr_free(cq_alarm);
-}
diff --git a/src/core/surface/completion_queue.h b/src/core/surface/completion_queue.h
index 12a7e9a147..5f8282e542 100644
--- a/src/core/surface/completion_queue.h
+++ b/src/core/surface/completion_queue.h
@@ -36,7 +36,6 @@
/* Internal API for completion queues */
-#include "src/core/iomgr/alarm.h"
#include "src/core/iomgr/pollset.h"
#include <grpc/grpc.h>
@@ -52,16 +51,6 @@ typedef struct grpc_cq_completion {
gpr_uintptr next;
} grpc_cq_completion;
-/** An alarm associated with a completion queue. */
-typedef struct grpc_cq_alarm {
- grpc_alarm alarm;
- grpc_cq_completion completion;
- /** completion queue where events about this alarm will be posted */
- grpc_completion_queue *cq;
- /** user supplied tag */
- void *tag;
-} grpc_cq_alarm;
-
#ifdef GRPC_CQ_REF_COUNT_DEBUG
void grpc_cq_internal_ref(grpc_completion_queue *cc, const char *reason,
const char *file, int line);
@@ -94,19 +83,4 @@ grpc_pollset *grpc_cq_pollset(grpc_completion_queue *cc);
void grpc_cq_mark_server_cq(grpc_completion_queue *cc);
int grpc_cq_is_server_cq(grpc_completion_queue *cc);
-/** Create a completion queue alarm instance associated to \a cq.
- *
- * Once the alarm expires (at \a deadline) or it's cancelled (see ...), an event
- * with tag \a tag will be added to \a cq. If the alarm expired, the event's
- * success bit will be true, false otherwise (ie, upon cancellation). */
-grpc_cq_alarm *grpc_cq_alarm_create(grpc_completion_queue *cq,
- gpr_timespec deadline, void *tag);
-
-/** Cancel a completion queue alarm. Calling this function ove an alarm that has
- * already run has no effect. */
-void grpc_cq_alarm_cancel(grpc_cq_alarm *cq_alarm);
-
-/** Destroy the given completion queue alarm, cancelling it in the process. */
-void grpc_cq_alarm_destroy(grpc_cq_alarm *cq_alarm);
-
#endif /* GRPC_INTERNAL_CORE_SURFACE_COMPLETION_QUEUE_H */