GRPC Core  0.11.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pollset_posix.h
Go to the documentation of this file.
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 <poll.h>
38 
39 #include <grpc/support/sync.h>
41 
43 
44 /* forward declare only in this file to avoid leaking impl details via
45  pollset.h; real users of grpc_fd should always include 'fd_posix.h' and not
46  use the struct tag */
47 struct grpc_fd;
48 
49 typedef struct grpc_pollset_worker {
54 
55 typedef struct grpc_pollset {
56  /* pollsets under posix can mutate representation as fds are added and
57  removed.
58  For example, we may choose a poll() based implementation on linux for
59  few fds, and an epoll() based implementation for many fds */
67  void (*shutdown_done_cb)(void *arg);
69  union {
70  int fd;
71  void *ptr;
72  } data;
73 } grpc_pollset;
74 
76  void (*add_fd)(grpc_pollset *pollset, struct grpc_fd *fd,
77  int and_unlock_pollset);
78  void (*del_fd)(grpc_pollset *pollset, struct grpc_fd *fd,
79  int and_unlock_pollset);
80  void (*maybe_work)(grpc_pollset *pollset, grpc_pollset_worker *worker,
81  gpr_timespec deadline, gpr_timespec now,
82  int allow_synchronous_callback);
83  void (*finish_shutdown)(grpc_pollset *pollset);
84  void (*destroy)(grpc_pollset *pollset);
85 };
86 
87 #define GRPC_POLLSET_MU(pollset) (&(pollset)->mu)
88 
89 /* Add an fd to a pollset */
90 void grpc_pollset_add_fd(grpc_pollset *pollset, struct grpc_fd *fd);
91 /* Force remove an fd from a pollset (normally they are removed on the next
92  poll after an fd is orphaned) */
93 void grpc_pollset_del_fd(grpc_pollset *pollset, struct grpc_fd *fd);
94 
95 /* Returns the fd to listen on for kicks */
97 /* Call after polling has been kicked to leave the kicked state */
99 
100 /* Convert a timespec to milliseconds:
101  - very small or negative poll times are clamped to zero to do a
102  non-blocking poll (which becomes spin polling)
103  - other small values are rounded up to one millisecond
104  - longer than a millisecond polls are rounded up to the next nearest
105  millisecond to avoid spinning
106  - infinite timeouts are converted to -1 */
108  gpr_timespec now);
109 
110 /* turn a pollset into a multipoller: platform specific */
112  struct grpc_fd **fds,
113  size_t fd_count);
115 
116 void grpc_poll_become_multipoller(grpc_pollset *pollset, struct grpc_fd **fds,
117  size_t fd_count);
118 
119 /* Return 1 if the pollset has active threads in grpc_pollset_work (pollset must
120  * be locked) */
122 
123 /* override to allow tests to hook poll() usage */
124 typedef int (*grpc_poll_function_type)(struct pollfd *, nfds_t, int);
126 
127 #endif /* GRPC_INTERNAL_CORE_IOMGR_POLLSET_POSIX_H */
grpc_platform_become_multipoller_type grpc_platform_become_multipoller
void(* destroy)(grpc_pollset *pollset)
Definition: pollset_posix.h:84
union grpc_pollset::@15 data
Definition: wakeup_fd_posix.h:83
grpc_poll_function_type grpc_poll_function
int fd
Definition: fd_posix.h:53
grpc_wakeup_fd wakeup_fd
Definition: pollset_posix.h:50
void * ptr
Definition: pollset_posix.h:71
void(* del_fd)(grpc_pollset *pollset, struct grpc_fd *fd, int and_unlock_pollset)
Definition: pollset_posix.h:78
void(* grpc_platform_become_multipoller_type)(grpc_pollset *pollset, struct grpc_fd **fds, size_t fd_count)
Definition: pollset_posix.h:111
struct grpc_pollset_worker grpc_pollset_worker
void * shutdown_done_arg
Definition: pollset_posix.h:68
int kicked_without_pollers
Definition: pollset_posix.h:66
Definition: pollset_posix.h:75
int fd
Definition: pollset_posix.h:70
gpr_mu mu
Definition: pollset_posix.h:61
grpc_pollset_worker root_worker
Definition: pollset_posix.h:62
struct grpc_pollset_worker * prev
Definition: pollset_posix.h:52
Definition: pollset_posix.h:55
struct grpc_pollset grpc_pollset
void(* finish_shutdown)(grpc_pollset *pollset)
Definition: pollset_posix.h:83
int grpc_pollset_has_workers(grpc_pollset *pollset)
Definition: sync_win32.h:39
int(* grpc_poll_function_type)(struct pollfd *, nfds_t, int)
Definition: pollset_posix.h:124
void(* maybe_work)(grpc_pollset *pollset, grpc_pollset_worker *worker, gpr_timespec deadline, gpr_timespec now, int allow_synchronous_callback)
Definition: pollset_posix.h:80
int grpc_kick_read_fd(grpc_pollset *p)
struct arg arg
void grpc_poll_become_multipoller(grpc_pollset *pollset, struct grpc_fd **fds, size_t fd_count)
void grpc_pollset_add_fd(grpc_pollset *pollset, struct grpc_fd *fd)
int shutting_down
Definition: pollset_posix.h:64
Definition: time.h:63
Definition: pollset_posix.h:49
void(* shutdown_done_cb)(void *arg)
Definition: pollset_posix.h:67
int grpc_poll_deadline_to_millis_timeout(gpr_timespec deadline, gpr_timespec now)
int in_flight_cbs
Definition: pollset_posix.h:63
struct grpc_pollset_worker * next
Definition: pollset_posix.h:51
void(* add_fd)(grpc_pollset *pollset, struct grpc_fd *fd, int and_unlock_pollset)
Definition: pollset_posix.h:76
void grpc_pollset_del_fd(grpc_pollset *pollset, struct grpc_fd *fd)
const grpc_pollset_vtable * vtable
Definition: pollset_posix.h:60
int called_shutdown
Definition: pollset_posix.h:65
void grpc_kick_drain(grpc_pollset *p)
Definition: fd_posix.h:52