gRPC  0.6.0
 All Classes Namespaces Functions Variables Enumerations Properties Pages
fd_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_FD_POSIX_H
35 #define GRPC_INTERNAL_CORE_IOMGR_FD_POSIX_H
36 
37 #include "src/core/iomgr/iomgr.h"
38 #include "src/core/iomgr/pollset.h"
39 #include <grpc/support/atm.h>
40 #include <grpc/support/sync.h>
41 #include <grpc/support/time.h>
42 
43 typedef struct {
44  grpc_iomgr_cb_func cb;
45  void *cb_arg;
47 
48 typedef struct grpc_fd grpc_fd;
49 
50 typedef struct grpc_fd_watcher {
51  struct grpc_fd_watcher *next;
52  struct grpc_fd_watcher *prev;
53  grpc_pollset *pollset;
54  grpc_fd *fd;
56 
57 struct grpc_fd {
58  int fd;
59  /* refst format:
60  bit0: 1=active/0=orphaned
61  bit1-n: refcount
62  meaning that mostly we ref by two to avoid altering the orphaned bit,
63  and just unref by 1 when we're ready to flag the object as orphaned */
64  gpr_atm refst;
65 
66  gpr_mu set_state_mu;
67  gpr_atm shutdown;
68 
69  gpr_mu watcher_mu;
70  grpc_fd_watcher watcher_root;
71 
72  gpr_atm readst;
73  gpr_atm writest;
74 
75  grpc_iomgr_cb_func on_done;
76  void *on_done_user_data;
77  struct grpc_fd *freelist_next;
78 };
79 
80 /* Create a wrapped file descriptor.
81  Requires fd is a non-blocking file descriptor.
82  This takes ownership of closing fd. */
83 grpc_fd *grpc_fd_create(int fd);
84 
85 /* Releases fd to be asynchronously destroyed.
86  on_done is called when the underlying file descriptor is definitely close()d.
87  If on_done is NULL, no callback will be made.
88  Requires: *fd initialized; no outstanding notify_on_read or
89  notify_on_write. */
90 void grpc_fd_orphan(grpc_fd *fd, grpc_iomgr_cb_func on_done, void *user_data);
91 
92 /* Begin polling on an fd.
93  Registers that the given pollset is interested in this fd - so that if read
94  or writability interest changes, the pollset can be kicked to pick up that
95  new interest.
96  Return value is:
97  (fd_needs_read? read_mask : 0) | (fd_needs_write? write_mask : 0)
98  i.e. a combination of read_mask and write_mask determined by the fd's current
99  interest in said events.
100  Polling strategies that do not need to alter their behavior depending on the
101  fd's current interest (such as epoll) do not need to call this function. */
102 gpr_uint32 grpc_fd_begin_poll(grpc_fd *fd, grpc_pollset *pollset,
103  gpr_uint32 read_mask, gpr_uint32 write_mask,
104  grpc_fd_watcher *rec);
105 /* Complete polling previously started with grpc_fd_begin_poll */
106 void grpc_fd_end_poll(grpc_fd_watcher *rec);
107 
108 /* Return 1 if this fd is orphaned, 0 otherwise */
109 int grpc_fd_is_orphaned(grpc_fd *fd);
110 
111 /* Cause any current callbacks to error out with GRPC_CALLBACK_CANCELLED. */
112 void grpc_fd_shutdown(grpc_fd *fd);
113 
114 /* Register read interest, causing read_cb to be called once when fd becomes
115  readable, on deadline specified by deadline, or on shutdown triggered by
116  grpc_fd_shutdown.
117  read_cb will be called with read_cb_arg when *fd becomes readable.
118  read_cb is Called with status of GRPC_CALLBACK_SUCCESS if readable,
119  GRPC_CALLBACK_TIMED_OUT if the call timed out,
120  and CANCELLED if the call was cancelled.
121 
122  Requires:This method must not be called before the read_cb for any previous
123  call runs. Edge triggered events are used whenever they are supported by the
124  underlying platform. This means that users must drain fd in read_cb before
125  calling notify_on_read again. Users are also expected to handle spurious
126  events, i.e read_cb is called while nothing can be readable from fd */
127 void grpc_fd_notify_on_read(grpc_fd *fd, grpc_iomgr_closure *closure);
128 
129 /* Exactly the same semantics as above, except based on writable events. */
130 void grpc_fd_notify_on_write(grpc_fd *fd, grpc_iomgr_closure *closure);
131 
132 /* Notification from the poller to an fd that it has become readable or
133  writable.
134  If allow_synchronous_callback is 1, allow running the fd callback inline
135  in this callstack, otherwise register an asynchronous callback and return */
136 void grpc_fd_become_readable(grpc_fd *fd, int allow_synchronous_callback);
137 void grpc_fd_become_writable(grpc_fd *fd, int allow_synchronous_callback);
138 
139 /* Reference counting for fds */
140 void grpc_fd_ref(grpc_fd *fd);
141 void grpc_fd_unref(grpc_fd *fd);
142 
143 void grpc_fd_global_init(void);
144 void grpc_fd_global_shutdown(void);
145 
146 #endif /* GRPC_INTERNAL_CORE_IOMGR_FD_POSIX_H */
Definition: pollset_posix.h:48
Definition: sync_win32.h:41
Definition: fd_posix.h:43
Definition: fd_posix.h:50
Definition: fd_posix.h:57