Skip to content

Commit 9c54376

Browse files
remove macros
1 parent ef6f457 commit 9c54376

File tree

1 file changed

+23
-31
lines changed

1 file changed

+23
-31
lines changed

Modules/socketmodule.c

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ Local naming conventions:
110110
#include "pycore_fileutils.h" // _Py_set_inheritable()
111111
#include "pycore_moduleobject.h" // _PyModule_GetState
112112
#include "pycore_time.h" // _PyTime_AsMilliseconds()
113-
#include "pycore_pyatomic_ft_wrappers.h"
114113

115114
#ifdef _Py_MEMORY_SANITIZER
116115
# include <sanitizer/msan_interface.h>
@@ -547,33 +546,22 @@ typedef struct _socket_state {
547546
by this module (but not argument type or memory errors, etc.). */
548547
PyObject *socket_herror;
549548
PyObject *socket_gaierror;
550-
} socket_state;
551-
552-
/* Default timeout for new sockets */
553-
static PyTime_t defaulttimeout = _PYTIME_FROMSECONDS(-1);
554549

555-
#define SET_DEFAULT_TIMEOUT(timeout) FT_ATOMIC_STORE_INT64_RELAXED(defaulttimeout, timeout)
556-
#define GET_DEFAULT_TIMEOUT() FT_ATOMIC_LOAD_INT64_RELAXED(defaulttimeout)
550+
/* Default timeout for new sockets */
551+
PyTime_t defaulttimeout;
552+
} socket_state;
557553

558554
#if defined(HAVE_ACCEPT) || defined(HAVE_ACCEPT4)
559555
#if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
560556
/* accept4() is available on Linux 2.6.28+ and glibc 2.10 */
561557
static int accept4_works = -1;
562-
563-
#define SET_ACCEPT4_WORKS(value) FT_ATOMIC_STORE_INT_RELAXED(accept4_works, value)
564-
#define GET_ACCEPT4_WORKS() FT_ATOMIC_LOAD_INT_RELAXED(accept4_works)
565-
566558
#endif
567559
#endif
568560

569561
#ifdef SOCK_CLOEXEC
570562
/* socket() and socketpair() fail with EINVAL on Linux kernel older
571563
* than 2.6.27 if SOCK_CLOEXEC flag is set in the socket type. */
572564
static int sock_cloexec_works = -1;
573-
574-
#define SET_SOCK_CLOEXEC_WORKS(value) FT_ATOMIC_STORE_INT_RELAXED(sock_cloexec_works, value)
575-
#define GET_SOCK_CLOEXEC_WORKS() FT_ATOMIC_LOAD_INT_RELAXED(sock_cloexec_works)
576-
577565
#endif
578566

