gRPC  0.6.0
 All Classes Namespaces Functions Variables Enumerations Properties Pages
pollset_posix.h
1 /*
2  *
3  * Copyright 2015, 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_CORE_IOMGR_POLLSET_POSIX_H
35 #define GRPC_INTERNAL_CORE_IOMGR_POLLSET_POSIX_H
36 
37 #include <grpc/support/sync.h>
38 
39 #include "src/core/iomgr/pollset_kick.h"
40 
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 */
46 struct grpc_fd;
47 
48 typedef 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;
56  grpc_pollset_kick_state kick_state;
57  int counter;
58  int in_flight_cbs;
59  int shutting_down;
60  void (*shutdown_done_cb)(void *arg);
61  void *shutdown_done_arg;
62  union {
63  int fd;
64  void *ptr;
65  } data;
66 } grpc_pollset;
67 
69  void (*add_fd)(grpc_pollset *pollset, struct grpc_fd *fd);
70  void (*del_fd)(grpc_pollset *pollset, struct grpc_fd *fd);
71  int (*maybe_work)(grpc_pollset *pollset, gpr_timespec deadline,
72  gpr_timespec now, int allow_synchronous_callback);
73  void (*kick)(grpc_pollset *pollset);
74  void (*destroy)(grpc_pollset *pollset);
75 };
76 
77 #define GRPC_POLLSET_MU(pollset) (&(pollset)->mu)
78 #define GRPC_POLLSET_CV(pollset) (&(pollset)->cv)
79 
80 /* Add an fd to a pollset */
81 void grpc_pollset_add_fd(grpc_pollset *pollset, struct grpc_fd *fd);
82 /* Force remove an fd from a pollset (normally they are removed on the next
83  poll after an fd is orphaned) */
84 void grpc_pollset_del_fd(grpc_pollset *pollset, struct grpc_fd *fd);
85 
86 /* Force any current pollers to break polling: it's the callers responsibility
87  to ensure that the pollset indeed needs to be kicked - no verification that
88  the pollset is actually performing polling work is done. At worst this will
89  result in spurious wakeups if performed at the wrong moment.
90  Does not touch pollset->mu. */
91 void grpc_pollset_force_kick(grpc_pollset *pollset);
92 /* Returns the fd to listen on for kicks */
93 int grpc_kick_read_fd(grpc_pollset *p);
94 /* Call after polling has been kicked to leave the kicked state */
95 void grpc_kick_drain(grpc_pollset *p);
96 
97 /* All fds get added to a backup pollset to ensure that progress is made
98  regardless of applications listening to events. Relying on this is slow
99  however (the backup pollset only listens every 100ms or so) - so it's not
100  to be relied on. */
101 grpc_pollset *grpc_backup_pollset(void);
102 
103 /* turn a pollset into a multipoller: platform specific */
104 void grpc_platform_become_multipoller(grpc_pollset *pollset,
105  struct grpc_fd **fds, size_t fd_count);
106 
107 #endif /* GRPC_INTERNAL_CORE_IOMGR_POLLSET_POSIX_H */
Definition: cmdline.c:46
Definition: pollset_posix.h:68
Definition: pollset_posix.h:48
Definition: sync_win32.h:41
Definition: pollset_kick_posix.h:45
Definition: time.h:48
Definition: fd_posix.h:57