| 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..dce35307c94a37c8c162118f9b1ab7d6ef1f9cee 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,40 @@ 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::Send(Port* port, uword mask) {
|
| Object* message = Smi::FromWord(mask);
|
| port->Lock();
|
|
|