Skip to content

Commit

Permalink
Fix deadlock caused by timeout overflow (#3998)
Browse files Browse the repository at this point in the history
b/346812667

(cherry picked from commit abc3419)
  • Loading branch information
haozheng-cobalt authored and anonymous1-me committed Sep 21, 2024
1 parent d67f9f6 commit 5ab09c8
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions starboard/common/condition_variable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,24 @@ bool ConditionVariable::WaitTimed(int64_t duration) const {
bool was_signaled = SbConditionVariableIsSignaled(
SbConditionVariableWaitTimed(&condition_, mutex_->mutex(), duration));
#else
if (duration < 0) {
duration = 0;
}
#if !SB_HAS_QUIRK(NO_CONDATTR_SETCLOCK_SUPPORT)
int64_t timeout_time_usec = starboard::CurrentMonotonicTime();
#else
int64_t timeout_time_usec = starboard::CurrentPosixTime();
#endif // !SB_HAS_QUIRK(NO_CONDATTR_SETCLOCK_SUPPORT)
timeout_time_usec += duration;

// Detect overflow if timeout is near kSbInt64Max. Since timeout can't be
// negative at this point, if it goes negative after adding now, we know we've
// gone over. Especially posix now, which has a 400 year advantage over
// Chromium (Windows) now.
if (timeout_time_usec < 0) {
timeout_time_usec = kSbInt64Max;
}

struct timespec timeout;
timeout.tv_sec = timeout_time_usec / 1000'000;
timeout.tv_nsec = (timeout_time_usec % 1000'000) * 1000;
Expand Down

0 comments on commit 5ab09c8

Please sign in to comment.