blob: ef8405cca848c88a24f6005753932613771525b4 [file] [log] [blame]
David Garcia Quintasf747bbc2015-10-04 23:09:47 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
David Garcia Quintasf747bbc2015-10-04 23:09:47 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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 Quintasf747bbc2015-10-04 23:09:47 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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 Quintasf747bbc2015-10-04 23:09:47 -070016 *
17 */
18
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070019#include <grpc/grpc.h>
20#include <grpc/support/alloc.h>
Craig Tiller9533d042016-03-25 17:11:06 -070021#include "src/core/lib/iomgr/timer.h"
22#include "src/core/lib/surface/completion_queue.h"
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070023
24struct grpc_alarm {
25 grpc_timer alarm;
Masood Malekghassemib5b43722017-01-05 15:07:26 -080026 grpc_closure on_alarm;
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070027 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
34static void do_nothing_end_completion(grpc_exec_ctx *exec_ctx, void *arg,
35 grpc_cq_completion *c) {}
36
Craig Tillerc027e772016-05-03 16:27:00 -070037static void alarm_cb(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) {
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070038 grpc_alarm *alarm = arg;
Craig Tillerc027e772016-05-03 16:27:00 -070039 grpc_cq_end_op(exec_ctx, alarm->cq, alarm->tag, error,
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070040 do_nothing_end_completion, NULL, &alarm->completion);
41}
42
43grpc_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 Pai090c8672016-02-11 14:43:43 -080052 grpc_cq_begin_op(cq, tag);
ncteisen274bbbe2017-06-08 14:57:11 -070053 GRPC_CLOSURE_INIT(&alarm->on_alarm, alarm_cb, alarm,
Masood Malekghassemib5b43722017-01-05 15:07:26 -080054 grpc_schedule_on_exec_ctx);
David Garcia Quintas01608732016-02-22 17:49:45 -080055 grpc_timer_init(&exec_ctx, &alarm->alarm,
56 gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC),
Masood Malekghassemib5b43722017-01-05 15:07:26 -080057 &alarm->on_alarm, gpr_now(GPR_CLOCK_MONOTONIC));
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070058 grpc_exec_ctx_finish(&exec_ctx);
59 return alarm;
60}
61
62void 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
68void grpc_alarm_destroy(grpc_alarm *alarm) {
Craig Tillerf8401102017-04-17 09:47:28 -070069 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070070 grpc_alarm_cancel(alarm);
Craig Tillerf8401102017-04-17 09:47:28 -070071 GRPC_CQ_INTERNAL_UNREF(&exec_ctx, alarm->cq, "alarm");
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070072 gpr_free(alarm);
Craig Tillerf8401102017-04-17 09:47:28 -070073 grpc_exec_ctx_finish(&exec_ctx);
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070074}