blob: db13f8ac69c977e886cc297de0685bc6a5d8bb74 [file] [log] [blame]
ctillerd79b4862014-12-17 16:36:59 -08001/*
2 *
Craig Tillerc605c622016-02-19 16:24:26 -08003 * Copyright 2015-2016, 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 Tiller69b093b2016-02-25 19:04:07 -080041#include "src/core/iomgr/exec_ctx.h"
42
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);
56void grpc_pollset_init(grpc_pollset *pollset, gpr_mu **mu);
Craig Tillere8b5f622015-11-02 14:15:03 -080057/* Begin shutting down the pollset, and call closure when done.
Nicolas "Pixel" Nobleb9012fc2016-03-14 23:19:04 +010058 * pollset's mutex must be held */
Craig Tillera82950e2015-09-22 12:33:20 -070059void grpc_pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
60 grpc_closure *closure);
Craig Tillere8b5f622015-11-02 14:15:03 -080061/** Reset the pollset to its initial state (perhaps with some cached objects);
62 * must have been previously shutdown */
63void grpc_pollset_reset(grpc_pollset *pollset);
Craig Tillera82950e2015-09-22 12:33:20 -070064void grpc_pollset_destroy(grpc_pollset *pollset);
ctillerd79b4862014-12-17 16:36:59 -080065
ctiller1a277ec2015-01-07 14:03:30 -080066/* Do some work on a pollset.
67 May involve invoking asynchronous callbacks, or actually polling file
68 descriptors.
Nicolas "Pixel" Nobleb9012fc2016-03-14 23:19:04 +010069 Requires pollset's mutex locked.
70 May unlock its mutex during its execution.
Craig Tiller5ddbb9d2015-07-29 15:58:11 -070071
72 worker is a (platform-specific) handle that can be used to wake up
73 from grpc_pollset_work before any events are received and before the timeout
74 has expired. It is both initialized and destroyed by grpc_pollset_work.
75 Initialization of worker is guaranteed to occur BEFORE the
Nicolas "Pixel" Nobleb9012fc2016-03-14 23:19:04 +010076 pollset's mutex is released for the first time by grpc_pollset_work
77 and it is guaranteed that it will not be released by grpc_pollset_work
78 AFTER worker has been destroyed.
Craig Tiller5ddbb9d2015-07-29 15:58:11 -070079
Craig Tillerdfff1b82015-09-21 14:39:57 -070080 Tries not to block past deadline.
Craig Tillerd9ccbbf2015-09-22 09:30:00 -070081 May call grpc_closure_list_run on grpc_closure_list, without holding the
82 pollset
Craig Tillerdfff1b82015-09-21 14:39:57 -070083 lock */
Craig Tillera82950e2015-09-22 12:33:20 -070084void grpc_pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
Craig Tillerd0a8ae12016-02-18 08:01:19 -080085 grpc_pollset_worker **worker, gpr_timespec now,
Craig Tillera82950e2015-09-22 12:33:20 -070086 gpr_timespec deadline);
ctiller1a277ec2015-01-07 14:03:30 -080087
Craig Tiller6174b9a2015-06-18 08:13:05 -070088/* Break one polling thread out of polling work for this pollset.
Craig Tiller5ddbb9d2015-07-29 15:58:11 -070089 If specific_worker is GRPC_POLLSET_KICK_BROADCAST, kick ALL the workers.
90 Otherwise, if specific_worker is non-NULL, then kick that worker. */
Craig Tillera82950e2015-09-22 12:33:20 -070091void grpc_pollset_kick(grpc_pollset *pollset,
92 grpc_pollset_worker *specific_worker);
ctiller1a277ec2015-01-07 14:03:30 -080093
Craig Tiller9a4dddd2016-03-25 17:08:13 -070094#endif /* GRPC_CORE_LIB_IOMGR_POLLSET_H */