579567
static inline socket_state *
@@ -1069,7 +1057,7 @@ init_sockobject(socket_state *state, PySocketSockObject *s,
10691057
else
10701058
#endif
10711059
{
1072-
s->sock_timeout = GET_DEFAULT_TIMEOUT();
1060+
s->sock_timeout = _Py_atomic_load_int64_relaxed(&state->defaulttimeout);
10731061
if (s->sock_timeout >= 0) {
10741062
if (internal_setblocking(s, 0) == -1) {
10751063
return -1;
@@ -2879,15 +2867,15 @@ sock_accept_impl(PySocketSockObject *s, void *data)
28792867
#endif
28802868

28812869
#if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
2882-
if (GET_ACCEPT4_WORKS() != 0) {
2870+
if (_Py_atomic_load_int_relaxed(&accept4_works) != 0) {
28832871
ctx->result = accept4(s->sock_fd, addr, paddrlen,
28842872
SOCK_CLOEXEC);
2885-
if (ctx->result == INVALID_SOCKET && GET_ACCEPT4_WORKS() == -1) {
2873+
if (ctx->result == INVALID_SOCKET && _Py_atomic_load_int_relaxed(&accept4_works) == -1) {
28862874
/* On Linux older than 2.6.28, accept4() fails with ENOSYS */
2887-
SET_ACCEPT4_WORKS(errno != ENOSYS);
2875+
_Py_atomic_store_int_relaxed(&accept4_works, errno != ENOSYS);
28882876
}
28892877
}
2890-
if (GET_ACCEPT4_WORKS() == 0)
2878+
if (_Py_atomic_load_int_relaxed(&accept4_works) == 0)
28912879
ctx->result = accept(s->sock_fd, addr, paddrlen);
28922880
#else
28932881
ctx->result = accept(s->sock_fd, addr, paddrlen);
@@ -2940,7 +2928,7 @@ sock_accept(PySocketSockObject *s, PyObject *Py_UNUSED(ignored))
29402928
#else
29412929

29422930
#if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
2943-
if (!GET_ACCEPT4_WORKS())
2931+
if (!_Py_atomic_load_int_relaxed(&accept4_works))
29442932
#endif
29452933
{
29462934
if (_Py_set_inheritable(newfd, 0, NULL) < 0) {
@@ -5556,16 +5544,16 @@ sock_initobj_impl(PySocketSockObject *self, int family, int type, int proto,
55565544
/* UNIX */
55575545
Py_BEGIN_ALLOW_THREADS
55585546
#ifdef SOCK_CLOEXEC
5559-
if (GET_SOCK_CLOEXEC_WORKS() != 0) {
5547+
if (_Py_atomic_load_int_relaxed(&sock_cloexec_works) != 0) {
55605548
fd = socket(family, type | SOCK_CLOEXEC, proto);
5561-
if (GET_SOCK_CLOEXEC_WORKS() == -1) {
5549+
if (_Py_atomic_load_int_relaxed(&sock_cloexec_works) == -1) {
55625550
if (fd >= 0) {
5563-
SET_SOCK_CLOEXEC_WORKS(1);
5551+
_Py_atomic_store_int_relaxed(&sock_cloexec_works, 1);
55645552
}
55655553

55665554
else if (errno == EINVAL) {
55675555
/* Linux older than 2.6.27 does not support SOCK_CLOEXEC */
5568-
SET_SOCK_CLOEXEC_WORKS(0);
5556+
_Py_atomic_store_int_relaxed(&sock_cloexec_works, 0);
55695557
fd = socket(family, type, proto);
55705558
}
55715559
}
@@ -6324,15 +6312,15 @@ socket_socketpair(PyObject *self, PyObject *args)
63246312
/* Create a pair of socket fds */
63256313
Py_BEGIN_ALLOW_THREADS
63266314
#ifdef SOCK_CLOEXEC
6327-
if (GET_SOCK_CLOEXEC_WORKS() != 0) {
6315+
if (_Py_atomic_load_int_relaxed(&sock_cloexec_works) != 0) {
63286316
ret = socketpair(family, type | SOCK_CLOEXEC, proto, sv);
6329-
if (GET_SOCK_CLOEXEC_WORKS() == -1) {
6317+
if (_Py_atomic_load_int_relaxed(&sock_cloexec_works) == -1) {
63306318
if (ret >= 0) {
6331-
SET_SOCK_CLOEXEC_WORKS(1);
6319+
_Py_atomic_store_int_relaxed(&sock_cloexec_works, 1);
63326320
}
63336321
else if (errno == EINVAL) {
63346322
/* Linux older than 2.6.27 does not support SOCK_CLOEXEC */
6335-
SET_SOCK_CLOEXEC_WORKS(0);
6323+
_Py_atomic_store_int_relaxed(&sock_cloexec_works, 0);
63366324
ret = socketpair(family, type, proto, sv);
63376325
}
63386326
}
@@ -6968,7 +6956,8 @@ Get host and port for a sockaddr.");
69686956
static PyObject *
69696957
socket_getdefaulttimeout(PyObject *self, PyObject *Py_UNUSED(ignored))
69706958
{
6971-
PyTime_t timeout = GET_DEFAULT_TIMEOUT();
6959+
socket_state *state = get_module_state(self);
6960+
PyTime_t timeout = _Py_atomic_load_int64_relaxed(&state->defaulttimeout);
69726961
if (timeout < 0) {
69736962
Py_RETURN_NONE;
69746963
}
@@ -6993,7 +6982,8 @@ socket_setdefaulttimeout(PyObject *self, PyObject *arg)
69936982
if (socket_parse_timeout(&timeout, arg) < 0)
69946983
return NULL;
69956984

6996-
SET_DEFAULT_TIMEOUT(timeout);
6985+
socket_state *state = get_module_state(self);
6986+
_Py_atomic_store_int64_relaxed(&state->defaulttimeout, timeout);
69976987

69986988
Py_RETURN_NONE;
69996989
}
@@ -7439,6 +7429,8 @@ socket_exec(PyObject *m)
74397429

74407430
socket_state *state = get_module_state(m);
74417431

7432+
_Py_atomic_store_int64_relaxed(&state->defaulttimeout, _PYTIME_FROMSECONDS(-1));
7433+
74427434
#define ADD_EXC(MOD, NAME, VAR, BASE) do { \
74437435
VAR = PyErr_NewException("socket." NAME, BASE, NULL); \
74447436
if (VAR == NULL) { \

0 commit comments

Comments
 (0)