blob: d84a278b183d2c2dc95af3faa7ab053e84c661ca [file] [log] [blame]
ctiller18b49ab2014-12-09 14:39:16 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
ctiller18b49ab2014-12-09 14:39:16 -08004 * 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
Craig Tiller9a4dddd2016-03-25 17:08:13 -070034#ifndef GRPC_CORE_LIB_IOMGR_TIMER_H
35#define GRPC_CORE_LIB_IOMGR_TIMER_H
ctiller18b49ab2014-12-09 14:39:16 -080036
murgatroid999030c812016-09-16 13:25:08 -070037#include "src/core/lib/iomgr/port.h"
38
39#ifdef GRPC_UV
40#include "src/core/lib/iomgr/timer_uv.h"
41#else
42#include "src/core/lib/iomgr/timer_generic.h"
43#endif /* GRPC_UV */
44
ctiller18b49ab2014-12-09 14:39:16 -080045#include <grpc/support/port_platform.h>
46#include <grpc/support/time.h>
Craig Tiller9533d042016-03-25 17:11:06 -070047#include "src/core/lib/iomgr/exec_ctx.h"
48#include "src/core/lib/iomgr/iomgr.h"
ctiller18b49ab2014-12-09 14:39:16 -080049
murgatroid999030c812016-09-16 13:25:08 -070050typedef struct grpc_timer grpc_timer;
ctiller18b49ab2014-12-09 14:39:16 -080051
Masood Malekghassemib5b43722017-01-05 15:07:26 -080052/* Initialize *timer. When expired or canceled, closure will be called with
53 error set to indicate if it expired (GRPC_ERROR_NONE) or was canceled
54 (GRPC_ERROR_CANCELLED). timer_cb is guaranteed to be called exactly once, and
55 application code should check the error to determine how it was invoked. The
56 application callback is also responsible for maintaining information about
57 when to free up any user-level state. */
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070058void grpc_timer_init(grpc_exec_ctx *exec_ctx, grpc_timer *timer,
Masood Malekghassemib5b43722017-01-05 15:07:26 -080059 gpr_timespec deadline, grpc_closure *closure,
60 gpr_timespec now);
ctiller18b49ab2014-12-09 14:39:16 -080061
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070062/* Note that there is no timer destroy function. This is because the
63 timer is a one-time occurrence with a guarantee that the callback will
ctiller18b49ab2014-12-09 14:39:16 -080064 be called exactly once, either at expiration or cancellation. Thus, all
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070065 the internal timer event management state is destroyed just before
ctiller18b49ab2014-12-09 14:39:16 -080066 that callback is invoked. If the user has additional state associated with
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070067 the timer, the user is responsible for determining when it is safe to
ctiller18b49ab2014-12-09 14:39:16 -080068 destroy that state. */
69
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070070/* Cancel an *timer.
ctiller18b49ab2014-12-09 14:39:16 -080071 There are three cases:
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070072 1. We normally cancel the timer
73 2. The timer has already run
74 3. We can't cancel the timer because it is "in flight".
ctiller18b49ab2014-12-09 14:39:16 -080075
76 In all of these cases, the cancellation is still considered successful.
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070077 They are essentially distinguished in that the timer_cb will be run
Mark D. Roth7c8b7562016-10-05 07:34:01 -070078 exactly once from either the cancellation (with error GRPC_ERROR_CANCELLED)
79 or from the activation (with error GRPC_ERROR_NONE).
ctiller18b49ab2014-12-09 14:39:16 -080080
ctiller3bf466f2014-12-19 16:21:57 -080081 Note carefully that the callback function MAY occur in the same callstack
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070082 as grpc_timer_cancel. It's expected that most timers will be cancelled (their
ctiller3bf466f2014-12-19 16:21:57 -080083 primary use is to implement deadlines), and so this code is optimized such
84 that cancellation costs as little as possible. Making callbacks run inline
85 matches this aim.
86
Mark D. Roth7c8b7562016-10-05 07:34:01 -070087 Requires: cancel() must happen after init() on a given timer */
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070088void grpc_timer_cancel(grpc_exec_ctx *exec_ctx, grpc_timer *timer);
ctiller18b49ab2014-12-09 14:39:16 -080089
Craig Tillerccdea192016-02-16 08:06:46 -080090/* iomgr internal api for dealing with timers */
91
92/* Check for timers to be run, and run them.
Craig Tiller311445f2016-02-18 07:31:39 -080093 Return true if timer callbacks were executed.
Craig Tillerccdea192016-02-16 08:06:46 -080094 If next is non-null, TRY to update *next with the next running timer
95 IF that timer occurs before *next current value.
96 *next is never guaranteed to be updated on any given execution; however,
97 with high probability at least one thread in the system will see an update
98 at any time slice. */
Craig Tiller311445f2016-02-18 07:31:39 -080099bool grpc_timer_check(grpc_exec_ctx *exec_ctx, gpr_timespec now,
100 gpr_timespec *next);
Craig Tillerccdea192016-02-16 08:06:46 -0800101void grpc_timer_list_init(gpr_timespec now);
Craig Tiller311445f2016-02-18 07:31:39 -0800102void grpc_timer_list_shutdown(grpc_exec_ctx *exec_ctx);
Craig Tillerccdea192016-02-16 08:06:46 -0800103
104/* the following must be implemented by each iomgr implementation */
105
106void grpc_kick_poller(void);
107
Craig Tiller9a4dddd2016-03-25 17:08:13 -0700108#endif /* GRPC_CORE_LIB_IOMGR_TIMER_H */