ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 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 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 34 | #ifndef GRPC_CORE_LIB_IOMGR_TIMER_H |
| 35 | #define GRPC_CORE_LIB_IOMGR_TIMER_H |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 36 | |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 37 | #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 | |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 45 | #include <grpc/support/port_platform.h> |
| 46 | #include <grpc/support/time.h> |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 47 | #include "src/core/lib/iomgr/exec_ctx.h" |
| 48 | #include "src/core/lib/iomgr/iomgr.h" |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 49 | |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 50 | typedef struct grpc_timer grpc_timer; |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 51 | |
Masood Malekghassemi | b5b4372 | 2017-01-05 15:07:26 -0800 | [diff] [blame] | 52 | /* 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 Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 58 | void grpc_timer_init(grpc_exec_ctx *exec_ctx, grpc_timer *timer, |
Masood Malekghassemi | b5b4372 | 2017-01-05 15:07:26 -0800 | [diff] [blame] | 59 | gpr_timespec deadline, grpc_closure *closure, |
| 60 | gpr_timespec now); |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 61 | |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 62 | /* 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 |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 64 | be called exactly once, either at expiration or cancellation. Thus, all |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 65 | the internal timer event management state is destroyed just before |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 66 | that callback is invoked. If the user has additional state associated with |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 67 | the timer, the user is responsible for determining when it is safe to |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 68 | destroy that state. */ |
| 69 | |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 70 | /* Cancel an *timer. |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 71 | There are three cases: |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 72 | 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". |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 75 | |
| 76 | In all of these cases, the cancellation is still considered successful. |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 77 | They are essentially distinguished in that the timer_cb will be run |
Mark D. Roth | 7c8b756 | 2016-10-05 07:34:01 -0700 | [diff] [blame] | 78 | exactly once from either the cancellation (with error GRPC_ERROR_CANCELLED) |
| 79 | or from the activation (with error GRPC_ERROR_NONE). |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 80 | |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 81 | Note carefully that the callback function MAY occur in the same callstack |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 82 | as grpc_timer_cancel. It's expected that most timers will be cancelled (their |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 83 | 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. Roth | 7c8b756 | 2016-10-05 07:34:01 -0700 | [diff] [blame] | 87 | Requires: cancel() must happen after init() on a given timer */ |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 88 | void grpc_timer_cancel(grpc_exec_ctx *exec_ctx, grpc_timer *timer); |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 89 | |
Craig Tiller | ccdea19 | 2016-02-16 08:06:46 -0800 | [diff] [blame] | 90 | /* iomgr internal api for dealing with timers */ |
| 91 | |
| 92 | /* Check for timers to be run, and run them. |
Craig Tiller | 311445f | 2016-02-18 07:31:39 -0800 | [diff] [blame] | 93 | Return true if timer callbacks were executed. |
Craig Tiller | ccdea19 | 2016-02-16 08:06:46 -0800 | [diff] [blame] | 94 | 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 Tiller | 311445f | 2016-02-18 07:31:39 -0800 | [diff] [blame] | 99 | bool grpc_timer_check(grpc_exec_ctx *exec_ctx, gpr_timespec now, |
| 100 | gpr_timespec *next); |
Craig Tiller | ccdea19 | 2016-02-16 08:06:46 -0800 | [diff] [blame] | 101 | void grpc_timer_list_init(gpr_timespec now); |
Craig Tiller | 311445f | 2016-02-18 07:31:39 -0800 | [diff] [blame] | 102 | void grpc_timer_list_shutdown(grpc_exec_ctx *exec_ctx); |
Craig Tiller | ccdea19 | 2016-02-16 08:06:46 -0800 | [diff] [blame] | 103 | |
| 104 | /* the following must be implemented by each iomgr implementation */ |
| 105 | |
| 106 | void grpc_kick_poller(void); |
| 107 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 108 | #endif /* GRPC_CORE_LIB_IOMGR_TIMER_H */ |