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

Side by Side Diff: dart/runtime/bin/eventhandler_linux.cc

Issue 879353003: Introduce optional 'bool shared' parameter to ServerSocket.bind() ... (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Addressed comments Created 5 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « dart/runtime/bin/eventhandler_linux.h ('k') | dart/runtime/bin/eventhandler_macos.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "platform/globals.h" 5 #include "platform/globals.h"
6 #if defined(TARGET_OS_LINUX) 6 #if defined(TARGET_OS_LINUX)
7 7
8 #include "bin/eventhandler.h" 8 #include "bin/eventhandler.h"
9 #include "bin/eventhandler_linux.h"
9 10
10 #include <errno.h> // NOLINT 11 #include <errno.h> // NOLINT
11 #include <pthread.h> // NOLINT 12 #include <pthread.h> // NOLINT
12 #include <stdio.h> // NOLINT 13 #include <stdio.h> // NOLINT
13 #include <string.h> // NOLINT 14 #include <string.h> // NOLINT
14 #include <sys/epoll.h> // NOLINT 15 #include <sys/epoll.h> // NOLINT
15 #include <sys/stat.h> // NOLINT 16 #include <sys/stat.h> // NOLINT
16 #include <sys/timerfd.h> // NOLINT 17 #include <sys/timerfd.h> // NOLINT
17 #include <unistd.h> // NOLINT 18 #include <unistd.h> // NOLINT
18 #include <fcntl.h> // NOLINT 19 #include <fcntl.h> // NOLINT
19 20
20 #include "bin/dartutils.h" 21 #include "bin/dartutils.h"
21 #include "bin/fdutils.h" 22 #include "bin/fdutils.h"
22 #include "bin/log.h" 23 #include "bin/log.h"
24 #include "bin/lockers.h"
23 #include "bin/socket.h" 25 #include "bin/socket.h"
24 #include "bin/thread.h" 26 #include "bin/thread.h"
25 #include "platform/utils.h" 27 #include "platform/utils.h"
26 28
27 29
28 namespace dart { 30 namespace dart {
29 namespace bin { 31 namespace bin {
30 32
31 static const int kInterruptMessageSize = sizeof(InterruptMessage);
32 static const int kTimerId = -1;
33 static const int kShutdownId = -2;
34 33
35 34 intptr_t DescriptorInfo::GetPollEvents() {
36 intptr_t SocketData::GetPollEvents() {
37 // Do not ask for EPOLLERR and EPOLLHUP explicitly as they are 35 // Do not ask for EPOLLERR and EPOLLHUP explicitly as they are
38 // triggered anyway. 36 // triggered anyway.
39 intptr_t events = 0; 37 intptr_t events = 0;
40 if ((mask_ & (1 << kInEvent)) != 0) { 38 if ((Mask() & (1 << kInEvent)) != 0) {
41 events |= EPOLLIN; 39 events |= EPOLLIN;
42 } 40 }
43 if ((mask_ & (1 << kOutEvent)) != 0) { 41 if ((Mask() & (1 << kOutEvent)) != 0) {
44 events |= EPOLLOUT; 42 events |= EPOLLOUT;
45 } 43 }
46 return events; 44 return events;
47 } 45 }
48 46
49 47
50 // Unregister the file descriptor for a SocketData structure with epoll. 48 // Unregister the file descriptor for a DescriptorInfo structure with
51 static void RemoveFromEpollInstance(intptr_t epoll_fd_, SocketData* sd) { 49 // epoll.
50 static void RemoveFromEpollInstance(intptr_t epoll_fd_,
51 DescriptorInfo* di) {
52 VOID_NO_RETRY_EXPECTED(epoll_ctl(epoll_fd_, 52 VOID_NO_RETRY_EXPECTED(epoll_ctl(epoll_fd_,
53 EPOLL_CTL_DEL, 53 EPOLL_CTL_DEL,
54 sd->fd(), 54 di->fd(),
55 NULL)); 55 NULL));
56 } 56 }
57 57
58 58
59 static void AddToEpollInstance(intptr_t epoll_fd_, SocketData* sd) { 59 static void AddToEpollInstance(intptr_t epoll_fd_, DescriptorInfo* di) {
60 struct epoll_event event; 60 struct epoll_event event;
61 event.events = EPOLLRDHUP | sd->GetPollEvents(); 61 event.events = EPOLLRDHUP | di->GetPollEvents();
62 if (!sd->IsListeningSocket()) { 62 if (!di->IsListeningSocket()) {
63 event.events |= EPOLLET; 63 event.events |= EPOLLET;
64 } 64 }
65 event.data.ptr = sd; 65 event.data.ptr = di;
66 int status = NO_RETRY_EXPECTED(epoll_ctl(epoll_fd_, 66 int status = NO_RETRY_EXPECTED(epoll_ctl(epoll_fd_,
67 EPOLL_CTL_ADD, 67 EPOLL_CTL_ADD,
68 sd->fd(), 68 di->fd(),
69 &event)); 69 &event));
70 if (status == -1) { 70 if (status == -1) {
71 // TODO(kustermann): Verify that the dart end is handling this correctly &
72 // adapt this code to work for multiple listening sockets.
73
71 // Epoll does not accept the file descriptor. It could be due to 74 // Epoll does not accept the file descriptor. It could be due to
72 // already closed file descriptor, or unuspported devices, such 75 // already closed file descriptor, or unuspported devices, such
73 // as /dev/null. In such case, mark the file descriptor as closed, 76 // as /dev/null. In such case, mark the file descriptor as closed,
74 // so dart will handle it accordingly. 77 // so dart will handle it accordingly.
75 DartUtils::PostInt32(sd->port(), 1 << kCloseEvent); 78 DartUtils::PostInt32(di->NextPort(), 1 << kCloseEvent);
76 } 79 }
77 } 80 }
78 81
79 82
80 EventHandlerImplementation::EventHandlerImplementation() 83 EventHandlerImplementation::EventHandlerImplementation()
81 : socket_map_(&HashMap::SamePointerValue, 16) { 84 : socket_map_(&HashMap::SamePointerValue, 16) {
82 intptr_t result; 85 intptr_t result;
83 result = NO_RETRY_EXPECTED(pipe(interrupt_fds_)); 86 result = NO_RETRY_EXPECTED(pipe(interrupt_fds_));
84 if (result != 0) { 87 if (result != 0) {
85 FATAL("Pipe creation failed"); 88 FATAL("Pipe creation failed");
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 129
127 130
128 EventHandlerImplementation::~EventHandlerImplementation() { 131 EventHandlerImplementation::~EventHandlerImplementation() {
129 VOID_TEMP_FAILURE_RETRY(close(epoll_fd_)); 132 VOID_TEMP_FAILURE_RETRY(close(epoll_fd_));
130 VOID_TEMP_FAILURE_RETRY(close(timer_fd_)); 133 VOID_TEMP_FAILURE_RETRY(close(timer_fd_));
131 VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[0])); 134 VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[0]));
132 VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[1])); 135 VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[1]));
133 } 136 }
134 137
135 138
136 SocketData* EventHandlerImplementation::GetSocketData(intptr_t fd, 139 DescriptorInfo* EventHandlerImplementation::GetDescriptorInfo(
137 bool is_listening) { 140 intptr_t fd, bool is_listening) {
138 ASSERT(fd >= 0); 141 ASSERT(fd >= 0);
139 HashMap::Entry* entry = socket_map_.Lookup( 142 HashMap::Entry* entry = socket_map_.Lookup(
140 GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd), true); 143 GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd), true);
141 ASSERT(entry != NULL); 144 ASSERT(entry != NULL);
142 SocketData* sd = reinterpret_cast<SocketData*>(entry->value); 145 DescriptorInfo* di =
143 if (sd == NULL) { 146 reinterpret_cast<DescriptorInfo*>(entry->value);
147 if (di == NULL) {
144 // If there is no data in the hash map for this file descriptor a 148 // If there is no data in the hash map for this file descriptor a
145 // new SocketData for the file descriptor is inserted. 149 // new DescriptorInfo for the file descriptor is inserted.
146 if (is_listening) { 150 if (is_listening) {
147 sd = new ListeningSocketData(fd); 151 di = new DescriptorInfoMultiple(fd);
148 } else { 152 } else {
149 sd = new SocketData(fd); 153 di = new DescriptorInfoSingle(fd);
150 } 154 }
151 entry->value = sd; 155 entry->value = di;
152 } 156 }
153 ASSERT(fd == sd->fd()); 157 ASSERT(fd == di->fd());
154 return sd; 158 return di;
155 } 159 }
156 160
157 161
158 void EventHandlerImplementation::WakeupHandler(intptr_t id, 162 void EventHandlerImplementation::WakeupHandler(intptr_t id,
159 Dart_Port dart_port, 163 Dart_Port dart_port,
160 int64_t data) { 164 int64_t data) {
161 InterruptMessage msg; 165 InterruptMessage msg;
162 msg.id = id; 166 msg.id = id;
163 msg.dart_port = dart_port; 167 msg.dart_port = dart_port;
164 msg.data = data; 168 msg.data = data;
(...skipping 25 matching lines...) Expand all
190 if (timeout_queue_.HasTimeout()) { 194 if (timeout_queue_.HasTimeout()) {
191 int64_t millis = timeout_queue_.CurrentTimeout(); 195 int64_t millis = timeout_queue_.CurrentTimeout();
192 it.it_value.tv_sec = millis / 1000; 196 it.it_value.tv_sec = millis / 1000;
193 it.it_value.tv_nsec = (millis % 1000) * 1000000; 197 it.it_value.tv_nsec = (millis % 1000) * 1000000;
194 } 198 }
195 VOID_NO_RETRY_EXPECTED( 199 VOID_NO_RETRY_EXPECTED(
196 timerfd_settime(timer_fd_, TFD_TIMER_ABSTIME, &it, NULL)); 200 timerfd_settime(timer_fd_, TFD_TIMER_ABSTIME, &it, NULL));
197 } else if (msg[i].id == kShutdownId) { 201 } else if (msg[i].id == kShutdownId) {
198 shutdown_ = true; 202 shutdown_ = true;
199 } else { 203 } else {
200 SocketData* sd = GetSocketData( 204 DescriptorInfo* di = GetDescriptorInfo(
201 msg[i].id, (msg[i].data & (1 << kListeningSocket)) != 0); 205 msg[i].id, (msg[i].data & (1 << kListeningSocket)) != 0);
202 if (IS_COMMAND(msg[i].data, kShutdownReadCommand)) { 206 if (IS_COMMAND(msg[i].data, kShutdownReadCommand)) {
203 ASSERT(!sd->IsListeningSocket()); 207 ASSERT(!di->IsListeningSocket());
204 // Close the socket for reading. 208 // Close the socket for reading.
205 VOID_NO_RETRY_EXPECTED(shutdown(sd->fd(), SHUT_RD)); 209 VOID_NO_RETRY_EXPECTED(shutdown(di->fd(), SHUT_RD));
206 } else if (IS_COMMAND(msg[i].data, kShutdownWriteCommand)) { 210 } else if (IS_COMMAND(msg[i].data, kShutdownWriteCommand)) {
207 ASSERT(!sd->IsListeningSocket()); 211 ASSERT(!di->IsListeningSocket());
208 // Close the socket for writing. 212 // Close the socket for writing.
209 VOID_NO_RETRY_EXPECTED(shutdown(sd->fd(), SHUT_WR)); 213 VOID_NO_RETRY_EXPECTED(shutdown(di->fd(), SHUT_WR));
210 } else if (IS_COMMAND(msg[i].data, kCloseCommand)) { 214 } else if (IS_COMMAND(msg[i].data, kCloseCommand)) {
211 // Close the socket and free system resources and move on to next 215 // Close the socket and free system resources and move on to next
212 // message. 216 // message.
213 if (sd->RemovePort(msg[i].dart_port)) { 217 bool no_more_listeners = di->RemovePort(msg[i].dart_port);
214 RemoveFromEpollInstance(epoll_fd_, sd); 218 if (no_more_listeners) {
215 intptr_t fd = sd->fd(); 219 RemoveFromEpollInstance(epoll_fd_, di);
216 sd->Close(); 220 }
217 socket_map_.Remove(GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd)); 221
218 delete sd; 222 intptr_t fd = di->fd();
223 if (di->IsListeningSocket()) {
224 // We only close the socket file descriptor from the operating
225 // system if there are no other dart socket objects which
226 // are listening on the same (address, port) combination.
227 {
228 MutexLocker ml(globalTcpListeningSocketRegistry.mutex());
229 if (globalTcpListeningSocketRegistry.CloseSafe(fd)) {
230 socket_map_.Remove(
231 GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd));
232 di->Close();
233 delete di;
234 }
235 }
236 } else {
237 ASSERT(no_more_listeners);
238 socket_map_.Remove(
239 GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd));
240 di->Close();
241 delete di;
219 } 242 }
220 DartUtils::PostInt32(msg[i].dart_port, 1 << kDestroyedEvent); 243 DartUtils::PostInt32(msg[i].dart_port, 1 << kDestroyedEvent);
221 } else if (IS_COMMAND(msg[i].data, kReturnTokenCommand)) { 244 } else if (IS_COMMAND(msg[i].data, kReturnTokenCommand)) {
222 int count = TOKEN_COUNT(msg[i].data); 245 int count = TOKEN_COUNT(msg[i].data);
223 if (sd->ReturnToken(msg[i].dart_port, count)) { 246 if (di->ReturnTokens(msg[i].dart_port, count)) {
224 AddToEpollInstance(epoll_fd_, sd); 247 AddToEpollInstance(epoll_fd_, di);
225 } 248 }
226 } else { 249 } else {
227 ASSERT_NO_COMMAND(msg[i].data); 250 ASSERT_NO_COMMAND(msg[i].data);
228 // Setup events to wait for. 251 bool had_listeners = di->HasNextPort();
229 if (sd->AddPort(msg[i].dart_port)) { 252 di->SetPortAndMask(msg[i].dart_port, msg[i].data & EVENT_MASK);
230 sd->SetMask(msg[i].data); 253 bool has_listeners = di->HasNextPort();
231 AddToEpollInstance(epoll_fd_, sd); 254
255 // Add/Remove from epoll set depending on previous and current state.
256 if (!had_listeners && has_listeners) {
257 AddToEpollInstance(epoll_fd_, di);
258 } else if (had_listeners && !has_listeners) {
259 RemoveFromEpollInstance(epoll_fd_, di);
232 } 260 }
233 } 261 }
234 } 262 }
235 } 263 }
236 } 264 }
237 265
238 #ifdef DEBUG_POLL 266 #ifdef DEBUG_POLL
239 static void PrintEventMask(intptr_t fd, intptr_t events) { 267 static void PrintEventMask(intptr_t fd, intptr_t events) {
240 Log::Print("%d ", fd); 268 Log::Print("%d ", fd);
241 if ((events & EPOLLIN) != 0) Log::Print("EPOLLIN "); 269 if ((events & EPOLLIN) != 0) Log::Print("EPOLLIN ");
242 if ((events & EPOLLPRI) != 0) Log::Print("EPOLLPRI "); 270 if ((events & EPOLLPRI) != 0) Log::Print("EPOLLPRI ");
243 if ((events & EPOLLOUT) != 0) Log::Print("EPOLLOUT "); 271 if ((events & EPOLLOUT) != 0) Log::Print("EPOLLOUT ");
244 if ((events & EPOLLERR) != 0) Log::Print("EPOLLERR "); 272 if ((events & EPOLLERR) != 0) Log::Print("EPOLLERR ");
245 if ((events & EPOLLHUP) != 0) Log::Print("EPOLLHUP "); 273 if ((events & EPOLLHUP) != 0) Log::Print("EPOLLHUP ");
246 if ((events & EPOLLRDHUP) != 0) Log::Print("EPOLLRDHUP "); 274 if ((events & EPOLLRDHUP) != 0) Log::Print("EPOLLRDHUP ");
247 int all_events = EPOLLIN | EPOLLPRI | EPOLLOUT | 275 int all_events = EPOLLIN | EPOLLPRI | EPOLLOUT |
248 EPOLLERR | EPOLLHUP | EPOLLRDHUP; 276 EPOLLERR | EPOLLHUP | EPOLLRDHUP;
249 if ((events & ~all_events) != 0) { 277 if ((events & ~all_events) != 0) {
250 Log::Print("(and %08x) ", events & ~all_events); 278 Log::Print("(and %08x) ", events & ~all_events);
251 } 279 }
252 Log::Print("(available %d) ", FDUtils::AvailableBytes(fd)); 280 Log::Print("(available %d) ", FDUtils::AvailableBytes(fd));
253 281
254 Log::Print("\n"); 282 Log::Print("\n");
255 } 283 }
256 #endif 284 #endif
257 285
258 intptr_t EventHandlerImplementation::GetPollEvents(intptr_t events, 286 intptr_t EventHandlerImplementation::GetPollEvents(intptr_t events,
259 SocketData* sd) { 287 DescriptorInfo* di) {
260 #ifdef DEBUG_POLL 288 #ifdef DEBUG_POLL
261 PrintEventMask(sd->fd(), events); 289 PrintEventMask(di->fd(), events);
262 #endif 290 #endif
263 if (events & EPOLLERR) { 291 if (events & EPOLLERR) {
264 // Return error only if EPOLLIN is present. 292 // Return error only if EPOLLIN is present.
265 return (events & EPOLLIN) ? (1 << kErrorEvent) : 0; 293 return (events & EPOLLIN) ? (1 << kErrorEvent) : 0;
266 } 294 }
267 intptr_t event_mask = 0; 295 intptr_t event_mask = 0;
268 if (events & EPOLLIN) event_mask |= (1 << kInEvent); 296 if (events & EPOLLIN) event_mask |= (1 << kInEvent);
269 if (events & EPOLLOUT) event_mask |= (1 << kOutEvent); 297 if (events & EPOLLOUT) event_mask |= (1 << kOutEvent);
270 if (events & (EPOLLHUP | EPOLLRDHUP)) event_mask |= (1 << kCloseEvent); 298 if (events & (EPOLLHUP | EPOLLRDHUP)) event_mask |= (1 << kCloseEvent);
271 return event_mask; 299 return event_mask;
272 } 300 }
273 301
274 302
275 void EventHandlerImplementation::HandleEvents(struct epoll_event* events, 303 void EventHandlerImplementation::HandleEvents(struct epoll_event* events,
276 int size) { 304 int size) {
277 bool interrupt_seen = false; 305 bool interrupt_seen = false;
278 for (int i = 0; i < size; i++) { 306 for (int i = 0; i < size; i++) {
279 if (events[i].data.ptr == NULL) { 307 if (events[i].data.ptr == NULL) {
280 interrupt_seen = true; 308 interrupt_seen = true;
281 } else if (events[i].data.fd == timer_fd_) { 309 } else if (events[i].data.fd == timer_fd_) {
282 int64_t val; 310 int64_t val;
283 VOID_TEMP_FAILURE_RETRY_NO_SIGNAL_BLOCKER( 311 VOID_TEMP_FAILURE_RETRY_NO_SIGNAL_BLOCKER(
284 read(timer_fd_, &val, sizeof(val))); 312 read(timer_fd_, &val, sizeof(val)));
285 if (timeout_queue_.HasTimeout()) { 313 if (timeout_queue_.HasTimeout()) {
286 DartUtils::PostNull(timeout_queue_.CurrentPort()); 314 DartUtils::PostNull(timeout_queue_.CurrentPort());
287 timeout_queue_.RemoveCurrent(); 315 timeout_queue_.RemoveCurrent();
288 } 316 }
289 } else { 317 } else {
290 SocketData* sd = reinterpret_cast<SocketData*>(events[i].data.ptr); 318 DescriptorInfo* di =
291 intptr_t event_mask = GetPollEvents(events[i].events, sd); 319 reinterpret_cast<DescriptorInfo*>(events[i].data.ptr);
320 intptr_t event_mask = GetPollEvents(events[i].events, di);
292 if (event_mask != 0) { 321 if (event_mask != 0) {
293 Dart_Port port = sd->port(); 322 Dart_Port port = di->NextPort();
294 if (sd->TakeToken()) { 323 ASSERT(port != 0);
324 if (di->TakeToken()) {
295 // Took last token, remove from epoll. 325 // Took last token, remove from epoll.
296 RemoveFromEpollInstance(epoll_fd_, sd); 326 RemoveFromEpollInstance(epoll_fd_, di);
297 } 327 }
298 ASSERT(port != 0);
299 DartUtils::PostInt32(port, event_mask); 328 DartUtils::PostInt32(port, event_mask);
300 } 329 }
301 } 330 }
302 } 331 }
303 if (interrupt_seen) { 332 if (interrupt_seen) {
304 // Handle after socket events, so we avoid closing a socket before we handle 333 // Handle after socket events, so we avoid closing a socket before we handle
305 // the current events. 334 // the current events.
306 HandleInterruptFd(); 335 HandleInterruptFd();
307 } 336 }
308 } 337 }
(...skipping 17 matching lines...) Expand all
326 } else { 355 } else {
327 handler_impl->HandleEvents(events, result); 356 handler_impl->HandleEvents(events, result);
328 } 357 }
329 } 358 }
330 delete handler; 359 delete handler;
331 } 360 }
332 361
333 362
334 void EventHandlerImplementation::Start(EventHandler* handler) { 363 void EventHandlerImplementation::Start(EventHandler* handler) {
335 int result = Thread::Start(&EventHandlerImplementation::Poll, 364 int result = Thread::Start(&EventHandlerImplementation::Poll,
336 reinterpret_cast<uword>(handler)); 365 reinterpret_cast<uword>(handler));
337 if (result != 0) { 366 if (result != 0) {
338 FATAL1("Failed to start event handler thread %d", result); 367 FATAL1("Failed to start event handler thread %d", result);
339 } 368 }
340 } 369 }
341 370
342 371
343 void EventHandlerImplementation::Shutdown() { 372 void EventHandlerImplementation::Shutdown() {
344 SendData(kShutdownId, 0, 0); 373 SendData(kShutdownId, 0, 0);
345 } 374 }
346 375
(...skipping 13 matching lines...) Expand all
360 389
361 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { 390 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) {
362 // The hashmap does not support keys with value 0. 391 // The hashmap does not support keys with value 0.
363 return dart::Utils::WordHash(fd + 1); 392 return dart::Utils::WordHash(fd + 1);
364 } 393 }
365 394
366 } // namespace bin 395 } // namespace bin
367 } // namespace dart 396 } // namespace dart
368 397
369 #endif // defined(TARGET_OS_LINUX) 398 #endif // defined(TARGET_OS_LINUX)
OLDNEW
« no previous file with comments | « dart/runtime/bin/eventhandler_linux.h ('k') | dart/runtime/bin/eventhandler_macos.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698