ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
ctiller | d79b486 | 2014-12-17 16:36:59 -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_POLLSET_H |
| 35 | #define GRPC_CORE_LIB_IOMGR_POLLSET_H |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 36 | |
ctiller | 1a277ec | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 37 | #include <grpc/support/port_platform.h> |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 38 | #include <grpc/support/sync.h> |
Craig Tiller | 93d970a | 2015-01-09 13:35:04 -0800 | [diff] [blame] | 39 | #include <grpc/support/time.h> |
ctiller | 1a277ec | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 40 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 41 | #include "src/core/lib/iomgr/exec_ctx.h" |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 42 | |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 43 | #define GRPC_POLLSET_KICK_BROADCAST ((grpc_pollset_worker *)1) |
| 44 | |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 45 | /* A grpc_pollset is a set of file descriptors that a higher level item is |
| 46 | interested in. For example: |
| 47 | - a server will typically keep a pollset containing all connected channels, |
| 48 | so that it can find new calls to service |
| 49 | - a completion queue might keep a pollset with an entry for each transport |
| 50 | that is servicing a call that it's tracking */ |
ctiller | 1a277ec | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 51 | |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 52 | typedef struct grpc_pollset grpc_pollset; |
| 53 | typedef struct grpc_pollset_worker grpc_pollset_worker; |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 54 | |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 55 | size_t grpc_pollset_size(void); |
Craig Tiller | 6f41788 | 2017-02-16 14:09:39 -0800 | [diff] [blame] | 56 | /* Initialize a pollset: assumes *pollset contains all zeros */ |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 57 | void grpc_pollset_init(grpc_pollset *pollset, gpr_mu **mu); |
Craig Tiller | e8b5f62 | 2015-11-02 14:15:03 -0800 | [diff] [blame] | 58 | /* Begin shutting down the pollset, and call closure when done. |
Nicolas "Pixel" Noble | b9012fc | 2016-03-14 23:19:04 +0100 | [diff] [blame] | 59 | * pollset's mutex must be held */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 60 | void grpc_pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 61 | grpc_closure *closure); |
| 62 | void grpc_pollset_destroy(grpc_pollset *pollset); |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 63 | |
ctiller | 1a277ec | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 64 | /* Do some work on a pollset. |
| 65 | May involve invoking asynchronous callbacks, or actually polling file |
| 66 | descriptors. |
Nicolas "Pixel" Noble | b9012fc | 2016-03-14 23:19:04 +0100 | [diff] [blame] | 67 | Requires pollset's mutex locked. |
| 68 | May unlock its mutex during its execution. |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 69 | |
| 70 | worker is a (platform-specific) handle that can be used to wake up |
| 71 | from grpc_pollset_work before any events are received and before the timeout |
| 72 | has expired. It is both initialized and destroyed by grpc_pollset_work. |
| 73 | Initialization of worker is guaranteed to occur BEFORE the |
Nicolas "Pixel" Noble | b9012fc | 2016-03-14 23:19:04 +0100 | [diff] [blame] | 74 | pollset's mutex is released for the first time by grpc_pollset_work |
| 75 | and it is guaranteed that it will not be released by grpc_pollset_work |
| 76 | AFTER worker has been destroyed. |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 77 | |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 78 | Tries not to block past deadline. |
Craig Tiller | d9ccbbf | 2015-09-22 09:30:00 -0700 | [diff] [blame] | 79 | May call grpc_closure_list_run on grpc_closure_list, without holding the |
| 80 | pollset |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 81 | lock */ |
Craig Tiller | 4f1d0f3 | 2016-05-06 17:12:37 -0700 | [diff] [blame] | 82 | grpc_error *grpc_pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 83 | grpc_pollset_worker **worker, gpr_timespec now, |
| 84 | gpr_timespec deadline) GRPC_MUST_USE_RESULT; |
ctiller | 1a277ec | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 85 | |
Craig Tiller | 6174b9a | 2015-06-18 08:13:05 -0700 | [diff] [blame] | 86 | /* Break one polling thread out of polling work for this pollset. |
Craig Tiller | 5ddbb9d | 2015-07-29 15:58:11 -0700 | [diff] [blame] | 87 | If specific_worker is GRPC_POLLSET_KICK_BROADCAST, kick ALL the workers. |
| 88 | Otherwise, if specific_worker is non-NULL, then kick that worker. */ |
Craig Tiller | 4f1d0f3 | 2016-05-06 17:12:37 -0700 | [diff] [blame] | 89 | grpc_error *grpc_pollset_kick(grpc_pollset *pollset, |
| 90 | grpc_pollset_worker *specific_worker) |
| 91 | GRPC_MUST_USE_RESULT; |
ctiller | 1a277ec | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 92 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 93 | #endif /* GRPC_CORE_LIB_IOMGR_POLLSET_H */ |