David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 19 | #include <grpc/grpc.h> |
| 20 | #include <grpc/support/alloc.h> |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 21 | #include "src/core/lib/iomgr/timer.h" |
| 22 | #include "src/core/lib/surface/completion_queue.h" |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 23 | |
| 24 | struct grpc_alarm { |
| 25 | grpc_timer alarm; |
Masood Malekghassemi | b5b4372 | 2017-01-05 15:07:26 -0800 | [diff] [blame] | 26 | grpc_closure on_alarm; |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 27 | grpc_cq_completion completion; |
| 28 | /** completion queue where events about this alarm will be posted */ |
| 29 | grpc_completion_queue *cq; |
| 30 | /** user supplied tag */ |
| 31 | void *tag; |
| 32 | }; |
| 33 | |
| 34 | static void do_nothing_end_completion(grpc_exec_ctx *exec_ctx, void *arg, |
| 35 | grpc_cq_completion *c) {} |
| 36 | |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 37 | static void alarm_cb(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) { |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 38 | grpc_alarm *alarm = arg; |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 39 | grpc_cq_end_op(exec_ctx, alarm->cq, alarm->tag, error, |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 40 | do_nothing_end_completion, NULL, &alarm->completion); |
| 41 | } |
| 42 | |
| 43 | grpc_alarm *grpc_alarm_create(grpc_completion_queue *cq, gpr_timespec deadline, |
| 44 | void *tag) { |
| 45 | grpc_alarm *alarm = gpr_malloc(sizeof(grpc_alarm)); |
| 46 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 47 | |
| 48 | GRPC_CQ_INTERNAL_REF(cq, "alarm"); |
| 49 | alarm->cq = cq; |
| 50 | alarm->tag = tag; |
| 51 | |
Vijay Pai | 090c867 | 2016-02-11 14:43:43 -0800 | [diff] [blame] | 52 | grpc_cq_begin_op(cq, tag); |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 53 | GRPC_CLOSURE_INIT(&alarm->on_alarm, alarm_cb, alarm, |
Masood Malekghassemi | b5b4372 | 2017-01-05 15:07:26 -0800 | [diff] [blame] | 54 | grpc_schedule_on_exec_ctx); |
David Garcia Quintas | 0160873 | 2016-02-22 17:49:45 -0800 | [diff] [blame] | 55 | grpc_timer_init(&exec_ctx, &alarm->alarm, |
| 56 | gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC), |
Masood Malekghassemi | b5b4372 | 2017-01-05 15:07:26 -0800 | [diff] [blame] | 57 | &alarm->on_alarm, gpr_now(GPR_CLOCK_MONOTONIC)); |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 58 | grpc_exec_ctx_finish(&exec_ctx); |
| 59 | return alarm; |
| 60 | } |
| 61 | |
| 62 | void grpc_alarm_cancel(grpc_alarm *alarm) { |
| 63 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 64 | grpc_timer_cancel(&exec_ctx, &alarm->alarm); |
| 65 | grpc_exec_ctx_finish(&exec_ctx); |
| 66 | } |
| 67 | |
| 68 | void grpc_alarm_destroy(grpc_alarm *alarm) { |
Craig Tiller | f840110 | 2017-04-17 09:47:28 -0700 | [diff] [blame] | 69 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 70 | grpc_alarm_cancel(alarm); |
Craig Tiller | f840110 | 2017-04-17 09:47:28 -0700 | [diff] [blame] | 71 | GRPC_CQ_INTERNAL_UNREF(&exec_ctx, alarm->cq, "alarm"); |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 72 | gpr_free(alarm); |
Craig Tiller | f840110 | 2017-04-17 09:47:28 -0700 | [diff] [blame] | 73 | grpc_exec_ctx_finish(&exec_ctx); |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 74 | } |