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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 pollfds->events |= POLLRDHUP; | |
144 if ((mask & (1 << kInEvent)) != 0) { | 146 if ((mask & (1 << kInEvent)) != 0) { |
145 pollfds->events |= POLLIN; | 147 pollfds->events |= POLLIN; |
146 } | 148 } |
147 if ((mask & (1 << kOutEvent)) != 0) { | 149 if ((mask & (1 << kOutEvent)) != 0) { |
148 pollfds->events |= POLLOUT; | 150 pollfds->events |= POLLOUT; |
149 } | 151 } |
| 152 pollfds->events |= POLLRDHUP; |
150 } | 153 } |
151 | 154 |
152 | 155 |
153 struct pollfd* EventHandlerImplementation::GetPollFds(intptr_t* pollfds_size) { | 156 struct pollfd* EventHandlerImplementation::GetPollFds(intptr_t* pollfds_size) { |
154 struct pollfd* pollfds; | 157 struct pollfd* pollfds; |
155 | 158 |
156 intptr_t numPollfds = 1 + port_map_entries_; | 159 intptr_t numPollfds = 1 + port_map_entries_; |
157 pollfds = reinterpret_cast<struct pollfd*>(calloc(sizeof(struct pollfd), | 160 pollfds = reinterpret_cast<struct pollfd*>(calloc(sizeof(struct pollfd), |
158 numPollfds)); | 161 numPollfds)); |
159 pollfds[0].fd = interrupt_fds_[0]; | 162 pollfds[0].fd = interrupt_fds_[0]; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 CloseFd(msg.id); | 209 CloseFd(msg.id); |
207 } else { | 210 } else { |
208 SetPort(msg.id, msg.dart_port, msg.data); | 211 SetPort(msg.id, msg.dart_port, msg.data); |
209 } | 212 } |
210 } | 213 } |
211 } | 214 } |
212 | 215 |
213 | 216 |
214 intptr_t EventHandlerImplementation::GetPollEvents(struct pollfd* pollfd) { | 217 intptr_t EventHandlerImplementation::GetPollEvents(struct pollfd* pollfd) { |
215 intptr_t event_mask = 0; | 218 intptr_t event_mask = 0; |
216 /* | 219 if (IsListeningSocket(pollfd->fd)) { |
217 * We prioritize the events in the following order. | 220 // For listening sockets the POLLIN event indicate that there are |
218 */ | 221 // connections ready for accept unless accompanied with one of the |
219 if ((pollfd->revents & POLLIN) != 0) { | 222 // other flags. |
220 if (FDUtils::AvailableBytes(pollfd->fd) != 0) { | 223 if ((pollfd->revents & POLLIN) != 0) { |
221 event_mask = (1 << kInEvent); | 224 if ((pollfd->revents & POLLHUP) != 0) event_mask |= (1 << kCloseEvent); |
222 } else if (((pollfd->revents & POLLHUP) != 0) || | 225 if ((pollfd->revents & POLLERR) != 0) event_mask |= (1 << kErrorEvent); |
223 ((pollfd->revents & POLLRDHUP) != 0)) { | 226 if (event_mask == 0) event_mask |= (1 << kInEvent); |
224 event_mask = (1 << kCloseEvent); | |
225 } else if ((pollfd->revents & POLLERR) != 0) { | |
226 event_mask = (1 << kErrorEvent); | |
227 } else { | |
228 /* | |
229 * Accept event. | |
230 */ | |
231 event_mask = (1 << kInEvent); | |
232 } | 227 } |
233 } | 228 } else { |
| 229 // Prioritize data events over close and error events. |
| 230 if ((pollfd->revents & POLLIN) != 0) { |
| 231 if (FDUtils::AvailableBytes(pollfd->fd) != 0) { |
| 232 event_mask = (1 << kInEvent); |
| 233 } else if (((pollfd->revents & POLLHUP) != 0) || |
| 234 ((pollfd->revents & POLLRDHUP) != 0)) { |
| 235 event_mask = (1 << kCloseEvent); |
| 236 } else if ((pollfd->revents & POLLERR) != 0) { |
| 237 event_mask = (1 << kErrorEvent); |
| 238 } |
| 239 } |
234 | 240 |
235 if ((pollfd->revents & POLLOUT) != 0) { | 241 if ((pollfd->revents & POLLOUT) != 0) event_mask |= (1 << kOutEvent); |
236 event_mask |= (1 << kOutEvent); | |
237 } | 242 } |
238 | 243 |
239 return event_mask; | 244 return event_mask; |
240 } | 245 } |
241 | 246 |
242 | 247 |
243 void EventHandlerImplementation::HandleEvents(struct pollfd* pollfds, | 248 void EventHandlerImplementation::HandleEvents(struct pollfd* pollfds, |
244 int pollfds_size, | 249 int pollfds_size, |
245 int result_size) { | 250 int result_size) { |
246 if ((pollfds[0].revents & POLLIN) != 0) { | 251 if ((pollfds[0].revents & POLLIN) != 0) { |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 FATAL("Create start event handler thread"); | 323 FATAL("Create start event handler thread"); |
319 } | 324 } |
320 } | 325 } |
321 | 326 |
322 | 327 |
323 void EventHandlerImplementation::SendData(intptr_t id, | 328 void EventHandlerImplementation::SendData(intptr_t id, |
324 Dart_Port dart_port, | 329 Dart_Port dart_port, |
325 intptr_t data) { | 330 intptr_t data) { |
326 RegisterFdWakeup(id, dart_port, data); | 331 RegisterFdWakeup(id, dart_port, data); |
327 } | 332 } |
OLD | NEW |