blob: e19ce697b8d3edba687fe404195dff68c9ee5692 [file] [log] [blame]
ctillerd79b4862014-12-17 16:36:59 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
ctillerd79b4862014-12-17 16:36:59 -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_POLLSET_H
35#define GRPC_CORE_LIB_IOMGR_POLLSET_H
ctillerd79b4862014-12-17 16:36:59 -080036
ctiller1a277ec2015-01-07 14:03:30 -080037#include <grpc/support/port_platform.h>
Craig Tiller69b093b2016-02-25 19:04:07 -080038#include <grpc/support/sync.h>
Craig Tiller93d970a2015-01-09 13:35:04 -080039#include <grpc/support/time.h>
ctiller1a277ec2015-01-07 14:03:30 -080040
Craig Tiller9533d042016-03-25 17:11:06 -070041#include "src/core/lib/iomgr/exec_ctx.h"
Craig Tiller69b093b2016-02-25 19:04:07 -080042
Craig Tiller5ddbb9d2015-07-29 15:58:11 -070043#define GRPC_POLLSET_KICK_BROADCAST ((grpc_pollset_worker *)1)
44
ctillerd79b4862014-12-17 16:36:59 -080045/* 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 */
ctiller1a277ec2015-01-07 14:03:30 -080051
Craig Tiller69b093b2016-02-25 19:04:07 -080052typedef struct grpc_pollset grpc_pollset;
53typedef struct grpc_pollset_worker grpc_pollset_worker;
ctillerd79b4862014-12-17 16:36:59 -080054
Craig Tiller69b093b2016-02-25 19:04:07 -080055size_t grpc_pollset_size(void);
Craig Tiller6f417882017-02-16 14:09:39 -080056/* Initialize a pollset: assumes *pollset contains all zeros */
Craig Tiller69b093b2016-02-25 19:04:07 -080057void grpc_pollset_init(grpc_pollset *pollset, gpr_mu **mu);
Craig Tillere8b5f622015-11-02 14:15:03 -080058/* Begin shutting down the pollset, and call closure when done.
Nicolas "Pixel" Nobleb9012fc2016-03-14 23:19:04 +010059 * pollset's mutex must be held */
Craig Tillera82950e2015-09-22 12:33:20 -070060void grpc_pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
61 grpc_closure *closure);
62void grpc_pollset_destroy(grpc_pollset *pollset);
ctillerd79b4862014-12-17 16:36:59 -080063
ctiller1a277ec2015-01-07 14:03:30 -080064/* Do some work on a pollset.
65 May involve invoking asynchronous callbacks, or actually polling file
66 descriptors.
Nicolas "Pixel" Nobleb9012fc2016-03-14 23:19:04 +010067 Requires pollset's mutex locked.
68 May unlock its mutex during its execution.
Craig Tiller5ddbb9d2015-07-29 15:58:11 -070069
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" Nobleb9012fc2016-03-14 23:19:04 +010074 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 Tiller5ddbb9d2015-07-29 15:58:11 -070077
Craig Tillerdfff1b82015-09-21 14:39:57 -070078 Tries not to block past deadline.
Craig Tillerd9ccbbf2015-09-22 09:30:00 -070079 May call grpc_closure_list_run on grpc_closure_list, without holding the
80 pollset
Craig Tillerdfff1b82015-09-21 14:39:57 -070081 lock */
Craig Tiller4f1d0f32016-05-06 17:12:37 -070082grpc_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;
ctiller1a277ec2015-01-07 14:03:30 -080085
Craig Tiller6174b9a2015-06-18 08:13:05 -070086/* Break one polling thread out of polling work for this pollset.
Craig Tiller5ddbb9d2015-07-29 15:58:11 -070087 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 Tiller4f1d0f32016-05-06 17:12:37 -070089grpc_error *grpc_pollset_kick(grpc_pollset *pollset,
90 grpc_pollset_worker *specific_worker)
91 GRPC_MUST_USE_RESULT;
ctiller1a277ec2015-01-07 14:03:30 -080092
Craig Tiller9a4dddd2016-03-25 17:08:13 -070093#endif /* GRPC_CORE_LIB_IOMGR_POLLSET_H */