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

Unified Diff: src/vm/event_handler_posix.cc

Issue 1375373004: Use the event-handler for timers. (Closed) Base URL: git@github.com:dart-lang/fletch.git@master
Patch Set: Created 5 years, 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/vm/event_handler_macos.cc ('k') | src/vm/heap.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/vm/event_handler_posix.cc
diff --git a/src/vm/event_handler_posix.cc b/src/vm/event_handler_posix.cc
index c9fb1b213370b0d5b41118abc762868fc04a8bbf..04958c91ea3a72dc792473726459121d3c2c6c5e 100644
--- a/src/vm/event_handler_posix.cc
+++ b/src/vm/event_handler_posix.cc
@@ -22,6 +22,7 @@ namespace fletch {
EventHandler::EventHandler()
: monitor_(Platform::CreateMonitor()),
fd_(-1),
+ next_timeout_(INT64_MAX),
read_fd_(-1),
write_fd_(-1) {
}
@@ -68,6 +69,71 @@ int EventHandler::GetEventHandler() {
return fd_;
}
+void EventHandler::ScheduleTimeout(int64 timeout, Port* port) {
+ ASSERT(timeout != INT64_MAX);
+
+ // Be sure it's running.
+ GetEventHandler();
+
+ ScopedMonitorLock scoped_lock(monitor_);
+
+ auto it = timeouts_.Find(port);
+ if (it == timeouts_.End()) {
+ if (timeout == -1) return;
+ timeouts_[port] = timeout;
+ next_timeout_ = Utils::Minimum(next_timeout_, timeout);
+ // Be sure to mark the port as referenced.
+ port->IncrementRef();
+ } else if (timeout == -1) {
+ timeouts_.Erase(it);
+ // TODO(ajohnsen): We could consider a heap structure to avoid O(n) in this
+ // case?
+ int64 next_timeout = INT64_MAX;
+ for (auto it = timeouts_.Begin(); it != timeouts_.End(); ++it) {
+ next_timeout = Utils::Minimum(next_timeout, it->second);
+ }
+ next_timeout_ = next_timeout;
+ // The port is no longer "referenced" by the event manager.
+ port->DecrementRef();
+ } else {
+ timeouts_[port] = timeout;
+ next_timeout_ = Utils::Minimum(next_timeout_, timeout);
+ }
+ char b = 0;
+ write(write_fd_, &b, 1);
+}
+
+void EventHandler::HandleTimeouts() {
+ // Check timeouts.
+ int64 current_time = Platform::GetMicroseconds() / 1000;
+
+ ScopedMonitorLock scoped_lock(monitor_);
+ if (next_timeout_ > current_time) return;
+
+ int64 next_timeout = INT64_MAX;
+
+ // TODO(ajohnsen): We could consider a heap structure to avoid O(n^2) in
+ // this case?
+ // The following is O(n^2), because we can't continue iterating a hash-map
+ // once we have removed from it.
+ while (true) {
+ bool found = false;
+ for (auto it = timeouts_.Begin(); it != timeouts_.End(); ++it) {
+ if (it->second <= current_time) {
+ Send(it->first, 0);
+ timeouts_.Erase(it);
+ found = true;
+ break;
+ } else {
+ next_timeout = Utils::Minimum(next_timeout, it->second);
+ }
+ }
+ if (!found) break;
+ }
+
+ next_timeout_ = next_timeout;
+}
+
void EventHandler::Send(Port* port, uword mask) {
Object* message = Smi::FromWord(mask);
port->Lock();
« no previous file with comments | « src/vm/event_handler_macos.cc ('k') | src/vm/heap.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698