OLD | NEW |
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); | 33 static const int kInterruptMessageSize = sizeof(InterruptMessage); |
32 static const int kTimerId = -1; | 34 static const int kTimerId = -1; |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 ASSERT(!sd->IsListeningSocket()); | 205 ASSERT(!sd->IsListeningSocket()); |
204 // Close the socket for reading. | 206 // Close the socket for reading. |
205 VOID_NO_RETRY_EXPECTED(shutdown(sd->fd(), SHUT_RD)); | 207 VOID_NO_RETRY_EXPECTED(shutdown(sd->fd(), SHUT_RD)); |
206 } else if (IS_COMMAND(msg[i].data, kShutdownWriteCommand)) { | 208 } else if (IS_COMMAND(msg[i].data, kShutdownWriteCommand)) { |
207 ASSERT(!sd->IsListeningSocket()); | 209 ASSERT(!sd->IsListeningSocket()); |
208 // Close the socket for writing. | 210 // Close the socket for writing. |
209 VOID_NO_RETRY_EXPECTED(shutdown(sd->fd(), SHUT_WR)); | 211 VOID_NO_RETRY_EXPECTED(shutdown(sd->fd(), SHUT_WR)); |
210 } else if (IS_COMMAND(msg[i].data, kCloseCommand)) { | 212 } else if (IS_COMMAND(msg[i].data, kCloseCommand)) { |
211 // Close the socket and free system resources and move on to next | 213 // Close the socket and free system resources and move on to next |
212 // message. | 214 // message. |
213 if (sd->RemovePort(msg[i].dart_port)) { | 215 bool no_more_listeners = sd->RemovePort(msg[i].dart_port); |
| 216 if (no_more_listeners) { |
214 RemoveFromEpollInstance(epoll_fd_, sd); | 217 RemoveFromEpollInstance(epoll_fd_, sd); |
| 218 } |
| 219 |
| 220 if (sd->IsListeningSocket()) { |
215 intptr_t fd = sd->fd(); | 221 intptr_t fd = sd->fd(); |
216 sd->Close(); | 222 // We only close the socket file descriptor from the operating |
217 socket_map_.Remove(GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd)); | 223 // system if there are no other dart socket objects which |
218 delete sd; | 224 // are listening on the same (address, port) combination. |
| 225 { |
| 226 MutexLocker ml(globalTcpListeningSocketRegistry.mutex()); |
| 227 if (globalTcpListeningSocketRegistry.CloseSafe(sd->fd())) { |
| 228 ASSERT(no_more_listeners); |
| 229 socket_map_.Remove( |
| 230 GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd)); |
| 231 sd->Close(); |
| 232 delete sd; |
| 233 } |
| 234 } |
| 235 } else { |
| 236 if (no_more_listeners) { |
| 237 intptr_t fd = sd->fd(); |
| 238 socket_map_.Remove( |
| 239 GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd)); |
| 240 sd->Close(); |
| 241 delete sd; |
| 242 } |
219 } | 243 } |
220 DartUtils::PostInt32(msg[i].dart_port, 1 << kDestroyedEvent); | 244 DartUtils::PostInt32(msg[i].dart_port, 1 << kDestroyedEvent); |
221 } else if (IS_COMMAND(msg[i].data, kReturnTokenCommand)) { | 245 } else if (IS_COMMAND(msg[i].data, kReturnTokenCommand)) { |
222 int count = TOKEN_COUNT(msg[i].data); | 246 int count = TOKEN_COUNT(msg[i].data); |
223 if (sd->ReturnToken(msg[i].dart_port, count)) { | 247 if (sd->ReturnToken(msg[i].dart_port, count)) { |
224 AddToEpollInstance(epoll_fd_, sd); | 248 AddToEpollInstance(epoll_fd_, sd); |
225 } | 249 } |
226 } else { | 250 } else { |
227 ASSERT_NO_COMMAND(msg[i].data); | 251 ASSERT_NO_COMMAND(msg[i].data); |
228 // Setup events to wait for. | 252 // Setup events to wait for. |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 } else { | 350 } else { |
327 handler_impl->HandleEvents(events, result); | 351 handler_impl->HandleEvents(events, result); |
328 } | 352 } |
329 } | 353 } |
330 delete handler; | 354 delete handler; |
331 } | 355 } |
332 | 356 |
333 | 357 |
334 void EventHandlerImplementation::Start(EventHandler* handler) { | 358 void EventHandlerImplementation::Start(EventHandler* handler) { |
335 int result = Thread::Start(&EventHandlerImplementation::Poll, | 359 int result = Thread::Start(&EventHandlerImplementation::Poll, |
336 reinterpret_cast<uword>(handler)); | 360 reinterpret_cast<uword>(handler)); |
337 if (result != 0) { | 361 if (result != 0) { |
338 FATAL1("Failed to start event handler thread %d", result); | 362 FATAL1("Failed to start event handler thread %d", result); |
339 } | 363 } |
340 } | 364 } |
341 | 365 |
342 | 366 |
343 void EventHandlerImplementation::Shutdown() { | 367 void EventHandlerImplementation::Shutdown() { |
344 SendData(kShutdownId, 0, 0); | 368 SendData(kShutdownId, 0, 0); |
345 } | 369 } |
346 | 370 |
(...skipping 13 matching lines...) Expand all Loading... |
360 | 384 |
361 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { | 385 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { |
362 // The hashmap does not support keys with value 0. | 386 // The hashmap does not support keys with value 0. |
363 return dart::Utils::WordHash(fd + 1); | 387 return dart::Utils::WordHash(fd + 1); |
364 } | 388 } |
365 | 389 |
366 } // namespace bin | 390 } // namespace bin |
367 } // namespace dart | 391 } // namespace dart |
368 | 392 |
369 #endif // defined(TARGET_OS_LINUX) | 393 #endif // defined(TARGET_OS_LINUX) |
OLD | NEW |