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 #ifndef BIN_EVENTHANDLER_LINUX_H_ | 5 #ifndef BIN_EVENTHANDLER_LINUX_H_ |
6 #define BIN_EVENTHANDLER_LINUX_H_ | 6 #define BIN_EVENTHANDLER_LINUX_H_ |
7 | 7 |
8 #if !defined(BIN_EVENTHANDLER_H_) | 8 #if !defined(BIN_EVENTHANDLER_H_) |
9 #error Do not include eventhandler_linux.h directly; use eventhandler.h instead. | 9 #error Do not include eventhandler_linux.h directly; use eventhandler.h instead. |
10 #endif | 10 #endif |
11 | 11 |
12 #include <errno.h> | 12 #include <errno.h> |
13 #include <sys/epoll.h> | 13 #include <sys/epoll.h> |
14 #include <sys/socket.h> | 14 #include <sys/socket.h> |
15 #include <unistd.h> | 15 #include <unistd.h> |
16 | 16 |
17 #include "platform/hashmap.h" | 17 #include "platform/hashmap.h" |
18 #include "platform/signal_blocker.h" | 18 #include "platform/signal_blocker.h" |
19 | 19 |
20 | 20 |
21 namespace dart { | 21 namespace dart { |
22 namespace bin { | 22 namespace bin { |
23 | 23 |
24 class InterruptMessage { | 24 class DescriptorInfo : public DescriptorInfoBase { |
25 public: | 25 public: |
26 intptr_t id; | 26 explicit DescriptorInfo(intptr_t fd) : DescriptorInfoBase(fd) { } |
27 Dart_Port dart_port; | |
28 int64_t data; | |
29 }; | |
30 | 27 |
31 template<typename T> | 28 virtual ~DescriptorInfo() { } |
32 class CircularLinkedList { | |
33 public: | |
34 CircularLinkedList() : head_(NULL) {} | |
35 | 29 |
36 // Returns true if the list was empty. | 30 intptr_t GetPollEvents(); |
37 bool Add(T t) { | 31 |
38 Entry* e = new Entry(t); | 32 virtual void Close() { |
39 if (head_ == NULL) { | 33 VOID_TEMP_FAILURE_RETRY(close(fd_)); |
40 // Empty list, make e head, and point to itself. | 34 fd_ = -1; |
41 e->next_ = e; | |
42 e->prev_ = e; | |
43 head_ = e; | |
44 return true; | |
45 } else { | |
46 // Insert e as the last element in the list. | |
47 e->prev_ = head_->prev_; | |
48 e->next_ = head_; | |
49 e->prev_->next_ = e; | |
50 head_->prev_ = e; | |
51 return false; | |
52 } | |
53 } | 35 } |
54 | |
55 void RemoveHead() { | |
56 Entry* e = head_; | |
57 if (e->next_ == e) { | |
58 head_ = NULL; | |
59 } else { | |
60 e->prev_->next_ = e->next_; | |
61 e->next_->prev_ = e->prev_; | |
62 head_ = e->next_; | |
63 } | |
64 delete e; | |
65 } | |
66 | |
67 T head() const { return head_->t; } | |
68 | |
69 bool HasHead() { | |
70 return head_ != NULL; | |
71 } | |
72 | |
73 void Rotate() { | |
74 head_ = head_->next_; | |
75 } | |
76 | |
77 private: | |
78 struct Entry { | |
79 explicit Entry(const T& t) : t(t) {} | |
80 const T t; | |
81 Entry* next_; | |
82 Entry* prev_; | |
83 }; | |
84 | |
85 Entry* head_; | |
86 }; | 36 }; |
87 | 37 |
88 | 38 |
89 class ListeningSocketData; | 39 class DescriptorInfoSingle |
90 class SocketData { | 40 : public DescriptorInfoSingleMixin<DescriptorInfo> { |
91 public: | 41 public: |
92 explicit SocketData(intptr_t fd) | 42 explicit DescriptorInfoSingle(intptr_t fd) |
93 : fd_(fd), port_(0), mask_(0), tokens_(16) { | 43 : DescriptorInfoSingleMixin(fd) {} |
94 ASSERT(fd_ != -1); | 44 virtual ~DescriptorInfoSingle() {} |
95 } | |
96 | |
97 virtual ~SocketData() { | |
98 } | |
99 | |
100 intptr_t GetPollEvents(); | |
101 | |
102 void Close() { | |
103 port_ = 0; | |
104 mask_ = 0; | |
105 VOID_TEMP_FAILURE_RETRY(close(fd_)); | |
106 fd_ = -1; | |
107 } | |
108 | |
109 void SetMask(intptr_t mask) { | |
110 ASSERT(fd_ != -1); | |
111 mask_ = mask; | |
112 } | |
113 | |
114 intptr_t fd() { return fd_; } | |
115 virtual Dart_Port port() { return port_; } | |
116 | |
117 virtual bool IsListeningSocket() const { return false; } | |
118 | |
119 virtual bool AddPort(Dart_Port port) { | |
120 ASSERT(port_ == 0); | |
121 port_ = port; | |
122 return true; | |
123 } | |
124 | |
125 virtual bool RemovePort(Dart_Port port) { | |
126 ASSERT(port_ == 0 || port_ == port); | |
127 return true; | |
128 } | |
129 | |
130 // Returns true if the last token was taken. | |
131 virtual bool TakeToken() { | |
132 ASSERT(tokens_ > 0); | |
133 tokens_--; | |
134 return tokens_ == 0; | |
135 } | |
136 | |
137 // Returns true if the tokens was 0 before adding. | |
138 virtual bool ReturnToken(Dart_Port port, int count) { | |
139 ASSERT(port_ == port); | |
140 ASSERT(tokens_ >= 0); | |
141 bool was_empty = tokens_ == 0; | |
142 tokens_ += count; | |
143 return was_empty; | |
144 } | |
145 | |
146 bool HasTokens() const { return tokens_ > 0; } | |
147 | |
148 protected: | |
149 intptr_t fd_; | |
150 Dart_Port port_; | |
151 intptr_t mask_; | |
152 int tokens_; | |
153 }; | 45 }; |
154 | 46 |
155 | 47 |
156 class ListeningSocketData : public SocketData { | 48 class DescriptorInfoMultiple |
157 private: | 49 : public DescriptorInfoMultipleMixin<DescriptorInfo> { |
158 static const int kTokenCount = 4; | |
159 | |
160 static bool SamePortValue(void* key1, void* key2) { | |
161 return reinterpret_cast<Dart_Port>(key1) == | |
162 reinterpret_cast<Dart_Port>(key2); | |
163 } | |
164 | |
165 static uint32_t GetHashmapHashFromPort(Dart_Port port) { | |
166 return static_cast<uint32_t>(port & 0xFFFFFFFF); | |
167 } | |
168 | |
169 static void* GetHashmapKeyFromPort(Dart_Port port) { | |
170 return reinterpret_cast<void*>(port); | |
171 } | |
172 | |
173 public: | 50 public: |
174 explicit ListeningSocketData(intptr_t fd) | 51 explicit DescriptorInfoMultiple(intptr_t fd) |
175 : SocketData(fd), | 52 : DescriptorInfoMultipleMixin(fd) {} |
176 tokens_map_(&SamePortValue, 4) {} | 53 virtual ~DescriptorInfoMultiple() {} |
177 | |
178 bool IsListeningSocket() const { return true; } | |
179 | |
180 bool AddPort(Dart_Port port) { | |
181 HashMap::Entry* entry = tokens_map_.Lookup( | |
182 GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), true); | |
183 entry->value = reinterpret_cast<void*>(kTokenCount); | |
184 return live_ports_.Add(port); | |
185 } | |
186 | |
187 virtual bool RemovePort(Dart_Port port) { | |
188 HashMap::Entry* entry = tokens_map_.Lookup( | |
189 GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), false); | |
190 if (entry != NULL) { | |
191 intptr_t tokens = reinterpret_cast<intptr_t>(entry->value); | |
192 if (tokens == 0) { | |
193 while (idle_ports_.head() != port) { | |
194 idle_ports_.Rotate(); | |
195 } | |
196 idle_ports_.RemoveHead(); | |
197 } else { | |
198 while (live_ports_.head() != port) { | |
199 live_ports_.Rotate(); | |
200 } | |
201 live_ports_.RemoveHead(); | |
202 } | |
203 tokens_map_.Remove( | |
204 GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port)); | |
205 } else { | |
206 // NOTE: This is a listening socket which has been immediately closed. | |
207 // | |
208 // If a listening socket is not listened on, the event handler does not | |
209 // know about it beforehand. So the first time the event handler knows | |
210 // about it, is when it is supposed to be closed. We therefore do nothing | |
211 // here. | |
212 // | |
213 // But whether to close it, depends on whether other isolates have it open | |
214 // as well or not. | |
215 } | |
216 return !live_ports_.HasHead(); | |
217 } | |
218 | |
219 bool TakeToken() { | |
220 ASSERT(live_ports_.HasHead()); | |
221 Dart_Port port = live_ports_.head(); | |
222 HashMap::Entry* entry = tokens_map_.Lookup( | |
223 GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), false); | |
224 ASSERT(entry != NULL); | |
225 intptr_t tokens = reinterpret_cast<intptr_t>(entry->value); | |
226 tokens--; | |
227 entry->value = reinterpret_cast<void*>(tokens); | |
228 if (tokens == 0) { | |
229 live_ports_.RemoveHead(); | |
230 idle_ports_.Add(port); | |
231 if (!live_ports_.HasHead()) { | |
232 return true; | |
233 } | |
234 } else { | |
235 live_ports_.Rotate(); | |
236 } | |
237 return false; | |
238 } | |
239 | |
240 Dart_Port port() { return live_ports_.head(); } | |
241 | |
242 bool ReturnToken(Dart_Port port, int count) { | |
243 HashMap::Entry* entry = tokens_map_.Lookup( | |
244 GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), false); | |
245 ASSERT(entry != NULL); | |
246 intptr_t tokens = reinterpret_cast<intptr_t>(entry->value); | |
247 tokens += count; | |
248 entry->value = reinterpret_cast<void*>(tokens); | |
249 if (tokens == count) { | |
250 // Return to live_ports_. | |
251 while (idle_ports_.head() != port) { | |
252 idle_ports_.Rotate(); | |
253 } | |
254 idle_ports_.RemoveHead(); | |
255 bool was_empty = !live_ports_.HasHead(); | |
256 live_ports_.Add(port); | |
257 return was_empty; | |
258 } | |
259 return false; | |
260 } | |
261 | |
262 private: | |
263 CircularLinkedList<Dart_Port> live_ports_; | |
264 CircularLinkedList<Dart_Port> idle_ports_; | |
265 HashMap tokens_map_; | |
266 }; | 54 }; |
267 | 55 |
268 | 56 |
269 class EventHandlerImplementation { | 57 class EventHandlerImplementation { |
270 public: | 58 public: |
271 EventHandlerImplementation(); | 59 EventHandlerImplementation(); |
272 ~EventHandlerImplementation(); | 60 ~EventHandlerImplementation(); |
273 | 61 |
274 // Gets the socket data structure for a given file | 62 // Gets the socket data structure for a given file |
275 // descriptor. Creates a new one if one is not found. | 63 // descriptor. Creates a new one if one is not found. |
276 SocketData* GetSocketData(intptr_t fd, bool is_listening); | 64 DescriptorInfo* GetDescriptorInfo(intptr_t fd, bool is_listening); |
277 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); | 65 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); |
278 void Start(EventHandler* handler); | 66 void Start(EventHandler* handler); |
279 void Shutdown(); | 67 void Shutdown(); |
280 | 68 |
281 private: | 69 private: |
282 void HandleEvents(struct epoll_event* events, int size); | 70 void HandleEvents(struct epoll_event* events, int size); |
283 static void Poll(uword args); | 71 static void Poll(uword args); |
284 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); | 72 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); |
285 void HandleInterruptFd(); | 73 void HandleInterruptFd(); |
286 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); | 74 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); |
287 intptr_t GetPollEvents(intptr_t events, SocketData* sd); | 75 intptr_t GetPollEvents(intptr_t events, DescriptorInfo* sd); |
288 static void* GetHashmapKeyFromFd(intptr_t fd); | 76 static void* GetHashmapKeyFromFd(intptr_t fd); |
289 static uint32_t GetHashmapHashFromFd(intptr_t fd); | 77 static uint32_t GetHashmapHashFromFd(intptr_t fd); |
290 | 78 |
291 HashMap socket_map_; | 79 HashMap socket_map_; |
292 TimeoutQueue timeout_queue_; | 80 TimeoutQueue timeout_queue_; |
293 bool shutdown_; | 81 bool shutdown_; |
294 int interrupt_fds_[2]; | 82 int interrupt_fds_[2]; |
295 int epoll_fd_; | 83 int epoll_fd_; |
296 int timer_fd_; | 84 int timer_fd_; |
297 }; | 85 }; |
298 | 86 |
299 } // namespace bin | 87 } // namespace bin |
300 } // namespace dart | 88 } // namespace dart |
301 | 89 |
302 #endif // BIN_EVENTHANDLER_LINUX_H_ | 90 #endif // BIN_EVENTHANDLER_LINUX_H_ |
OLD | NEW |