blob: f62433707e73fd0ac24bf7fdd9b92ad0fd0d6eda [file] [log] [blame]
ctiller58393c22015-01-07 14:03:30 -08001/*
2 *
3 * Copyright 2014, Google Inc.
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
34#ifndef __GRPC_INTERNAL_IOMGR_POLLSET_POSIX_H_
35#define __GRPC_INTERNAL_IOMGR_POLLSET_POSIX_H_
36
37#include <grpc/support/sync.h>
38
David Klempner7f3ed1e2015-01-16 15:35:56 -080039#include "src/core/iomgr/pollset_kick.h"
40
ctiller58393c22015-01-07 14:03:30 -080041typedef struct grpc_pollset_vtable grpc_pollset_vtable;
42
43/* forward declare only in this file to avoid leaking impl details via
44 pollset.h; real users of grpc_fd should always include 'fd_posix.h' and not
45 use the struct tag */
46struct grpc_fd;
47
48typedef struct grpc_pollset {
49 /* pollsets under posix can mutate representation as fds are added and
50 removed.
51 For example, we may choose a poll() based implementation on linux for
52 few fds, and an epoll() based implementation for many fds */
53 const grpc_pollset_vtable *vtable;
54 gpr_mu mu;
55 gpr_cv cv;
David Klempner7f3ed1e2015-01-16 15:35:56 -080056 grpc_pollset_kick_state kick_state;
ctiller58393c22015-01-07 14:03:30 -080057 int counter;
58 union {
59 int fd;
60 void *ptr;
61 } data;
62} grpc_pollset;
63
64struct grpc_pollset_vtable {
65 void (*add_fd)(grpc_pollset *pollset, struct grpc_fd *fd);
66 void (*del_fd)(grpc_pollset *pollset, struct grpc_fd *fd);
67 int (*maybe_work)(grpc_pollset *pollset, gpr_timespec deadline,
68 gpr_timespec now, int allow_synchronous_callback);
69 void (*destroy)(grpc_pollset *pollset);
70};
71
72#define GRPC_POLLSET_MU(pollset) (&(pollset)->mu)
73#define GRPC_POLLSET_CV(pollset) (&(pollset)->cv)
74
75/* Add an fd to a pollset */
76void grpc_pollset_add_fd(grpc_pollset *pollset, struct grpc_fd *fd);
77/* Force remove an fd from a pollset (normally they are removed on the next
78 poll after an fd is orphaned) */
79void grpc_pollset_del_fd(grpc_pollset *pollset, struct grpc_fd *fd);
80
81/* Force any current pollers to break polling */
82void grpc_pollset_force_kick(grpc_pollset *pollset);
83/* Returns the fd to listen on for kicks */
84int grpc_kick_read_fd(grpc_pollset *p);
85/* Call after polling has been kicked to leave the kicked state */
86void grpc_kick_drain(grpc_pollset *p);
87
88/* All fds get added to a backup pollset to ensure that progress is made
89 regardless of applications listening to events. Relying on this is slow
90 however (the backup pollset only listens every 100ms or so) - so it's not
91 to be relied on. */
Craig Tiller32946d32015-01-15 11:37:30 -080092grpc_pollset *grpc_backup_pollset(void);
ctiller58393c22015-01-07 14:03:30 -080093
94/* turn a pollset into a multipoller: platform specific */
95void grpc_platform_become_multipoller(grpc_pollset *pollset,
96 struct grpc_fd **fds, size_t fd_count);
97
98#endif /* __GRPC_INTERNAL_IOMGR_POLLSET_POSIX_H_ */