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

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

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