| OLD | NEW |
| 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_H_ | 5 #ifndef BIN_EVENTHANDLER_H_ |
| 6 #define BIN_EVENTHANDLER_H_ | 6 #define BIN_EVENTHANDLER_H_ |
| 7 | 7 |
| 8 #include "bin/builtin.h" | 8 #include "bin/builtin.h" |
| 9 #include "bin/dartutils.h" | 9 #include "bin/dartutils.h" |
| 10 #include "bin/isolate_data.h" | 10 #include "bin/isolate_data.h" |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 | 101 |
| 102 void RemoveCurrent() { | 102 void RemoveCurrent() { |
| 103 UpdateTimeout(CurrentPort(), -1); | 103 UpdateTimeout(CurrentPort(), -1); |
| 104 } | 104 } |
| 105 | 105 |
| 106 void UpdateTimeout(Dart_Port port, int64_t timeout); | 106 void UpdateTimeout(Dart_Port port, int64_t timeout); |
| 107 | 107 |
| 108 private: | 108 private: |
| 109 Timeout* next_timeout_; | 109 Timeout* next_timeout_; |
| 110 Timeout* timeouts_; | 110 Timeout* timeouts_; |
| 111 |
| 112 DISALLOW_COPY_AND_ASSIGN(TimeoutQueue); |
| 111 }; | 113 }; |
| 112 | 114 |
| 113 | 115 |
| 114 class InterruptMessage { | 116 class InterruptMessage { |
| 115 public: | 117 public: |
| 116 intptr_t id; | 118 intptr_t id; |
| 117 Dart_Port dart_port; | 119 Dart_Port dart_port; |
| 118 int64_t data; | 120 int64_t data; |
| 119 }; | 121 }; |
| 120 | 122 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 | 216 |
| 215 private: | 217 private: |
| 216 struct Entry { | 218 struct Entry { |
| 217 explicit Entry(const T& t) : t(t), next_(NULL), prev_(NULL) {} | 219 explicit Entry(const T& t) : t(t), next_(NULL), prev_(NULL) {} |
| 218 const T t; | 220 const T t; |
| 219 Entry* next_; | 221 Entry* next_; |
| 220 Entry* prev_; | 222 Entry* prev_; |
| 221 }; | 223 }; |
| 222 | 224 |
| 223 Entry* head_; | 225 Entry* head_; |
| 226 |
| 227 DISALLOW_COPY_AND_ASSIGN(CircularLinkedList); |
| 224 }; | 228 }; |
| 225 | 229 |
| 226 | 230 |
| 227 class DescriptorInfoBase { | 231 class DescriptorInfoBase { |
| 228 public: | 232 public: |
| 229 explicit DescriptorInfoBase(intptr_t fd) : fd_(fd) { | 233 explicit DescriptorInfoBase(intptr_t fd) : fd_(fd) { |
| 230 ASSERT(fd_ != -1); | 234 ASSERT(fd_ != -1); |
| 231 } | 235 } |
| 232 | 236 |
| 233 virtual ~DescriptorInfoBase() {} | 237 virtual ~DescriptorInfoBase() {} |
| (...skipping 27 matching lines...) Expand all Loading... |
| 261 | 265 |
| 262 // Returns the union of event masks of all ports. If a port has a non-positive | 266 // Returns the union of event masks of all ports. If a port has a non-positive |
| 263 // token count it's mask is assumed to be 0. | 267 // token count it's mask is assumed to be 0. |
| 264 virtual intptr_t Mask() = 0; | 268 virtual intptr_t Mask() = 0; |
| 265 | 269 |
| 266 // Closes this descriptor. | 270 // Closes this descriptor. |
| 267 virtual void Close() = 0; | 271 virtual void Close() = 0; |
| 268 | 272 |
| 269 protected: | 273 protected: |
| 270 intptr_t fd_; | 274 intptr_t fd_; |
| 275 |
| 276 private: |
| 277 DISALLOW_COPY_AND_ASSIGN(DescriptorInfoBase); |
| 271 }; | 278 }; |
| 272 | 279 |
| 273 | 280 |
| 274 // Describes a OS descriptor (e.g. file descriptor on linux or HANDLE on | 281 // Describes a OS descriptor (e.g. file descriptor on linux or HANDLE on |
| 275 // windows) which is connected to a single Dart_Port. | 282 // windows) which is connected to a single Dart_Port. |
| 276 // | 283 // |
| 277 // Subclasses of this class can be e.g. connected tcp sockets. | 284 // Subclasses of this class can be e.g. connected tcp sockets. |
| 278 template<typename DI> | 285 template<typename DI> |
| 279 class DescriptorInfoSingleMixin : public DI { | 286 class DescriptorInfoSingleMixin : public DI { |
| 280 private: | 287 private: |
| 281 static const int kTokenCount = 16; | 288 static const int kTokenCount = 16; |
| 282 | 289 |
| 283 public: | 290 public: |
| 284 explicit DescriptorInfoSingleMixin(intptr_t fd, bool disable_tokens) | 291 DescriptorInfoSingleMixin(intptr_t fd, bool disable_tokens) |
| 285 : DI(fd), port_(0), tokens_(kTokenCount), mask_(0), | 292 : DI(fd), port_(0), tokens_(kTokenCount), mask_(0), |
| 286 disable_tokens_(disable_tokens) {} | 293 disable_tokens_(disable_tokens) {} |
| 287 | 294 |
| 288 virtual ~DescriptorInfoSingleMixin() { } | 295 virtual ~DescriptorInfoSingleMixin() { } |
| 289 | 296 |
| 290 virtual bool IsListeningSocket() const { return false; } | 297 virtual bool IsListeningSocket() const { return false; } |
| 291 | 298 |
| 292 virtual void SetPortAndMask(Dart_Port port, intptr_t mask) { | 299 virtual void SetPortAndMask(Dart_Port port, intptr_t mask) { |
| 293 ASSERT(port_ == 0 || port == port_); | 300 ASSERT(port_ == 0 || port == port_); |
| 294 port_ = port; | 301 port_ = port; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 | 356 |
| 350 virtual void Close() { | 357 virtual void Close() { |
| 351 DI::Close(); | 358 DI::Close(); |
| 352 } | 359 } |
| 353 | 360 |
| 354 private: | 361 private: |
| 355 Dart_Port port_; | 362 Dart_Port port_; |
| 356 int tokens_; | 363 int tokens_; |
| 357 intptr_t mask_; | 364 intptr_t mask_; |
| 358 bool disable_tokens_; | 365 bool disable_tokens_; |
| 366 |
| 367 DISALLOW_COPY_AND_ASSIGN(DescriptorInfoSingleMixin); |
| 359 }; | 368 }; |
| 360 | 369 |
| 361 | 370 |
| 362 // Describes a OS descriptor (e.g. file descriptor on linux or HANDLE on | 371 // Describes a OS descriptor (e.g. file descriptor on linux or HANDLE on |
| 363 // windows) which is connected to multiple Dart_Port's. | 372 // windows) which is connected to multiple Dart_Port's. |
| 364 // | 373 // |
| 365 // Subclasses of this class can be e.g. a listening socket which multiple | 374 // Subclasses of this class can be e.g. a listening socket which multiple |
| 366 // isolates are listening on. | 375 // isolates are listening on. |
| 367 template<typename DI> | 376 template<typename DI> |
| 368 class DescriptorInfoMultipleMixin : public DI { | 377 class DescriptorInfoMultipleMixin : public DI { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 393 | 402 |
| 394 struct PortEntry { | 403 struct PortEntry { |
| 395 Dart_Port dart_port; | 404 Dart_Port dart_port; |
| 396 intptr_t is_reading; | 405 intptr_t is_reading; |
| 397 intptr_t token_count; | 406 intptr_t token_count; |
| 398 | 407 |
| 399 bool IsReady() { return token_count > 0 && is_reading; } | 408 bool IsReady() { return token_count > 0 && is_reading; } |
| 400 }; | 409 }; |
| 401 | 410 |
| 402 public: | 411 public: |
| 403 explicit DescriptorInfoMultipleMixin(intptr_t fd, bool disable_tokens) | 412 DescriptorInfoMultipleMixin(intptr_t fd, bool disable_tokens) |
| 404 : DI(fd), tokens_map_(&SamePortValue, kTokenCount), | 413 : DI(fd), tokens_map_(&SamePortValue, kTokenCount), |
| 405 disable_tokens_(disable_tokens) {} | 414 disable_tokens_(disable_tokens) {} |
| 406 | 415 |
| 407 virtual ~DescriptorInfoMultipleMixin() {} | 416 virtual ~DescriptorInfoMultipleMixin() {} |
| 408 | 417 |
| 409 virtual bool IsListeningSocket() const { return true; } | 418 virtual bool IsListeningSocket() const { return true; } |
| 410 | 419 |
| 411 virtual void SetPortAndMask(Dart_Port port, intptr_t mask) { | 420 virtual void SetPortAndMask(Dart_Port port, intptr_t mask) { |
| 412 HashMap::Entry* entry = tokens_map_.Lookup( | 421 HashMap::Entry* entry = tokens_map_.Lookup( |
| 413 GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), true); | 422 GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), true); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 entry = tokens_map_.Next(entry)) { | 543 entry = tokens_map_.Next(entry)) { |
| 535 PortEntry* pentry = reinterpret_cast<PortEntry*>(entry->value); | 544 PortEntry* pentry = reinterpret_cast<PortEntry*>(entry->value); |
| 536 DartUtils::PostInt32(pentry->dart_port, events); | 545 DartUtils::PostInt32(pentry->dart_port, events); |
| 537 | 546 |
| 538 // Update token count. | 547 // Update token count. |
| 539 bool was_ready = pentry->IsReady(); | 548 bool was_ready = pentry->IsReady(); |
| 540 if (!disable_tokens_) { | 549 if (!disable_tokens_) { |
| 541 pentry->token_count--; | 550 pentry->token_count--; |
| 542 } | 551 } |
| 543 | 552 |
| 544 if (was_ready && pentry->token_count <= 0) { | 553 if (was_ready && (pentry->token_count <= 0)) { |
| 545 active_readers_.Remove(pentry); | 554 active_readers_.Remove(pentry); |
| 546 } | 555 } |
| 547 } | 556 } |
| 548 } | 557 } |
| 549 | 558 |
| 550 virtual void ReturnTokens(Dart_Port port, int count) { | 559 virtual void ReturnTokens(Dart_Port port, int count) { |
| 551 HashMap::Entry* entry = tokens_map_.Lookup( | 560 HashMap::Entry* entry = tokens_map_.Lookup( |
| 552 GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), false); | 561 GetHashmapKeyFromPort(port), GetHashmapHashFromPort(port), false); |
| 553 ASSERT(entry != NULL); | 562 ASSERT(entry != NULL); |
| 554 | 563 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 579 // The [Dart_Port]s which are not paused (i.e. are interested in read events, | 588 // The [Dart_Port]s which are not paused (i.e. are interested in read events, |
| 580 // i.e. `mask == (1 << kInEvent)`) and we have enough tokens to communicate | 589 // i.e. `mask == (1 << kInEvent)`) and we have enough tokens to communicate |
| 581 // with them. | 590 // with them. |
| 582 CircularLinkedList<PortEntry *> active_readers_; | 591 CircularLinkedList<PortEntry *> active_readers_; |
| 583 | 592 |
| 584 // A convenience mapping: | 593 // A convenience mapping: |
| 585 // Dart_Port -> struct PortEntry { dart_port, mask, token_count } | 594 // Dart_Port -> struct PortEntry { dart_port, mask, token_count } |
| 586 HashMap tokens_map_; | 595 HashMap tokens_map_; |
| 587 | 596 |
| 588 bool disable_tokens_; | 597 bool disable_tokens_; |
| 598 |
| 599 DISALLOW_COPY_AND_ASSIGN(DescriptorInfoMultipleMixin); |
| 589 }; | 600 }; |
| 590 | 601 |
| 591 | |
| 592 } // namespace bin | 602 } // namespace bin |
| 593 } // namespace dart | 603 } // namespace dart |
| 594 | 604 |
| 595 // The event handler delegation class is OS specific. | 605 // The event handler delegation class is OS specific. |
| 596 #if defined(TARGET_OS_ANDROID) | 606 #if defined(TARGET_OS_ANDROID) |
| 597 #include "bin/eventhandler_android.h" | 607 #include "bin/eventhandler_android.h" |
| 598 #elif defined(TARGET_OS_LINUX) | 608 #elif defined(TARGET_OS_LINUX) |
| 599 #include "bin/eventhandler_linux.h" | 609 #include "bin/eventhandler_linux.h" |
| 600 #elif defined(TARGET_OS_MACOS) | 610 #elif defined(TARGET_OS_MACOS) |
| 601 #include "bin/eventhandler_macos.h" | 611 #include "bin/eventhandler_macos.h" |
| 602 #elif defined(TARGET_OS_WINDOWS) | 612 #elif defined(TARGET_OS_WINDOWS) |
| 603 #include "bin/eventhandler_win.h" | 613 #include "bin/eventhandler_win.h" |
| 604 #else | 614 #else |
| 605 #error Unknown target os. | 615 #error Unknown target os. |
| 606 #endif | 616 #endif |
| 607 | 617 |
| 608 namespace dart { | 618 namespace dart { |
| 609 namespace bin { | 619 namespace bin { |
| 610 | 620 |
| 611 class EventHandler { | 621 class EventHandler { |
| 612 public: | 622 public: |
| 623 EventHandler() {} |
| 613 void SendData(intptr_t id, Dart_Port dart_port, int64_t data) { | 624 void SendData(intptr_t id, Dart_Port dart_port, int64_t data) { |
| 614 delegate_.SendData(id, dart_port, data); | 625 delegate_.SendData(id, dart_port, data); |
| 615 } | 626 } |
| 616 | 627 |
| 617 /** | 628 /** |
| 618 * Signal to main thread that event handler is done. | 629 * Signal to main thread that event handler is done. |
| 619 */ | 630 */ |
| 620 void NotifyShutdownDone(); | 631 void NotifyShutdownDone(); |
| 621 | 632 |
| 622 /** | 633 /** |
| 623 * Start the event-handler. | 634 * Start the event-handler. |
| 624 */ | 635 */ |
| 625 static void Start(); | 636 static void Start(); |
| 626 | 637 |
| 627 /** | 638 /** |
| 628 * Stop the event-handler. It's expected that there will be no further calls | 639 * Stop the event-handler. It's expected that there will be no further calls |
| 629 * to SendData after a call to Stop. | 640 * to SendData after a call to Stop. |
| 630 */ | 641 */ |
| 631 static void Stop(); | 642 static void Stop(); |
| 632 | 643 |
| 633 static EventHandlerImplementation* delegate(); | 644 static EventHandlerImplementation* delegate(); |
| 634 | 645 |
| 635 private: | 646 private: |
| 636 friend class EventHandlerImplementation; | 647 friend class EventHandlerImplementation; |
| 637 EventHandlerImplementation delegate_; | 648 EventHandlerImplementation delegate_; |
| 649 |
| 650 DISALLOW_COPY_AND_ASSIGN(EventHandler); |
| 638 }; | 651 }; |
| 639 | 652 |
| 640 } // namespace bin | 653 } // namespace bin |
| 641 } // namespace dart | 654 } // namespace dart |
| 642 | 655 |
| 643 #endif // BIN_EVENTHANDLER_H_ | 656 #endif // BIN_EVENTHANDLER_H_ |
| OLD | NEW |