Chromium Code Reviews| Index: dart/runtime/bin/eventhandler.h |
| diff --git a/dart/runtime/bin/eventhandler.h b/dart/runtime/bin/eventhandler.h |
| index 70636bf80df43dd5128bacba69f7ed6ad7392b5d..13bd1ab39bb069cee1e859156462af4f6b5df059 100644 |
| --- a/dart/runtime/bin/eventhandler.h |
| +++ b/dart/runtime/bin/eventhandler.h |
| @@ -6,8 +6,11 @@ |
| #define BIN_EVENTHANDLER_H_ |
| #include "bin/builtin.h" |
| +#include "bin/dartutils.h" |
| #include "bin/isolate_data.h" |
| +#include "platform/hashmap.h" |
| + |
| namespace dart { |
| namespace bin { |
| @@ -44,6 +47,9 @@ enum MessageFlags { |
| ((data & COMMAND_MASK) == (1 << command_bit)) // NOLINT |
| #define IS_EVENT(data, event_bit) \ |
| ((data & EVENT_MASK) == (1 << event_bit)) // NOLINT |
| +#define IS_IO_EVENT(data) \ |
| + ((data & (1 << kInEvent | 1 << kOutEvent | 1 << kCloseEvent)) != 0 && \ |
| + (data & ~(1 << kInEvent | 1 << kOutEvent | 1 << kCloseEvent)) == 0) |
| #define IS_LISTENING_SOCKET(data) \ |
| ((data & (1 << kListeningSocket)) != 0) // NOLINT |
| #define TOKEN_COUNT(data) (data & ((1 << kCloseCommand) - 1)) |
| @@ -155,14 +161,42 @@ class CircularLinkedList { |
| delete e; |
| } |
| + void Remove(T item) { |
| + if (head_ == NULL) { |
| + return; |
| + } else if (head_ == head_->next_) { |
| + if (head_->t == item) { |
| + delete head_; |
| + head_ = NULL; |
| + return; |
| + } |
| + } else { |
| + Entry *current = head_; |
| + do { |
| + if (current->t == item) { |
| + Entry *next = current->next_; |
| + Entry *prev = current->prev_; |
| + prev->next_ = next; |
| + next->prev_ = prev; |
| + delete current; |
| + return; |
| + } |
| + current = current->next_; |
| + } while (current != head_); |
| + } |
| + } |
| + |
| + |
| T head() const { return head_->t; } |
| - bool HasHead() { |
| + bool HasHead() const { |
| return head_ != NULL; |
| } |
| void Rotate() { |
| - head_ = head_->next_; |
| + if (head_ != NULL && head_->next_ != NULL) { |
|
wibling
2015/02/06 14:09:28
NIT: you could assert head_->next != NULL inside t
kustermann
2015/02/09 08:50:21
Done.
|
| + head_ = head_->next_; |
| + } |
| } |
| private: |
| @@ -176,6 +210,328 @@ class CircularLinkedList { |
| Entry* head_; |
| }; |
| + |
| +class DescriptorInfoBase { |
| + public: |
| + explicit DescriptorInfoBase(intptr_t fd) : fd_(fd) { |
| + ASSERT(fd_ != -1); |
| + } |
| + |
| + virtual ~DescriptorInfoBase() {} |
| + |
| + // The OS descriptor. |
| + intptr_t fd() { return fd_; } |
| + |
| + // Whether this descriptor refers to an underlying OS socket. |
| + virtual bool IsListeningSocket() const = 0; |
| + |
| + // Inserts or updates a new Dart_Port which is interested in events specified |
| + // in `mask`. |
| + virtual void SetPortAndMask(Dart_Port port, intptr_t mask) = 0; |
| + |
| + // Removes a port from the interested listeners. |
| + virtual void RemovePort(Dart_Port port) = 0; |
| + |
| + // Returns a port to which `events_ready` can be sent to. It will also |
| + // decrease the token count by 1. |
| + virtual Dart_Port NextNotifyDartPort(intptr_t events_ready) = 0; |
| + |
| + // Will post `data` to all known Dart_Ports. It will also decrease the token |
| + // count by 1. |
| + virtual void NotifyAllDartPorts(uintptr_t events) = 0; |
| + |
| + // Returns true if the tokens was 0 before adding. |
| + virtual void ReturnTokens(Dart_Port port, int count) = 0; |
| + |
| + // Returns the bit-wise OR of events t |
|
Søren Gjesse
2015/02/06 14:42:17
Strange comment.
kustermann
2015/02/09 08:50:21
Done.
|
| + virtual intptr_t Mask() = 0; |
| + |
| + // Closes this descriptor. |
| + virtual void Close() = 0; |
| + |
| + protected: |
| + intptr_t fd_; |
| +}; |
| + |
| + |
| +// 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 |
|
Søren Gjesse
2015/02/06 14:42:17
Please end comment with .
kustermann
2015/02/09 08:50:21
Done.
|
| +template<typename SI> |
|
Søren Gjesse
2015/02/06 14:42:18
Maybe change SI to DI.
kustermann
2015/02/09 08:50:21
Done.
|
| +class DescriptorInfoSingleMixin : public SI { |
| + public: |
| + explicit DescriptorInfoSingleMixin(intptr_t fd) |
| + : SI(fd), port_(0), tokens_(16), mask_(0) {} |
|
Søren Gjesse
2015/02/06 14:42:17
Pull 16 into a constant.
kustermann
2015/02/09 08:50:21
Done.
|
| + |
| + virtual ~DescriptorInfoSingleMixin() { } |
| + |
| + virtual bool IsListeningSocket() const { return false; } |
| + |
| + virtual void SetPortAndMask(Dart_Port port, intptr_t mask) { |
| + ASSERT(port_ == 0 || port == port_); |
| + port_ = port; |
| + mask_ = mask; |
| + } |
| + |
| + virtual void RemovePort(Dart_Port port) { |
| + // TODO(dart:io): Find out where we call RemovePort() with the invalid |
| + // port. Afterwards remove the part in the ASSERT here. |
| + ASSERT(port_ == 0 || port_ == port); |
| + port_ = 0; |
| + mask_ = 0; |
| + } |
| + |
| + virtual Dart_Port NextNotifyDartPort(intptr_t events_ready) { |
| + ASSERT(IS_IO_EVENT(events_ready) || |
| + IS_EVENT(events_ready, kDestroyedEvent)); |
| + tokens_--; |
| + return port_; |
| + } |
| + |
| + virtual void NotifyAllDartPorts(uintptr_t events) { |
| + // Unexpected close or error events are the only ones we broadcast to all |
| + // listeners and are the only ones where we do not count tokens. |
| + ASSERT(IS_EVENT(events, kCloseEvent) || |
| + IS_EVENT(events, kErrorEvent)); |
| + |
| + if (port_ != 0) { |
| + DartUtils::PostInt32(port_, events); |
| + } |
| + tokens_--; |
| + } |
| + |
| + virtual void ReturnTokens(Dart_Port port, int count) { |
| + ASSERT(port_ == port); |
| + ASSERT(tokens_ >= 0); |
| + tokens_ += count; |
| + } |
| + |
| + virtual intptr_t Mask() { |
| + if (tokens_ <= 0) { |
| + return 0; |
| + } |
| + return mask_; |
| + } |
| + |
| + virtual void Close() { |
| + SI::Close(); |
| + } |
| + |
| + private: |
| + Dart_Port port_; |
| + int tokens_; |
| + intptr_t mask_; |
| +}; |
| + |
| + |
| +// 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> |
|
Søren Gjesse
2015/02/06 14:42:17
Maybe change SI to DI.
kustermann
2015/02/09 08:50:21
Done.
|
| +class DescriptorInfoMultipleMixin : 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); |
| + } |
| + |
| + static bool IsReadingMask(intptr_t mask) { |
| + if (mask == (1 << kInEvent)) { |
| + return true; |
| + } else { |
| + ASSERT(mask == 0); |
| + return false; |
| + } |
| + } |
| + |
| + struct PortEntry { |
| + Dart_Port dart_port; |
| + intptr_t is_reading; |
| + intptr_t token_count; |
| + |
| + bool IsReady() { return token_count > 0 && is_reading; } |
| + }; |
| + |
| + public: |
| + explicit DescriptorInfoMultipleMixin(intptr_t fd) |
| + : SI(fd), tokens_map_(&SamePortValue, 4) {} |
| + |
| + virtual ~DescriptorInfoMultipleMixin() {} |
| + |
| + virtual bool IsListeningSocket() const { return true; } |
| + |
| + virtual void SetPortAndMask(Dart_Port port, intptr_t mask) { |
| + HashMap::Entry* entry = tokens_map_.Lookup( |
| + GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), true); |
| + PortEntry* pentry; |
| + if (entry->value == NULL) { |
| + pentry = new PortEntry(); |
| + pentry->dart_port = port; |
| + pentry->token_count = kTokenCount; |
| + pentry->is_reading = IsReadingMask(mask); |
| + entry->value = reinterpret_cast<void*>(pentry); |
| + |
| + if (pentry->IsReady()) { |
| + active_readers_.Add(pentry); |
| + } |
| + } else { |
| + pentry = reinterpret_cast<PortEntry*>(entry->value); |
| + bool was_ready = pentry->IsReady(); |
| + pentry->is_reading = IsReadingMask(mask); |
| + bool is_ready = pentry->IsReady(); |
| + |
| + if (was_ready && !is_ready) { |
| + active_readers_.Remove(pentry); |
| + } else if (!was_ready && is_ready) { |
| + active_readers_.Add(pentry); |
| + } |
| + } |
| + |
| +#ifdef DEBUG |
| + // To ensure that all readers are ready. |
| + PortEntry* root = reinterpret_cast<PortEntry*>(active_readers_.head()); |
| + |
| + int ready_count = 0; |
| + if (root != NULL) { |
| + PortEntry* current = root; |
| + do { |
| + ASSERT(current->IsReady()); |
| + ready_count++; |
| + active_readers_.Rotate(); |
| + current = active_readers_.head(); |
| + } while (current != root); |
| + } |
| + for (HashMap::Entry *entry = tokens_map_.Start(); |
| + entry != NULL; |
| + entry = tokens_map_.Next(entry)) { |
| + PortEntry* pentry = reinterpret_cast<PortEntry*>(entry->value); |
| + if (pentry->IsReady()) { |
| + ready_count--; |
| + } |
| + } |
| + // Ensure all ready items are in `active_readers_`. |
| + ASSERT(ready_count == 0); |
| +#endif |
| + } |
| + |
| + virtual void RemovePort(Dart_Port port) { |
| + HashMap::Entry* entry = tokens_map_.Lookup( |
| + GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), false); |
| + if (entry != NULL) { |
| + PortEntry* pentry = reinterpret_cast<PortEntry*>(entry->value); |
| + if (pentry->IsReady()) { |
| + active_readers_.Remove(pentry); |
| + } |
| + tokens_map_.Remove( |
| + GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port)); |
| + delete pentry; |
| + } 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. |
| + } |
| + } |
| + |
| + virtual Dart_Port NextNotifyDartPort(intptr_t events_ready) { |
| + // We're only sending `kInEvents` if there are multiple listeners (which is |
| + // listening socktes). |
| + ASSERT(IS_EVENT(events_ready, kInEvent) || |
| + IS_EVENT(events_ready, kDestroyedEvent)); |
| + |
| + if (active_readers_.HasHead()) { |
| + PortEntry* pentry = reinterpret_cast<PortEntry*>(active_readers_.head()); |
| + |
| + // Update token count. |
| + pentry->token_count--; |
| + if (pentry->token_count <= 0) { |
| + active_readers_.RemoveHead(); |
| + } else { |
| + active_readers_.Rotate(); |
| + } |
| + |
| + return pentry->dart_port; |
| + } |
| + return 0; |
| + } |
| + |
| + virtual void NotifyAllDartPorts(uintptr_t events) { |
| + // Unexpected close or error events are the only ones we broadcast to all |
| + // listeners and are the only ones where we do not count tokens. |
| + ASSERT(IS_EVENT(events, kCloseEvent) || |
| + IS_EVENT(events, kErrorEvent)); |
| + |
| + for (HashMap::Entry *entry = tokens_map_.Start(); |
| + entry != NULL; |
| + entry = tokens_map_.Next(entry)) { |
| + PortEntry* pentry = reinterpret_cast<PortEntry*>(entry->value); |
| + DartUtils::PostInt32(pentry->dart_port, events); |
| + |
| + // Update token count. |
|
Søren Gjesse
2015/02/06 14:42:18
The comment above says that we are not counting to
kustermann
2015/02/09 08:50:21
It says:
// Will post `data` to all known Dart_Po
|
| + bool was_ready = pentry->IsReady(); |
| + pentry->token_count--; |
| + |
| + if (was_ready && pentry->token_count <= 0) { |
| + active_readers_.Remove(pentry); |
| + } |
| + } |
| + } |
| + |
| + virtual void ReturnTokens(Dart_Port port, int count) { |
| + HashMap::Entry* entry = tokens_map_.Lookup( |
| + GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), false); |
| + ASSERT(entry != NULL); |
| + |
| + PortEntry* pentry = reinterpret_cast<PortEntry*>(entry->value); |
| + bool was_ready = pentry->IsReady(); |
|
Søren Gjesse
2015/02/06 14:42:18
Assert tokens >= + before and <= kTokenCount after
kustermann
2015/02/09 08:50:21
Done. - Though it is still not clear if we can ass
|
| + pentry->token_count += count; |
| + bool is_ready = pentry->token_count > 0 && pentry->IsReady(); |
| + if (!was_ready && is_ready) { |
| + active_readers_.Add(pentry); |
| + } |
| + } |
| + |
| + virtual intptr_t Mask() { |
| + if (active_readers_.HasHead()) { |
| + return 1 << kInEvent; |
| + } |
| + return 0; |
| + } |
| + |
| + virtual void Close() { |
| + SI::Close(); |
| + } |
| + |
| + private: |
| + // The [Dart_Port]s which are not paused (i.e. are interested in read events, |
| + // i.e. `mask == (1 << kInEvent)`) and we have enough tokens to communicate |
| + // with them. |
| + CircularLinkedList<PortEntry *> active_readers_; |
| + |
| + // A convenience mapping: |
| + // Dart_Port -> struct PortEntry { dart_port, mask, token_count } |
| + HashMap tokens_map_; |
| +}; |
| + |
| + |
| } // namespace bin |
| } // namespace dart |