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 "bin/socket.h" | |
24 #include "platform/hashmap.h" | 23 #include "platform/hashmap.h" |
25 #include "platform/thread.h" | 24 #include "platform/thread.h" |
26 #include "platform/utils.h" | 25 #include "platform/utils.h" |
27 | 26 |
28 | 27 |
29 namespace dart { | 28 namespace dart { |
30 namespace bin { | 29 namespace bin { |
31 | 30 |
| 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 | 34 |
34 | 35 |
35 static void AddToEpollInstance(intptr_t epoll_fd_, | 36 intptr_t SocketData::GetPollEvents() { |
36 int fd, Dart_Port port, | 37 // Do not ask for EPOLLERR and EPOLLHUP explicitly as they are |
37 int mask) { | 38 // triggered anyway. |
| 39 intptr_t events = EPOLLET | EPOLLRDHUP; |
| 40 if ((mask_ & (1 << kInEvent)) != 0) { |
| 41 events |= EPOLLIN; |
| 42 } |
| 43 if ((mask_ & (1 << kOutEvent)) != 0) { |
| 44 events |= EPOLLOUT; |
| 45 } |
| 46 return events; |
| 47 } |
| 48 |
| 49 |
| 50 // Unregister the file descriptor for a SocketData structure with epoll. |
| 51 static void RemoveFromEpollInstance(intptr_t epoll_fd_, SocketData* sd) { |
| 52 VOID_TEMP_FAILURE_RETRY(epoll_ctl(epoll_fd_, |
| 53 EPOLL_CTL_DEL, |
| 54 sd->fd(), |
| 55 NULL)); |
| 56 } |
| 57 |
| 58 |
| 59 static void AddToEpollInstance(intptr_t epoll_fd_, SocketData* sd) { |
38 struct epoll_event event; | 60 struct epoll_event event; |
39 event.events = EPOLLET | EPOLLRDHUP; | 61 event.events = sd->GetPollEvents(); |
40 if ((mask & (1 << kInEvent)) != 0) event.events |= EPOLLIN; | 62 event.data.ptr = sd; |
41 if ((mask & (1 << kOutEvent)) != 0) event.events |= EPOLLOUT; | |
42 // Be sure we don't collide with the TIMER_BIT. | |
43 if (port == ILLEGAL_PORT) { | |
44 FATAL("Illigal port sent to event handler"); | |
45 } | |
46 event.data.u64 = port; | |
47 int status = TEMP_FAILURE_RETRY(epoll_ctl(epoll_fd_, | 63 int status = TEMP_FAILURE_RETRY(epoll_ctl(epoll_fd_, |
48 EPOLL_CTL_ADD, | 64 EPOLL_CTL_ADD, |
49 fd, | 65 sd->fd(), |
50 &event)); | 66 &event)); |
51 if (status == -1) { | 67 if (status == -1) { |
52 // 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 |
53 // already closed file descriptor, or unuspported devices, such | 69 // already closed file descriptor, or unuspported devices, such |
54 // 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, |
55 // so dart will handle it accordingly. | 71 // so dart will handle it accordingly. |
56 DartUtils::PostInt32(port, 1 << kCloseEvent); | 72 DartUtils::PostInt32(sd->port(), 1 << kCloseEvent); |
57 } | 73 } |
58 } | 74 } |
59 | 75 |
60 | 76 |
61 EventHandlerImplementation::EventHandlerImplementation() : shutdown_(false) { | 77 EventHandlerImplementation::EventHandlerImplementation() |
| 78 : socket_map_(&HashMap::SamePointerValue, 16) { |
| 79 intptr_t result; |
| 80 result = TEMP_FAILURE_RETRY(pipe(interrupt_fds_)); |
| 81 if (result != 0) { |
| 82 FATAL("Pipe creation failed"); |
| 83 } |
| 84 FDUtils::SetNonBlocking(interrupt_fds_[0]); |
| 85 FDUtils::SetCloseOnExec(interrupt_fds_[0]); |
| 86 FDUtils::SetCloseOnExec(interrupt_fds_[1]); |
| 87 shutdown_ = false; |
62 // The initial size passed to epoll_create is ignore on newer (>= | 88 // The initial size passed to epoll_create is ignore on newer (>= |
63 // 2.6.8) Linux versions | 89 // 2.6.8) Linux versions |
64 static const int kEpollInitialSize = 64; | 90 static const int kEpollInitialSize = 64; |
65 epoll_fd_ = TEMP_FAILURE_RETRY(epoll_create(kEpollInitialSize)); | 91 epoll_fd_ = TEMP_FAILURE_RETRY(epoll_create(kEpollInitialSize)); |
66 if (epoll_fd_ == -1) { | 92 if (epoll_fd_ == -1) { |
67 FATAL1("Failed creating epoll file descriptor: %i", errno); | 93 FATAL1("Failed creating epoll file descriptor: %i", errno); |
68 } | 94 } |
69 FDUtils::SetCloseOnExec(epoll_fd_); | 95 FDUtils::SetCloseOnExec(epoll_fd_); |
| 96 // Register the interrupt_fd with the epoll instance. |
| 97 struct epoll_event event; |
| 98 event.events = EPOLLIN; |
| 99 event.data.ptr = NULL; |
| 100 int status = TEMP_FAILURE_RETRY(epoll_ctl(epoll_fd_, |
| 101 EPOLL_CTL_ADD, |
| 102 interrupt_fds_[0], |
| 103 &event)); |
| 104 if (status == -1) { |
| 105 FATAL("Failed adding interrupt fd to epoll instance"); |
| 106 } |
70 timer_fd_ = TEMP_FAILURE_RETRY(timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC)); | 107 timer_fd_ = TEMP_FAILURE_RETRY(timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC)); |
71 if (timer_fd_ == -1) { | 108 if (timer_fd_ == -1) { |
72 FATAL1("Failed creating timerfd file descriptor: %i", errno); | 109 FATAL1("Failed creating timerfd file descriptor: %i", errno); |
73 } | 110 } |
74 // Register the timer_fd_ with the epoll instance. | 111 // Register the timer_fd_ with the epoll instance. |
75 struct epoll_event event; | |
76 event.events = EPOLLIN; | 112 event.events = EPOLLIN; |
77 event.data.u64 = ILLEGAL_PORT; // Use ILLEGAL_PORT to identify timer-fd. | 113 event.data.fd = timer_fd_; |
78 int status = TEMP_FAILURE_RETRY(epoll_ctl(epoll_fd_, | 114 status = TEMP_FAILURE_RETRY(epoll_ctl(epoll_fd_, |
79 EPOLL_CTL_ADD, | 115 EPOLL_CTL_ADD, |
80 timer_fd_, | 116 timer_fd_, |
81 &event)); | 117 &event)); |
82 if (status == -1) { | 118 if (status == -1) { |
83 FATAL2( | 119 FATAL2( |
84 "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); |
85 } | 121 } |
86 } | 122 } |
87 | 123 |
88 | 124 |
89 EventHandlerImplementation::~EventHandlerImplementation() { | 125 EventHandlerImplementation::~EventHandlerImplementation() { |
90 TEMP_FAILURE_RETRY(close(epoll_fd_)); | 126 VOID_TEMP_FAILURE_RETRY(close(epoll_fd_)); |
91 TEMP_FAILURE_RETRY(close(timer_fd_)); | 127 VOID_TEMP_FAILURE_RETRY(close(timer_fd_)); |
| 128 VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[0])); |
| 129 VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[1])); |
| 130 } |
| 131 |
| 132 |
| 133 SocketData* EventHandlerImplementation::GetSocketData(intptr_t fd) { |
| 134 ASSERT(fd >= 0); |
| 135 HashMap::Entry* entry = socket_map_.Lookup( |
| 136 GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd), true); |
| 137 ASSERT(entry != NULL); |
| 138 SocketData* sd = reinterpret_cast<SocketData*>(entry->value); |
| 139 if (sd == NULL) { |
| 140 // If there is no data in the hash map for this file descriptor a |
| 141 // new SocketData for the file descriptor is inserted. |
| 142 sd = new SocketData(fd); |
| 143 entry->value = sd; |
| 144 } |
| 145 ASSERT(fd == sd->fd()); |
| 146 return sd; |
| 147 } |
| 148 |
| 149 |
| 150 void EventHandlerImplementation::WakeupHandler(intptr_t id, |
| 151 Dart_Port dart_port, |
| 152 int64_t data) { |
| 153 InterruptMessage msg; |
| 154 msg.id = id; |
| 155 msg.dart_port = dart_port; |
| 156 msg.data = data; |
| 157 // WriteToBlocking will write up to 512 bytes atomically, and since our msg |
| 158 // is smaller than 512, we don't need a thread lock. |
| 159 // See: http://linux.die.net/man/7/pipe, section 'Pipe_buf'. |
| 160 ASSERT(kInterruptMessageSize < PIPE_BUF); |
| 161 intptr_t result = |
| 162 FDUtils::WriteToBlocking(interrupt_fds_[1], &msg, kInterruptMessageSize); |
| 163 if (result != kInterruptMessageSize) { |
| 164 if (result == -1) { |
| 165 perror("Interrupt message failure:"); |
| 166 } |
| 167 FATAL1("Interrupt message failure. Wrote %" Pd " bytes.", result); |
| 168 } |
| 169 } |
| 170 |
| 171 |
| 172 void EventHandlerImplementation::HandleInterruptFd() { |
| 173 const intptr_t MAX_MESSAGES = kInterruptMessageSize; |
| 174 InterruptMessage msg[MAX_MESSAGES]; |
| 175 ssize_t bytes = TEMP_FAILURE_RETRY( |
| 176 read(interrupt_fds_[0], msg, MAX_MESSAGES * kInterruptMessageSize)); |
| 177 for (ssize_t i = 0; i < bytes / kInterruptMessageSize; i++) { |
| 178 if (msg[i].id == kTimerId) { |
| 179 timeout_queue_.UpdateTimeout(msg[i].dart_port, msg[i].data); |
| 180 struct itimerspec it; |
| 181 memset(&it, 0, sizeof(it)); |
| 182 if (timeout_queue_.HasTimeout()) { |
| 183 int64_t millis = timeout_queue_.CurrentTimeout(); |
| 184 it.it_value.tv_sec = millis / 1000; |
| 185 it.it_value.tv_nsec = (millis % 1000) * 1000000; |
| 186 } |
| 187 timerfd_settime(timer_fd_, TFD_TIMER_ABSTIME, &it, NULL); |
| 188 } else if (msg[i].id == kShutdownId) { |
| 189 shutdown_ = true; |
| 190 } else { |
| 191 SocketData* sd = GetSocketData(msg[i].id); |
| 192 if ((msg[i].data & (1 << kShutdownReadCommand)) != 0) { |
| 193 ASSERT(msg[i].data == (1 << kShutdownReadCommand)); |
| 194 // Close the socket for reading. |
| 195 shutdown(sd->fd(), SHUT_RD); |
| 196 } else if ((msg[i].data & (1 << kShutdownWriteCommand)) != 0) { |
| 197 ASSERT(msg[i].data == (1 << kShutdownWriteCommand)); |
| 198 // Close the socket for writing. |
| 199 shutdown(sd->fd(), SHUT_WR); |
| 200 } else if ((msg[i].data & (1 << kCloseCommand)) != 0) { |
| 201 ASSERT(msg[i].data == (1 << kCloseCommand)); |
| 202 // Close the socket and free system resources and move on to |
| 203 // next message. |
| 204 RemoveFromEpollInstance(epoll_fd_, sd); |
| 205 intptr_t fd = sd->fd(); |
| 206 sd->Close(); |
| 207 socket_map_.Remove(GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd)); |
| 208 delete sd; |
| 209 DartUtils::PostInt32(msg[i].dart_port, 1 << kDestroyedEvent); |
| 210 } else if ((msg[i].data & (1 << kReturnTokenCommand)) != 0) { |
| 211 if (sd->ReturnToken()) { |
| 212 AddToEpollInstance(epoll_fd_, sd); |
| 213 } |
| 214 } else { |
| 215 // Setup events to wait for. |
| 216 sd->SetPortAndMask(msg[i].dart_port, msg[i].data); |
| 217 AddToEpollInstance(epoll_fd_, sd); |
| 218 } |
| 219 } |
| 220 } |
92 } | 221 } |
93 | 222 |
94 #ifdef DEBUG_POLL | 223 #ifdef DEBUG_POLL |
95 static void PrintEventMask(intptr_t events) { | 224 static void PrintEventMask(intptr_t fd, intptr_t events) { |
96 // TODO(ajohnsen): When DEBUG_POLL is enabled, we could add the fd to the | 225 Log::Print("%d ", fd); |
97 // epoll-data as well. | |
98 if ((events & EPOLLIN) != 0) Log::Print("EPOLLIN "); | 226 if ((events & EPOLLIN) != 0) Log::Print("EPOLLIN "); |
99 if ((events & EPOLLPRI) != 0) Log::Print("EPOLLPRI "); | 227 if ((events & EPOLLPRI) != 0) Log::Print("EPOLLPRI "); |
100 if ((events & EPOLLOUT) != 0) Log::Print("EPOLLOUT "); | 228 if ((events & EPOLLOUT) != 0) Log::Print("EPOLLOUT "); |
101 if ((events & EPOLLERR) != 0) Log::Print("EPOLLERR "); | 229 if ((events & EPOLLERR) != 0) Log::Print("EPOLLERR "); |
102 if ((events & EPOLLHUP) != 0) Log::Print("EPOLLHUP "); | 230 if ((events & EPOLLHUP) != 0) Log::Print("EPOLLHUP "); |
103 if ((events & EPOLLRDHUP) != 0) Log::Print("EPOLLRDHUP "); | 231 if ((events & EPOLLRDHUP) != 0) Log::Print("EPOLLRDHUP "); |
104 int all_events = EPOLLIN | EPOLLPRI | EPOLLOUT | | 232 int all_events = EPOLLIN | EPOLLPRI | EPOLLOUT | |
105 EPOLLERR | EPOLLHUP | EPOLLRDHUP; | 233 EPOLLERR | EPOLLHUP | EPOLLRDHUP; |
106 if ((events & ~all_events) != 0) { | 234 if ((events & ~all_events) != 0) { |
107 Log::Print("(and %08x) ", events & ~all_events); | 235 Log::Print("(and %08x) ", events & ~all_events); |
108 } | 236 } |
| 237 Log::Print("(available %d) ", FDUtils::AvailableBytes(fd)); |
109 | 238 |
110 Log::Print("\n"); | 239 Log::Print("\n"); |
111 } | 240 } |
112 #endif | 241 #endif |
113 | 242 |
114 intptr_t EventHandlerImplementation::GetPollEvents(intptr_t events) { | 243 intptr_t EventHandlerImplementation::GetPollEvents(intptr_t events, |
| 244 SocketData* sd) { |
115 #ifdef DEBUG_POLL | 245 #ifdef DEBUG_POLL |
116 PrintEventMask(events); | 246 PrintEventMask(sd->fd(), events); |
117 #endif | 247 #endif |
118 if (events & EPOLLERR) { | 248 if (events & EPOLLERR) { |
119 // Return only error if EPOLLIN is present. | 249 // Return error only if EPOLLIN is present. |
120 return (events & EPOLLIN) ? (1 << kErrorEvent) : 0; | 250 return (events & EPOLLIN) ? (1 << kErrorEvent) : 0; |
121 } | 251 } |
122 intptr_t event_mask = 0; | 252 intptr_t event_mask = 0; |
123 if (events & EPOLLIN) event_mask |= (1 << kInEvent); | 253 if (events & EPOLLIN) event_mask |= (1 << kInEvent); |
124 if (events & EPOLLOUT) event_mask |= (1 << kOutEvent); | 254 if (events & EPOLLOUT) event_mask |= (1 << kOutEvent); |
125 if (events & (EPOLLHUP | EPOLLRDHUP)) event_mask |= (1 << kCloseEvent); | 255 if (events & (EPOLLHUP | EPOLLRDHUP)) event_mask |= (1 << kCloseEvent); |
126 return event_mask; | 256 return event_mask; |
127 } | 257 } |
128 | 258 |
129 | 259 |
130 void EventHandlerImplementation::HandleEvents(struct epoll_event* events, | 260 void EventHandlerImplementation::HandleEvents(struct epoll_event* events, |
131 int size) { | 261 int size) { |
| 262 bool interrupt_seen = false; |
132 for (int i = 0; i < size; i++) { | 263 for (int i = 0; i < size; i++) { |
133 uint64_t data = events[i].data.u64; | 264 if (events[i].data.ptr == NULL) { |
134 // ILLEGAL_PORT is used to identify timer-fd. | 265 interrupt_seen = true; |
135 if (data == ILLEGAL_PORT) { | 266 } else if (events[i].data.fd == timer_fd_) { |
136 int64_t val; | 267 int64_t val; |
137 VOID_TEMP_FAILURE_RETRY(read(timer_fd_, &val, sizeof(val))); | 268 VOID_TEMP_FAILURE_RETRY(read(timer_fd_, &val, sizeof(val))); |
138 timer_mutex_.Lock(); | |
139 if (timeout_queue_.HasTimeout()) { | 269 if (timeout_queue_.HasTimeout()) { |
140 DartUtils::PostNull(timeout_queue_.CurrentPort()); | 270 DartUtils::PostNull(timeout_queue_.CurrentPort()); |
141 timeout_queue_.RemoveCurrent(); | 271 timeout_queue_.RemoveCurrent(); |
142 } | 272 } |
143 timer_mutex_.Unlock(); | |
144 } else { | 273 } else { |
145 int32_t event_mask = GetPollEvents(events[i].events); | 274 SocketData* sd = reinterpret_cast<SocketData*>(events[i].data.ptr); |
| 275 intptr_t event_mask = GetPollEvents(events[i].events, sd); |
146 if (event_mask != 0) { | 276 if (event_mask != 0) { |
147 Dart_Port port = data; | 277 if (sd->TakeToken()) { |
| 278 // Took last token, remove from epoll. |
| 279 RemoveFromEpollInstance(epoll_fd_, sd); |
| 280 } |
| 281 Dart_Port port = sd->port(); |
148 ASSERT(port != 0); | 282 ASSERT(port != 0); |
149 DartUtils::PostInt32(port, event_mask); | 283 DartUtils::PostInt32(port, event_mask); |
150 } | 284 } |
151 } | 285 } |
152 } | 286 } |
| 287 if (interrupt_seen) { |
| 288 // Handle after socket events, so we avoid closing a socket before we handle |
| 289 // the current events. |
| 290 HandleInterruptFd(); |
| 291 } |
153 } | 292 } |
154 | 293 |
155 | 294 |
156 void EventHandlerImplementation::Poll(uword args) { | 295 void EventHandlerImplementation::Poll(uword args) { |
157 // Main event-handler thread loop. | |
158 static const intptr_t kMaxEvents = 16; | 296 static const intptr_t kMaxEvents = 16; |
159 struct epoll_event events[kMaxEvents]; | 297 struct epoll_event events[kMaxEvents]; |
160 EventHandler* handler = reinterpret_cast<EventHandler*>(args); | 298 EventHandler* handler = reinterpret_cast<EventHandler*>(args); |
161 EventHandlerImplementation* handler_impl = &handler->delegate_; | 299 EventHandlerImplementation* handler_impl = &handler->delegate_; |
162 ASSERT(handler_impl != NULL); | 300 ASSERT(handler_impl != NULL); |
163 while (!handler_impl->shutdown_) { | 301 while (!handler_impl->shutdown_) { |
164 intptr_t result = TEMP_FAILURE_RETRY(epoll_wait(handler_impl->epoll_fd_, | 302 intptr_t result = TEMP_FAILURE_RETRY(epoll_wait(handler_impl->epoll_fd_, |
165 events, | 303 events, |
166 kMaxEvents, | 304 kMaxEvents, |
167 -1)); | 305 -1)); |
(...skipping 13 matching lines...) Expand all Loading... |
181 void EventHandlerImplementation::Start(EventHandler* handler) { | 319 void EventHandlerImplementation::Start(EventHandler* handler) { |
182 int result = dart::Thread::Start(&EventHandlerImplementation::Poll, | 320 int result = dart::Thread::Start(&EventHandlerImplementation::Poll, |
183 reinterpret_cast<uword>(handler)); | 321 reinterpret_cast<uword>(handler)); |
184 if (result != 0) { | 322 if (result != 0) { |
185 FATAL1("Failed to start event handler thread %d", result); | 323 FATAL1("Failed to start event handler thread %d", result); |
186 } | 324 } |
187 } | 325 } |
188 | 326 |
189 | 327 |
190 void EventHandlerImplementation::Shutdown() { | 328 void EventHandlerImplementation::Shutdown() { |
191 shutdown_ = true; | 329 SendData(kShutdownId, 0, 0); |
192 } | 330 } |
193 | 331 |
194 | 332 |
195 void EventHandlerImplementation::Notify(intptr_t id, | 333 void EventHandlerImplementation::SendData(intptr_t id, |
196 Dart_Port dart_port, | 334 Dart_Port dart_port, |
197 int64_t data) { | 335 int64_t data) { |
198 // This method is called by isolates, that is, not in the event-handler | 336 WakeupHandler(id, dart_port, data); |
199 // thread. | 337 } |
200 if (id == kTimerId) { | 338 |
201 // Lock this region, as multiple isolates may attempt to update | 339 |
202 // timeout_queue_. | 340 void* EventHandlerImplementation::GetHashmapKeyFromFd(intptr_t fd) { |
203 // TODO(ajohnsen): Consider using a timer-fd per isolate to avoid the lock. | 341 // The hashmap does not support keys with value 0. |
204 timer_mutex_.Lock(); | 342 return reinterpret_cast<void*>(fd + 1); |
205 timeout_queue_.UpdateTimeout(dart_port, data); | 343 } |
206 struct itimerspec it; | 344 |
207 memset(&it, 0, sizeof(it)); | 345 |
208 if (timeout_queue_.HasTimeout()) { | 346 uint32_t EventHandlerImplementation::GetHashmapHashFromFd(intptr_t fd) { |
209 int64_t millis = timeout_queue_.CurrentTimeout(); | 347 // The hashmap does not support keys with value 0. |
210 it.it_value.tv_sec = millis / 1000; | 348 return dart::Utils::WordHash(fd + 1); |
211 it.it_value.tv_nsec = (millis % 1000) * 1000000; | |
212 } | |
213 timerfd_settime(timer_fd_, TFD_TIMER_ABSTIME, &it, NULL); | |
214 timer_mutex_.Unlock(); | |
215 } else { | |
216 if ((data & (1 << kShutdownReadCommand)) != 0) { | |
217 ASSERT(data == (1 << kShutdownReadCommand)); | |
218 // Close the socket for reading. | |
219 shutdown(id, SHUT_RD); | |
220 } else if ((data & (1 << kShutdownWriteCommand)) != 0) { | |
221 ASSERT(data == (1 << kShutdownWriteCommand)); | |
222 // Close the socket for writing. | |
223 shutdown(id, SHUT_WR); | |
224 } else if ((data & (1 << kCloseCommand)) != 0) { | |
225 ASSERT(data == (1 << kCloseCommand)); | |
226 // Close the socket and free system resources and move on to | |
227 // next message. | |
228 // This will also remove the file descriptor from epoll. | |
229 Socket::Close(id); | |
230 DartUtils::PostInt32(dart_port, 1 << kDestroyedEvent); | |
231 } else { | |
232 // Add to epoll - this is the first time we see it. | |
233 AddToEpollInstance(epoll_fd_, id, dart_port, data); | |
234 } | |
235 } | |
236 } | 349 } |
237 | 350 |
238 } // namespace bin | 351 } // namespace bin |
239 } // namespace dart | 352 } // namespace dart |
240 | 353 |
241 #endif // defined(TARGET_OS_LINUX) | 354 #endif // defined(TARGET_OS_LINUX) |
OLD | NEW |