blob: 466600d582c619da93a9ac09dd2589ceb2e55b90 [file] [log] [blame]
ctiller18b49ab2014-12-09 14:39:16 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
ctiller18b49ab2014-12-09 14:39:16 -08004 *
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
ctiller18b49ab2014-12-09 14:39:16 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
ctiller18b49ab2014-12-09 14:39:16 -080010 *
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.
ctiller18b49ab2014-12-09 14:39:16 -080016 *
17 */
18
Craig Tiller9a4dddd2016-03-25 17:08:13 -070019#ifndef GRPC_CORE_LIB_IOMGR_TIMER_H
20#define GRPC_CORE_LIB_IOMGR_TIMER_H
ctiller18b49ab2014-12-09 14:39:16 -080021
murgatroid999030c812016-09-16 13:25:08 -070022#include "src/core/lib/iomgr/port.h"
23
24#ifdef GRPC_UV
25#include "src/core/lib/iomgr/timer_uv.h"
26#else
27#include "src/core/lib/iomgr/timer_generic.h"
28#endif /* GRPC_UV */
29
ctiller18b49ab2014-12-09 14:39:16 -080030#include <grpc/support/port_platform.h>
31#include <grpc/support/time.h>
Craig Tiller9533d042016-03-25 17:11:06 -070032#include "src/core/lib/iomgr/exec_ctx.h"
33#include "src/core/lib/iomgr/iomgr.h"
ctiller18b49ab2014-12-09 14:39:16 -080034
Yash Tibrewala7e6d652017-09-20 18:56:37 -070035#ifdef __cplusplus
36extern "C" {
37#endif
38
murgatroid999030c812016-09-16 13:25:08 -070039typedef struct grpc_timer grpc_timer;
ctiller18b49ab2014-12-09 14:39:16 -080040
Masood Malekghassemib5b43722017-01-05 15:07:26 -080041/* Initialize *timer. When expired or canceled, closure will be called with
42 error set to indicate if it expired (GRPC_ERROR_NONE) or was canceled
43 (GRPC_ERROR_CANCELLED). timer_cb is guaranteed to be called exactly once, and
44 application code should check the error to determine how it was invoked. The
45 application callback is also responsible for maintaining information about
46 when to free up any user-level state. */
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070047void grpc_timer_init(grpc_exec_ctx *exec_ctx, grpc_timer *timer,
Masood Malekghassemib5b43722017-01-05 15:07:26 -080048 gpr_timespec deadline, grpc_closure *closure,
49 gpr_timespec now);
ctiller18b49ab2014-12-09 14:39:16 -080050
Vijay Pai58c33ba2017-09-01 14:08:42 -070051/* Initialize *timer without setting it. This can later be passed through
52 the regular init or cancel */
53void grpc_timer_init_unset(grpc_timer *timer);
54
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070055/* Note that there is no timer destroy function. This is because the
56 timer is a one-time occurrence with a guarantee that the callback will
ctiller18b49ab2014-12-09 14:39:16 -080057 be called exactly once, either at expiration or cancellation. Thus, all
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070058 the internal timer event management state is destroyed just before
ctiller18b49ab2014-12-09 14:39:16 -080059 that callback is invoked. If the user has additional state associated with
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070060 the timer, the user is responsible for determining when it is safe to
ctiller18b49ab2014-12-09 14:39:16 -080061 destroy that state. */
62
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070063/* Cancel an *timer.
ctiller18b49ab2014-12-09 14:39:16 -080064 There are three cases:
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070065 1. We normally cancel the timer
66 2. The timer has already run
67 3. We can't cancel the timer because it is "in flight".
ctiller18b49ab2014-12-09 14:39:16 -080068
69 In all of these cases, the cancellation is still considered successful.
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070070 They are essentially distinguished in that the timer_cb will be run
Mark D. Roth7c8b7562016-10-05 07:34:01 -070071 exactly once from either the cancellation (with error GRPC_ERROR_CANCELLED)
72 or from the activation (with error GRPC_ERROR_NONE).
ctiller18b49ab2014-12-09 14:39:16 -080073
ctiller3bf466f2014-12-19 16:21:57 -080074 Note carefully that the callback function MAY occur in the same callstack
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070075 as grpc_timer_cancel. It's expected that most timers will be cancelled (their
ctiller3bf466f2014-12-19 16:21:57 -080076 primary use is to implement deadlines), and so this code is optimized such
77 that cancellation costs as little as possible. Making callbacks run inline
78 matches this aim.
79
Mark D. Roth7c8b7562016-10-05 07:34:01 -070080 Requires: cancel() must happen after init() on a given timer */
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070081void grpc_timer_cancel(grpc_exec_ctx *exec_ctx, grpc_timer *timer);
ctiller18b49ab2014-12-09 14:39:16 -080082
Craig Tillerccdea192016-02-16 08:06:46 -080083/* iomgr internal api for dealing with timers */
84
Craig Tillerb2ef1cf2017-05-30 09:25:48 -070085typedef enum {
86 GRPC_TIMERS_NOT_CHECKED,
87 GRPC_TIMERS_CHECKED_AND_EMPTY,
88 GRPC_TIMERS_FIRED,
89} grpc_timer_check_result;
90
Craig Tillerccdea192016-02-16 08:06:46 -080091/* Check for timers to be run, and run them.
Craig Tiller311445f2016-02-18 07:31:39 -080092 Return true if timer callbacks were executed.
Craig Tillerccdea192016-02-16 08:06:46 -080093 If next is non-null, TRY to update *next with the next running timer
94 IF that timer occurs before *next current value.
95 *next is never guaranteed to be updated on any given execution; however,
96 with high probability at least one thread in the system will see an update
97 at any time slice. */
Craig Tillerb2ef1cf2017-05-30 09:25:48 -070098grpc_timer_check_result grpc_timer_check(grpc_exec_ctx *exec_ctx,
99 gpr_timespec now, gpr_timespec *next);
Craig Tillerccdea192016-02-16 08:06:46 -0800100void grpc_timer_list_init(gpr_timespec now);
Craig Tiller311445f2016-02-18 07:31:39 -0800101void grpc_timer_list_shutdown(grpc_exec_ctx *exec_ctx);
Craig Tillerccdea192016-02-16 08:06:46 -0800102
Craig Tiller185f6c92017-03-17 08:33:19 -0700103/* Consume a kick issued by grpc_kick_poller */
104void grpc_timer_consume_kick(void);
105
Craig Tillerccdea192016-02-16 08:06:46 -0800106/* the following must be implemented by each iomgr implementation */
107
108void grpc_kick_poller(void);
109
Yash Tibrewala7e6d652017-09-20 18:56:37 -0700110#ifdef __cplusplus
111}
112#endif
113
Craig Tiller9a4dddd2016-03-25 17:08:13 -0700114#endif /* GRPC_CORE_LIB_IOMGR_TIMER_H */