blob: fa2cdee9646afae7af224baa769e5665c6cf0a13 [file] [log] [blame]
murgatroid999030c812016-09-16 13:25:08 -07001/*
2 *
3 * Copyright 2016, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include "src/core/lib/iomgr/port.h"
35
36#if GRPC_UV
37
38#include <grpc/support/alloc.h>
39#include <grpc/support/log.h>
40
41#include "src/core/lib/iomgr/timer.h"
42
43#include <uv.h>
44
murgatroid99dedb9232016-09-26 13:54:04 -070045static void timer_close_callback(uv_handle_t *handle) { gpr_free(handle); }
murgatroid999030c812016-09-16 13:25:08 -070046
47static void stop_uv_timer(uv_timer_t *handle) {
48 uv_timer_stop(handle);
murgatroid99dedb9232016-09-26 13:54:04 -070049 uv_unref((uv_handle_t *)handle);
murgatroid99dedb9232016-09-26 13:54:04 -070050 uv_close((uv_handle_t *)handle, timer_close_callback);
murgatroid999030c812016-09-16 13:25:08 -070051}
52
53void run_expired_timer(uv_timer_t *handle) {
murgatroid99dedb9232016-09-26 13:54:04 -070054 grpc_timer *timer = (grpc_timer *)handle->data;
murgatroid999030c812016-09-16 13:25:08 -070055 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
murgatroid999030c812016-09-16 13:25:08 -070056 GPR_ASSERT(!timer->triggered);
57 timer->triggered = 1;
Masood Malekghassemib5b43722017-01-05 15:07:26 -080058 grpc_closure_sched(&exec_ctx, timer->closure, GRPC_ERROR_NONE);
murgatroid999030c812016-09-16 13:25:08 -070059 stop_uv_timer(handle);
60 grpc_exec_ctx_finish(&exec_ctx);
61}
62
63void grpc_timer_init(grpc_exec_ctx *exec_ctx, grpc_timer *timer,
Masood Malekghassemib5b43722017-01-05 15:07:26 -080064 gpr_timespec deadline, grpc_closure *closure,
65 gpr_timespec now) {
murgatroid999030c812016-09-16 13:25:08 -070066 uint64_t timeout;
murgatroid997871f732016-09-23 13:49:05 -070067 uv_timer_t *uv_timer;
Masood Malekghassemib5b43722017-01-05 15:07:26 -080068 timer->closure = closure;
murgatroid999030c812016-09-16 13:25:08 -070069 if (gpr_time_cmp(deadline, now) <= 0) {
70 timer->triggered = 1;
Masood Malekghassemib5b43722017-01-05 15:07:26 -080071 grpc_closure_sched(exec_ctx, timer->closure, GRPC_ERROR_NONE);
murgatroid999030c812016-09-16 13:25:08 -070072 return;
73 }
74 timer->triggered = 0;
75 timeout = (uint64_t)gpr_time_to_millis(gpr_time_sub(deadline, now));
murgatroid997871f732016-09-23 13:49:05 -070076 uv_timer = gpr_malloc(sizeof(uv_timer_t));
77 uv_timer_init(uv_default_loop(), uv_timer);
78 uv_timer->data = timer;
79 timer->uv_timer = uv_timer;
80 uv_timer_start(uv_timer, run_expired_timer, timeout, 0);
murgatroid999030c812016-09-16 13:25:08 -070081}
82
83void grpc_timer_cancel(grpc_exec_ctx *exec_ctx, grpc_timer *timer) {
84 if (!timer->triggered) {
murgatroid999030c812016-09-16 13:25:08 -070085 timer->triggered = 1;
Masood Malekghassemib5b43722017-01-05 15:07:26 -080086 grpc_closure_sched(exec_ctx, timer->closure, GRPC_ERROR_CANCELLED);
murgatroid99dedb9232016-09-26 13:54:04 -070087 stop_uv_timer((uv_timer_t *)timer->uv_timer);
murgatroid999030c812016-09-16 13:25:08 -070088 }
89}
90
91bool grpc_timer_check(grpc_exec_ctx *exec_ctx, gpr_timespec now,
92 gpr_timespec *next) {
93 return false;
94}
95
96void grpc_timer_list_init(gpr_timespec now) {}
97void grpc_timer_list_shutdown(grpc_exec_ctx *exec_ctx) {}
98
99#endif /* GRPC_UV */