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 DescriptorInfo : public DescriptorInfoBase { | 24 class InterruptMessage { |
25 public: | 25 public: |
26 explicit DescriptorInfo(intptr_t fd) : DescriptorInfoBase(fd) { } | 26 intptr_t id; |
27 | 27 Dart_Port dart_port; |
28 virtual ~DescriptorInfo() { } | 28 int64_t data; |
| 29 }; |
| 30 |
| 31 template<typename T> |
| 32 class CircularLinkedList { |
| 33 public: |
| 34 CircularLinkedList() : head_(NULL) {} |
| 35 |
| 36 // Returns true if the list was empty. |
| 37 bool Add(T t) { |
| 38 Entry* e = new Entry(t); |
| 39 if (head_ == NULL) { |
| 40 // Empty list, make e head, and point to itself. |
| 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 } |
| 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 }; |
| 87 |
| 88 |
| 89 class ListeningSocketData; |
| 90 class SocketData { |
| 91 public: |
| 92 explicit SocketData(intptr_t fd) |
| 93 : fd_(fd), port_(0), mask_(0), tokens_(16) { |
| 94 ASSERT(fd_ != -1); |
| 95 } |
| 96 |
| 97 virtual ~SocketData() { |
| 98 } |
29 | 99 |
30 intptr_t GetPollEvents(); | 100 intptr_t GetPollEvents(); |
31 | 101 |
32 virtual void Close() { | 102 void Close() { |
| 103 port_ = 0; |
| 104 mask_ = 0; |
33 VOID_TEMP_FAILURE_RETRY(close(fd_)); | 105 VOID_TEMP_FAILURE_RETRY(close(fd_)); |
34 fd_ = -1; | 106 fd_ = -1; |
35 } | 107 } |
36 }; | 108 |
37 | 109 void SetMask(intptr_t mask) { |
38 | 110 ASSERT(fd_ != -1); |
39 class DescriptorInfoSingle | 111 mask_ = mask; |
40 : public DescriptorInfoSingleMixin<DescriptorInfo> { | 112 } |
41 public: | 113 |
42 explicit DescriptorInfoSingle(intptr_t fd) | 114 intptr_t fd() { return fd_; } |
43 : DescriptorInfoSingleMixin(fd) {} | 115 virtual Dart_Port port() { return port_; } |
44 virtual ~DescriptorInfoSingle() {} | 116 |
45 }; | 117 virtual bool IsListeningSocket() const { return false; } |
46 | 118 |
47 | 119 virtual bool AddPort(Dart_Port port) { |
48 class DescriptorInfoMultiple | 120 ASSERT(port_ == 0); |
49 : public DescriptorInfoMultipleMixin<DescriptorInfo> { | 121 port_ = port; |
50 public: | 122 return true; |
51 explicit DescriptorInfoMultiple(intptr_t fd) | 123 } |
52 : DescriptorInfoMultipleMixin(fd) {} | 124 |
53 virtual ~DescriptorInfoMultiple() {} | 125 virtual bool RemovePort(Dart_Port port) { |
54 }; | 126 ASSERT(port_ == 0 || port_ == port); |
55 | 127 return true; |
56 | 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 }; |
| 154 |
| 155 |
| 156 class ListeningSocketData : public SocketData { |
| 157 private: |
| 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: |
| 174 explicit ListeningSocketData(intptr_t fd) |
| 175 : SocketData(fd), |
| 176 tokens_map_(&SamePortValue, 4) {} |
| 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 }; |
| 267 |
| 268 |
57 class EventHandlerImplementation { | 269 class EventHandlerImplementation { |
58 public: | 270 public: |
59 EventHandlerImplementation(); | 271 EventHandlerImplementation(); |
60 ~EventHandlerImplementation(); | 272 ~EventHandlerImplementation(); |
61 | 273 |
62 // Gets the socket data structure for a given file | 274 // Gets the socket data structure for a given file |
63 // descriptor. Creates a new one if one is not found. | 275 // descriptor. Creates a new one if one is not found. |
64 DescriptorInfo* GetDescriptorInfo(intptr_t fd, bool is_listening); | 276 SocketData* GetSocketData(intptr_t fd, bool is_listening); |
65 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); | 277 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); |
66 void Start(EventHandler* handler); | 278 void Start(EventHandler* handler); |
67 void Shutdown(); | 279 void Shutdown(); |
68 | 280 |
69 private: | 281 private: |
70 void HandleEvents(struct epoll_event* events, int size); | 282 void HandleEvents(struct epoll_event* events, int size); |
71 static void Poll(uword args); | 283 static void Poll(uword args); |
72 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); | 284 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); |
73 void HandleInterruptFd(); | 285 void HandleInterruptFd(); |
74 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); | 286 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); |
75 intptr_t GetPollEvents(intptr_t events, DescriptorInfo* sd); | 287 intptr_t GetPollEvents(intptr_t events, SocketData* sd); |
76 static void* GetHashmapKeyFromFd(intptr_t fd); | 288 static void* GetHashmapKeyFromFd(intptr_t fd); |
77 static uint32_t GetHashmapHashFromFd(intptr_t fd); | 289 static uint32_t GetHashmapHashFromFd(intptr_t fd); |
78 | 290 |
79 HashMap socket_map_; | 291 HashMap socket_map_; |
80 TimeoutQueue timeout_queue_; | 292 TimeoutQueue timeout_queue_; |
81 bool shutdown_; | 293 bool shutdown_; |
82 int interrupt_fds_[2]; | 294 int interrupt_fds_[2]; |
83 int epoll_fd_; | 295 int epoll_fd_; |
84 int timer_fd_; | 296 int timer_fd_; |
85 }; | 297 }; |
86 | 298 |
87 } // namespace bin | 299 } // namespace bin |
88 } // namespace dart | 300 } // namespace dart |
89 | 301 |
90 #endif // BIN_EVENTHANDLER_LINUX_H_ | 302 #endif // BIN_EVENTHANDLER_LINUX_H_ |
OLD | NEW |