| Index: dart/runtime/bin/eventhandler.h
|
| diff --git a/dart/runtime/bin/eventhandler.h b/dart/runtime/bin/eventhandler.h
|
| index 70636bf80df43dd5128bacba69f7ed6ad7392b5d..34c5e750899c721c0fb7272cd62254469d004e25 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,43 @@ 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) {
|
| + ASSERT(head_->next_ != NULL);
|
| + head_ = head_->next_;
|
| + }
|
| }
|
|
|
| private:
|
| @@ -176,6 +211,335 @@ 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 listening 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 for this port.
|
| + 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 for all ports.
|
| + virtual void NotifyAllDartPorts(uintptr_t events) = 0;
|
| +
|
| + // Returns `count` tokens for the given port.
|
| + virtual void ReturnTokens(Dart_Port port, int count) = 0;
|
| +
|
| + // Returns the union of event masks of all ports. If a port has a non-positive
|
| + // token count it's mask is assumed to be 0.
|
| + 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.
|
| +template<typename DI>
|
| +class DescriptorInfoSingleMixin : public DI {
|
| + private:
|
| + static const int kTokenCount = 16;
|
| +
|
| + public:
|
| + explicit DescriptorInfoSingleMixin(intptr_t fd)
|
| + : DI(fd), port_(0), tokens_(kTokenCount), mask_(0) {}
|
| +
|
| + 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;
|
| + ASSERT(tokens_ <= kTokenCount);
|
| + }
|
| +
|
| + virtual intptr_t Mask() {
|
| + if (tokens_ <= 0) {
|
| + return 0;
|
| + }
|
| + return mask_;
|
| + }
|
| +
|
| + virtual void Close() {
|
| + DI::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 DI>
|
| +class DescriptorInfoMultipleMixin : public DI {
|
| + 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)
|
| + : DI(fd), tokens_map_(&SamePortValue, kTokenCount) {}
|
| +
|
| + 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.
|
| + 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();
|
| + ASSERT(pentry->token_count >= 0);
|
| + pentry->token_count += count;
|
| + ASSERT(pentry->token_count <= kTokenCount);
|
| + 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() {
|
| + DI::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
|
|
|
|
|