| 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 | 9 |
| 10 #include <errno.h> // NOLINT | 10 #include <errno.h> // NOLINT |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 EPOLL_CTL_DEL, | 41 EPOLL_CTL_DEL, |
| 42 sd->fd(), | 42 sd->fd(), |
| 43 NULL)); | 43 NULL)); |
| 44 if (status == -1) { | 44 if (status == -1) { |
| 45 FATAL("Failed unregistering events for file descriptor"); | 45 FATAL("Failed unregistering events for file descriptor"); |
| 46 } | 46 } |
| 47 sd->set_tracked_by_epoll(false); | 47 sd->set_tracked_by_epoll(false); |
| 48 } | 48 } |
| 49 | 49 |
| 50 | 50 |
| 51 static void AddToEpollInstance(intptr_t epoll_fd_, SocketData* sd) { | 51 static void AddToEpollInstance(intptr_t epoll_fd_, SocketData* sd, int mask) { |
| 52 ASSERT(!sd->tracked_by_epoll()); | 52 ASSERT(!sd->tracked_by_epoll()); |
| 53 struct epoll_event event; | 53 struct epoll_event event; |
| 54 event.events = EPOLLET | EPOLLRDHUP; | 54 event.events = EPOLLET | EPOLLRDHUP; |
| 55 if ((sd->mask() & (1 << kInEvent)) != 0) event.events |= EPOLLIN; | 55 if ((mask & (1 << kInEvent)) != 0) event.events |= EPOLLIN; |
| 56 if ((sd->mask() & (1 << kOutEvent)) != 0) event.events |= EPOLLOUT; | 56 if ((mask & (1 << kOutEvent)) != 0) event.events |= EPOLLOUT; |
| 57 event.data.ptr = sd; | 57 event.data.ptr = sd; |
| 58 int status = TEMP_FAILURE_RETRY(epoll_ctl(epoll_fd_, | 58 int status = TEMP_FAILURE_RETRY(epoll_ctl(epoll_fd_, |
| 59 EPOLL_CTL_ADD, | 59 EPOLL_CTL_ADD, |
| 60 sd->fd(), | 60 sd->fd(), |
| 61 &event)); | 61 &event)); |
| 62 if (status == -1) { | 62 if (status == -1) { |
| 63 // Epoll does not accept the file descriptor. It could be due to | 63 // Epoll does not accept the file descriptor. It could be due to |
| 64 // already closed file descriptor, or unuspported devices, such | 64 // already closed file descriptor, or unuspported devices, such |
| 65 // as /dev/null. In such case, mark the file descriptor as closed, | 65 // as /dev/null. In such case, mark the file descriptor as closed, |
| 66 // so dart will handle it accordingly. | 66 // so dart will handle it accordingly. |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 it.it_value.tv_sec = millis / 1000; | 183 it.it_value.tv_sec = millis / 1000; |
| 184 it.it_value.tv_nsec = (millis % 1000) * 1000000; | 184 it.it_value.tv_nsec = (millis % 1000) * 1000000; |
| 185 } | 185 } |
| 186 timerfd_settime(timer_fd_, TFD_TIMER_ABSTIME, &it, NULL); | 186 timerfd_settime(timer_fd_, TFD_TIMER_ABSTIME, &it, NULL); |
| 187 } else if (msg[i].id == kShutdownId) { | 187 } else if (msg[i].id == kShutdownId) { |
| 188 shutdown_ = true; | 188 shutdown_ = true; |
| 189 } else { | 189 } else { |
| 190 bool is_new = false; | 190 bool is_new = false; |
| 191 SocketData* sd = GetSocketData(msg[i].id, &is_new); | 191 SocketData* sd = GetSocketData(msg[i].id, &is_new); |
| 192 if (is_new) { | 192 if (is_new) { |
| 193 sd->SetPortAndMask(msg[i].dart_port, msg[i].data); | 193 sd->SetPort(msg[i].dart_port); |
| 194 AddToEpollInstance(epoll_fd_, sd); | 194 AddToEpollInstance(epoll_fd_, sd, msg[i].data); |
| 195 } | 195 } |
| 196 if ((msg[i].data & (1 << kShutdownReadCommand)) != 0) { | 196 if ((msg[i].data & (1 << kShutdownReadCommand)) != 0) { |
| 197 ASSERT(msg[i].data == (1 << kShutdownReadCommand)); | 197 ASSERT(msg[i].data == (1 << kShutdownReadCommand)); |
| 198 // Close the socket for reading. | 198 // Close the socket for reading. |
| 199 sd->ShutdownRead(); | 199 sd->ShutdownRead(); |
| 200 } else if ((msg[i].data & (1 << kShutdownWriteCommand)) != 0) { | 200 } else if ((msg[i].data & (1 << kShutdownWriteCommand)) != 0) { |
| 201 ASSERT(msg[i].data == (1 << kShutdownWriteCommand)); | 201 ASSERT(msg[i].data == (1 << kShutdownWriteCommand)); |
| 202 // Close the socket for writing. | 202 // Close the socket for writing. |
| 203 sd->ShutdownWrite(); | 203 sd->ShutdownWrite(); |
| 204 } else if ((msg[i].data & (1 << kCloseCommand)) != 0) { | 204 } else if ((msg[i].data & (1 << kCloseCommand)) != 0) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 234 | 234 |
| 235 Log::Print("\n"); | 235 Log::Print("\n"); |
| 236 } | 236 } |
| 237 #endif | 237 #endif |
| 238 | 238 |
| 239 intptr_t EventHandlerImplementation::GetPollEvents(intptr_t events, | 239 intptr_t EventHandlerImplementation::GetPollEvents(intptr_t events, |
| 240 SocketData* sd) { | 240 SocketData* sd) { |
| 241 #ifdef DEBUG_POLL | 241 #ifdef DEBUG_POLL |
| 242 PrintEventMask(sd->fd(), events); | 242 PrintEventMask(sd->fd(), events); |
| 243 #endif | 243 #endif |
| 244 if (events & EPOLLERR) { |
| 245 // Return only error if EPOLLIN is present. |
| 246 return (events & EPOLLIN) ? (1 << kErrorEvent) : 0; |
| 247 } |
| 244 intptr_t event_mask = 0; | 248 intptr_t event_mask = 0; |
| 245 if (sd->IsListeningSocket()) { | 249 if (events & EPOLLIN) event_mask |= (1 << kInEvent); |
| 246 // For listening sockets the EPOLLIN event indicate that there are | 250 if (events & EPOLLOUT) event_mask |= (1 << kOutEvent); |
| 247 // connections ready for accept unless accompanied with one of the | 251 if (events & (EPOLLHUP | EPOLLRDHUP)) event_mask |= (1 << kCloseEvent); |
| 248 // other flags. | |
| 249 if ((events & EPOLLIN) != 0) { | |
| 250 if ((events & EPOLLHUP) != 0) event_mask |= (1 << kCloseEvent); | |
| 251 if ((events & EPOLLERR) != 0) event_mask |= (1 << kErrorEvent); | |
| 252 if (event_mask == 0) event_mask |= (1 << kInEvent); | |
| 253 } | |
| 254 } else { | |
| 255 // Prioritize data events over close and error events. | |
| 256 if ((events & (EPOLLIN | EPOLLHUP | EPOLLRDHUP)) != 0) { | |
| 257 // If we have EPOLLIN and we have available bytes, report that. | |
| 258 if ((events & EPOLLIN) != 0) { | |
| 259 event_mask = (1 << kInEvent); | |
| 260 } | |
| 261 if ((events & (EPOLLHUP | EPOLLRDHUP)) != 0) { | |
| 262 // If both EPOLLHUP and EPOLLERR are reported treat it as an | |
| 263 // error. | |
| 264 if ((events & EPOLLERR) != 0) { | |
| 265 event_mask = (1 << kErrorEvent); | |
| 266 } else { | |
| 267 event_mask |= (1 << kCloseEvent); | |
| 268 } | |
| 269 } else if ((events & EPOLLERR) != 0) { | |
| 270 event_mask = (1 << kErrorEvent); | |
| 271 } | |
| 272 } | |
| 273 | |
| 274 if ((events & EPOLLOUT) != 0) { | |
| 275 if ((events & EPOLLERR) != 0) { | |
| 276 if (!sd->IsPipe()) { | |
| 277 event_mask = (1 << kErrorEvent); | |
| 278 } | |
| 279 } else { | |
| 280 event_mask |= (1 << kOutEvent); | |
| 281 } | |
| 282 } | |
| 283 } | |
| 284 | |
| 285 return event_mask; | 252 return event_mask; |
| 286 } | 253 } |
| 287 | 254 |
| 288 | 255 |
| 289 void EventHandlerImplementation::HandleEvents(struct epoll_event* events, | 256 void EventHandlerImplementation::HandleEvents(struct epoll_event* events, |
| 290 int size) { | 257 int size) { |
| 291 bool interrupt_seen = false; | 258 bool interrupt_seen = false; |
| 292 for (int i = 0; i < size; i++) { | 259 for (int i = 0; i < size; i++) { |
| 293 if (events[i].data.ptr == NULL) { | 260 if (events[i].data.ptr == NULL) { |
| 294 interrupt_seen = true; | 261 interrupt_seen = true; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 | 337 |
| 371 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { | 338 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { |
| 372 // The hashmap does not support keys with value 0. | 339 // The hashmap does not support keys with value 0. |
| 373 return dart::Utils::WordHash(fd + 1); | 340 return dart::Utils::WordHash(fd + 1); |
| 374 } | 341 } |
| 375 | 342 |
| 376 } // namespace bin | 343 } // namespace bin |
| 377 } // namespace dart | 344 } // namespace dart |
| 378 | 345 |
| 379 #endif // defined(TARGET_OS_LINUX) | 346 #endif // defined(TARGET_OS_LINUX) |
| OLD | NEW |