Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1206)

Side by Side Diff: webrtc/base/physicalsocketserver.h

Issue 2880923002: Support epoll in PhysicalSocketServer. (Closed)
Patch Set: Defer adding/removing of dispatchers while processing events. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | webrtc/base/physicalsocketserver.cc » ('j') | webrtc/base/physicalsocketserver.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #ifndef WEBRTC_BASE_PHYSICALSOCKETSERVER_H__ 11 #ifndef WEBRTC_BASE_PHYSICALSOCKETSERVER_H__
12 #define WEBRTC_BASE_PHYSICALSOCKETSERVER_H__ 12 #define WEBRTC_BASE_PHYSICALSOCKETSERVER_H__
13 13
14 #if defined(WEBRTC_POSIX) && defined(WEBRTC_LINUX)
15 #include <sys/epoll.h>
16 #define WEBRTC_USE_EPOLL 1
17 #endif
18
14 #include <memory> 19 #include <memory>
20 #include <set>
15 #include <vector> 21 #include <vector>
16 22
17 #include "webrtc/base/nethelpers.h" 23 #include "webrtc/base/nethelpers.h"
18 #include "webrtc/base/socketserver.h" 24 #include "webrtc/base/socketserver.h"
19 #include "webrtc/base/criticalsection.h" 25 #include "webrtc/base/criticalsection.h"
20 26
21 #if defined(WEBRTC_POSIX) 27 #if defined(WEBRTC_POSIX)
22 typedef int SOCKET; 28 typedef int SOCKET;
23 #endif // WEBRTC_POSIX 29 #endif // WEBRTC_POSIX
24 30
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 75
70 // Internal Factory for Accept (virtual so it can be overwritten in tests). 76 // Internal Factory for Accept (virtual so it can be overwritten in tests).
71 virtual AsyncSocket* WrapSocket(SOCKET s); 77 virtual AsyncSocket* WrapSocket(SOCKET s);
72 78
73 // SocketServer: 79 // SocketServer:
74 bool Wait(int cms, bool process_io) override; 80 bool Wait(int cms, bool process_io) override;
75 void WakeUp() override; 81 void WakeUp() override;
76 82
77 void Add(Dispatcher* dispatcher); 83 void Add(Dispatcher* dispatcher);
78 void Remove(Dispatcher* dispatcher); 84 void Remove(Dispatcher* dispatcher);
85 void Update(Dispatcher* dispatcher);
79 86
80 #if defined(WEBRTC_POSIX) 87 #if defined(WEBRTC_POSIX)
81 // Sets the function to be executed in response to the specified POSIX signal. 88 // Sets the function to be executed in response to the specified POSIX signal.
82 // The function is executed from inside Wait() using the "self-pipe trick"-- 89 // The function is executed from inside Wait() using the "self-pipe trick"--
83 // regardless of which thread receives the signal--and hence can safely 90 // regardless of which thread receives the signal--and hence can safely
84 // manipulate user-level data structures. 91 // manipulate user-level data structures.
85 // "handler" may be SIG_IGN, SIG_DFL, or a user-specified function, just like 92 // "handler" may be SIG_IGN, SIG_DFL, or a user-specified function, just like
86 // with signal(2). 93 // with signal(2).
87 // Only one PhysicalSocketServer should have user-level signal handlers. 94 // Only one PhysicalSocketServer should have user-level signal handlers.
88 // Dispatching signals on multiple PhysicalSocketServers is not reliable. 95 // Dispatching signals on multiple PhysicalSocketServers is not reliable.
89 // The signal mask is not modified. It is the caller's responsibily to 96 // The signal mask is not modified. It is the caller's responsibily to
90 // maintain it as desired. 97 // maintain it as desired.
91 virtual bool SetPosixSignalHandler(int signum, void (*handler)(int)); 98 virtual bool SetPosixSignalHandler(int signum, void (*handler)(int));
92 99
93 protected: 100 protected:
94 Dispatcher* signal_dispatcher(); 101 Dispatcher* signal_dispatcher();
95 #endif 102 #endif
96 103
97 private: 104 private:
98 typedef std::vector<Dispatcher*> DispatcherList; 105 typedef std::vector<Dispatcher*> DispatcherList;
106 typedef std::set<Dispatcher*> DispatcherSet;
99 typedef std::vector<size_t*> IteratorList; 107 typedef std::vector<size_t*> IteratorList;
100 108
101 #if defined(WEBRTC_POSIX) 109 #if defined(WEBRTC_POSIX)
110 bool WaitSelect(int cms, bool process_io);
111 void AddRemovePendingDispatchers();
102 static bool InstallSignal(int signum, void (*handler)(int)); 112 static bool InstallSignal(int signum, void (*handler)(int));
103 113
104 std::unique_ptr<PosixSignalDispatcher> signal_dispatcher_; 114 std::unique_ptr<PosixSignalDispatcher> signal_dispatcher_;
105 #endif 115 #endif // WEBRTC_POSIX
116 #if defined(WEBRTC_USE_EPOLL)
117 void AddEpoll(Dispatcher* dispatcher);
118 void RemoveEpoll(Dispatcher* dispatcher);
119 void UpdateEpoll(Dispatcher* dispatcher);
120 bool WaitEpoll(int cms);
121 bool WaitPoll(int cms, Dispatcher* dispatcher);
122
123 int epoll_fd_ = INVALID_SOCKET;
124 std::vector<struct epoll_event> epoll_events_;
125 #endif // WEBRTC_USE_EPOLL
126 #if defined(WEBRTC_WIN)
106 DispatcherList dispatchers_; 127 DispatcherList dispatchers_;
107 IteratorList iterators_; 128 IteratorList iterators_;
129 #else
130 DispatcherSet dispatchers_;
131 DispatcherSet pending_add_dispatchers_;
132 DispatcherSet pending_remove_dispatchers_;
133 bool processing_dispatchers_ = false;
134 #endif
Taylor Brandstetter 2017/05/30 22:05:50 This code is going to confusing to maintain if |di
joachim 2017/05/31 17:18:03 I agree. As commented before, I wanted to switch t
108 Signaler* signal_wakeup_; 135 Signaler* signal_wakeup_;
109 CriticalSection crit_; 136 CriticalSection crit_;
110 bool fWait_; 137 bool fWait_;
111 #if defined(WEBRTC_WIN) 138 #if defined(WEBRTC_WIN)
112 WSAEVENT socket_ev_; 139 WSAEVENT socket_ev_;
113 #endif 140 #endif
114 }; 141 };
115 142
116 class PhysicalSocket : public AsyncSocket, public sigslot::has_slots<> { 143 class PhysicalSocket : public AsyncSocket, public sigslot::has_slots<> {
117 public: 144 public:
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 // Make virtual so ::sendto can be overwritten in tests. 192 // Make virtual so ::sendto can be overwritten in tests.
166 virtual int DoSendTo(SOCKET socket, const char* buf, int len, int flags, 193 virtual int DoSendTo(SOCKET socket, const char* buf, int len, int flags,
167 const struct sockaddr* dest_addr, socklen_t addrlen); 194 const struct sockaddr* dest_addr, socklen_t addrlen);
168 195
169 void OnResolveResult(AsyncResolverInterface* resolver); 196 void OnResolveResult(AsyncResolverInterface* resolver);
170 197
171 void UpdateLastError(); 198 void UpdateLastError();
172 void MaybeRemapSendError(); 199 void MaybeRemapSendError();
173 200
174 uint8_t enabled_events() const { return enabled_events_; } 201 uint8_t enabled_events() const { return enabled_events_; }
175 void SetEnabledEvents(uint8_t events); 202 virtual void SetEnabledEvents(uint8_t events);
176 void EnableEvents(uint8_t events); 203 virtual void EnableEvents(uint8_t events);
177 void DisableEvents(uint8_t events); 204 virtual void DisableEvents(uint8_t events);
178 205
179 static int TranslateOption(Option opt, int* slevel, int* sopt); 206 static int TranslateOption(Option opt, int* slevel, int* sopt);
180 207
181 PhysicalSocketServer* ss_; 208 PhysicalSocketServer* ss_;
182 SOCKET s_; 209 SOCKET s_;
183 bool udp_; 210 bool udp_;
184 CriticalSection crit_; 211 CriticalSection crit_;
185 int error_ GUARDED_BY(crit_); 212 int error_ GUARDED_BY(crit_);
186 ConnState state_; 213 ConnState state_;
187 AsyncResolver* resolver_; 214 AsyncResolver* resolver_;
(...skipping 25 matching lines...) Expand all
213 int GetDescriptor() override; 240 int GetDescriptor() override;
214 bool IsDescriptorClosed() override; 241 bool IsDescriptorClosed() override;
215 #endif 242 #endif
216 243
217 uint32_t GetRequestedEvents() override; 244 uint32_t GetRequestedEvents() override;
218 void OnPreEvent(uint32_t ff) override; 245 void OnPreEvent(uint32_t ff) override;
219 void OnEvent(uint32_t ff, int err) override; 246 void OnEvent(uint32_t ff, int err) override;
220 247
221 int Close() override; 248 int Close() override;
222 249
250 #if defined(WEBRTC_USE_EPOLL)
251 protected:
252 void StartBatchedEventUpdates();
253 void FinishBatchedEventUpdates();
254
255 void SetEnabledEvents(uint8_t events) override;
256 void EnableEvents(uint8_t events) override;
257 void DisableEvents(uint8_t events) override;
258 #endif
259
260 private:
223 #if defined(WEBRTC_WIN) 261 #if defined(WEBRTC_WIN)
224 private:
225 static int next_id_; 262 static int next_id_;
226 int id_; 263 int id_;
227 bool signal_close_; 264 bool signal_close_;
228 int signal_err_; 265 int signal_err_;
229 #endif // WEBRTC_WIN 266 #endif // WEBRTC_WIN
267 #if defined(WEBRTC_USE_EPOLL)
268 void MaybeUpdateDispatcher(uint8_t old_events);
269
270 int saved_enabled_events_ = -1;
271 #endif
230 }; 272 };
231 273
232 } // namespace rtc 274 } // namespace rtc
233 275
234 #endif // WEBRTC_BASE_PHYSICALSOCKETSERVER_H__ 276 #endif // WEBRTC_BASE_PHYSICALSOCKETSERVER_H__
OLDNEW
« no previous file with comments | « no previous file | webrtc/base/physicalsocketserver.cc » ('j') | webrtc/base/physicalsocketserver.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698