Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Side by Side Diff: dart/runtime/bin/eventhandler_linux.cc

Issue 905733002: Extract common Mask/Dart_Port settings of linux event handler implementation to eventhandler.h (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include "bin/eventhandler_linux.h"
9 10
10 #include <errno.h> // NOLINT 11 #include <errno.h> // NOLINT
11 #include <pthread.h> // NOLINT 12 #include <pthread.h> // NOLINT
12 #include <stdio.h> // NOLINT 13 #include <stdio.h> // NOLINT
13 #include <string.h> // NOLINT 14 #include <string.h> // NOLINT
14 #include <sys/epoll.h> // NOLINT 15 #include <sys/epoll.h> // NOLINT
15 #include <sys/stat.h> // NOLINT 16 #include <sys/stat.h> // NOLINT
16 #include <sys/timerfd.h> // NOLINT 17 #include <sys/timerfd.h> // NOLINT
17 #include <unistd.h> // NOLINT 18 #include <unistd.h> // NOLINT
18 #include <fcntl.h> // NOLINT 19 #include <fcntl.h> // NOLINT
19 20
20 #include "bin/dartutils.h" 21 #include "bin/dartutils.h"
21 #include "bin/fdutils.h" 22 #include "bin/fdutils.h"
22 #include "bin/log.h" 23 #include "bin/log.h"
24 #include "bin/lockers.h"
23 #include "bin/socket.h" 25 #include "bin/socket.h"
24 #include "bin/thread.h" 26 #include "bin/thread.h"
25 #include "platform/utils.h" 27 #include "platform/utils.h"
26 28
27 29
28 namespace dart { 30 namespace dart {
29 namespace bin { 31 namespace bin {
30 32
31 33
32 intptr_t SocketData::GetPollEvents() { 34 intptr_t DescriptorInfo::GetPollEvents() {
33 // Do not ask for EPOLLERR and EPOLLHUP explicitly as they are 35 // Do not ask for EPOLLERR and EPOLLHUP explicitly as they are
34 // triggered anyway. 36 // triggered anyway.
35 intptr_t events = 0; 37 intptr_t events = 0;
36 if ((mask_ & (1 << kInEvent)) != 0) { 38 if ((Mask() & (1 << kInEvent)) != 0) {
37 events |= EPOLLIN; 39 events |= EPOLLIN;
38 } 40 }
39 if ((mask_ & (1 << kOutEvent)) != 0) { 41 if ((Mask() & (1 << kOutEvent)) != 0) {
40 events |= EPOLLOUT; 42 events |= EPOLLOUT;
41 } 43 }
42 return events; 44 return events;
43 } 45 }
44 46
45 47
46 // Unregister the file descriptor for a SocketData structure with epoll. 48 // Unregister the file descriptor for a DescriptorInfo structure with
47 static void RemoveFromEpollInstance(intptr_t epoll_fd_, SocketData* sd) { 49 // epoll.
50 static void RemoveFromEpollInstance(intptr_t epoll_fd_,
51 DescriptorInfo* di) {
48 VOID_NO_RETRY_EXPECTED(epoll_ctl(epoll_fd_, 52 VOID_NO_RETRY_EXPECTED(epoll_ctl(epoll_fd_,
49 EPOLL_CTL_DEL, 53 EPOLL_CTL_DEL,
50 sd->fd(), 54 di->fd(),
51 NULL)); 55 NULL));
52 } 56 }
53 57
54 58
55 static void AddToEpollInstance(intptr_t epoll_fd_, SocketData* sd) { 59 static void AddToEpollInstance(intptr_t epoll_fd_, DescriptorInfo* di) {
56 struct epoll_event event; 60 struct epoll_event event;
57 event.events = EPOLLRDHUP | sd->GetPollEvents(); 61 event.events = EPOLLRDHUP | di->GetPollEvents();
58 if (!sd->IsListeningSocket()) { 62 if (!di->IsListeningSocket()) {
59 event.events |= EPOLLET; 63 event.events |= EPOLLET;
60 } 64 }
61 event.data.ptr = sd; 65 event.data.ptr = di;
62 int status = NO_RETRY_EXPECTED(epoll_ctl(epoll_fd_, 66 int status = NO_RETRY_EXPECTED(epoll_ctl(epoll_fd_,
63 EPOLL_CTL_ADD, 67 EPOLL_CTL_ADD,
64 sd->fd(), 68 di->fd(),
65 &event)); 69 &event));
66 if (status == -1) { 70 if (status == -1) {
71 // TODO(dart:io): Verify that the dart end is handling this correctly.
72
67 // Epoll does not accept the file descriptor. It could be due to 73 // Epoll does not accept the file descriptor. It could be due to
68 // already closed file descriptor, or unuspported devices, such 74 // already closed file descriptor, or unuspported devices, such
69 // as /dev/null. In such case, mark the file descriptor as closed, 75 // as /dev/null. In such case, mark the file descriptor as closed,
70 // so dart will handle it accordingly. 76 // so dart will handle it accordingly.
71 DartUtils::PostInt32(sd->port(), 1 << kCloseEvent); 77 di->NotifyAllDartPorts(1 << kCloseEvent);
72 } 78 }
73 } 79 }
74 80
75 81
76 EventHandlerImplementation::EventHandlerImplementation() 82 EventHandlerImplementation::EventHandlerImplementation()
77 : socket_map_(&HashMap::SamePointerValue, 16) { 83 : socket_map_(&HashMap::SamePointerValue, 16) {
78 intptr_t result; 84 intptr_t result;
79 result = NO_RETRY_EXPECTED(pipe(interrupt_fds_)); 85 result = NO_RETRY_EXPECTED(pipe(interrupt_fds_));
80 if (result != 0) { 86 if (result != 0) {
81 FATAL("Pipe creation failed"); 87 FATAL("Pipe creation failed");
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 128
123 129
124 EventHandlerImplementation::~EventHandlerImplementation() { 130 EventHandlerImplementation::~EventHandlerImplementation() {
125 VOID_TEMP_FAILURE_RETRY(close(epoll_fd_)); 131 VOID_TEMP_FAILURE_RETRY(close(epoll_fd_));
126 VOID_TEMP_FAILURE_RETRY(close(timer_fd_)); 132 VOID_TEMP_FAILURE_RETRY(close(timer_fd_));
127 VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[0])); 133 VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[0]));
128 VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[1])); 134 VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[1]));
129 } 135 }
130 136
131 137
132 SocketData* EventHandlerImplementation::GetSocketData(intptr_t fd, 138 void EventHandlerImplementation::UpdateEpollInstance(intptr_t old_mask,
133 bool is_listening) { 139 DescriptorInfo *di) {
140 intptr_t new_mask = di->Mask();
141 if (old_mask != 0 && new_mask == 0) {
142 RemoveFromEpollInstance(epoll_fd_, di);
143 } else if (old_mask == 0 && new_mask != 0) {
144 AddToEpollInstance(epoll_fd_, di);
145 } else if (old_mask != 0 && new_mask != 0) {
146 if (di->IsListeningSocket()) {
147 ASSERT(old_mask == new_mask);
148 } else {
Søren Gjesse 2015/02/06 14:42:18 Use UpdateEpollInstance here. Maybe also on Androi
kustermann 2015/02/09 08:50:22 That would be an infinite recursive call, this fun
Søren Gjesse 2015/02/09 09:09:21 It probably isn't. We should still consider using
149 RemoveFromEpollInstance(epoll_fd_, di);
150 AddToEpollInstance(epoll_fd_, di);
151 }
152 }
153 }
154
155
156 DescriptorInfo* EventHandlerImplementation::GetDescriptorInfo(
157 intptr_t fd, bool is_listening) {
134 ASSERT(fd >= 0); 158 ASSERT(fd >= 0);
135 HashMap::Entry* entry = socket_map_.Lookup( 159 HashMap::Entry* entry = socket_map_.Lookup(
136 GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd), true); 160 GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd), true);
137 ASSERT(entry != NULL); 161 ASSERT(entry != NULL);
138 SocketData* sd = reinterpret_cast<SocketData*>(entry->value); 162 DescriptorInfo* di =
139 if (sd == NULL) { 163 reinterpret_cast<DescriptorInfo*>(entry->value);
164 if (di == NULL) {
140 // If there is no data in the hash map for this file descriptor a 165 // If there is no data in the hash map for this file descriptor a
141 // new SocketData for the file descriptor is inserted. 166 // new DescriptorInfo for the file descriptor is inserted.
142 if (is_listening) { 167 if (is_listening) {
143 sd = new ListeningSocketData(fd); 168 di = new DescriptorInfoMultiple(fd);
144 } else { 169 } else {
145 sd = new SocketData(fd); 170 di = new DescriptorInfoSingle(fd);
146 } 171 }
147 entry->value = sd; 172 entry->value = di;
148 } 173 }
149 ASSERT(fd == sd->fd()); 174 ASSERT(fd == di->fd());
150 return sd; 175 return di;
151 } 176 }
152 177
153 178
154 void EventHandlerImplementation::WakeupHandler(intptr_t id, 179 void EventHandlerImplementation::WakeupHandler(intptr_t id,
155 Dart_Port dart_port, 180 Dart_Port dart_port,
156 int64_t data) { 181 int64_t data) {
157 InterruptMessage msg; 182 InterruptMessage msg;
158 msg.id = id; 183 msg.id = id;
159 msg.dart_port = dart_port; 184 msg.dart_port = dart_port;
160 msg.data = data; 185 msg.data = data;
(...skipping 27 matching lines...) Expand all
188 it.it_value.tv_sec = millis / 1000; 213 it.it_value.tv_sec = millis / 1000;
189 it.it_value.tv_nsec = (millis % 1000) * 1000000; 214 it.it_value.tv_nsec = (millis % 1000) * 1000000;
190 } 215 }
191 VOID_NO_RETRY_EXPECTED( 216 VOID_NO_RETRY_EXPECTED(
192 timerfd_settime(timer_fd_, TFD_TIMER_ABSTIME, &it, NULL)); 217 timerfd_settime(timer_fd_, TFD_TIMER_ABSTIME, &it, NULL));
193 } else if (msg[i].id == kShutdownId) { 218 } else if (msg[i].id == kShutdownId) {
194 shutdown_ = true; 219 shutdown_ = true;
195 } else { 220 } else {
196 ASSERT((msg[i].data & COMMAND_MASK) != 0); 221 ASSERT((msg[i].data & COMMAND_MASK) != 0);
197 222
198 SocketData* sd = GetSocketData( 223 DescriptorInfo* di = GetDescriptorInfo(
199 msg[i].id, IS_LISTENING_SOCKET(msg[i].data)); 224 msg[i].id, IS_LISTENING_SOCKET(msg[i].data));
200 if (IS_COMMAND(msg[i].data, kShutdownReadCommand)) { 225 if (IS_COMMAND(msg[i].data, kShutdownReadCommand)) {
201 ASSERT(!sd->IsListeningSocket()); 226 ASSERT(!di->IsListeningSocket());
202 // Close the socket for reading. 227 // Close the socket for reading.
203 VOID_NO_RETRY_EXPECTED(shutdown(sd->fd(), SHUT_RD)); 228 VOID_NO_RETRY_EXPECTED(shutdown(di->fd(), SHUT_RD));
204 } else if (IS_COMMAND(msg[i].data, kShutdownWriteCommand)) { 229 } else if (IS_COMMAND(msg[i].data, kShutdownWriteCommand)) {
205 ASSERT(!sd->IsListeningSocket()); 230 ASSERT(!di->IsListeningSocket());
206 // Close the socket for writing. 231 // Close the socket for writing.
207 VOID_NO_RETRY_EXPECTED(shutdown(sd->fd(), SHUT_WR)); 232 VOID_NO_RETRY_EXPECTED(shutdown(di->fd(), SHUT_WR));
208 } else if (IS_COMMAND(msg[i].data, kCloseCommand)) { 233 } else if (IS_COMMAND(msg[i].data, kCloseCommand)) {
209 // Close the socket and free system resources and move on to next 234 // Close the socket and free system resources and move on to next
210 // message. 235 // message.
211 if (sd->RemovePort(msg[i].dart_port)) { 236 intptr_t old_mask = di->Mask();
212 RemoveFromEpollInstance(epoll_fd_, sd); 237 Dart_Port port = msg[i].dart_port;
213 intptr_t fd = sd->fd(); 238 di->RemovePort(port);
214 sd->Close(); 239 intptr_t new_mask = di->Mask();
215 socket_map_.Remove(GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd)); 240 UpdateEpollInstance(old_mask, di);
216 delete sd; 241
242 intptr_t fd = di->fd();
243 if (di->IsListeningSocket()) {
244 // We only close the socket file descriptor from the operating
245 // system if there are no other dart socket objects which
246 // are listening on the same (address, port) combination.
247
248 // TODO: This assumes that all sockets listen before we close.
249 // This needs to be synchronized with a global datastructure.
250 if (new_mask == 0) {
251 socket_map_.Remove(
252 GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd));
253 di->Close();
254 delete di;
255 }
256 } else {
257 ASSERT(new_mask == 0);
258 socket_map_.Remove(
259 GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd));
260 di->Close();
261 delete di;
217 } 262 }
218 DartUtils::PostInt32(msg[i].dart_port, 1 << kDestroyedEvent); 263
264 DartUtils::PostInt32(port, 1 << kDestroyedEvent);
219 } else if (IS_COMMAND(msg[i].data, kReturnTokenCommand)) { 265 } else if (IS_COMMAND(msg[i].data, kReturnTokenCommand)) {
220 int count = TOKEN_COUNT(msg[i].data); 266 int count = TOKEN_COUNT(msg[i].data);
221 if (sd->ReturnToken(msg[i].dart_port, count)) { 267 intptr_t old_mask = di->Mask();
222 AddToEpollInstance(epoll_fd_, sd); 268 di->ReturnTokens(msg[i].dart_port, count);
223 } 269 UpdateEpollInstance(old_mask, di);
224 } else if (IS_COMMAND(msg[i].data, kSetEventMaskCommand)) { 270 } else if (IS_COMMAND(msg[i].data, kSetEventMaskCommand)) {
225 // `events` can only have kInEvent/kOutEvent flags set. 271 // `events` can only have kInEvent/kOutEvent flags set.
226 intptr_t events = msg[i].data & EVENT_MASK; 272 intptr_t events = msg[i].data & EVENT_MASK;
227 ASSERT(0 == (events & ~(1 << kInEvent | 1 << kOutEvent))); 273 ASSERT(0 == (events & ~(1 << kInEvent | 1 << kOutEvent)));
228 274
229 // Setup events to wait for. 275 intptr_t old_mask = di->Mask();
230 if (sd->AddPort(msg[i].dart_port)) { 276 di->SetPortAndMask(msg[i].dart_port, msg[i].data & EVENT_MASK);
231 sd->SetMask(events); 277 UpdateEpollInstance(old_mask, di);
232 AddToEpollInstance(epoll_fd_, sd);
233 }
234 } else { 278 } else {
235 UNREACHABLE(); 279 UNREACHABLE();
236 } 280 }
237 } 281 }
238 } 282 }
239 } 283 }
240 284
241 #ifdef DEBUG_POLL 285 #ifdef DEBUG_POLL
242 static void PrintEventMask(intptr_t fd, intptr_t events) { 286 static void PrintEventMask(intptr_t fd, intptr_t events) {
243 Log::Print("%d ", fd); 287 Log::Print("%d ", fd);
244 if ((events & EPOLLIN) != 0) Log::Print("EPOLLIN "); 288 if ((events & EPOLLIN) != 0) Log::Print("EPOLLIN ");
245 if ((events & EPOLLPRI) != 0) Log::Print("EPOLLPRI "); 289 if ((events & EPOLLPRI) != 0) Log::Print("EPOLLPRI ");
246 if ((events & EPOLLOUT) != 0) Log::Print("EPOLLOUT "); 290 if ((events & EPOLLOUT) != 0) Log::Print("EPOLLOUT ");
247 if ((events & EPOLLERR) != 0) Log::Print("EPOLLERR "); 291 if ((events & EPOLLERR) != 0) Log::Print("EPOLLERR ");
248 if ((events & EPOLLHUP) != 0) Log::Print("EPOLLHUP "); 292 if ((events & EPOLLHUP) != 0) Log::Print("EPOLLHUP ");
249 if ((events & EPOLLRDHUP) != 0) Log::Print("EPOLLRDHUP "); 293 if ((events & EPOLLRDHUP) != 0) Log::Print("EPOLLRDHUP ");
250 int all_events = EPOLLIN | EPOLLPRI | EPOLLOUT | 294 int all_events = EPOLLIN | EPOLLPRI | EPOLLOUT |
251 EPOLLERR | EPOLLHUP | EPOLLRDHUP; 295 EPOLLERR | EPOLLHUP | EPOLLRDHUP;
252 if ((events & ~all_events) != 0) { 296 if ((events & ~all_events) != 0) {
253 Log::Print("(and %08x) ", events & ~all_events); 297 Log::Print("(and %08x) ", events & ~all_events);
254 } 298 }
255 Log::Print("(available %d) ", FDUtils::AvailableBytes(fd)); 299 Log::Print("(available %d) ", FDUtils::AvailableBytes(fd));
256 300
257 Log::Print("\n"); 301 Log::Print("\n");
258 } 302 }
259 #endif 303 #endif
260 304
261 intptr_t EventHandlerImplementation::GetPollEvents(intptr_t events, 305 intptr_t EventHandlerImplementation::GetPollEvents(intptr_t events,
262 SocketData* sd) { 306 DescriptorInfo* di) {
263 #ifdef DEBUG_POLL 307 #ifdef DEBUG_POLL
264 PrintEventMask(sd->fd(), events); 308 PrintEventMask(di->fd(), events);
265 #endif 309 #endif
266 if (events & EPOLLERR) { 310 if (events & EPOLLERR) {
267 // Return error only if EPOLLIN is present. 311 // Return error only if EPOLLIN is present.
268 return (events & EPOLLIN) ? (1 << kErrorEvent) : 0; 312 return (events & EPOLLIN) ? (1 << kErrorEvent) : 0;
269 } 313 }
270 intptr_t event_mask = 0; 314 intptr_t event_mask = 0;
271 if (events & EPOLLIN) event_mask |= (1 << kInEvent); 315 if (events & EPOLLIN) event_mask |= (1 << kInEvent);
272 if (events & EPOLLOUT) event_mask |= (1 << kOutEvent); 316 if (events & EPOLLOUT) event_mask |= (1 << kOutEvent);
273 if (events & (EPOLLHUP | EPOLLRDHUP)) event_mask |= (1 << kCloseEvent); 317 if (events & (EPOLLHUP | EPOLLRDHUP)) event_mask |= (1 << kCloseEvent);
274 return event_mask; 318 return event_mask;
275 } 319 }
276 320
277 321
278 void EventHandlerImplementation::HandleEvents(struct epoll_event* events, 322 void EventHandlerImplementation::HandleEvents(struct epoll_event* events,
279 int size) { 323 int size) {
280 bool interrupt_seen = false; 324 bool interrupt_seen = false;
281 for (int i = 0; i < size; i++) { 325 for (int i = 0; i < size; i++) {
282 if (events[i].data.ptr == NULL) { 326 if (events[i].data.ptr == NULL) {
283 interrupt_seen = true; 327 interrupt_seen = true;
284 } else if (events[i].data.fd == timer_fd_) { 328 } else if (events[i].data.fd == timer_fd_) {
285 int64_t val; 329 int64_t val;
286 VOID_TEMP_FAILURE_RETRY_NO_SIGNAL_BLOCKER( 330 VOID_TEMP_FAILURE_RETRY_NO_SIGNAL_BLOCKER(
287 read(timer_fd_, &val, sizeof(val))); 331 read(timer_fd_, &val, sizeof(val)));
288 if (timeout_queue_.HasTimeout()) { 332 if (timeout_queue_.HasTimeout()) {
289 DartUtils::PostNull(timeout_queue_.CurrentPort()); 333 DartUtils::PostNull(timeout_queue_.CurrentPort());
290 timeout_queue_.RemoveCurrent(); 334 timeout_queue_.RemoveCurrent();
291 } 335 }
292 } else { 336 } else {
293 SocketData* sd = reinterpret_cast<SocketData*>(events[i].data.ptr); 337 DescriptorInfo* di =
294 intptr_t event_mask = GetPollEvents(events[i].events, sd); 338 reinterpret_cast<DescriptorInfo*>(events[i].data.ptr);
339 intptr_t event_mask = GetPollEvents(events[i].events, di);
340
341 if ((event_mask & (1 << kErrorEvent)) != 0) {
342 di->NotifyAllDartPorts(event_mask);
343 }
344 event_mask &= ~(1 << kErrorEvent);
345
295 if (event_mask != 0) { 346 if (event_mask != 0) {
296 Dart_Port port = sd->port(); 347 intptr_t old_mask = di->Mask();
297 if (sd->TakeToken()) { 348 Dart_Port port = di->NextNotifyDartPort(event_mask);
298 // Took last token, remove from epoll.
299 RemoveFromEpollInstance(epoll_fd_, sd);
300 }
301 ASSERT(port != 0); 349 ASSERT(port != 0);
350 UpdateEpollInstance(old_mask, di);
302 DartUtils::PostInt32(port, event_mask); 351 DartUtils::PostInt32(port, event_mask);
303 } 352 }
304 } 353 }
305 } 354 }
306 if (interrupt_seen) { 355 if (interrupt_seen) {
307 // Handle after socket events, so we avoid closing a socket before we handle 356 // Handle after socket events, so we avoid closing a socket before we handle
308 // the current events. 357 // the current events.
309 HandleInterruptFd(); 358 HandleInterruptFd();
310 } 359 }
311 } 360 }
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 413
365 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { 414 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) {
366 // The hashmap does not support keys with value 0. 415 // The hashmap does not support keys with value 0.
367 return dart::Utils::WordHash(fd + 1); 416 return dart::Utils::WordHash(fd + 1);
368 } 417 }
369 418
370 } // namespace bin 419 } // namespace bin
371 } // namespace dart 420 } // namespace dart
372 421
373 #endif // defined(TARGET_OS_LINUX) 422 #endif // defined(TARGET_OS_LINUX)
OLDNEW
« dart/runtime/bin/eventhandler.h ('K') | « dart/runtime/bin/eventhandler_linux.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698