murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 45 | static void timer_close_callback(uv_handle_t *handle) { gpr_free(handle); } |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 46 | |
| 47 | static void stop_uv_timer(uv_timer_t *handle) { |
| 48 | uv_timer_stop(handle); |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 49 | uv_unref((uv_handle_t *)handle); |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 50 | uv_close((uv_handle_t *)handle, timer_close_callback); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void run_expired_timer(uv_timer_t *handle) { |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 54 | grpc_timer *timer = (grpc_timer *)handle->data; |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 55 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 56 | GPR_ASSERT(!timer->triggered); |
| 57 | timer->triggered = 1; |
Masood Malekghassemi | b5b4372 | 2017-01-05 15:07:26 -0800 | [diff] [blame] | 58 | grpc_closure_sched(&exec_ctx, timer->closure, GRPC_ERROR_NONE); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 59 | stop_uv_timer(handle); |
| 60 | grpc_exec_ctx_finish(&exec_ctx); |
| 61 | } |
| 62 | |
| 63 | void grpc_timer_init(grpc_exec_ctx *exec_ctx, grpc_timer *timer, |
Masood Malekghassemi | b5b4372 | 2017-01-05 15:07:26 -0800 | [diff] [blame] | 64 | gpr_timespec deadline, grpc_closure *closure, |
| 65 | gpr_timespec now) { |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 66 | uint64_t timeout; |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 67 | uv_timer_t *uv_timer; |
Masood Malekghassemi | b5b4372 | 2017-01-05 15:07:26 -0800 | [diff] [blame] | 68 | timer->closure = closure; |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 69 | if (gpr_time_cmp(deadline, now) <= 0) { |
| 70 | timer->triggered = 1; |
Masood Malekghassemi | b5b4372 | 2017-01-05 15:07:26 -0800 | [diff] [blame] | 71 | grpc_closure_sched(exec_ctx, timer->closure, GRPC_ERROR_NONE); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 72 | return; |
| 73 | } |
| 74 | timer->triggered = 0; |
| 75 | timeout = (uint64_t)gpr_time_to_millis(gpr_time_sub(deadline, now)); |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 76 | 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); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void grpc_timer_cancel(grpc_exec_ctx *exec_ctx, grpc_timer *timer) { |
| 84 | if (!timer->triggered) { |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 85 | timer->triggered = 1; |
Masood Malekghassemi | b5b4372 | 2017-01-05 15:07:26 -0800 | [diff] [blame] | 86 | grpc_closure_sched(exec_ctx, timer->closure, GRPC_ERROR_CANCELLED); |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 87 | stop_uv_timer((uv_timer_t *)timer->uv_timer); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | bool grpc_timer_check(grpc_exec_ctx *exec_ctx, gpr_timespec now, |
| 92 | gpr_timespec *next) { |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | void grpc_timer_list_init(gpr_timespec now) {} |
| 97 | void grpc_timer_list_shutdown(grpc_exec_ctx *exec_ctx) {} |
| 98 | |
| 99 | #endif /* GRPC_UV */ |