bpo-41710: Add private _PyDeadline_Get() function (GH-28674)

Add a private C API for deadlines: add _PyDeadline_Init() and
_PyDeadline_Get() functions.

* Add _PyTime_Add() and _PyTime_Mul() functions which compute t1+t2
  and t1*t2 and clamp the result on overflow.
* _PyTime_MulDiv() now uses _PyTime_Add() and _PyTime_Mul().
This commit is contained in:
Victor Stinner 2021-10-01 13:29:25 +02:00 committed by GitHub
parent 54957f16a6
commit 833fdf126c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 177 additions and 109 deletions

View file

@ -75,20 +75,20 @@ EnterNonRecursiveMutex(PNRMUTEX mutex, DWORD milliseconds)
}
}
} else if (milliseconds != 0) {
/* wait at least until the target */
_PyTime_t now = _PyTime_GetPerfCounter();
/* wait at least until the deadline */
_PyTime_t nanoseconds = _PyTime_FromNanoseconds((_PyTime_t)milliseconds * 1000000);
_PyTime_t target = now + nanoseconds;
_PyTime_t deadline = _PyTime_Add(_PyTime_GetPerfCounter(), nanoseconds);
while (mutex->locked) {
_PyTime_t microseconds = _PyTime_AsMicroseconds(nanoseconds, _PyTime_ROUND_TIMEOUT);
_PyTime_t microseconds = _PyTime_AsMicroseconds(nanoseconds,
_PyTime_ROUND_TIMEOUT);
if (PyCOND_TIMEDWAIT(&mutex->cv, &mutex->cs, microseconds) < 0) {
result = WAIT_FAILED;
break;
}
now = _PyTime_GetPerfCounter();
if (target <= now)
nanoseconds = deadline - _PyTime_GetPerfCounter();
if (nanoseconds <= 0) {
break;
nanoseconds = target - now;
}
}
}
if (!mutex->locked) {