diff --git a/sandboxed_api/sandbox2/util/deadline_manager.cc b/sandboxed_api/sandbox2/util/deadline_manager.cc index ea064fbb..0caf172d 100644 --- a/sandboxed_api/sandbox2/util/deadline_manager.cc +++ b/sandboxed_api/sandbox2/util/deadline_manager.cc @@ -16,10 +16,8 @@ #include -#include #include #include -#include #include "absl/base/call_once.h" #include "absl/flags/flag.h" @@ -139,46 +137,27 @@ void DeadlineManager::Run() { return (!queue_.empty() && next_deadline != (*queue_.begin())->deadline) || cancelled_; }; - for (;;) { - std::vector to_be_notified; - { - absl::MutexLock lock(&queue_mutex_); - if (cancelled_) { - break; - } - next_deadline = absl::InfiniteFuture(); - if (!queue_.empty()) { - next_deadline = (*queue_.begin())->deadline; - } - if (queue_mutex_.AwaitWithDeadline( - absl::Condition(&next_deadline_changed_or_cancelled), - next_deadline)) { - continue; - } - absl::Time current = std::max(absl::Now(), next_deadline); - while (!queue_.empty() && (*queue_.begin())->deadline <= current) { - to_be_notified.push_back(*queue_.begin()); - queue_.erase(queue_.begin()); - } + absl::MutexLock lock(&queue_mutex_); + while (!cancelled_) { + next_deadline = absl::InfiniteFuture(); + if (!queue_.empty()) { + next_deadline = (*queue_.begin())->deadline; + } + if (queue_mutex_.AwaitWithDeadline( + absl::Condition(&next_deadline_changed_or_cancelled), + next_deadline)) { + continue; } - std::vector to_reinsert; - absl::Time next_notification_time = - RoundUpTo(absl::Now() + kResolution, kResolution); - for (DeadlineRegistration::Data* entry : to_be_notified) { + absl::Time next_notification_time = RoundUpTo(absl::Now(), kResolution); + while (!queue_.empty() && (*queue_.begin())->deadline <= next_deadline) { + DeadlineRegistration::Data* entry = *queue_.begin(); + queue_.erase(queue_.begin()); absl::MutexLock lock(&entry->mutex); entry->expired = true; if (entry->in_blocking_fn) { util::Syscall(__NR_tgkill, getpid(), entry->tid, signal_nr_); entry->deadline = next_notification_time; - to_reinsert.push_back(entry); - } - } - { - absl::MutexLock lock(&queue_mutex_); - for (DeadlineRegistration::Data* entry : to_reinsert) { - if (entry->deadline != absl::InfiniteFuture()) { - queue_.insert(entry); - } + queue_.insert(entry); } } } diff --git a/sandboxed_api/sandbox2/util/deadline_manager.h b/sandboxed_api/sandbox2/util/deadline_manager.h index 2dcc24bc..727282f2 100644 --- a/sandboxed_api/sandbox2/util/deadline_manager.h +++ b/sandboxed_api/sandbox2/util/deadline_manager.h @@ -71,9 +71,7 @@ class DeadlineRegistration { struct Data { absl::Mutex mutex; - // Adjusted under both DeadlineManager::queue_mutex_ and Data::mutex. - // DeadlineManager thread reads it holding just queue_mutex_ and writes - // holding just Data::mutex. + // Changed only under both DeadlineManager::queue_mutex_ and Data::mutex. absl::Time deadline = absl::InfiniteFuture(); pid_t ABSL_GUARDED_BY(mutex) tid = -1; bool ABSL_GUARDED_BY(mutex) in_blocking_fn = false;