Skip to content

Commit

Permalink
socketmodule.c: use relaxed atomics for global 'defaulttimeout'
Browse files Browse the repository at this point in the history
  • Loading branch information
colesbury committed Apr 23, 2023
1 parent 70856f1 commit 360a79c
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1016,8 +1016,8 @@ init_sockobject(PySocketSockObject *s,
else
#endif
{
s->sock_timeout = defaulttimeout;
if (defaulttimeout >= 0) {
s->sock_timeout = _Py_atomic_load_int64_relaxed(&defaulttimeout);
if (s->sock_timeout >= 0) {
if (internal_setblocking(s, 0) == -1) {
return -1;
}
Expand Down Expand Up @@ -6873,11 +6873,12 @@ Get host and port for a sockaddr.");
static PyObject *
socket_getdefaulttimeout(PyObject *self, PyObject *Py_UNUSED(ignored))
{
if (defaulttimeout < 0) {
_PyTime_t timeout = _Py_atomic_load_int64_relaxed(&defaulttimeout);
if (timeout < 0) {
Py_RETURN_NONE;
}
else {
double seconds = _PyTime_AsSecondsDouble(defaulttimeout);
double seconds = _PyTime_AsSecondsDouble(timeout);
return PyFloat_FromDouble(seconds);
}
}
Expand All @@ -6897,7 +6898,7 @@ socket_setdefaulttimeout(PyObject *self, PyObject *arg)
if (socket_parse_timeout(&timeout, arg) < 0)
return NULL;

defaulttimeout = timeout;
_Py_atomic_store_int64_relaxed(&defaulttimeout, timeout);

Py_RETURN_NONE;
}
Expand Down

0 comments on commit 360a79c

Please sign in to comment.