Bug 1829026 - Handle KERN_ABORTED from semaphore_wait. r=aosmond

semaphore_wait and semaphore_timedwait can return KERN_ABORTED, much
like POSIX semaphores can return EINTR, and need to restart their
waits for correct behavior.

Differential Revision: https://phabricator.services.mozilla.com/D195952
This commit is contained in:
Lee Salzman 2023-12-18 18:10:47 +00:00
Родитель 30d651cabd
Коммит 3a82876743
1 изменённых файлов: 26 добавлений и 6 удалений

Просмотреть файл

@ -7,6 +7,7 @@
#include "CrossProcessSemaphore.h"
#include "nsDebug.h"
#include "nsISupportsImpl.h"
#include <mach/mach_time.h>
static const uint64_t kNsPerUs = 1000;
static const uint64_t kNsPerSec = 1000000000;
@ -47,14 +48,33 @@ CrossProcessSemaphore::~CrossProcessSemaphore() {
bool CrossProcessSemaphore::Wait(const Maybe<TimeDuration>& aWaitTime) {
MOZ_ASSERT(mSemaphore, "Improper construction of semaphore.");
int kr = KERN_OPERATION_TIMED_OUT;
// semaphore_(timed)wait may be interrupted by KERN_ABORTED. Carefully restart
// the wait until it either succeeds or times out.
if (aWaitTime.isNothing()) {
return semaphore_wait(mSemaphore.get()) == KERN_SUCCESS;
do {
kr = semaphore_wait(mSemaphore.get());
} while (kr == KERN_ABORTED);
} else {
mach_timebase_info_data_t tb;
if (mach_timebase_info(&tb) != KERN_SUCCESS) {
return false;
}
uint64_t now = (mach_absolute_time() * tb.numer) / tb.denom;
uint64_t deadline = now + uint64_t(kNsPerUs * aWaitTime->ToMicroseconds());
while (now <= deadline) {
uint64_t ns = deadline - now;
mach_timespec_t ts;
ts.tv_sec = ns / kNsPerSec;
ts.tv_nsec = ns % kNsPerSec;
kr = semaphore_timedwait(mSemaphore.get(), ts);
if (kr != KERN_ABORTED) {
break;
}
now = (mach_absolute_time() * tb.numer) / tb.denom;
}
}
uint64_t ns = uint64_t(kNsPerUs * aWaitTime->ToMicroseconds());
mach_timespec_t ts;
ts.tv_sec = ns / kNsPerSec;
ts.tv_nsec = ns % kNsPerSec;
return semaphore_timedwait(mSemaphore.get(), ts) == KERN_SUCCESS;
return kr == KERN_SUCCESS;
}
void CrossProcessSemaphore::Signal() {