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 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 | 126 |
127 | 127 |
128 EventHandlerImplementation::~EventHandlerImplementation() { | 128 EventHandlerImplementation::~EventHandlerImplementation() { |
129 VOID_TEMP_FAILURE_RETRY(close(epoll_fd_)); | 129 VOID_TEMP_FAILURE_RETRY(close(epoll_fd_)); |
130 VOID_TEMP_FAILURE_RETRY(close(timer_fd_)); | 130 VOID_TEMP_FAILURE_RETRY(close(timer_fd_)); |
131 VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[0])); | 131 VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[0])); |
132 VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[1])); | 132 VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[1])); |
133 } | 133 } |
134 | 134 |
135 | 135 |
136 SocketData* EventHandlerImplementation::GetSocketData(intptr_t fd) { | 136 SocketData* EventHandlerImplementation::GetSocketData(intptr_t fd, |
| 137 bool is_listening) { |
137 ASSERT(fd >= 0); | 138 ASSERT(fd >= 0); |
138 HashMap::Entry* entry = socket_map_.Lookup( | 139 HashMap::Entry* entry = socket_map_.Lookup( |
139 GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd), true); | 140 GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd), true); |
140 ASSERT(entry != NULL); | 141 ASSERT(entry != NULL); |
141 SocketData* sd = reinterpret_cast<SocketData*>(entry->value); | 142 SocketData* sd = reinterpret_cast<SocketData*>(entry->value); |
142 if (sd == NULL) { | 143 if (sd == NULL) { |
143 // 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 |
144 // new SocketData for the file descriptor is inserted. | 145 // new SocketData for the file descriptor is inserted. |
145 sd = new SocketData(fd); | 146 if (is_listening) { |
| 147 sd = new ListeningSocketData(fd); |
| 148 } else { |
| 149 sd = new SocketData(fd); |
| 150 } |
146 entry->value = sd; | 151 entry->value = sd; |
147 } | 152 } |
148 ASSERT(fd == sd->fd()); | 153 ASSERT(fd == sd->fd()); |
149 return sd; | 154 return sd; |
150 } | 155 } |
151 | 156 |
152 | 157 |
153 void EventHandlerImplementation::WakeupHandler(intptr_t id, | 158 void EventHandlerImplementation::WakeupHandler(intptr_t id, |
154 Dart_Port dart_port, | 159 Dart_Port dart_port, |
155 int64_t data) { | 160 int64_t data) { |
(...skipping 29 matching lines...) Expand all Loading... |
185 if (timeout_queue_.HasTimeout()) { | 190 if (timeout_queue_.HasTimeout()) { |
186 int64_t millis = timeout_queue_.CurrentTimeout(); | 191 int64_t millis = timeout_queue_.CurrentTimeout(); |
187 it.it_value.tv_sec = millis / 1000; | 192 it.it_value.tv_sec = millis / 1000; |
188 it.it_value.tv_nsec = (millis % 1000) * 1000000; | 193 it.it_value.tv_nsec = (millis % 1000) * 1000000; |
189 } | 194 } |
190 VOID_NO_RETRY_EXPECTED( | 195 VOID_NO_RETRY_EXPECTED( |
191 timerfd_settime(timer_fd_, TFD_TIMER_ABSTIME, &it, NULL)); | 196 timerfd_settime(timer_fd_, TFD_TIMER_ABSTIME, &it, NULL)); |
192 } else if (msg[i].id == kShutdownId) { | 197 } else if (msg[i].id == kShutdownId) { |
193 shutdown_ = true; | 198 shutdown_ = true; |
194 } else { | 199 } else { |
195 SocketData* sd = GetSocketData(msg[i].id); | 200 SocketData* sd = GetSocketData( |
| 201 msg[i].id, (msg[i].data & (1 << kListeningSocket)) != 0); |
196 if ((msg[i].data & (1 << kShutdownReadCommand)) != 0) { | 202 if ((msg[i].data & (1 << kShutdownReadCommand)) != 0) { |
197 ASSERT(msg[i].data == (1 << kShutdownReadCommand)); | 203 ASSERT(msg[i].data == (1 << kShutdownReadCommand)); |
| 204 ASSERT(!sd->IsListeningSocket()); |
198 // Close the socket for reading. | 205 // Close the socket for reading. |
199 VOID_NO_RETRY_EXPECTED(shutdown(sd->fd(), SHUT_RD)); | 206 VOID_NO_RETRY_EXPECTED(shutdown(sd->fd(), SHUT_RD)); |
200 } else if ((msg[i].data & (1 << kShutdownWriteCommand)) != 0) { | 207 } else if ((msg[i].data & (1 << kShutdownWriteCommand)) != 0) { |
201 ASSERT(msg[i].data == (1 << kShutdownWriteCommand)); | 208 ASSERT(msg[i].data == (1 << kShutdownWriteCommand)); |
| 209 ASSERT(!sd->IsListeningSocket()); |
202 // Close the socket for writing. | 210 // Close the socket for writing. |
203 VOID_NO_RETRY_EXPECTED(shutdown(sd->fd(), SHUT_WR)); | 211 VOID_NO_RETRY_EXPECTED(shutdown(sd->fd(), SHUT_WR)); |
204 } else if ((msg[i].data & (1 << kCloseCommand)) != 0) { | 212 } else if ((msg[i].data & (1 << kCloseCommand)) != 0) { |
205 ASSERT(msg[i].data == (1 << kCloseCommand)); | 213 ASSERT(msg[i].data == (1 << kCloseCommand)); |
206 // Close the socket and free system resources and move on to | 214 // Close the socket and free system resources and move on to |
207 // next message. | 215 // next message. |
208 RemoveFromEpollInstance(epoll_fd_, sd); | 216 if (sd->RemovePort(msg[i].dart_port)) { |
209 intptr_t fd = sd->fd(); | 217 RemoveFromEpollInstance(epoll_fd_, sd); |
210 sd->Close(); | 218 intptr_t fd = sd->fd(); |
211 socket_map_.Remove(GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd)); | 219 sd->Close(); |
212 delete sd; | 220 socket_map_.Remove(GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd)); |
| 221 delete sd; |
| 222 } |
213 DartUtils::PostInt32(msg[i].dart_port, 1 << kDestroyedEvent); | 223 DartUtils::PostInt32(msg[i].dart_port, 1 << kDestroyedEvent); |
214 } else if ((msg[i].data & (1 << kReturnTokenCommand)) != 0) { | 224 } else if ((msg[i].data & (1 << kReturnTokenCommand)) != 0) { |
215 int count = msg[i].data & ((1 << kReturnTokenCommand) - 1); | 225 int count = msg[i].data & ((1 << kReturnTokenCommand) - 1); |
216 for (int i = 0; i < count; i++) { | 226 if (sd->ReturnToken(msg[i].dart_port, count)) { |
217 if (sd->ReturnToken()) { | 227 AddToEpollInstance(epoll_fd_, sd); |
218 AddToEpollInstance(epoll_fd_, sd); | |
219 } | |
220 } | 228 } |
221 } else { | 229 } else { |
222 // Setup events to wait for. | 230 // Setup events to wait for. |
223 sd->SetPortAndMask(msg[i].dart_port, msg[i].data); | 231 if (sd->AddPort(msg[i].dart_port)) { |
224 AddToEpollInstance(epoll_fd_, sd); | 232 sd->SetMask(msg[i].data); |
| 233 AddToEpollInstance(epoll_fd_, sd); |
| 234 } |
225 } | 235 } |
226 } | 236 } |
227 } | 237 } |
228 } | 238 } |
229 | 239 |
230 #ifdef DEBUG_POLL | 240 #ifdef DEBUG_POLL |
231 static void PrintEventMask(intptr_t fd, intptr_t events) { | 241 static void PrintEventMask(intptr_t fd, intptr_t events) { |
232 Log::Print("%d ", fd); | 242 Log::Print("%d ", fd); |
233 if ((events & EPOLLIN) != 0) Log::Print("EPOLLIN "); | 243 if ((events & EPOLLIN) != 0) Log::Print("EPOLLIN "); |
234 if ((events & EPOLLPRI) != 0) Log::Print("EPOLLPRI "); | 244 if ((events & EPOLLPRI) != 0) Log::Print("EPOLLPRI "); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 VOID_TEMP_FAILURE_RETRY_NO_SIGNAL_BLOCKER( | 285 VOID_TEMP_FAILURE_RETRY_NO_SIGNAL_BLOCKER( |
276 read(timer_fd_, &val, sizeof(val))); | 286 read(timer_fd_, &val, sizeof(val))); |
277 if (timeout_queue_.HasTimeout()) { | 287 if (timeout_queue_.HasTimeout()) { |
278 DartUtils::PostNull(timeout_queue_.CurrentPort()); | 288 DartUtils::PostNull(timeout_queue_.CurrentPort()); |
279 timeout_queue_.RemoveCurrent(); | 289 timeout_queue_.RemoveCurrent(); |
280 } | 290 } |
281 } else { | 291 } else { |
282 SocketData* sd = reinterpret_cast<SocketData*>(events[i].data.ptr); | 292 SocketData* sd = reinterpret_cast<SocketData*>(events[i].data.ptr); |
283 intptr_t event_mask = GetPollEvents(events[i].events, sd); | 293 intptr_t event_mask = GetPollEvents(events[i].events, sd); |
284 if (event_mask != 0) { | 294 if (event_mask != 0) { |
| 295 Dart_Port port = sd->port(); |
285 if (sd->TakeToken()) { | 296 if (sd->TakeToken()) { |
286 // Took last token, remove from epoll. | 297 // Took last token, remove from epoll. |
287 RemoveFromEpollInstance(epoll_fd_, sd); | 298 RemoveFromEpollInstance(epoll_fd_, sd); |
288 } | 299 } |
289 Dart_Port port = sd->port(); | |
290 ASSERT(port != 0); | 300 ASSERT(port != 0); |
291 DartUtils::PostInt32(port, event_mask); | 301 DartUtils::PostInt32(port, event_mask); |
292 } | 302 } |
293 } | 303 } |
294 } | 304 } |
295 if (interrupt_seen) { | 305 if (interrupt_seen) { |
296 // Handle after socket events, so we avoid closing a socket before we handle | 306 // Handle after socket events, so we avoid closing a socket before we handle |
297 // the current events. | 307 // the current events. |
298 HandleInterruptFd(); | 308 HandleInterruptFd(); |
299 } | 309 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 | 362 |
353 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { | 363 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { |
354 // The hashmap does not support keys with value 0. | 364 // The hashmap does not support keys with value 0. |
355 return dart::Utils::WordHash(fd + 1); | 365 return dart::Utils::WordHash(fd + 1); |
356 } | 366 } |
357 | 367 |
358 } // namespace bin | 368 } // namespace bin |
359 } // namespace dart | 369 } // namespace dart |
360 | 370 |
361 #endif // defined(TARGET_OS_LINUX) | 371 #endif // defined(TARGET_OS_LINUX) |
OLD | NEW |