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

Side by Side Diff: runtime/bin/eventhandler_linux.h

Issue 250513002: Add support for cloning server-sockets. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Review feedback. Created 6 years, 7 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
« no previous file with comments | « no previous file | runtime/bin/eventhandler_linux.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #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
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 CircularLinkedList {
33 public:
34 CircularLinkedList() : head_(NULL) {}
31 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;
32 class SocketData { 90 class SocketData {
33 public: 91 public:
34 explicit SocketData(intptr_t fd) : fd_(fd), port_(0), mask_(0), tokens_(16) { 92 explicit SocketData(intptr_t fd)
93 : fd_(fd), port_(0), mask_(0), tokens_(16) {
35 ASSERT(fd_ != -1); 94 ASSERT(fd_ != -1);
36 } 95 }
37 96
97 virtual ~SocketData() {
98 }
99
38 intptr_t GetPollEvents(); 100 intptr_t GetPollEvents();
39 101
40 void Close() { 102 void Close() {
41 port_ = 0; 103 port_ = 0;
42 mask_ = 0; 104 mask_ = 0;
43 VOID_TEMP_FAILURE_RETRY(close(fd_)); 105 VOID_TEMP_FAILURE_RETRY(close(fd_));
44 fd_ = -1; 106 fd_ = -1;
45 } 107 }
46 108
47 void SetPortAndMask(Dart_Port port, intptr_t mask) { 109 void SetMask(intptr_t mask) {
48 ASSERT(fd_ != -1); 110 ASSERT(fd_ != -1);
49 port_ = port;
50 mask_ = mask; 111 mask_ = mask;
51 } 112 }
52 113
53 intptr_t fd() { return fd_; } 114 intptr_t fd() { return fd_; }
54 Dart_Port port() { return port_; } 115 virtual Dart_Port port() { return port_; }
55 116
56 bool IsListeningSocket() { return (mask_ & (1 << kListeningSocket)) != 0; } 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 }
57 129
58 // Returns true if the last token was taken. 130 // Returns true if the last token was taken.
59 bool TakeToken() { 131 virtual bool TakeToken() {
60 ASSERT(tokens_ > 0); 132 ASSERT(tokens_ > 0);
61 tokens_--; 133 tokens_--;
62 return tokens_ == 0; 134 return tokens_ == 0;
63 } 135 }
64 136
65 // Returns true if the tokens was 0 before adding. 137 // Returns true if the tokens was 0 before adding.
66 bool ReturnToken() { 138 virtual bool ReturnToken(Dart_Port port, int count) {
139 ASSERT(port_ == port);
67 ASSERT(tokens_ >= 0); 140 ASSERT(tokens_ >= 0);
68 tokens_++; 141 bool was_empty = tokens_ == 0;
69 return tokens_ == 1; 142 tokens_ += count;
143 return was_empty;
70 } 144 }
71 145
72 private: 146 bool HasTokens() const { return tokens_ > 0; }
147
148 protected:
73 intptr_t fd_; 149 intptr_t fd_;
74 Dart_Port port_; 150 Dart_Port port_;
75 intptr_t mask_; 151 intptr_t mask_;
76 int tokens_; 152 int tokens_;
77 }; 153 };
78 154
79 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 ASSERT(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 return !live_ports_.HasHead();
206 }
207
208 bool TakeToken() {
209 ASSERT(live_ports_.HasHead());
210 Dart_Port port = live_ports_.head();
211 HashMap::Entry* entry = tokens_map_.Lookup(
212 GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), false);
213 ASSERT(entry != NULL);
214 intptr_t tokens = reinterpret_cast<intptr_t>(entry->value);
215 tokens--;
216 entry->value = reinterpret_cast<void*>(tokens);
217 if (tokens == 0) {
218 live_ports_.RemoveHead();
219 idle_ports_.Add(port);
220 if (!live_ports_.HasHead()) {
221 return true;
222 }
223 } else {
224 live_ports_.Rotate();
225 }
226 return false;
227 }
228
229 Dart_Port port() { return live_ports_.head(); }
230
231 bool ReturnToken(Dart_Port port, int count) {
232 HashMap::Entry* entry = tokens_map_.Lookup(
233 GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), false);
234 ASSERT(entry != NULL);
235 intptr_t tokens = reinterpret_cast<intptr_t>(entry->value);
236 tokens += count;
237 entry->value = reinterpret_cast<void*>(tokens);
238 if (tokens == count) {
239 // Return to live_ports_.
240 while (idle_ports_.head() != port) {
241 idle_ports_.Rotate();
242 }
243 idle_ports_.RemoveHead();
244 bool was_empty = !live_ports_.HasHead();
245 live_ports_.Add(port);
246 return was_empty;
247 }
248 return false;
249 }
250
251 private:
252 CircularLinkedList<Dart_Port> live_ports_;
253 CircularLinkedList<Dart_Port> idle_ports_;
254 HashMap tokens_map_;
255 };
256
257
80 class EventHandlerImplementation { 258 class EventHandlerImplementation {
81 public: 259 public:
82 EventHandlerImplementation(); 260 EventHandlerImplementation();
83 ~EventHandlerImplementation(); 261 ~EventHandlerImplementation();
84 262
85 // Gets the socket data structure for a given file 263 // Gets the socket data structure for a given file
86 // descriptor. Creates a new one if one is not found. 264 // descriptor. Creates a new one if one is not found.
87 SocketData* GetSocketData(intptr_t fd); 265 SocketData* GetSocketData(intptr_t fd, bool is_listening);
88 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); 266 void SendData(intptr_t id, Dart_Port dart_port, int64_t data);
89 void Start(EventHandler* handler); 267 void Start(EventHandler* handler);
90 void Shutdown(); 268 void Shutdown();
91 269
92 private: 270 private:
93 void HandleEvents(struct epoll_event* events, int size); 271 void HandleEvents(struct epoll_event* events, int size);
94 static void Poll(uword args); 272 static void Poll(uword args);
95 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); 273 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data);
96 void HandleInterruptFd(); 274 void HandleInterruptFd();
97 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); 275 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask);
98 intptr_t GetPollEvents(intptr_t events, SocketData* sd); 276 intptr_t GetPollEvents(intptr_t events, SocketData* sd);
99 static void* GetHashmapKeyFromFd(intptr_t fd); 277 static void* GetHashmapKeyFromFd(intptr_t fd);
100 static uint32_t GetHashmapHashFromFd(intptr_t fd); 278 static uint32_t GetHashmapHashFromFd(intptr_t fd);
101 279
102 HashMap socket_map_; 280 HashMap socket_map_;
103 TimeoutQueue timeout_queue_; 281 TimeoutQueue timeout_queue_;
104 bool shutdown_; 282 bool shutdown_;
105 int interrupt_fds_[2]; 283 int interrupt_fds_[2];
106 int epoll_fd_; 284 int epoll_fd_;
107 int timer_fd_; 285 int timer_fd_;
108 }; 286 };
109 287
110 } // namespace bin 288 } // namespace bin
111 } // namespace dart 289 } // namespace dart
112 290
113 #endif // BIN_EVENTHANDLER_LINUX_H_ 291 #endif // BIN_EVENTHANDLER_LINUX_H_
OLDNEW
« no previous file with comments | « no previous file | runtime/bin/eventhandler_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698