Debian Codegraph @

include/ruby/internal/intern/select/win32.h

raw
1 #ifndef RBIMPL_INTERN_SELECT_WIN32_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RBIMPL_INTERN_SELECT_WIN32_H
3 /**
4 * @file
5 * @author Ruby developers <[email protected]>
6 * @copyright This file is a part of the programming language Ruby.
7 * Permission is hereby granted, to either redistribute and/or
8 * modify this file, provided that the conditions mentioned in the
9 * file COPYING are met. Consult the file for details.
10 * @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are
11 * implementation details. Don't take them as canon. They could
12 * rapidly appear then vanish. The name (path) of this header file
13 * is also an implementation detail. Do not expect it to persist
14 * at the place it is now. Developers are free to move it anywhere
15 * anytime at will.
16 * @note To ruby-core: remember that this header can be possibly
17 * recursively included from extension libraries written in C++.
18 * Do not expect for instance `__VA_ARGS__` is always available.
19 * We assume C99 for ruby itself but we don't assume languages of
20 * extension libraries. They could be written in C++98.
21 * @brief Public APIs to provide ::rb_fd_select().
22 */
23 #include "ruby/internal/dosish.h" /* for rb_w32_select */
24 #include "ruby/internal/attr/nonnull.h"
25 #include "ruby/internal/attr/pure.h"
26 #include "ruby/internal/attr/noalias.h"
27 #include "ruby/internal/dllexport.h"
28 #include "ruby/assert.h"
29
30 /**@cond INTERNAL_MACRO */
31 #define rb_fd_zero rb_fd_zero
32 #define rb_fd_clr rb_fd_clr
33 #define rb_fd_isset rb_fd_isset
34 #define rb_fd_copy rb_fd_copy
35 #define rb_fd_dup rb_fd_dup
36 #define rb_fd_ptr rb_fd_ptr
37 #define rb_fd_max rb_fd_max
38 /** @endcond */
39
40 RBIMPL_SYMBOL_EXPORT_BEGIN()
41
42 struct timeval;
43
44 /**
45 * The data structure which wraps the fd_set bitmap used by select(2). This
46 * allows Ruby to use FD sets larger than that allowed by historic limitations
47 * on modern platforms.
48 */
49 typedef struct {
50 int capa; /**< Maximum allowed number of FDs. */
51 fd_set *fdset; /**< File descriptors buffer. */
52 } rb_fdset_t;
53
54 RBIMPL_ATTR_NONNULL(())
55 /**
56 * (Re-)initialises a fdset. One must be initialised before other `rb_fd_*`
57 * operations. Analogous to calling `malloc(3)` to allocate an `fd_set`.
58 *
59 * @param[out] f An fdset to squash.
60 * @post `f` holds no file descriptors.
61 *
62 * @internal
63 *
64 * Can't this leak memory if the same `f` is passed twice...?
65 */
66 void rb_fd_init(rb_fdset_t *f);
67
68 RBIMPL_ATTR_NONNULL(())
69 /**
70 * Destroys the ::rb_fdset_t, releasing any memory and resources it used. It
71 * must be reinitialised using rb_fd_init() before future use. Analogous to
72 * calling `free(3)` to release memory for an `fd_set`.
73 *
74 * @param[out] f An fdset to squash.
75 * @post `f` holds no file descriptors.
76 */
77 void rb_fd_term(rb_fdset_t *f);
78
79 RBIMPL_ATTR_NONNULL(())
80 /**
81 * Sets an fd to a fdset.
82 *
83 * @param[in] fd A file descriptor.
84 * @param[out] f Target fdset.
85 * @post `f` holds `fd`.
86 */
87 void rb_fd_set(int fd, rb_fdset_t *f);
88
89 RBIMPL_ATTR_NONNULL(())
90 /**
91 * Destructively overwrites an fdset with another.
92 *
93 * @param[out] dst Target fdset.
94 * @param[in] src Source fdset.
95 * @param[in] max Maximum number of file descriptors to copy.
96 * @post `dst` is a copy of `src`.
97 */
98 void rb_w32_fd_copy(rb_fdset_t *dst, const fd_set *src, int max);
99
100 RBIMPL_ATTR_NONNULL(())
101 /**
102 * Identical to rb_w32_fd_copy(), except it copies unlimited number of file
103 * descriptors.
104 *
105 * @param[out] dst Target fdset.
106 * @param[in] src Source fdset.
107 * @post `dst` is a copy of `src`.
108 */
109 void rb_w32_fd_dup(rb_fdset_t *dst, const rb_fdset_t *src);
110
111 RBIMPL_SYMBOL_EXPORT_END()
112
113 RBIMPL_ATTR_NONNULL(())
114 RBIMPL_ATTR_NOALIAS()
115 /**
116 * Wipes out the current set of FDs.
117 *
118 * @param[out] f The fdset to clear.
119 * @post `f` has no FDs.
120 */
121 static inline void
122 rb_fd_zero(rb_fdset_t *f)
123 {
124 f->fdset->fd_count = 0;
125 }
126
127 RBIMPL_ATTR_NONNULL(())
128 /**
129 * Releases a specific FD from the given fdset.
130 *
131 * @param[in] n Target FD.
132 * @param[out] f The fdset that holds `n`.
133 * @post `f` doesn't hold n.
134 */
135 static inline void
136 rb_fd_clr(int n, rb_fdset_t *f)
137 {
138 rb_w32_fdclr(n, f->fdset);
139 }
140
141 RBIMPL_ATTR_NONNULL(())
142 /**
143 * Queries if the given FD is in the given set.
144 *
145 * @param[in] n Target FD.
146 * @param[in] f The fdset to scan.
147 * @retval 1 Yes there is.
148 * @retval 0 No there isn't.
149 */
150 static inline int
151 rb_fd_isset(int n, rb_fdset_t *f)
152 {
153 return rb_w32_fdisset(n, f->fdset);
154 }
155
156 RBIMPL_ATTR_NONNULL(())
157 /**
158 * Destructively overwrites an fdset with another.
159 *
160 * @param[out] dst Target fdset.
161 * @param[in] src Source fdset.
162 * @param[in] n Maximum number of file descriptors to copy.
163 * @post `dst` is a copy of `src`.
164 */
165 static inline void
166 rb_fd_copy(rb_fdset_t *dst, const fd_set *src, int n)
167 {
168 rb_w32_fd_copy(dst, src, n);
169 }
170
171 RBIMPL_ATTR_NONNULL(())
172 /**
173 * Identical to rb_fd_copy(), except it copies unlimited number of file
174 * descriptors.
175 *
176 * @param[out] dst Target fdset.
177 * @param[in] src Source fdset.
178 * @post `dst` is a copy of `src`.
179 */
180 static inline void
181 rb_fd_dup(rb_fdset_t *dst, const rb_fdset_t *src)
182 {
183 rb_w32_fd_dup(dst, src);
184 }
185
186 /**
187 * Waits for multiple file descriptors at once.
188 *
189 * @param[in] n Max FD in everything passed, plus one.
190 * @param[in,out] rfds Set of FDs to wait for reads.
191 * @param[in,out] wfds Set of FDs to wait for writes.
192 * @param[in,out] efds Set of FDs to wait for OOBs.
193 * @param[in,out] timeout Max blocking duration.
194 * @retval -1 Failed, errno set.
195 * @retval 0 Timeout exceeded.
196 * @retval otherwise Total number of file descriptors returned.
197 * @post `rfds` contains readable FDs.
198 * @post `wfds` contains writable FDs.
199 * @post `efds` contains exceptional FDs.
200 * @post `timeout` is the time left.
201 * @note All pointers are allowed to be null pointers.
202 *
203 * @internal
204 *
205 * This can wait for `SOCKET` and `HANDLE` at once. In order to achieve that
206 * property we heavily touch the internals of MSVCRT. We `CreateFile` a
207 * `"NUL"` alongside of a socket and directly manipulate its `struct ioinfo`.
208 * This is of course a very dirty hack. If we could design the API today we
209 * could use `CancellIoEx`. But we are older than that Win32 API.
210 */
211 static inline int
212 rb_fd_select(int n, rb_fdset_t *rfds, rb_fdset_t *wfds, rb_fdset_t *efds, struct timeval *timeout)
213 {
214 return rb_w32_select(
215 n,
216 rfds ? rfds->fdset : NULL,
217 wfds ? wfds->fdset : NULL,
218 efds ? efds->fdset : NULL,
219 timeout);
220 }
221
222 RBIMPL_ATTR_NONNULL(())
223 RBIMPL_ATTR_PURE()
224 /**
225 * Raw pointer to `fd_set`.
226 *
227 * @param[in] f Target fdset.
228 * @retval NULL `f` is already terminated by rb_fd_term().
229 * @retval otherwise Underlying fd_set.
230 *
231 * @internal
232 *
233 * Extension library must not touch raw pointers. It was a bad idea to let
234 * them use it.
235 */
236 static inline fd_set *
237 rb_fd_ptr(const rb_fdset_t *f)
238 {
239 return f->fdset;
240 }
241
242 RBIMPL_ATTR_NONNULL(())
243 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
244 /**
245 * It seems this function has no use. Maybe just remove?
246 *
247 * @param[in] f A set.
248 * @return Number of file descriptors stored.
249 */
250 static inline int
251 rb_fd_max(const rb_fdset_t *f)
252 {
253 const fd_set *p = f->fdset;
254
255 RBIMPL_ASSERT_OR_ASSUME(p);
256 return p->fd_count;
257 }
258
259 #endif /* RBIMPL_INTERN_SELECT_WIN32_H */