ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [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 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 34 | #ifndef GRPC_INTERNAL_CORE_IOMGR_ALARM_H |
| 35 | #define GRPC_INTERNAL_CORE_IOMGR_ALARM_H |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 36 | |
| 37 | #include "src/core/iomgr/iomgr.h" |
| 38 | #include <grpc/support/port_platform.h> |
| 39 | #include <grpc/support/time.h> |
| 40 | |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 41 | typedef struct grpc_alarm { |
| 42 | gpr_timespec deadline; |
| 43 | gpr_uint32 heap_index; /* INVALID_HEAP_INDEX if not in heap */ |
Craig Tiller | cae5bf5 | 2015-07-02 08:46:59 -0700 | [diff] [blame] | 44 | int triggered; |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 45 | struct grpc_alarm *next; |
| 46 | struct grpc_alarm *prev; |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 47 | grpc_iomgr_cb_func cb; |
| 48 | void *cb_arg; |
| 49 | } grpc_alarm; |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 50 | |
| 51 | /* Initialize *alarm. When expired or canceled, alarm_cb will be called with |
| 52 | *alarm_cb_arg and status to indicate if it expired (SUCCESS) or was |
| 53 | canceled (CANCELLED). alarm_cb is guaranteed to be called exactly once, |
| 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. */ |
| 57 | void grpc_alarm_init(grpc_alarm *alarm, gpr_timespec deadline, |
| 58 | grpc_iomgr_cb_func alarm_cb, void *alarm_cb_arg, |
| 59 | gpr_timespec now); |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 60 | |
| 61 | /* Note that there is no alarm destroy function. This is because the |
| 62 | alarm is a one-time occurrence with a guarantee that the callback will |
| 63 | be called exactly once, either at expiration or cancellation. Thus, all |
| 64 | the internal alarm event management state is destroyed just before |
| 65 | that callback is invoked. If the user has additional state associated with |
| 66 | the alarm, the user is responsible for determining when it is safe to |
| 67 | destroy that state. */ |
| 68 | |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 69 | /* Cancel an *alarm. |
| 70 | There are three cases: |
| 71 | 1. We normally cancel the alarm |
| 72 | 2. The alarm has already run |
| 73 | 3. We can't cancel the alarm because it is "in flight". |
| 74 | |
| 75 | In all of these cases, the cancellation is still considered successful. |
| 76 | They are essentially distinguished in that the alarm_cb will be run |
| 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 |
| 81 | as grpc_alarm_cancel. It's expected that most alarms will be cancelled (their |
| 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 | |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 86 | Requires: cancel() must happen after add() on a given alarm */ |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 87 | void grpc_alarm_cancel(grpc_alarm *alarm); |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 88 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 89 | #endif /* GRPC_INTERNAL_CORE_IOMGR_ALARM_H */ |