OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-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/iomgr/timer.h" |
| 35 #include "src/core/surface/completion_queue.h" |
| 36 #include <grpc/grpc.h> |
| 37 #include <grpc/support/alloc.h> |
| 38 |
| 39 struct grpc_alarm { |
| 40 grpc_timer alarm; |
| 41 grpc_cq_completion completion; |
| 42 /** completion queue where events about this alarm will be posted */ |
| 43 grpc_completion_queue *cq; |
| 44 /** user supplied tag */ |
| 45 void *tag; |
| 46 }; |
| 47 |
| 48 static void do_nothing_end_completion(grpc_exec_ctx *exec_ctx, void *arg, |
| 49 grpc_cq_completion *c) {} |
| 50 |
| 51 static void alarm_cb(grpc_exec_ctx *exec_ctx, void *arg, bool success) { |
| 52 grpc_alarm *alarm = arg; |
| 53 grpc_cq_end_op(exec_ctx, alarm->cq, alarm->tag, success, |
| 54 do_nothing_end_completion, NULL, &alarm->completion); |
| 55 } |
| 56 |
| 57 grpc_alarm *grpc_alarm_create(grpc_completion_queue *cq, gpr_timespec deadline, |
| 58 void *tag) { |
| 59 grpc_alarm *alarm = gpr_malloc(sizeof(grpc_alarm)); |
| 60 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 61 |
| 62 GRPC_CQ_INTERNAL_REF(cq, "alarm"); |
| 63 alarm->cq = cq; |
| 64 alarm->tag = tag; |
| 65 |
| 66 grpc_cq_begin_op(cq, tag); |
| 67 grpc_timer_init(&exec_ctx, &alarm->alarm, |
| 68 gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC), |
| 69 alarm_cb, alarm, gpr_now(GPR_CLOCK_MONOTONIC)); |
| 70 grpc_exec_ctx_finish(&exec_ctx); |
| 71 return alarm; |
| 72 } |
| 73 |
| 74 void grpc_alarm_cancel(grpc_alarm *alarm) { |
| 75 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 76 grpc_timer_cancel(&exec_ctx, &alarm->alarm); |
| 77 grpc_exec_ctx_finish(&exec_ctx); |
| 78 } |
| 79 |
| 80 void grpc_alarm_destroy(grpc_alarm *alarm) { |
| 81 grpc_alarm_cancel(alarm); |
| 82 GRPC_CQ_INTERNAL_UNREF(alarm->cq, "alarm"); |
| 83 gpr_free(alarm); |
| 84 } |
OLD | NEW |