Chromium Code Reviews| 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 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 namespace dart { | 21 namespace dart { |
| 22 namespace bin { | 22 namespace bin { |
| 23 | 23 |
| 24 class InterruptMessage { | 24 class InterruptMessage { |
| 25 public: | 25 public: |
| 26 intptr_t id; | 26 intptr_t id; |
| 27 Dart_Port dart_port; | 27 Dart_Port dart_port; |
| 28 int64_t data; | 28 int64_t data; |
| 29 }; | 29 }; |
| 30 | 30 |
| 31 template<typename T> | |
| 32 class LinkedList { | |
|
Søren Gjesse
2014/04/25 12:24:44
Maybe call this CircularLinkedList as it is circul
Anders Johnsen
2014/05/01 11:34:50
Done.
| |
| 33 public: | |
| 34 LinkedList() : head_(NULL) {} | |
| 31 | 35 |
| 36 bool Add(T t) { | |
| 37 Entry* e = new Entry(t); | |
| 38 if (head_ == NULL) { | |
| 39 e->next_ = e; | |
| 40 e->prev_ = e; | |
| 41 head_ = e; | |
| 42 return true; | |
| 43 } else { | |
| 44 e->prev_ = head_->prev_; | |
| 45 e->next_ = head_; | |
| 46 e->prev_->next_ = e; | |
| 47 e->next_->prev_ = e; | |
| 48 return false; | |
| 49 } | |
| 50 } | |
| 51 | |
| 52 void RemoveHead() { | |
| 53 Entry* e = head_; | |
| 54 if (e->next_ == e) { | |
| 55 head_ = NULL; | |
| 56 } else { | |
| 57 e->prev_->next_ = e->next_; | |
| 58 e->next_->prev_ = e->prev_; | |
| 59 head_ = e->next_; | |
| 60 } | |
| 61 delete e; | |
| 62 } | |
| 63 | |
| 64 T head() const { return head_->t; } | |
| 65 | |
| 66 bool HasHead() { | |
| 67 return head_ != NULL; | |
| 68 } | |
| 69 | |
| 70 void MoveNext() { | |
| 71 head_ = head_->next_; | |
| 72 } | |
| 73 | |
| 74 private: | |
| 75 struct Entry { | |
| 76 explicit Entry(const T& t) : t(t) {} | |
| 77 const T t; | |
| 78 Entry* next_; | |
| 79 Entry* prev_; | |
| 80 }; | |
| 81 | |
| 82 Entry* head_; | |
| 83 }; | |
| 84 | |
| 85 | |
| 86 class ListeningSocketData; | |
| 32 class SocketData { | 87 class SocketData { |
| 33 public: | 88 public: |
| 34 explicit SocketData(intptr_t fd) : fd_(fd), port_(0), mask_(0), tokens_(16) { | 89 explicit SocketData(intptr_t fd) |
| 90 : fd_(fd), port_(0), mask_(0), tokens_(16) { | |
| 35 ASSERT(fd_ != -1); | 91 ASSERT(fd_ != -1); |
| 36 } | 92 } |
| 37 | 93 |
| 94 virtual ~SocketData() { | |
| 95 } | |
| 96 | |
| 38 intptr_t GetPollEvents(); | 97 intptr_t GetPollEvents(); |
| 39 | 98 |
| 40 void Close() { | 99 void Close() { |
| 41 port_ = 0; | 100 port_ = 0; |
| 42 mask_ = 0; | 101 mask_ = 0; |
| 43 VOID_TEMP_FAILURE_RETRY(close(fd_)); | 102 VOID_TEMP_FAILURE_RETRY(close(fd_)); |
| 44 fd_ = -1; | 103 fd_ = -1; |
| 45 } | 104 } |
| 46 | 105 |
| 47 void SetPortAndMask(Dart_Port port, intptr_t mask) { | 106 void SetMask(intptr_t mask) { |
| 48 ASSERT(fd_ != -1); | 107 ASSERT(fd_ != -1); |
| 49 port_ = port; | |
| 50 mask_ = mask; | 108 mask_ = mask; |
| 51 } | 109 } |
| 52 | 110 |
| 53 intptr_t fd() { return fd_; } | 111 intptr_t fd() { return fd_; } |
| 54 Dart_Port port() { return port_; } | 112 virtual Dart_Port port() { return port_; } |
| 55 | 113 |
| 56 bool IsListeningSocket() { return (mask_ & (1 << kListeningSocket)) != 0; } | 114 virtual bool IsListeningSocket() const { return false; } |
| 115 | |
| 116 virtual bool AddPort(Dart_Port port) { | |
| 117 ASSERT(port_ = 0); | |
| 118 port_ = port; | |
| 119 return true; | |
| 120 } | |
| 121 | |
| 122 virtual bool RemovePort(Dart_Port port) { | |
| 123 ASSERT(port_ == port); | |
| 124 return true; | |
| 125 } | |
| 57 | 126 |
| 58 // Returns true if the last token was taken. | 127 // Returns true if the last token was taken. |
| 59 bool TakeToken() { | 128 virtual bool TakeToken() { |
| 60 ASSERT(tokens_ > 0); | 129 ASSERT(tokens_ > 0); |
| 61 tokens_--; | 130 tokens_--; |
| 62 return tokens_ == 0; | 131 return tokens_ == 0; |
| 63 } | 132 } |
| 64 | 133 |
| 65 // Returns true if the tokens was 0 before adding. | 134 // Returns true if the tokens was 0 before adding. |
| 66 bool ReturnToken() { | 135 virtual bool ReturnToken(Dart_Port port, int count) { |
| 136 ASSERT(port_ == port); | |
| 67 ASSERT(tokens_ >= 0); | 137 ASSERT(tokens_ >= 0); |
| 68 tokens_++; | 138 bool was_empty = tokens_ == 0; |
| 69 return tokens_ == 1; | 139 tokens_ += count; |
| 140 return was_empty; | |
| 70 } | 141 } |
| 71 | 142 |
| 72 private: | 143 bool HasTokens() const { return tokens_ > 0; } |
| 144 | |
| 145 protected: | |
| 73 intptr_t fd_; | 146 intptr_t fd_; |
| 74 Dart_Port port_; | 147 Dart_Port port_; |
| 75 intptr_t mask_; | 148 intptr_t mask_; |
| 76 int tokens_; | 149 int tokens_; |
| 77 }; | 150 }; |
| 78 | 151 |
| 79 | 152 |
| 153 class ListeningSocketData : public SocketData { | |
| 154 private: | |
| 155 static bool SamePortValue(void* key1, void* key2) { | |
| 156 return reinterpret_cast<Dart_Port>(key1) == | |
| 157 reinterpret_cast<Dart_Port>(key2); | |
| 158 } | |
| 159 | |
| 160 static uint32_t GetHashmapHashFromPort(Dart_Port port) { | |
| 161 return static_cast<uint32_t>(port); | |
|
Søren Gjesse
2014/04/25 12:24:44
Use (port & 0xffffffff).
Anders Johnsen
2014/05/01 11:34:50
Done.
| |
| 162 } | |
| 163 | |
| 164 static void* GetHashmapKeyFromPort(Dart_Port port) { | |
| 165 return reinterpret_cast<void*>(port); | |
| 166 } | |
| 167 | |
| 168 public: | |
| 169 explicit ListeningSocketData(intptr_t fd) | |
| 170 : SocketData(fd), | |
| 171 tokens_map_(&SamePortValue, 4) {} | |
| 172 | |
| 173 bool IsListeningSocket() const { return true; } | |
| 174 | |
| 175 bool AddPort(Dart_Port port) { | |
| 176 HashMap::Entry* entry = tokens_map_.Lookup( | |
| 177 GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), true); | |
| 178 entry->value = reinterpret_cast<void*>(4); | |
|
Søren Gjesse
2014/04/25 12:24:44
Please add a constant for this.
Anders Johnsen
2014/05/01 11:34:50
Done.
| |
| 179 return live_ports_.Add(port); | |
| 180 } | |
| 181 | |
| 182 virtual bool RemovePort(Dart_Port port) { | |
| 183 HashMap::Entry* entry = tokens_map_.Lookup( | |
| 184 GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), false); | |
| 185 ASSERT(entry != NULL); | |
| 186 intptr_t tokens = reinterpret_cast<intptr_t>(entry->value); | |
| 187 if (tokens == 0) { | |
| 188 while (idle_ports_.head() != port) { | |
| 189 idle_ports_.MoveNext(); | |
| 190 } | |
| 191 idle_ports_.RemoveHead(); | |
| 192 } else { | |
| 193 while (live_ports_.head() != port) { | |
| 194 live_ports_.MoveNext(); | |
| 195 } | |
| 196 live_ports_.RemoveHead(); | |
| 197 } | |
| 198 tokens_map_.Remove( | |
| 199 GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port)); | |
| 200 return !live_ports_.HasHead(); | |
| 201 } | |
| 202 | |
| 203 bool TakeToken() { | |
| 204 ASSERT(live_ports_.HasHead()); | |
| 205 Dart_Port port = live_ports_.head(); | |
| 206 HashMap::Entry* entry = tokens_map_.Lookup( | |
| 207 GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), false); | |
| 208 ASSERT(entry != NULL); | |
| 209 intptr_t tokens = reinterpret_cast<intptr_t>(entry->value); | |
| 210 tokens--; | |
| 211 entry->value = reinterpret_cast<void*>(tokens); | |
| 212 if (tokens == 0) { | |
| 213 live_ports_.RemoveHead(); | |
| 214 idle_ports_.Add(port); | |
| 215 if (!live_ports_.HasHead()) { | |
| 216 return true; | |
| 217 } | |
| 218 } else { | |
| 219 live_ports_.MoveNext(); | |
| 220 } | |
| 221 return false; | |
| 222 } | |
| 223 | |
| 224 Dart_Port port() { return live_ports_.head(); } | |
| 225 | |
| 226 bool ReturnToken(Dart_Port port, int count) { | |
| 227 HashMap::Entry* entry = tokens_map_.Lookup( | |
| 228 GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), false); | |
| 229 ASSERT(entry != NULL); | |
| 230 intptr_t tokens = reinterpret_cast<intptr_t>(entry->value); | |
| 231 tokens += count; | |
| 232 entry->value = reinterpret_cast<void*>(tokens); | |
| 233 if (tokens == count) { | |
| 234 // Return to live_ports_. | |
| 235 while (idle_ports_.head() != port) { | |
| 236 idle_ports_.MoveNext(); | |
| 237 } | |
| 238 idle_ports_.RemoveHead(); | |
| 239 bool was_empty = !live_ports_.HasHead(); | |
| 240 live_ports_.Add(port); | |
| 241 return was_empty; | |
| 242 } | |
| 243 return false; | |
| 244 } | |
| 245 | |
| 246 private: | |
| 247 LinkedList<Dart_Port> live_ports_; | |
| 248 LinkedList<Dart_Port> idle_ports_; | |
| 249 HashMap tokens_map_; | |
| 250 }; | |
| 251 | |
| 252 | |
| 80 class EventHandlerImplementation { | 253 class EventHandlerImplementation { |
| 81 public: | 254 public: |
| 82 EventHandlerImplementation(); | 255 EventHandlerImplementation(); |
| 83 ~EventHandlerImplementation(); | 256 ~EventHandlerImplementation(); |
| 84 | 257 |
| 85 // Gets the socket data structure for a given file | 258 // Gets the socket data structure for a given file |
| 86 // descriptor. Creates a new one if one is not found. | 259 // descriptor. Creates a new one if one is not found. |
| 87 SocketData* GetSocketData(intptr_t fd); | 260 SocketData* GetSocketData(intptr_t fd, bool is_listening); |
| 88 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); | 261 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); |
| 89 void Start(EventHandler* handler); | 262 void Start(EventHandler* handler); |
| 90 void Shutdown(); | 263 void Shutdown(); |
| 91 | 264 |
| 92 private: | 265 private: |
| 93 void HandleEvents(struct epoll_event* events, int size); | 266 void HandleEvents(struct epoll_event* events, int size); |
| 94 static void Poll(uword args); | 267 static void Poll(uword args); |
| 95 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); | 268 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); |
| 96 void HandleInterruptFd(); | 269 void HandleInterruptFd(); |
| 97 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); | 270 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); |
| 98 intptr_t GetPollEvents(intptr_t events, SocketData* sd); | 271 intptr_t GetPollEvents(intptr_t events, SocketData* sd); |
| 99 static void* GetHashmapKeyFromFd(intptr_t fd); | 272 static void* GetHashmapKeyFromFd(intptr_t fd); |
| 100 static uint32_t GetHashmapHashFromFd(intptr_t fd); | 273 static uint32_t GetHashmapHashFromFd(intptr_t fd); |
| 101 | 274 |
| 102 HashMap socket_map_; | 275 HashMap socket_map_; |
| 103 TimeoutQueue timeout_queue_; | 276 TimeoutQueue timeout_queue_; |
| 104 bool shutdown_; | 277 bool shutdown_; |
| 105 int interrupt_fds_[2]; | 278 int interrupt_fds_[2]; |
| 106 int epoll_fd_; | 279 int epoll_fd_; |
| 107 int timer_fd_; | 280 int timer_fd_; |
| 108 }; | 281 }; |
| 109 | 282 |
| 110 } // namespace bin | 283 } // namespace bin |
| 111 } // namespace dart | 284 } // namespace dart |
| 112 | 285 |
| 113 #endif // BIN_EVENTHANDLER_LINUX_H_ | 286 #endif // BIN_EVENTHANDLER_LINUX_H_ |
| OLD | NEW |