OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 <errno.h> | 5 #include <errno.h> |
6 #include <poll.h> | 6 #include <poll.h> |
7 #include <pthread.h> | 7 #include <pthread.h> |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 #include <string.h> | 9 #include <string.h> |
10 #include <sys/time.h> | 10 #include <sys/time.h> |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 | 51 |
52 EventHandlerImplementation::~EventHandlerImplementation() { | 52 EventHandlerImplementation::~EventHandlerImplementation() { |
53 free(port_map_); | 53 free(port_map_); |
54 close(interrupt_fds_[0]); | 54 close(interrupt_fds_[0]); |
55 close(interrupt_fds_[1]); | 55 close(interrupt_fds_[1]); |
56 } | 56 } |
57 | 57 |
58 | 58 |
59 // TODO(hpayer): Use hash table instead of array. | 59 // TODO(hpayer): Use hash table instead of array. |
60 void EventHandlerImplementation::SetPort(intptr_t fd, | 60 void EventHandlerImplementation::SetPort(intptr_t fd, |
61 Dart_Port dart_port, | 61 Dart_Port dart_port, |
62 intptr_t mask) { | 62 intptr_t mask) { |
63 ASSERT(fd >= 0); | 63 ASSERT(fd >= 0); |
64 if (fd >= port_map_size_) { | 64 if (fd >= port_map_size_) { |
65 intptr_t new_port_map_size = port_map_size_; | 65 intptr_t new_port_map_size = port_map_size_; |
66 do { | 66 do { |
67 new_port_map_size = new_port_map_size * kPortMapGrowingFactor; | 67 new_port_map_size = new_port_map_size * kPortMapGrowingFactor; |
68 } while (fd >= new_port_map_size); | 68 } while (fd >= new_port_map_size); |
69 size_t new_port_map_bytes = new_port_map_size * sizeof(PortData); | 69 size_t new_port_map_bytes = new_port_map_size * sizeof(PortData); |
70 port_map_ = reinterpret_cast<PortData*>(realloc(port_map_, | 70 port_map_ = reinterpret_cast<PortData*>(realloc(port_map_, |
71 new_port_map_bytes)); | 71 new_port_map_bytes)); |
72 ASSERT(port_map_ != NULL); | 72 ASSERT(port_map_ != NULL); |
(...skipping 16 matching lines...) Expand all Loading... |
89 port_map_[fd].dart_port = dart_port; | 89 port_map_[fd].dart_port = dart_port; |
90 port_map_[fd].mask = mask; | 90 port_map_[fd].mask = mask; |
91 } | 91 } |
92 | 92 |
93 | 93 |
94 Dart_Port EventHandlerImplementation::PortFor(intptr_t fd) { | 94 Dart_Port EventHandlerImplementation::PortFor(intptr_t fd) { |
95 return port_map_[fd].dart_port; | 95 return port_map_[fd].dart_port; |
96 } | 96 } |
97 | 97 |
98 | 98 |
| 99 bool EventHandlerImplementation::IsListeningSocket(intptr_t fd) { |
| 100 return (port_map_[fd].mask & (1 << kListeningSocket)) != 0; |
| 101 } |
| 102 |
| 103 |
99 void EventHandlerImplementation::RegisterFdWakeup(intptr_t id, | 104 void EventHandlerImplementation::RegisterFdWakeup(intptr_t id, |
100 Dart_Port dart_port, | 105 Dart_Port dart_port, |
101 intptr_t data) { | 106 intptr_t data) { |
102 WakeupHandler(id, dart_port, data); | 107 WakeupHandler(id, dart_port, data); |
103 } | 108 } |
104 | 109 |
105 | 110 |
106 void EventHandlerImplementation::CloseFd(intptr_t id) { | 111 void EventHandlerImplementation::CloseFd(intptr_t id) { |
107 SetPort(id, 0, 0); | 112 SetPort(id, 0, 0); |
108 close(id); | 113 close(id); |
(...skipping 20 matching lines...) Expand all Loading... |
129 intptr_t result = | 134 intptr_t result = |
130 write(interrupt_fds_[1], &msg, kInterruptMessageSize); | 135 write(interrupt_fds_[1], &msg, kInterruptMessageSize); |
131 if (result != kInterruptMessageSize) { | 136 if (result != kInterruptMessageSize) { |
132 perror("Interrupt message failure"); | 137 perror("Interrupt message failure"); |
133 } | 138 } |
134 } | 139 } |
135 | 140 |
136 | 141 |
137 void EventHandlerImplementation::SetPollEvents(struct pollfd* pollfds, | 142 void EventHandlerImplementation::SetPollEvents(struct pollfd* pollfds, |
138 intptr_t mask) { | 143 intptr_t mask) { |
139 /* | 144 // Do not ask for POLLERR and POLLHUP explicitly as they are |
140 * We do not set POLLERR and POLLHUP explicitly since they are triggered | 145 // triggered anyway. |
141 * anyway. | |
142 */ | |
143 if ((mask & (1 << kInEvent)) != 0) { | 146 if ((mask & (1 << kInEvent)) != 0) { |
144 pollfds->events |= POLLIN; | 147 pollfds->events |= POLLIN; |
145 } | 148 } |
146 if ((mask & (1 << kOutEvent)) != 0) { | 149 if ((mask & (1 << kOutEvent)) != 0) { |
147 pollfds->events |= POLLOUT; | 150 pollfds->events |= POLLOUT; |
148 } | 151 } |
149 } | 152 } |
150 | 153 |
151 | 154 |
152 struct pollfd* EventHandlerImplementation::GetPollFds(intptr_t* pollfds_size) { | 155 struct pollfd* EventHandlerImplementation::GetPollFds(intptr_t* pollfds_size) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 CloseFd(msg.id); | 208 CloseFd(msg.id); |
206 } else { | 209 } else { |
207 SetPort(msg.id, msg.dart_port, msg.data); | 210 SetPort(msg.id, msg.dart_port, msg.data); |
208 } | 211 } |
209 } | 212 } |
210 } | 213 } |
211 | 214 |
212 | 215 |
213 intptr_t EventHandlerImplementation::GetPollEvents(struct pollfd* pollfd) { | 216 intptr_t EventHandlerImplementation::GetPollEvents(struct pollfd* pollfd) { |
214 intptr_t event_mask = 0; | 217 intptr_t event_mask = 0; |
215 /* | 218 if (IsListeningSocket(pollfd->fd)) { |
216 * We prioritize the events in the following order. | 219 // For listening sockets the POLLIN event indicate that there are |
217 */ | 220 // connections ready for accept unless accompanied with one of the |
218 if ((pollfd->revents & POLLIN) != 0) { | 221 // other flags. |
219 if (FDUtils::AvailableBytes(pollfd->fd) != 0) { | 222 if ((pollfd->revents & POLLIN) != 0) { |
220 event_mask = (1 << kInEvent); | 223 if ((pollfd->revents & POLLHUP) != 0) event_mask |= (1 << kCloseEvent); |
221 } else if ((pollfd->revents & POLLHUP) != 0) { | 224 if ((pollfd->revents & POLLERR) != 0) event_mask |= (1 << kErrorEvent); |
222 event_mask = (1 << kCloseEvent); | 225 if (event_mask == 0) event_mask |= (1 << kInEvent); |
223 } else if ((pollfd->revents & POLLERR) != 0) { | |
224 event_mask = (1 << kErrorEvent); | |
225 } else { | |
226 /* | |
227 * Accept event. | |
228 */ | |
229 event_mask = (1 << kInEvent); | |
230 } | 226 } |
231 } | 227 } else { |
| 228 // Prioritize data events over close and error events. |
| 229 if ((pollfd->revents & POLLIN) != 0) { |
| 230 if (FDUtils::AvailableBytes(pollfd->fd) != 0) { |
| 231 event_mask = (1 << kInEvent); |
| 232 } else if ((pollfd->revents & POLLHUP) != 0) { |
| 233 event_mask = (1 << kCloseEvent); |
| 234 } else if ((pollfd->revents & POLLERR) != 0) { |
| 235 event_mask = (1 << kErrorEvent); |
| 236 } |
| 237 } |
232 | 238 |
233 if ((pollfd->revents & POLLOUT) != 0) { | 239 if ((pollfd->revents & POLLOUT) != 0) event_mask |= (1 << kOutEvent); |
234 event_mask |= (1 << kOutEvent); | |
235 } | 240 } |
236 | 241 |
237 return event_mask; | 242 return event_mask; |
238 } | 243 } |
239 | 244 |
240 | 245 |
241 void EventHandlerImplementation::HandleEvents(struct pollfd* pollfds, | 246 void EventHandlerImplementation::HandleEvents(struct pollfd* pollfds, |
242 int pollfds_size, | 247 int pollfds_size, |
243 int result_size) { | 248 int result_size) { |
244 if ((pollfds[0].revents & POLLIN) != 0) { | 249 if ((pollfds[0].revents & POLLIN) != 0) { |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 FATAL("Create start event handler thread"); | 321 FATAL("Create start event handler thread"); |
317 } | 322 } |
318 } | 323 } |
319 | 324 |
320 | 325 |
321 void EventHandlerImplementation::SendData(intptr_t id, | 326 void EventHandlerImplementation::SendData(intptr_t id, |
322 Dart_Port dart_port, | 327 Dart_Port dart_port, |
323 intptr_t data) { | 328 intptr_t data) { |
324 RegisterFdWakeup(id, dart_port, data); | 329 RegisterFdWakeup(id, dart_port, data); |
325 } | 330 } |
OLD | NEW |