summaryrefslogtreecommitdiffstats
path: root/Modules/posixmodule.c
diff options
context:
space:
mode:
authorChristian Heimes <christian@python.org>2020-11-13 18:48:52 (GMT)
committerGitHub <noreply@github.com>2020-11-13 18:48:52 (GMT)
commitcd9fed6afba6f3ad2e7ef723501c739551a95fa8 (patch)
treecf6114ff87b83e0a3fabdbbf59d06eb10e55b6db /Modules/posixmodule.c
parentbbeb2d266d6fc1ca9778726d0397d9d6f7a946e3 (diff)
downloadcpython-cd9fed6afba6f3ad2e7ef723501c739551a95fa8.zip
cpython-cd9fed6afba6f3ad2e7ef723501c739551a95fa8.tar.gz
cpython-cd9fed6afba6f3ad2e7ef723501c739551a95fa8.tar.bz2
bpo-41001: Add os.eventfd() (#20930)
Co-authored-by: Kyle Stanley <aeros167@gmail.com>
Diffstat (limited to 'Modules/posixmodule.c')
-rw-r--r--Modules/posixmodule.c91
1 files changed, 91 insertions, 0 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 6b51d8a..0764453 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -518,6 +518,11 @@ extern char *ctermid_r(char *);
# include <linux/memfd.h>
#endif
+/* eventfd() */
+#ifdef HAVE_SYS_EVENTFD_H
+# include <sys/eventfd.h>
+#endif
+
#ifdef _Py_MEMORY_SANITIZER
# include <sanitizer/msan_interface.h>
#endif
@@ -12859,6 +12864,79 @@ os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags)
}
#endif
+#ifdef HAVE_EVENTFD
+/*[clinic input]
+os.eventfd
+
+ initval: unsigned_int
+ flags: int(c_default="EFD_CLOEXEC") = EFD_CLOEXEC
+
+Creates and returns an event notification file descriptor.
+[clinic start generated code]*/
+
+static PyObject *
+os_eventfd_impl(PyObject *module, unsigned int initval, int flags)
+/*[clinic end generated code: output=ce9c9bbd1446f2de input=66203e3c50c4028b]*/
+
+{
+ /* initval is limited to uint32_t, internal counter is uint64_t */
+ int fd;
+ Py_BEGIN_ALLOW_THREADS
+ fd = eventfd(initval, flags);
+ Py_END_ALLOW_THREADS
+ if (fd == -1) {
+ return PyErr_SetFromErrno(PyExc_OSError);
+ }
+ return PyLong_FromLong(fd);
+}
+
+/*[clinic input]
+os.eventfd_read
+
+ fd: fildes
+
+Read eventfd value
+[clinic start generated code]*/
+
+static PyObject *
+os_eventfd_read_impl(PyObject *module, int fd)
+/*[clinic end generated code: output=8f2c7b59a3521fd1 input=110f8b57fa596afe]*/
+{
+ eventfd_t value;
+ int result;
+ Py_BEGIN_ALLOW_THREADS
+ result = eventfd_read(fd, &value);
+ Py_END_ALLOW_THREADS
+ if (result == -1) {
+ return PyErr_SetFromErrno(PyExc_OSError);
+ }
+ return PyLong_FromUnsignedLongLong(value);
+}
+
+/*[clinic input]
+os.eventfd_write
+
+ fd: fildes
+ value: unsigned_long_long
+
+Write eventfd value.
+[clinic start generated code]*/
+
+static PyObject *
+os_eventfd_write_impl(PyObject *module, int fd, unsigned long long value)
+/*[clinic end generated code: output=bebd9040bbf987f5 input=156de8555be5a949]*/
+{
+ int result;
+ Py_BEGIN_ALLOW_THREADS
+ result = eventfd_write(fd, value);
+ Py_END_ALLOW_THREADS
+ if (result == -1) {
+ return PyErr_SetFromErrno(PyExc_OSError);
+ }
+ Py_RETURN_NONE;
+}
+#endif /* HAVE_EVENTFD */
+
/* Terminal size querying */
PyDoc_STRVAR(TerminalSize_docstring,
@@ -14619,6 +14697,9 @@ static PyMethodDef posix_methods[] = {
OS_FSPATH_METHODDEF
OS_GETRANDOM_METHODDEF
OS_MEMFD_CREATE_METHODDEF
+ OS_EVENTFD_METHODDEF
+ OS_EVENTFD_READ_METHODDEF
+ OS_EVENTFD_WRITE_METHODDEF
OS__ADD_DLL_DIRECTORY_METHODDEF
OS__REMOVE_DLL_DIRECTORY_METHODDEF
OS_WAITSTATUS_TO_EXITCODE_METHODDEF
@@ -15127,6 +15208,12 @@ all_ins(PyObject *m)
#ifdef MFD_HUGE_16GB
if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1;
#endif
+#endif /* HAVE_MEMFD_CREATE */
+
+#ifdef HAVE_EVENTFD
+ if (PyModule_AddIntMacro(m, EFD_CLOEXEC)) return -1;
+ if (PyModule_AddIntMacro(m, EFD_NONBLOCK)) return -1;
+ if (PyModule_AddIntMacro(m, EFD_SEMAPHORE)) return -1;
#endif
#if defined(__APPLE__)
@@ -15220,6 +15307,10 @@ static const struct have_function {
int (*probe)(void);
} have_functions[] = {
+#ifdef HAVE_EVENTFD
+ {"HAVE_EVENTFD", NULL},
+#endif
+
#ifdef HAVE_FACCESSAT
{ "HAVE_FACCESSAT", probe_faccessat },
#endif