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

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

Issue 905733002: Extract common Mask/Dart_Port settings of linux event handler implementation to eventhandler.h (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 5 years, 10 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
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
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 ListeningSocketData; 24 class DescriptorInfo : public DescriptorInfoBase {
25 class SocketData {
26 public: 25 public:
27 explicit SocketData(intptr_t fd) 26 explicit DescriptorInfo(intptr_t fd) : DescriptorInfoBase(fd) { }
28 : fd_(fd), port_(0), mask_(0), tokens_(16) {
29 ASSERT(fd_ != -1);
30 }
31 27
32 virtual ~SocketData() { 28 virtual ~DescriptorInfo() { }
33 }
34 29
35 intptr_t GetPollEvents(); 30 intptr_t GetPollEvents();
36 31
37 void Close() { 32 virtual void Close() {
38 port_ = 0;
39 mask_ = 0;
40 VOID_TEMP_FAILURE_RETRY(close(fd_)); 33 VOID_TEMP_FAILURE_RETRY(close(fd_));
41 fd_ = -1; 34 fd_ = -1;
42 } 35 }
43
44 void SetMask(intptr_t mask) {
45 ASSERT(fd_ != -1);
46 mask_ = mask;
47 }
48
49 intptr_t fd() { return fd_; }
50 virtual Dart_Port port() { return port_; }
51
52 virtual bool IsListeningSocket() const { return false; }
53
54 virtual bool AddPort(Dart_Port port) {
55 ASSERT(port_ == 0);
56 port_ = port;
57 return true;
58 }
59
60 virtual bool RemovePort(Dart_Port port) {
61 ASSERT(port_ == 0 || port_ == port);
62 return true;
63 }
64
65 // Returns true if the last token was taken.
66 virtual bool TakeToken() {
67 ASSERT(tokens_ > 0);
68 tokens_--;
69 return tokens_ == 0;
70 }
71
72 // Returns true if the tokens was 0 before adding.
73 virtual bool ReturnToken(Dart_Port port, int count) {
74 ASSERT(port_ == port);
75 ASSERT(tokens_ >= 0);
76 bool was_empty = tokens_ == 0;
77 tokens_ += count;
78 return was_empty;
79 }
80
81 bool HasTokens() const { return tokens_ > 0; }
82
83 protected:
84 intptr_t fd_;
85 Dart_Port port_;
86 intptr_t mask_;
87 int tokens_;
88 }; 36 };
89 37
90 38
91 class ListeningSocketData : public SocketData { 39 class DescriptorInfoSingle
92 private: 40 : public DescriptorInfoSingleMixin<DescriptorInfo> {
93 static const int kTokenCount = 4;
94
95 static bool SamePortValue(void* key1, void* key2) {
96 return reinterpret_cast<Dart_Port>(key1) ==
97 reinterpret_cast<Dart_Port>(key2);
98 }
99
100 static uint32_t GetHashmapHashFromPort(Dart_Port port) {
101 return static_cast<uint32_t>(port & 0xFFFFFFFF);
102 }
103
104 static void* GetHashmapKeyFromPort(Dart_Port port) {
105 return reinterpret_cast<void*>(port);
106 }
107
108 public: 41 public:
109 explicit ListeningSocketData(intptr_t fd) 42 explicit DescriptorInfoSingle(intptr_t fd)
110 : SocketData(fd), 43 : DescriptorInfoSingleMixin(fd) {}
111 tokens_map_(&SamePortValue, 4) {} 44 virtual ~DescriptorInfoSingle() {}
112
113 bool IsListeningSocket() const { return true; }
114
115 bool AddPort(Dart_Port port) {
116 HashMap::Entry* entry = tokens_map_.Lookup(
117 GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), true);
118 entry->value = reinterpret_cast<void*>(kTokenCount);
119 return live_ports_.Add(port);
120 }
121
122 virtual bool RemovePort(Dart_Port port) {
123 HashMap::Entry* entry = tokens_map_.Lookup(
124 GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), false);
125 if (entry != NULL) {
126 intptr_t tokens = reinterpret_cast<intptr_t>(entry->value);
127 if (tokens == 0) {
128 while (idle_ports_.head() != port) {
129 idle_ports_.Rotate();
130 }
131 idle_ports_.RemoveHead();
132 } else {
133 while (live_ports_.head() != port) {
134 live_ports_.Rotate();
135 }
136 live_ports_.RemoveHead();
137 }
138 tokens_map_.Remove(
139 GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port));
140 } else {
141 // NOTE: This is a listening socket which has been immediately closed.
142 //
143 // If a listening socket is not listened on, the event handler does not
144 // know about it beforehand. So the first time the event handler knows
145 // about it, is when it is supposed to be closed. We therefore do nothing
146 // here.
147 //
148 // But whether to close it, depends on whether other isolates have it open
149 // as well or not.
150 }
151 return !live_ports_.HasHead();
152 }
153
154 bool TakeToken() {
155 ASSERT(live_ports_.HasHead());
156 Dart_Port port = live_ports_.head();
157 HashMap::Entry* entry = tokens_map_.Lookup(
158 GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), false);
159 ASSERT(entry != NULL);
160 intptr_t tokens = reinterpret_cast<intptr_t>(entry->value);
161 tokens--;
162 entry->value = reinterpret_cast<void*>(tokens);
163 if (tokens == 0) {
164 live_ports_.RemoveHead();
165 idle_ports_.Add(port);
166 if (!live_ports_.HasHead()) {
167 return true;
168 }
169 } else {
170 live_ports_.Rotate();
171 }
172 return false;
173 }
174
175 Dart_Port port() { return live_ports_.head(); }
176
177 bool ReturnToken(Dart_Port port, int count) {
178 HashMap::Entry* entry = tokens_map_.Lookup(
179 GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), false);
180 ASSERT(entry != NULL);
181 intptr_t tokens = reinterpret_cast<intptr_t>(entry->value);
182 tokens += count;
183 entry->value = reinterpret_cast<void*>(tokens);
184 if (tokens == count) {
185 // Return to live_ports_.
186 while (idle_ports_.head() != port) {
187 idle_ports_.Rotate();
188 }
189 idle_ports_.RemoveHead();
190 bool was_empty = !live_ports_.HasHead();
191 live_ports_.Add(port);
192 return was_empty;
193 }
194 return false;
195 }
196
197 private:
198 CircularLinkedList<Dart_Port> live_ports_;
199 CircularLinkedList<Dart_Port> idle_ports_;
200 HashMap tokens_map_;
201 }; 45 };
202 46
203 47
48 class DescriptorInfoMultiple
49 : public DescriptorInfoMultipleMixin<DescriptorInfo> {
50 public:
51 explicit DescriptorInfoMultiple(intptr_t fd)
52 : DescriptorInfoMultipleMixin(fd) {}
53 virtual ~DescriptorInfoMultiple() {}
54 };
55
56
204 class EventHandlerImplementation { 57 class EventHandlerImplementation {
205 public: 58 public:
206 EventHandlerImplementation(); 59 EventHandlerImplementation();
207 ~EventHandlerImplementation(); 60 ~EventHandlerImplementation();
208 61
62 void UpdateEpollInstance(intptr_t old_mask, DescriptorInfo *di);
63
209 // Gets the socket data structure for a given file 64 // Gets the socket data structure for a given file
210 // descriptor. Creates a new one if one is not found. 65 // descriptor. Creates a new one if one is not found.
211 SocketData* GetSocketData(intptr_t fd, bool is_listening); 66 DescriptorInfo* GetDescriptorInfo(intptr_t fd, bool is_listening);
212 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); 67 void SendData(intptr_t id, Dart_Port dart_port, int64_t data);
213 void Start(EventHandler* handler); 68 void Start(EventHandler* handler);
214 void Shutdown(); 69 void Shutdown();
215 70
216 private: 71 private:
217 void HandleEvents(struct epoll_event* events, int size); 72 void HandleEvents(struct epoll_event* events, int size);
218 static void Poll(uword args); 73 static void Poll(uword args);
219 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); 74 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data);
220 void HandleInterruptFd(); 75 void HandleInterruptFd();
221 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); 76 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask);
222 intptr_t GetPollEvents(intptr_t events, SocketData* sd); 77 intptr_t GetPollEvents(intptr_t events, DescriptorInfo* di);
223 static void* GetHashmapKeyFromFd(intptr_t fd); 78 static void* GetHashmapKeyFromFd(intptr_t fd);
224 static uint32_t GetHashmapHashFromFd(intptr_t fd); 79 static uint32_t GetHashmapHashFromFd(intptr_t fd);
225 80
226 HashMap socket_map_; 81 HashMap socket_map_;
227 TimeoutQueue timeout_queue_; 82 TimeoutQueue timeout_queue_;
228 bool shutdown_; 83 bool shutdown_;
229 int interrupt_fds_[2]; 84 int interrupt_fds_[2];
230 int epoll_fd_; 85 int epoll_fd_;
231 int timer_fd_; 86 int timer_fd_;
232 }; 87 };
233 88
234 } // namespace bin 89 } // namespace bin
235 } // namespace dart 90 } // namespace dart
236 91
237 #endif // BIN_EVENTHANDLER_LINUX_H_ 92 #endif // BIN_EVENTHANDLER_LINUX_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698