Index: dart/runtime/bin/eventhandler.h |
diff --git a/dart/runtime/bin/eventhandler.h b/dart/runtime/bin/eventhandler.h |
index 80efaca65775a1c88f9dc4eba0ee7cf391872cda..183310ebc4c2e9e90b744102c8dfaa97813e6194 100644 |
--- a/dart/runtime/bin/eventhandler.h |
+++ b/dart/runtime/bin/eventhandler.h |
@@ -8,6 +8,8 @@ |
#include "bin/builtin.h" |
#include "bin/isolate_data.h" |
+#include "platform/hashmap.h" |
+ |
namespace dart { |
namespace bin { |
@@ -94,6 +96,333 @@ class TimeoutQueue { |
Timeout* timeouts_; |
}; |
+ |
+template<typename T> |
+class CircularLinkedList { |
+ public: |
+ CircularLinkedList() : head_(NULL) {} |
+ |
+ // Returns true if the list was empty. |
+ bool Add(T t) { |
+ Entry* e = new Entry(t); |
+ if (head_ == NULL) { |
+ // Empty list, make e head, and point to itself. |
+ e->next_ = e; |
+ e->prev_ = e; |
+ head_ = e; |
+ return true; |
+ } else { |
+ // Insert e as the last element in the list. |
+ e->prev_ = head_->prev_; |
+ e->next_ = head_; |
+ e->prev_->next_ = e; |
+ head_->prev_ = e; |
+ return false; |
+ } |
+ } |
+ |
+ void RemoveHead() { |
+ Entry* e = head_; |
+ if (e->next_ == e) { |
+ head_ = NULL; |
+ } else { |
+ e->prev_->next_ = e->next_; |
+ e->next_->prev_ = e->prev_; |
+ head_ = e->next_; |
+ } |
+ delete e; |
+ } |
+ |
+ T head() const { return head_->t; } |
+ |
+ bool HasHead() const { |
+ return head_ != NULL; |
+ } |
+ |
+ void Rotate() { |
+ head_ = head_->next_; |
+ } |
+ |
+ private: |
+ struct Entry { |
+ explicit Entry(const T& t) : t(t) {} |
+ const T t; |
+ Entry* next_; |
+ Entry* prev_; |
+ }; |
+ |
+ Entry* head_; |
+}; |
+ |
+ |
+class DescriptorInfo { |
+ public: |
+ explicit DescriptorInfo(intptr_t fd) : fd_(fd), mask_(0) { |
+ ASSERT(fd_ != -1); |
+ } |
+ |
+ virtual ~DescriptorInfo() { |
+ mask_ = 0; |
+ } |
+ |
+ intptr_t fd() { return fd_; } |
+ |
+ |
+ // Type of socket. |
+ |
+ virtual bool IsListeningSocket() const = 0; |
+ |
+ |
+ // Ports. |
+ |
+ virtual bool AddPort(Dart_Port port) = 0; |
+ |
+ virtual bool RemovePort(Dart_Port port) = 0; |
+ |
+ // Returns the next port which should be used for sending events to. |
+ virtual Dart_Port NextPort() = 0; |
+ |
+ virtual bool HasNextPort() = 0; |
+ |
+ |
+ // Tokens. |
+ |
+ // Returns true if the last token was taken. |
+ virtual bool TakeToken() = 0; |
+ |
+ // Returns true if the tokens was 0 before adding. |
+ virtual bool ReturnTokens(Dart_Port port, int count) = 0; |
+ |
+ // Returns true if for any registired Dart_port tokens are available. |
+ virtual bool HasTokens() const = 0; |
+ |
+ |
+ // Other. |
+ |
+ virtual void Close() = 0; |
+ |
+ void SetMask(intptr_t mask) { |
+ mask_ = mask; |
+ } |
+ |
+ intptr_t Mask() { return mask_; } |
+ |
+ protected: |
+ intptr_t fd_; |
+ // Mask of events to report through the port. |
+ intptr_t mask_; |
+}; |
+ |
+ |
+// Describes a OS descriptor (e.g. file descriptor on linux or HANDLE on |
+// windows) which is connected to a single Dart_Port. |
+// |
+// Subclasses of this class can be e.g. connected tcp sockets |
+template<typename SI> |
+class DescriptorInfoSingle : public SI { |
+ public: |
+ explicit DescriptorInfoSingle(intptr_t fd) : SI(fd), port_(0), tokens_(16) { |
+ } |
+ |
+ virtual ~DescriptorInfoSingle() { } |
+ |
+ virtual bool IsListeningSocket() const { return false; } |
+ |
+ virtual bool AddPort(Dart_Port port) { |
+ ASSERT(port_ == 0 || port == port_); |
Søren Gjesse
2015/02/02 10:56:14
This assert looks strange. Are we adding the same
kustermann
2015/02/03 10:45:35
I think so, I've observed this on windows I think.
|
+ port_ = port; |
+ return true; |
+ } |
+ |
+ virtual bool RemovePort(Dart_Port port) { |
+ ASSERT(port_ == 0 || port_ == port); |
Søren Gjesse
2015/02/02 10:56:14
Again, are we removing a port which is not added?
kustermann
2015/02/03 10:45:35
ditto. I'll add a TODO.
If you want, I can also v
|
+ port_ = 0; |
+ return true; |
+ } |
+ |
+ virtual Dart_Port NextPort() { |
+ ASSERT(port_ != 0); |
+ return port_; |
+ } |
+ |
+ virtual bool HasNextPort() { |
+ return port_ != 0; |
+ } |
+ |
+ virtual bool TakeToken() { |
+ ASSERT(tokens_ > 0); |
+ tokens_--; |
+ return tokens_ == 0; |
+ } |
+ |
+ virtual bool ReturnTokens(Dart_Port port, int count) { |
+ ASSERT(port_ == port); |
+ ASSERT(tokens_ >= 0); |
+ bool was_empty = tokens_ == 0; |
+ tokens_ += count; |
+ return was_empty; |
+ } |
+ |
+ virtual bool HasTokens() const { return tokens_ > 0; } |
+ |
+ virtual void Close() { |
+ SI::Close(); |
+ } |
+ |
+ private: |
+ Dart_Port port_; |
+ int tokens_; |
+}; |
+ |
+ |
+// Describes a OS descriptor (e.g. file descriptor on linux or HANDLE on |
+// windows) which is connected to multiple Dart_Port's. |
+// |
+// Subclasses of this class can be e.g. a listening socket which multiple |
+// isolates are listening on. |
+template<typename SI> |
+class DescriptorInfoMultiple : public SI { |
+ private: |
+ static const int kTokenCount = 4; |
+ |
+ static bool SamePortValue(void* key1, void* key2) { |
+ return reinterpret_cast<Dart_Port>(key1) == |
+ reinterpret_cast<Dart_Port>(key2); |
+ } |
+ |
+ static uint32_t GetHashmapHashFromPort(Dart_Port port) { |
+ return static_cast<uint32_t>(port & 0xFFFFFFFF); |
+ } |
+ |
+ static void* GetHashmapKeyFromPort(Dart_Port port) { |
+ return reinterpret_cast<void*>(port); |
+ } |
+ |
+ public: |
+ explicit DescriptorInfoMultiple(intptr_t fd) |
+ : SI(fd), tokens_map_(&SamePortValue, 4) {} |
+ |
+ virtual ~DescriptorInfoMultiple() {} |
+ |
+ virtual bool IsListeningSocket() const { return true; } |
+ |
+ virtual bool AddPort(Dart_Port port) { |
+ HashMap::Entry* entry = tokens_map_.Lookup( |
+ GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), true); |
+ entry->value = reinterpret_cast<void*>(kTokenCount); |
+ return live_ports_.Add(port); |
+ } |
+ |
+ virtual bool RemovePort(Dart_Port port) { |
+ HashMap::Entry* entry = tokens_map_.Lookup( |
+ GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), false); |
+ if (entry != NULL) { |
+ intptr_t tokens = reinterpret_cast<intptr_t>(entry->value); |
+ if (tokens == 0) { |
+ while (idle_ports_.head() != port) { |
+ idle_ports_.Rotate(); |
+ } |
+ idle_ports_.RemoveHead(); |
+ } else { |
+ while (live_ports_.head() != port) { |
+ live_ports_.Rotate(); |
+ } |
+ live_ports_.RemoveHead(); |
+ } |
+ tokens_map_.Remove( |
+ GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port)); |
+ } else { |
+ // NOTE: This is a listening socket which has been immediately closed. |
+ // |
+ // If a listening socket is not listened on, the event handler does not |
+ // know about it beforehand. So the first time the event handler knows |
+ // about it, is when it is supposed to be closed. We therefore do nothing |
+ // here. |
+ // |
+ // But whether to close it, depends on whether other isolates have it open |
+ // as well or not. |
+ } |
+ return !live_ports_.HasHead(); |
+ } |
+ |
+ virtual Dart_Port NextPort() { |
+ ASSERT(live_ports_.HasHead()); |
+ return live_ports_.head(); |
+ } |
+ |
+ virtual bool HasNextPort() { |
+ return live_ports_.HasHead(); |
+ } |
+ |
+ virtual bool TakeToken() { |
+ ASSERT(live_ports_.HasHead()); |
+ Dart_Port port = live_ports_.head(); |
+ HashMap::Entry* entry = tokens_map_.Lookup( |
+ GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), false); |
+ ASSERT(entry != NULL); |
+ intptr_t tokens = reinterpret_cast<intptr_t>(entry->value); |
+ tokens--; |
+ entry->value = reinterpret_cast<void*>(tokens); |
+ if (tokens == 0) { |
+ live_ports_.RemoveHead(); |
+ idle_ports_.Add(port); |
+ if (!live_ports_.HasHead()) { |
+ return true; |
+ } |
+ } else { |
+ live_ports_.Rotate(); |
+ } |
+ return false; |
+ } |
+ |
+ virtual bool ReturnTokens(Dart_Port port, int count) { |
+ HashMap::Entry* entry = tokens_map_.Lookup( |
+ GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), false); |
+ ASSERT(entry != NULL); |
+ intptr_t tokens = reinterpret_cast<intptr_t>(entry->value); |
+ tokens += count; |
+ entry->value = reinterpret_cast<void*>(tokens); |
+ if (tokens == count) { |
+ // Return to live_ports_. |
+ while (idle_ports_.head() != port) { |
+ idle_ports_.Rotate(); |
+ } |
+ idle_ports_.RemoveHead(); |
+ bool was_empty = !live_ports_.HasHead(); |
+ live_ports_.Add(port); |
+ return was_empty; |
+ } |
+ return false; |
+ } |
+ |
+ virtual bool HasTokens() const { |
+ return live_ports_.HasHead(); |
+ } |
+ |
+ virtual void Close() { |
+ SI::Close(); |
+ } |
+ |
+ private: |
+ CircularLinkedList<Dart_Port> live_ports_; |
+ CircularLinkedList<Dart_Port> idle_ports_; |
+ HashMap tokens_map_; |
+}; |
+ |
+ |
+class InterruptMessage { |
+ public: |
+ intptr_t id; |
+ Dart_Port dart_port; |
+ int64_t data; |
+}; |
+ |
+ |
+static const int kInterruptMessageSize = sizeof(InterruptMessage); |
+static const int kInfinityTimeout = -1; |
+static const int kTimerId = -1; |
+static const int kShutdownId = -2; |
+ |
} // namespace bin |
} // namespace dart |