| 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 |
| 11 #include <pthread.h> // NOLINT | 11 #include <pthread.h> // NOLINT |
| 12 #include <stdio.h> // NOLINT | 12 #include <stdio.h> // NOLINT |
| 13 #include <string.h> // NOLINT | 13 #include <string.h> // NOLINT |
| 14 #include <sys/epoll.h> // NOLINT | 14 #include <sys/epoll.h> // NOLINT |
| 15 #include <sys/stat.h> // NOLINT | 15 #include <sys/stat.h> // NOLINT |
| 16 #include <sys/timerfd.h> // NOLINT | 16 #include <sys/timerfd.h> // NOLINT |
| 17 #include <unistd.h> // NOLINT | 17 #include <unistd.h> // NOLINT |
| 18 #include <fcntl.h> // NOLINT | 18 #include <fcntl.h> // NOLINT |
| 19 | 19 |
| 20 #include "bin/dartutils.h" | 20 #include "bin/dartutils.h" |
| 21 #include "bin/fdutils.h" | 21 #include "bin/fdutils.h" |
| 22 #include "bin/log.h" | 22 #include "bin/log.h" |
| 23 #include "platform/hashmap.h" | 23 #include "bin/socket.h" |
| 24 #include "platform/thread.h" | 24 #include "platform/thread.h" |
| 25 #include "platform/utils.h" | 25 #include "platform/utils.h" |
| 26 | 26 |
| 27 | 27 |
| 28 namespace dart { | 28 namespace dart { |
| 29 namespace bin { | 29 namespace bin { |
| 30 | 30 |
| 31 static const int kInterruptMessageSize = sizeof(InterruptMessage); | 31 static const int kInterruptMessageSize = sizeof(InterruptMessage); |
| 32 static const int kTimerId = -1; | 32 static const int kTimerId = -1; |
| 33 static const int kShutdownId = -2; | 33 static const int kShutdownId = -2; |
| 34 | 34 |
| 35 | 35 |
| 36 intptr_t SocketData::GetPollEvents() { | 36 intptr_t SocketData::GetPollEvents() { |
| 37 // Do not ask for EPOLLERR and EPOLLHUP explicitly as they are | 37 // Do not ask for EPOLLERR and EPOLLHUP explicitly as they are |
| 38 // triggered anyway. | 38 // triggered anyway. |
| 39 intptr_t events = EPOLLET | EPOLLRDHUP; | 39 intptr_t events = EPOLLET | EPOLLRDHUP; |
| 40 if ((mask_ & (1 << kInEvent)) != 0) { | 40 if ((mask_ & (1 << kInEvent)) != 0) { |
| 41 events |= EPOLLIN; | 41 events |= EPOLLIN; |
| 42 } | 42 } |
| 43 if ((mask_ & (1 << kOutEvent)) != 0) { | 43 if ((mask_ & (1 << kOutEvent)) != 0) { |
| 44 events |= EPOLLOUT; | 44 events |= EPOLLOUT; |
| 45 } | 45 } |
| 46 return events; | 46 return events; |
| 47 } | 47 } |
| 48 | 48 |
| 49 | 49 |
| 50 // Unregister the file descriptor for a SocketData structure with epoll. | 50 // Unregister the file descriptor for a SocketData structure with epoll. |
| 51 static void RemoveFromEpollInstance(intptr_t epoll_fd_, SocketData* sd) { | 51 static void RemoveFromEpollInstance(intptr_t epoll_fd_, SocketData* sd) { |
| 52 VOID_TEMP_FAILURE_RETRY(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 sd->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_, SocketData* sd) { |
| 60 struct epoll_event event; | 60 struct epoll_event event; |
| 61 event.events = sd->GetPollEvents(); | 61 event.events = sd->GetPollEvents(); |
| 62 event.data.ptr = sd; | 62 event.data.ptr = sd; |
| 63 int status = TEMP_FAILURE_RETRY(epoll_ctl(epoll_fd_, | 63 int status = NO_RETRY_EXPECTED(epoll_ctl(epoll_fd_, |
| 64 EPOLL_CTL_ADD, | 64 EPOLL_CTL_ADD, |
| 65 sd->fd(), | 65 sd->fd(), |
| 66 &event)); | 66 &event)); |
| 67 if (status == -1) { | 67 if (status == -1) { |
| 68 // Epoll does not accept the file descriptor. It could be due to | 68 // Epoll does not accept the file descriptor. It could be due to |
| 69 // already closed file descriptor, or unuspported devices, such | 69 // already closed file descriptor, or unuspported devices, such |
| 70 // as /dev/null. In such case, mark the file descriptor as closed, | 70 // as /dev/null. In such case, mark the file descriptor as closed, |
| 71 // so dart will handle it accordingly. | 71 // so dart will handle it accordingly. |
| 72 DartUtils::PostInt32(sd->port(), 1 << kCloseEvent); | 72 DartUtils::PostInt32(sd->port(), 1 << kCloseEvent); |
| 73 } | 73 } |
| 74 } | 74 } |
| 75 | 75 |
| 76 | 76 |
| 77 EventHandlerImplementation::EventHandlerImplementation() | 77 EventHandlerImplementation::EventHandlerImplementation() |
| 78 : socket_map_(&HashMap::SamePointerValue, 16) { | 78 : socket_map_(&HashMap::SamePointerValue, 16) { |
| 79 intptr_t result; | 79 intptr_t result; |
| 80 result = TEMP_FAILURE_RETRY(pipe(interrupt_fds_)); | 80 result = NO_RETRY_EXPECTED(pipe(interrupt_fds_)); |
| 81 if (result != 0) { | 81 if (result != 0) { |
| 82 FATAL("Pipe creation failed"); | 82 FATAL("Pipe creation failed"); |
| 83 } | 83 } |
| 84 FDUtils::SetNonBlocking(interrupt_fds_[0]); | 84 FDUtils::SetNonBlocking(interrupt_fds_[0]); |
| 85 FDUtils::SetCloseOnExec(interrupt_fds_[0]); | 85 FDUtils::SetCloseOnExec(interrupt_fds_[0]); |
| 86 FDUtils::SetCloseOnExec(interrupt_fds_[1]); | 86 FDUtils::SetCloseOnExec(interrupt_fds_[1]); |
| 87 shutdown_ = false; | 87 shutdown_ = false; |
| 88 // The initial size passed to epoll_create is ignore on newer (>= | 88 // The initial size passed to epoll_create is ignore on newer (>= |
| 89 // 2.6.8) Linux versions | 89 // 2.6.8) Linux versions |
| 90 static const int kEpollInitialSize = 64; | 90 static const int kEpollInitialSize = 64; |
| 91 epoll_fd_ = TEMP_FAILURE_RETRY(epoll_create(kEpollInitialSize)); | 91 epoll_fd_ = NO_RETRY_EXPECTED(epoll_create(kEpollInitialSize)); |
| 92 if (epoll_fd_ == -1) { | 92 if (epoll_fd_ == -1) { |
| 93 FATAL1("Failed creating epoll file descriptor: %i", errno); | 93 FATAL1("Failed creating epoll file descriptor: %i", errno); |
| 94 } | 94 } |
| 95 FDUtils::SetCloseOnExec(epoll_fd_); | 95 FDUtils::SetCloseOnExec(epoll_fd_); |
| 96 // Register the interrupt_fd with the epoll instance. | 96 // Register the interrupt_fd with the epoll instance. |
| 97 struct epoll_event event; | 97 struct epoll_event event; |
| 98 event.events = EPOLLIN; | 98 event.events = EPOLLIN; |
| 99 event.data.ptr = NULL; | 99 event.data.ptr = NULL; |
| 100 int status = TEMP_FAILURE_RETRY(epoll_ctl(epoll_fd_, | 100 int status = NO_RETRY_EXPECTED(epoll_ctl(epoll_fd_, |
| 101 EPOLL_CTL_ADD, | 101 EPOLL_CTL_ADD, |
| 102 interrupt_fds_[0], | 102 interrupt_fds_[0], |
| 103 &event)); | 103 &event)); |
| 104 if (status == -1) { | 104 if (status == -1) { |
| 105 FATAL("Failed adding interrupt fd to epoll instance"); | 105 FATAL("Failed adding interrupt fd to epoll instance"); |
| 106 } | 106 } |
| 107 timer_fd_ = TEMP_FAILURE_RETRY(timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC)); | 107 timer_fd_ = NO_RETRY_EXPECTED(timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC)); |
| 108 if (timer_fd_ == -1) { | 108 if (timer_fd_ == -1) { |
| 109 FATAL1("Failed creating timerfd file descriptor: %i", errno); | 109 FATAL1("Failed creating timerfd file descriptor: %i", errno); |
| 110 } | 110 } |
| 111 // Register the timer_fd_ with the epoll instance. | 111 // Register the timer_fd_ with the epoll instance. |
| 112 event.events = EPOLLIN; | 112 event.events = EPOLLIN; |
| 113 event.data.fd = timer_fd_; | 113 event.data.fd = timer_fd_; |
| 114 status = TEMP_FAILURE_RETRY(epoll_ctl(epoll_fd_, | 114 status = NO_RETRY_EXPECTED(epoll_ctl(epoll_fd_, |
| 115 EPOLL_CTL_ADD, | 115 EPOLL_CTL_ADD, |
| 116 timer_fd_, | 116 timer_fd_, |
| 117 &event)); | 117 &event)); |
| 118 if (status == -1) { | 118 if (status == -1) { |
| 119 FATAL2( | 119 FATAL2( |
| 120 "Failed adding timerfd fd(%i) to epoll instance: %i", timer_fd_, errno); | 120 "Failed adding timerfd fd(%i) to epoll instance: %i", timer_fd_, errno); |
| 121 } | 121 } |
| 122 } | 122 } |
| 123 | 123 |
| 124 | 124 |
| 125 EventHandlerImplementation::~EventHandlerImplementation() { | 125 EventHandlerImplementation::~EventHandlerImplementation() { |
| 126 VOID_TEMP_FAILURE_RETRY(close(epoll_fd_)); | 126 VOID_TEMP_FAILURE_RETRY(close(epoll_fd_)); |
| 127 VOID_TEMP_FAILURE_RETRY(close(timer_fd_)); | 127 VOID_TEMP_FAILURE_RETRY(close(timer_fd_)); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 for (ssize_t i = 0; i < bytes / kInterruptMessageSize; i++) { | 177 for (ssize_t i = 0; i < bytes / kInterruptMessageSize; i++) { |
| 178 if (msg[i].id == kTimerId) { | 178 if (msg[i].id == kTimerId) { |
| 179 timeout_queue_.UpdateTimeout(msg[i].dart_port, msg[i].data); | 179 timeout_queue_.UpdateTimeout(msg[i].dart_port, msg[i].data); |
| 180 struct itimerspec it; | 180 struct itimerspec it; |
| 181 memset(&it, 0, sizeof(it)); | 181 memset(&it, 0, sizeof(it)); |
| 182 if (timeout_queue_.HasTimeout()) { | 182 if (timeout_queue_.HasTimeout()) { |
| 183 int64_t millis = timeout_queue_.CurrentTimeout(); | 183 int64_t millis = timeout_queue_.CurrentTimeout(); |
| 184 it.it_value.tv_sec = millis / 1000; | 184 it.it_value.tv_sec = millis / 1000; |
| 185 it.it_value.tv_nsec = (millis % 1000) * 1000000; | 185 it.it_value.tv_nsec = (millis % 1000) * 1000000; |
| 186 } | 186 } |
| 187 timerfd_settime(timer_fd_, TFD_TIMER_ABSTIME, &it, NULL); | 187 VOID_NO_RETRY_EXPECTED( |
| 188 timerfd_settime(timer_fd_, TFD_TIMER_ABSTIME, &it, NULL)); |
| 188 } else if (msg[i].id == kShutdownId) { | 189 } else if (msg[i].id == kShutdownId) { |
| 189 shutdown_ = true; | 190 shutdown_ = true; |
| 190 } else { | 191 } else { |
| 191 SocketData* sd = GetSocketData(msg[i].id); | 192 SocketData* sd = GetSocketData(msg[i].id); |
| 192 if ((msg[i].data & (1 << kShutdownReadCommand)) != 0) { | 193 if ((msg[i].data & (1 << kShutdownReadCommand)) != 0) { |
| 193 ASSERT(msg[i].data == (1 << kShutdownReadCommand)); | 194 ASSERT(msg[i].data == (1 << kShutdownReadCommand)); |
| 194 // Close the socket for reading. | 195 // Close the socket for reading. |
| 195 shutdown(sd->fd(), SHUT_RD); | 196 VOID_NO_RETRY_EXPECTED(shutdown(sd->fd(), SHUT_RD)); |
| 196 } else if ((msg[i].data & (1 << kShutdownWriteCommand)) != 0) { | 197 } else if ((msg[i].data & (1 << kShutdownWriteCommand)) != 0) { |
| 197 ASSERT(msg[i].data == (1 << kShutdownWriteCommand)); | 198 ASSERT(msg[i].data == (1 << kShutdownWriteCommand)); |
| 198 // Close the socket for writing. | 199 // Close the socket for writing. |
| 199 shutdown(sd->fd(), SHUT_WR); | 200 VOID_NO_RETRY_EXPECTED(shutdown(sd->fd(), SHUT_WR)); |
| 200 } else if ((msg[i].data & (1 << kCloseCommand)) != 0) { | 201 } else if ((msg[i].data & (1 << kCloseCommand)) != 0) { |
| 201 ASSERT(msg[i].data == (1 << kCloseCommand)); | 202 ASSERT(msg[i].data == (1 << kCloseCommand)); |
| 202 // Close the socket and free system resources and move on to | 203 // Close the socket and free system resources and move on to |
| 203 // next message. | 204 // next message. |
| 204 RemoveFromEpollInstance(epoll_fd_, sd); | 205 RemoveFromEpollInstance(epoll_fd_, sd); |
| 205 intptr_t fd = sd->fd(); | 206 intptr_t fd = sd->fd(); |
| 206 sd->Close(); | 207 sd->Close(); |
| 207 socket_map_.Remove(GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd)); | 208 socket_map_.Remove(GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd)); |
| 208 delete sd; | 209 delete sd; |
| 209 DartUtils::PostInt32(msg[i].dart_port, 1 << kDestroyedEvent); | 210 DartUtils::PostInt32(msg[i].dart_port, 1 << kDestroyedEvent); |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 | 346 |
| 346 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { | 347 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { |
| 347 // The hashmap does not support keys with value 0. | 348 // The hashmap does not support keys with value 0. |
| 348 return dart::Utils::WordHash(fd + 1); | 349 return dart::Utils::WordHash(fd + 1); |
| 349 } | 350 } |
| 350 | 351 |
| 351 } // namespace bin | 352 } // namespace bin |
| 352 } // namespace dart | 353 } // namespace dart |
| 353 | 354 |
| 354 #endif // defined(TARGET_OS_LINUX) | 355 #endif // defined(TARGET_OS_LINUX) |
| OLD | NEW |