Skip to content

Commit

Permalink
Fix DeadlineRegistration to be reusable after expiration
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 702664862
Change-Id: I9656622abe49e26209840c75760e1e4f90d66210
  • Loading branch information
happyCoder92 authored and copybara-github committed Dec 4, 2024
1 parent 0b99c48 commit 5366f9d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions sandboxed_api/sandbox2/util/deadline_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ void DeadlineManager::AdjustDeadline(DeadlineRegistration& registration,
absl::Time deadline) {
absl::MutexLock lock(&queue_mutex_);
queue_.erase(registration.data_.get());
absl::MutexLock data_lock(&registration.data_->mutex);
registration.data_->expired = false;
registration.data_->deadline = RoundUpTo(deadline, kResolution);
if (deadline != absl::InfiniteFuture()) {
queue_.insert(registration.data_.get());
Expand Down
2 changes: 1 addition & 1 deletion sandboxed_api/sandbox2/util/deadline_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class DeadlineRegistration {

struct Data {
absl::Mutex mutex;
// Changed only under DeadlineManager::queue_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;
Expand Down
15 changes: 15 additions & 0 deletions sandboxed_api/sandbox2/util/deadline_manager_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,20 @@ TEST(DeadlineManagerTest, DeadlineReset) {
EXPECT_GE(elapsed, absl::Milliseconds(200));
}

TEST(DeadlineManagerTest, CanBeReusedAfterExpiration) {
DeadlineManager manager("test");
DeadlineRegistration registration(manager);
for (int i = 0; i < 3; ++i) {
absl::Time start_time = absl::Now();
struct timespec ts = absl::ToTimespec(absl::Seconds(1));
registration.SetDeadline(start_time + absl::Milliseconds(100));
registration.ExecuteBlockingSyscall(
[&] { ASSERT_EQ(nanosleep(&ts, nullptr), -1); });
absl::Duration elapsed = absl::Now() - start_time;
EXPECT_GE(elapsed, absl::Milliseconds(100));
EXPECT_LE(elapsed, absl::Milliseconds(200));
}
}

} // namespace
} // namespace sandbox2

0 comments on commit 5366f9d

Please sign in to comment.