ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | f315bc1 | 2016-02-18 08:25:15 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, 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 | |
David Garcia Quintas | 3598d44 | 2016-03-15 14:53:05 -0700 | [diff] [blame] | 34 | #ifndef GRPC_CORE_IOMGR_TIMER_H |
| 35 | #define GRPC_CORE_IOMGR_TIMER_H |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 36 | |
| 37 | #include "src/core/iomgr/iomgr.h" |
Craig Tiller | 298751c | 2015-09-22 09:41:05 -0700 | [diff] [blame] | 38 | #include "src/core/iomgr/exec_ctx.h" |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 39 | #include <grpc/support/port_platform.h> |
| 40 | #include <grpc/support/time.h> |
| 41 | |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 42 | typedef struct grpc_timer { |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 43 | gpr_timespec deadline; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 44 | uint32_t heap_index; /* INVALID_HEAP_INDEX if not in heap */ |
Craig Tiller | cae5bf5 | 2015-07-02 08:46:59 -0700 | [diff] [blame] | 45 | int triggered; |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 46 | struct grpc_timer *next; |
| 47 | struct grpc_timer *prev; |
Craig Tiller | d1bec03 | 2015-09-18 17:29:00 -0700 | [diff] [blame] | 48 | grpc_closure closure; |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 49 | } grpc_timer; |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 50 | |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 51 | /* Initialize *timer. When expired or canceled, timer_cb will be called with |
| 52 | *timer_cb_arg and status to indicate if it expired (SUCCESS) or was |
| 53 | canceled (CANCELLED). timer_cb is guaranteed to be called exactly once, |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 54 | and application code should check the status to determine how it was |
| 55 | invoked. The application callback is also responsible for maintaining |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 56 | information about when to free up any user-level state. */ |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 57 | void grpc_timer_init(grpc_exec_ctx *exec_ctx, grpc_timer *timer, |
| 58 | gpr_timespec deadline, grpc_iomgr_cb_func timer_cb, |
| 59 | void *timer_cb_arg, gpr_timespec now); |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 60 | |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 61 | /* Note that there is no timer destroy function. This is because the |
| 62 | timer is a one-time occurrence with a guarantee that the callback will |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 63 | be called exactly once, either at expiration or cancellation. Thus, all |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 64 | the internal timer event management state is destroyed just before |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 65 | 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] | 66 | the timer, the user is responsible for determining when it is safe to |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 67 | destroy that state. */ |
| 68 | |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 69 | /* Cancel an *timer. |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 70 | There are three cases: |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 71 | 1. We normally cancel the timer |
| 72 | 2. The timer has already run |
| 73 | 3. We can't cancel the timer because it is "in flight". |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 74 | |
| 75 | In all of these cases, the cancellation is still considered successful. |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 76 | They are essentially distinguished in that the timer_cb will be run |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 77 | exactly once from either the cancellation (with status CANCELLED) |
| 78 | or from the activation (with status SUCCESS) |
| 79 | |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 80 | 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] | 81 | 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] | 82 | primary use is to implement deadlines), and so this code is optimized such |
| 83 | that cancellation costs as little as possible. Making callbacks run inline |
| 84 | matches this aim. |
| 85 | |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 86 | Requires: cancel() must happen after add() on a given timer */ |
| 87 | void grpc_timer_cancel(grpc_exec_ctx *exec_ctx, grpc_timer *timer); |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 88 | |
Craig Tiller | ccdea19 | 2016-02-16 08:06:46 -0800 | [diff] [blame] | 89 | /* iomgr internal api for dealing with timers */ |
| 90 | |
| 91 | /* Check for timers to be run, and run them. |
Craig Tiller | 311445f | 2016-02-18 07:31:39 -0800 | [diff] [blame] | 92 | Return true if timer callbacks were executed. |
Craig Tiller | ccdea19 | 2016-02-16 08:06:46 -0800 | [diff] [blame] | 93 | Drops drop_mu if it is non-null before executing callbacks. |
| 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 | |
David Garcia Quintas | 3598d44 | 2016-03-15 14:53:05 -0700 | [diff] [blame] | 108 | #endif /* GRPC_CORE_IOMGR_TIMER_H */ |