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

@ -840,18 +840,20 @@ sock_call_ex(PySocketSockObject *s,
if (deadline_initialized) {
/* recompute the timeout */
interval = deadline - _PyTime_GetMonotonicClock();
interval = _PyDeadline_Get(deadline);
}
else {
deadline_initialized = 1;
deadline = _PyTime_GetMonotonicClock() + timeout;
deadline = _PyDeadline_Init(timeout);
interval = timeout;
}
if (interval >= 0)
if (interval >= 0) {
res = internal_select(s, writing, interval, connect);
else
}
else {
res = 1;
}
}
else {
res = internal_select(s, writing, timeout, connect);
@ -4176,7 +4178,7 @@ sock_sendall(PySocketSockObject *s, PyObject *args)
Py_buffer pbuf;
struct sock_send ctx;
int has_timeout = (s->sock_timeout > 0);
_PyTime_t interval = s->sock_timeout;
_PyTime_t timeout = s->sock_timeout;
_PyTime_t deadline = 0;
int deadline_initialized = 0;
PyObject *res = NULL;
@ -4195,14 +4197,14 @@ sock_sendall(PySocketSockObject *s, PyObject *args)
if (has_timeout) {
if (deadline_initialized) {
/* recompute the timeout */
interval = deadline - _PyTime_GetMonotonicClock();
timeout = _PyDeadline_Get(deadline);
}
else {
deadline_initialized = 1;
deadline = _PyTime_GetMonotonicClock() + s->sock_timeout;
deadline = _PyDeadline_Init(timeout);
}
if (interval <= 0) {
if (timeout <= 0) {
PyErr_SetString(PyExc_TimeoutError, "timed out");
goto done;
}
@ -4211,7 +4213,7 @@ sock_sendall(PySocketSockObject *s, PyObject *args)
ctx.buf = buf;
ctx.len = len;
ctx.flags = flags;
if (sock_call_ex(s, 1, sock_send_impl, &ctx, 0, NULL, interval) < 0)
if (sock_call_ex(s, 1, sock_send_impl, &ctx, 0, NULL, timeout) < 0)
goto done;
n = ctx.result;
assert(n >= 0);