summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorRonald Oussoren <ronaldoussoren@mac.com>2022-11-22 10:14:23 (GMT)
committerGitHub <noreply@github.com>2022-11-22 10:14:23 (GMT)
commit959ba45d75953caa911e16b4c2a277978fc4b9b0 (patch)
treeff31e9022498a9ee7176466abb27b75cd041c59b /Modules
parent4d82f628c44490d6fbc3f6998d2473d1304d891f (diff)
downloadcpython-959ba45d75953caa911e16b4c2a277978fc4b9b0.zip
cpython-959ba45d75953caa911e16b4c2a277978fc4b9b0.tar.gz
cpython-959ba45d75953caa911e16b4c2a277978fc4b9b0.tar.bz2
GH-97001: Release GIL in termios extension (#99503)
Without releasing the GIL calls to termios APIs might block the entire interpreter.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/termios.c88
1 files changed, 76 insertions, 12 deletions
diff --git a/Modules/termios.c b/Modules/termios.c
index 354e5ca..fcc8f04 100644
--- a/Modules/termios.c
+++ b/Modules/termios.c
@@ -82,7 +82,12 @@ termios_tcgetattr_impl(PyObject *module, int fd)
{
termiosmodulestate *state = PyModule_GetState(module);
struct termios mode;
- if (tcgetattr(fd, &mode) == -1) {
+ int r;
+
+ Py_BEGIN_ALLOW_THREADS
+ r = tcgetattr(fd, &mode);
+ Py_END_ALLOW_THREADS
+ if (r == -1) {
return PyErr_SetFromErrno(state->TermiosError);
}
@@ -169,7 +174,12 @@ termios_tcsetattr_impl(PyObject *module, int fd, int when, PyObject *term)
/* Get the old mode, in case there are any hidden fields... */
termiosmodulestate *state = PyModule_GetState(module);
struct termios mode;
- if (tcgetattr(fd, &mode) == -1) {
+ int r;
+
+ Py_BEGIN_ALLOW_THREADS
+ r = tcgetattr(fd, &mode);
+ Py_END_ALLOW_THREADS
+ if (r == -1) {
return PyErr_SetFromErrno(state->TermiosError);
}
@@ -211,7 +221,12 @@ termios_tcsetattr_impl(PyObject *module, int fd, int when, PyObject *term)
return PyErr_SetFromErrno(state->TermiosError);
if (cfsetospeed(&mode, (speed_t) ospeed) == -1)
return PyErr_SetFromErrno(state->TermiosError);
- if (tcsetattr(fd, when, &mode) == -1)
+
+ Py_BEGIN_ALLOW_THREADS
+ r = tcsetattr(fd, when, &mode);
+ Py_END_ALLOW_THREADS
+
+ if (r == -1)
return PyErr_SetFromErrno(state->TermiosError);
Py_RETURN_NONE;
@@ -235,7 +250,13 @@ termios_tcsendbreak_impl(PyObject *module, int fd, int duration)
/*[clinic end generated code: output=5945f589b5d3ac66 input=dc2f32417691f8ed]*/
{
termiosmodulestate *state = PyModule_GetState(module);
- if (tcsendbreak(fd, duration) == -1) {
+ int r;
+
+ Py_BEGIN_ALLOW_THREADS
+ r = tcsendbreak(fd, duration);
+ Py_END_ALLOW_THREADS
+
+ if (r == -1) {
return PyErr_SetFromErrno(state->TermiosError);
}
@@ -256,7 +277,13 @@ termios_tcdrain_impl(PyObject *module, int fd)
/*[clinic end generated code: output=5fd86944c6255955 input=c99241b140b32447]*/
{
termiosmodulestate *state = PyModule_GetState(module);
- if (tcdrain(fd) == -1) {
+ int r;
+
+ Py_BEGIN_ALLOW_THREADS
+ r = tcdrain(fd);
+ Py_END_ALLOW_THREADS
+
+ if (r == -1) {
return PyErr_SetFromErrno(state->TermiosError);
}
@@ -282,7 +309,13 @@ termios_tcflush_impl(PyObject *module, int fd, int queue)
/*[clinic end generated code: output=2424f80312ec2f21 input=0f7d08122ddc07b5]*/
{
termiosmodulestate *state = PyModule_GetState(module);
- if (tcflush(fd, queue) == -1) {
+ int r;
+
+ Py_BEGIN_ALLOW_THREADS
+ r = tcflush(fd, queue);
+ Py_END_ALLOW_THREADS
+
+ if (r == -1) {
return PyErr_SetFromErrno(state->TermiosError);
}
@@ -308,7 +341,13 @@ termios_tcflow_impl(PyObject *module, int fd, int action)
/*[clinic end generated code: output=afd10928e6ea66eb input=c6aff0640b6efd9c]*/
{
termiosmodulestate *state = PyModule_GetState(module);
- if (tcflow(fd, action) == -1) {
+ int r;
+
+ Py_BEGIN_ALLOW_THREADS
+ r = tcflow(fd, action);
+ Py_END_ALLOW_THREADS
+
+ if (r == -1) {
return PyErr_SetFromErrno(state->TermiosError);
}
@@ -333,7 +372,13 @@ termios_tcgetwinsize_impl(PyObject *module, int fd)
#if defined(TIOCGWINSZ)
termiosmodulestate *state = PyModule_GetState(module);
struct winsize w;
- if (ioctl(fd, TIOCGWINSZ, &w) == -1) {
+ int r;
+
+ Py_BEGIN_ALLOW_THREADS
+ r = ioctl(fd, TIOCGWINSZ, &w);
+ Py_END_ALLOW_THREADS
+
+ if (r == -1) {
return PyErr_SetFromErrno(state->TermiosError);
}
@@ -352,7 +397,12 @@ termios_tcgetwinsize_impl(PyObject *module, int fd)
#elif defined(TIOCGSIZE)
termiosmodulestate *state = PyModule_GetState(module);
struct ttysize s;
- if (ioctl(fd, TIOCGSIZE, &s) == -1) {
+ int r;
+
+ Py_BEGIN_ALLOW_THREADS
+ r = ioctl(fd, TIOCGSIZE, &s);
+ Py_END_ALLOW_THREADS
+ if (r == -1) {
return PyErr_SetFromErrno(state->TermiosError);
}
@@ -433,15 +483,25 @@ termios_tcsetwinsize_impl(PyObject *module, int fd, PyObject *winsz)
return NULL;
}
- if (ioctl(fd, TIOCSWINSZ, &w) == -1) {
+ int r;
+ Py_BEGIN_ALLOW_THREADS
+ r = ioctl(fd, TIOCSWINSZ, &w);
+ Py_END_ALLOW_THREADS
+
+ if (r == -1) {
return PyErr_SetFromErrno(state->TermiosError);
}
Py_RETURN_NONE;
#elif defined(TIOCGSIZE) && defined(TIOCSSIZE)
struct ttysize s;
+ int r;
/* Get the old ttysize because it might have more fields. */
- if (ioctl(fd, TIOCGSIZE, &s) == -1) {
+ Py_BEGIN_ALLOW_THREADS
+ r = ioctl(fd, TIOCGSIZE, &s);
+ Py_END_ALLOW_THREADS
+
+ if (r == -1) {
return PyErr_SetFromErrno(state->TermiosError);
}
@@ -453,7 +513,11 @@ termios_tcsetwinsize_impl(PyObject *module, int fd, PyObject *winsz)
return NULL;
}
- if (ioctl(fd, TIOCSSIZE, &s) == -1) {
+ Py_BEGIN_ALLOW_THREADS
+ r = ioctl(fd, TIOCSSIZE, &s);
+ Py_END_ALLOW_THREADS
+
+ if (r == -1) {
return PyErr_SetFromErrno(state->TermiosError);
}