Skip to content

Commit

Permalink
semaphore.h: handle spurious wakeups in TimedWait() on Linux (#1021)
Browse files Browse the repository at this point in the history
  • Loading branch information
dconeybe authored Jul 11, 2022
1 parent c722ca7 commit 26e918b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
25 changes: 24 additions & 1 deletion app/src/semaphore.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,30 @@ class Semaphore {
return WaitForSingleObject(semaphore_, milliseconds) == 0;
#else // not windows and not mac - should be Linux.
timespec t = internal::MsToAbsoluteTimespec(milliseconds);
return sem_timedwait(semaphore_, &t) == 0;
while (true) {
int result = sem_timedwait(semaphore_, &t);
if (result == 0) {
// Return success, since we successfully locked the semaphore.
return true;
}
switch (errno) {
case EINTR:
// Restart the wait because we were woken up spuriously.
continue;
case ETIMEDOUT:
// Return failure, since the timeout expired.
return false;
case EINVAL:
assert("sem_timedwait() failed with EINVAL" == 0);
return false;
case EDEADLK:
assert("sem_timedwait() failed with EDEADLK" == 0);
return false;
default:
assert("sem_timedwait() failed with an unknown error" == 0);
return false;
}
}
#endif
}

Expand Down
7 changes: 7 additions & 0 deletions release_build_files/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,13 @@ workflow use only during the development of your app, not for publicly shipping
code.

## Release Notes
### Upcoming Changes
- Changes
- General (Android,Linux): Fixed a concurrency bug where waiting for an
event with a timeout could occasionally return prematurely, as if the
timeout had occurred
([#1021](https://github.com/firebase/firebase-cpp-sdk/pull/1021)).

### 9.2.0
- Changes
- GMA: Added the Google Mobile Ads SDK with updated support for AdMob. See
Expand Down

0 comments on commit 26e918b

Please sign in to comment.