Debian Codegraph @

srtcore/epoll.h

raw
1 /*
2 * SRT - Secure, Reliable, Transport
3 * Copyright (c) 2018 Haivision Systems Inc.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 */
10
11 /*****************************************************************************
12 Copyright (c) 2001 - 2010, The Board of Trustees of the University of Illinois.
13 All rights reserved.
14
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are
17 met:
18
19 * Redistributions of source code must retain the above
20 copyright notice, this list of conditions and the
21 following disclaimer.
22
23 * Redistributions in binary form must reproduce the
24 above copyright notice, this list of conditions
25 and the following disclaimer in the documentation
26 and/or other materials provided with the distribution.
27
28 * Neither the name of the University of Illinois
29 nor the names of its contributors may be used to
30 endorse or promote products derived from this
31 software without specific prior written permission.
32
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
34 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
35 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
36 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
37 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
38 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
39 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
40 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 *****************************************************************************/
45
46 /*****************************************************************************
47 written by
48 Yunhong Gu, last updated 08/20/2010
49 modified by
50 Haivision Systems Inc.
51 *****************************************************************************/
52
53 #ifndef INC_SRT_EPOLL_H
54 #define INC_SRT_EPOLL_H
55
56
57 #include <map>
58 #include <set>
59 #include <list>
60 #include "udt.h"
61
62 namespace srt
63 {
64
65 class CUDT;
66 class CRendezvousQueue;
67 class CUDTGroup;
68
69
70 class CEPollDesc
71 {
72 #ifdef __GNUG__
73 const int m_iID; // epoll ID
74 #else
75 const int m_iID SRT_ATR_UNUSED; // epoll ID
76 #endif
77 struct Wait;
78
79 struct Notice: public SRT_EPOLL_EVENT
80 {
81 Wait* parent;
82
83 Notice(Wait* p, SRTSOCKET sock, int ev): parent(p)
84 {
85 fd = sock;
86 events = ev;
87 }
88 };
89
90 /// The type for `m_USockEventNotice`, the pair contains:
91 /// * The back-pointer to the subscriber object for which this event notice serves
92 /// * The events currently being on
93 typedef std::list<Notice> enotice_t;
94
95 struct Wait
96 {
97 /// Events the subscriber is interested with. Only those will be
98 /// regarded when updating event flags.
99 int32_t watch;
100
101 /// Which events should be edge-triggered. When the event isn't
102 /// mentioned in `watch`, this bit flag is disregarded. Otherwise
103 /// it means that the event is to be waited for persistent state
104 /// if this flag is not present here, and for edge trigger, if
105 /// the flag is present here.
106 int32_t edge;
107
108 /// The current persistent state. This is usually duplicated in
109 /// a dedicated state object in `m_USockEventNotice`, however the state
110 /// here will stay forever as is, regardless of the edge/persistent
111 /// subscription mode for the event.
112 int32_t state;
113
114 /// The iterator to `m_USockEventNotice` container that contains the
115 /// event notice object for this subscription, or the value from
116 /// `nullNotice()` if there is no such object.
117 enotice_t::iterator notit;
118
119 Wait(explicit_t<int32_t> sub, explicit_t<int32_t> etr, enotice_t::iterator i)
120 :watch(sub)
121 ,edge(etr)
122 ,state(0)
123 ,notit(i)
124 {
125 }
126
127 int edgeOnly() { return edge & watch; }
128
129 /// Clear all flags for given direction from the notices
130 /// and subscriptions, and checks if this made the event list
131 /// for this watch completely empty.
132 /// @param direction event type that has to be cleared
133 /// @return true, if this cleared the last event (the caller
134 /// want to remove the subscription for this socket)
135 bool clear(int32_t direction)
136 {
137 if (watch & direction)
138 {
139 watch &= ~direction;
140 edge &= ~direction;
141 state &= ~direction;
142
143 return watch == 0;
144 }
145
146 return false;
147 }
148 };
149
150 typedef std::map<SRTSOCKET, Wait> ewatch_t;
151
152 #if ENABLE_HEAVY_LOGGING
153 std::string DisplayEpollWatch();
154 #endif
155
156 /// Sockets that are subscribed for events in this eid.
157 ewatch_t m_USockWatchState;
158
159 /// Objects representing changes in SRT sockets.
160 /// Objects are removed from here when an event is registered as edge-triggered.
161 /// Otherwise it is removed only when all events as per subscription
162 /// are no longer on.
163 enotice_t m_USockEventNotice;
164
165 // Special behavior
166 int32_t m_Flags;
167
168 enotice_t::iterator nullNotice() { return m_USockEventNotice.end(); }
169
170 // Only CEPoll class should have access to it.
171 // Guarding private access to the class is not necessary
172 // within the epoll module.
173 friend class CEPoll;
174
175 CEPollDesc(int id, int localID)
176 : m_iID(id)
177 , m_Flags(0)
178 , m_iLocalID(localID)
179 {
180 }
181
182 static const int32_t EF_NOCHECK_EMPTY = 1 << 0;
183 static const int32_t EF_CHECK_REP = 1 << 1;
184
185 int32_t flags() const { return m_Flags; }
186 bool flags(int32_t f) const { return (m_Flags & f) != 0; }
187 void set_flags(int32_t flg) { m_Flags |= flg; }
188 void clr_flags(int32_t flg) { m_Flags &= ~flg; }
189
190 // Container accessors for ewatch_t.
191 bool watch_empty() const { return m_USockWatchState.empty(); }
192 Wait* watch_find(SRTSOCKET sock)
193 {
194 ewatch_t::iterator i = m_USockWatchState.find(sock);
195 if (i == m_USockWatchState.end())
196 return NULL;
197 return &i->second;
198 }
199
200 // Container accessors for enotice_t.
201 enotice_t::iterator enotice_begin() { return m_USockEventNotice.begin(); }
202 enotice_t::iterator enotice_end() { return m_USockEventNotice.end(); }
203 enotice_t::const_iterator enotice_begin() const { return m_USockEventNotice.begin(); }
204 enotice_t::const_iterator enotice_end() const { return m_USockEventNotice.end(); }
205 bool enotice_empty() const { return m_USockEventNotice.empty(); }
206
207 const int m_iLocalID; // local system epoll ID
208 std::set<SYSSOCKET> m_sLocals; // set of local (non-UDT) descriptors
209
210 std::pair<ewatch_t::iterator, bool> addWatch(SRTSOCKET sock, explicit_t<int32_t> events, explicit_t<int32_t> et_events)
211 {
212 return m_USockWatchState.insert(std::make_pair(sock, Wait(events, et_events, nullNotice())));
213 }
214
215 void addEventNotice(Wait& wait, SRTSOCKET sock, int events)
216 {
217 // `events` contains bits to be set, so:
218 //
219 // 1. If no notice object exists, add it exactly with `events`.
220 // 2. If it exists, only set the bits from `events`.
221 // ASSUME: 'events' is not 0, that is, we have some readiness
222
223 if (wait.notit == nullNotice()) // No notice object
224 {
225 // Add new event notice and bind to the wait object.
226 m_USockEventNotice.push_back(Notice(&wait, sock, events));
227 wait.notit = --m_USockEventNotice.end();
228
229 return;
230 }
231
232 // We have an existing event notice, so update it
233 wait.notit->events |= events;
234 }
235
236 // This function only updates the corresponding event notice object
237 // according to the change in the events.
238 void updateEventNotice(Wait& wait, SRTSOCKET sock, int events, bool enable)
239 {
240 if (enable)
241 {
242 addEventNotice(wait, sock, events);
243 }
244 else
245 {
246 removeExcessEvents(wait, ~events);
247 }
248 }
249
250 void removeSubscription(SRTSOCKET u)
251 {
252 std::map<SRTSOCKET, Wait>::iterator i = m_USockWatchState.find(u);
253 if (i == m_USockWatchState.end())
254 return;
255
256 if (i->second.notit != nullNotice())
257 {
258 m_USockEventNotice.erase(i->second.notit);
259 // NOTE: no need to update the Wait::notit field
260 // because the Wait object is about to be removed anyway.
261 }
262 m_USockWatchState.erase(i);
263 }
264
265 void clearAll()
266 {
267 m_USockEventNotice.clear();
268 m_USockWatchState.clear();
269 }
270
271 void removeExistingNotices(Wait& wait)
272 {
273 m_USockEventNotice.erase(wait.notit);
274 wait.notit = nullNotice();
275 }
276
277 void removeEvents(Wait& wait)
278 {
279 if (wait.notit == nullNotice())
280 return;
281 removeExistingNotices(wait);
282 }
283
284 // This function removes notices referring to
285 // events that are NOT present in @a nevts, but
286 // may be among subscriptions and therefore potentially
287 // have an associated notice.
288 void removeExcessEvents(Wait& wait, int nevts)
289 {
290 // Update the event notice, should it exist
291 // If the watch points to a null notice, there's simply
292 // no notice there, so nothing to update or prospectively
293 // remove - but may be something to add.
294 if (wait.notit == nullNotice())
295 return;
296
297 // `events` contains bits to be cleared.
298 // 1. If there is no notice event, do nothing - clear already.
299 // 2. If there is a notice event, update by clearing the bits
300 // 2.1. If this made resulting state to be 0, also remove the notice.
301
302 const int newstate = wait.notit->events & nevts;
303 if (newstate)
304 {
305 wait.notit->events = newstate;
306 }
307 else
308 {
309 // If the new state is full 0 (no events),
310 // then remove the corresponding notice object
311 removeExistingNotices(wait);
312 }
313 }
314
315 bool checkEdge(enotice_t::iterator i)
316 {
317 // This function should check if this event was subscribed
318 // as edge-triggered, and if so, clear the event from the notice.
319 // Update events and check edge mode at the subscriber
320 i->events &= ~i->parent->edgeOnly();
321 if(!i->events)
322 {
323 removeExistingNotices(*i->parent);
324 return true;
325 }
326 return false;
327 }
328
329 /// This should work in a loop around the notice container of
330 /// the given eid container and clear out the notice for
331 /// particular event type. If this has cleared effectively the
332 /// last existing event, it should return the socket id
333 /// so that the caller knows to remove it also from subscribers.
334 ///
335 /// @param i iterator in the notice container
336 /// @param event event type to be cleared
337 /// @retval (socket) Socket to be removed from subscriptions
338 /// @retval SRT_INVALID_SOCK Nothing to be done (associated socket
339 /// still has other subscriptions)
340 SRTSOCKET clearEventSub(enotice_t::iterator i, int event)
341 {
342 // We need to remove the notice and subscription
343 // for this event. The 'i' iterator is safe to
344 // delete, even indirectly.
345
346 // This works merely like checkEdge, just on request to clear the
347 // identified event, if found.
348 if (i->events & event)
349 {
350 // The notice has a readiness flag on this event.
351 // This means that there exists also a subscription.
352 Wait* w = i->parent;
353 if (w->clear(event))
354 return i->fd;
355 }
356
357 return SRT_INVALID_SOCK;
358 }
359 };
360
361 class CEPoll
362 {
363 friend class srt::CUDT;
364 friend class srt::CUDTGroup;
365 friend class srt::CRendezvousQueue;
366
367 public:
368 CEPoll();
369 ~CEPoll();
370
371 public: // for CUDTUnited API
372
373 /// create a new EPoll.
374 /// @return new EPoll ID if success, otherwise an error number.
375 int create(CEPollDesc** ppd = 0);
376
377
378 /// delete all user sockets (SRT sockets) from an EPoll
379 /// @param [in] eid EPoll ID.
380 /// @return 0
381 int clear_usocks(int eid);
382
383 /// add a system socket to an EPoll.
384 /// @param [in] eid EPoll ID.
385 /// @param [in] s system Socket ID.
386 /// @param [in] events events to watch.
387 /// @return 0 if success, otherwise an error number.
388
389 int add_ssock(const int eid, const SYSSOCKET& s, const int* events = NULL);
390
391 /// remove a system socket event from an EPoll; socket will be removed if no events to watch.
392 /// @param [in] eid EPoll ID.
393 /// @param [in] s system socket ID.
394 /// @return 0 if success, otherwise an error number.
395
396 int remove_ssock(const int eid, const SYSSOCKET& s);
397 /// update a UDT socket events from an EPoll.
398 /// @param [in] eid EPoll ID.
399 /// @param [in] u UDT socket ID.
400 /// @param [in] events events to watch.
401 /// @return 0 if success, otherwise an error number.
402
403 int update_usock(const int eid, const SRTSOCKET& u, const int* events);
404
405 /// update a system socket events from an EPoll.
406 /// @param [in] eid EPoll ID.
407 /// @param [in] u UDT socket ID.
408 /// @param [in] events events to watch.
409 /// @return 0 if success, otherwise an error number.
410
411 int update_ssock(const int eid, const SYSSOCKET& s, const int* events = NULL);
412
413 /// wait for EPoll events or timeout.
414 /// @param [in] eid EPoll ID.
415 /// @param [out] readfds UDT sockets available for reading.
416 /// @param [out] writefds UDT sockets available for writing.
417 /// @param [in] msTimeOut timeout threshold, in milliseconds.
418 /// @param [out] lrfds system file descriptors for reading.
419 /// @param [out] lwfds system file descriptors for writing.
420 /// @return number of sockets available for IO.
421
422 int wait(const int eid, std::set<SRTSOCKET>* readfds, std::set<SRTSOCKET>* writefds, int64_t msTimeOut, std::set<SYSSOCKET>* lrfds, std::set<SYSSOCKET>* lwfds);
423
424 typedef std::map<SRTSOCKET, int> fmap_t;
425
426 /// Lightweit and more internal-reaching version of `uwait` for internal use only.
427 /// This function wait for sockets to be ready and reports them in `st` map.
428 ///
429 /// @param d the internal structure of the epoll container
430 /// @param st output container for the results: { socket_type, event }
431 /// @param msTimeOut timeout after which return with empty output is allowed
432 /// @param report_by_exception if true, errors will result in exception instead of returning -1
433 /// @retval -1 error occurred
434 /// @retval >=0 number of ready sockets (actually size of `st`)
435 int swait(CEPollDesc& d, fmap_t& st, int64_t msTimeOut, bool report_by_exception = true);
436
437 /// Empty subscription check - for internal use only.
438 bool empty(const CEPollDesc& d) const;
439
440 /// Reports which events are ready on the given socket.
441 /// @param mp socket event map returned by `swait`
442 /// @param sock which socket to ask
443 /// @return event flags for given socket, or 0 if none
444 static int ready(const fmap_t& mp, SRTSOCKET sock)
445 {
446 fmap_t::const_iterator y = mp.find(sock);
447 if (y == mp.end())
448 return 0;
449 return y->second;
450 }
451
452 /// Reports whether socket is ready for given event.
453 /// @param mp socket event map returned by `swait`
454 /// @param sock which socket to ask
455 /// @param event which events it should be ready for
456 /// @return true if the given socket is ready for given event
457 static bool isready(const fmap_t& mp, SRTSOCKET sock, SRT_EPOLL_OPT event)
458 {
459 return (ready(mp, sock) & event) != 0;
460 }
461
462 // Could be a template directly, but it's now hidden in the imp file.
463 void clear_ready_usocks(CEPollDesc& d, int direction);
464
465 /// wait for EPoll events or timeout optimized with explicit EPOLL_ERR event and the edge mode option.
466 /// @param [in] eid EPoll ID.
467 /// @param [out] fdsSet array of user socket events (SRT_EPOLL_IN | SRT_EPOLL_OUT | SRT_EPOLL_ERR).
468 /// @param [int] fdsSize of fds array
469 /// @param [in] msTimeOut timeout threshold, in milliseconds.
470 /// @return total of available events in the epoll system (can be greater than fdsSize)
471
472 int uwait(const int eid, SRT_EPOLL_EVENT* fdsSet, int fdsSize, int64_t msTimeOut);
473
474 /// close and release an EPoll.
475 /// @param [in] eid EPoll ID.
476 /// @return 0 if success, otherwise an error number.
477
478 int release(const int eid);
479
480 public: // for CUDT to acknowledge IO status
481
482 /// Update events available for a UDT socket. At the end this function
483 /// counts the number of updated EIDs with given events.
484 /// @param [in] uid UDT socket ID.
485 /// @param [in] eids EPoll IDs to be set
486 /// @param [in] events Combination of events to update
487 /// @param [in] enable true -> enable, otherwise disable
488 /// @return -1 if invalid events, otherwise the number of changes
489
490 int update_events(const SRTSOCKET& uid, std::set<int>& eids, int events, bool enable);
491
492 int setflags(const int eid, int32_t flags);
493
494 private:
495 int m_iIDSeed; // seed to generate a new ID
496 srt::sync::Mutex m_SeedLock;
497
498 std::map<int, CEPollDesc> m_mPolls; // all epolls
499 mutable srt::sync::Mutex m_EPollLock;
500 };
501
502 #if ENABLE_HEAVY_LOGGING
503 std::string DisplayEpollResults(const std::map<SRTSOCKET, int>& sockset);
504 #endif
505
506 } // namespace srt
507
508
509 #endif