From 4dc198dedd66e3247aa934fb91c9a158c3eea15a Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Mon, 29 Nov 2021 11:36:10 +0200 Subject: [PATCH] bpo-45847: Fix _multiprocessing on platforms without sem_open (GH-29833) --- Modules/_multiprocessing/clinic/semaphore.c.h | 86 +++++++++++++++++-- Modules/_multiprocessing/multiprocessing.c | 3 +- Modules/_multiprocessing/multiprocessing.h | 2 + Modules/_multiprocessing/semaphore.c | 4 + 4 files changed, 84 insertions(+), 11 deletions(-) diff --git a/Modules/_multiprocessing/clinic/semaphore.c.h b/Modules/_multiprocessing/clinic/semaphore.c.h index e1b9309e9f2..018447ad137 100644 --- a/Modules/_multiprocessing/clinic/semaphore.c.h +++ b/Modules/_multiprocessing/clinic/semaphore.c.h @@ -2,7 +2,7 @@ preserve [clinic start generated code]*/ -#if defined(MS_WINDOWS) +#if defined(HAVE_MP_SEMAPHORE) && defined(MS_WINDOWS) PyDoc_STRVAR(_multiprocessing_SemLock_acquire__doc__, "acquire($self, /, block=True, timeout=None)\n" @@ -52,9 +52,9 @@ exit: return return_value; } -#endif /* defined(MS_WINDOWS) */ +#endif /* defined(HAVE_MP_SEMAPHORE) && defined(MS_WINDOWS) */ -#if defined(MS_WINDOWS) +#if defined(HAVE_MP_SEMAPHORE) && defined(MS_WINDOWS) PyDoc_STRVAR(_multiprocessing_SemLock_release__doc__, "release($self, /)\n" @@ -74,9 +74,9 @@ _multiprocessing_SemLock_release(SemLockObject *self, PyObject *Py_UNUSED(ignore return _multiprocessing_SemLock_release_impl(self); } -#endif /* defined(MS_WINDOWS) */ +#endif /* defined(HAVE_MP_SEMAPHORE) && defined(MS_WINDOWS) */ -#if !defined(MS_WINDOWS) +#if defined(HAVE_MP_SEMAPHORE) && !defined(MS_WINDOWS) PyDoc_STRVAR(_multiprocessing_SemLock_acquire__doc__, "acquire($self, /, block=True, timeout=None)\n" @@ -126,9 +126,9 @@ exit: return return_value; } -#endif /* !defined(MS_WINDOWS) */ +#endif /* defined(HAVE_MP_SEMAPHORE) && !defined(MS_WINDOWS) */ -#if !defined(MS_WINDOWS) +#if defined(HAVE_MP_SEMAPHORE) && !defined(MS_WINDOWS) PyDoc_STRVAR(_multiprocessing_SemLock_release__doc__, "release($self, /)\n" @@ -148,7 +148,9 @@ _multiprocessing_SemLock_release(SemLockObject *self, PyObject *Py_UNUSED(ignore return _multiprocessing_SemLock_release_impl(self); } -#endif /* !defined(MS_WINDOWS) */ +#endif /* defined(HAVE_MP_SEMAPHORE) && !defined(MS_WINDOWS) */ + +#if defined(HAVE_MP_SEMAPHORE) static PyObject * _multiprocessing_SemLock_impl(PyTypeObject *type, int kind, int value, @@ -208,6 +210,10 @@ exit: return return_value; } +#endif /* defined(HAVE_MP_SEMAPHORE) */ + +#if defined(HAVE_MP_SEMAPHORE) + PyDoc_STRVAR(_multiprocessing_SemLock__rebuild__doc__, "_rebuild($type, handle, kind, maxvalue, name, /)\n" "--\n" @@ -240,6 +246,10 @@ exit: return return_value; } +#endif /* defined(HAVE_MP_SEMAPHORE) */ + +#if defined(HAVE_MP_SEMAPHORE) + PyDoc_STRVAR(_multiprocessing_SemLock__count__doc__, "_count($self, /)\n" "--\n" @@ -258,6 +268,10 @@ _multiprocessing_SemLock__count(SemLockObject *self, PyObject *Py_UNUSED(ignored return _multiprocessing_SemLock__count_impl(self); } +#endif /* defined(HAVE_MP_SEMAPHORE) */ + +#if defined(HAVE_MP_SEMAPHORE) + PyDoc_STRVAR(_multiprocessing_SemLock__is_mine__doc__, "_is_mine($self, /)\n" "--\n" @@ -276,6 +290,10 @@ _multiprocessing_SemLock__is_mine(SemLockObject *self, PyObject *Py_UNUSED(ignor return _multiprocessing_SemLock__is_mine_impl(self); } +#endif /* defined(HAVE_MP_SEMAPHORE) */ + +#if defined(HAVE_MP_SEMAPHORE) + PyDoc_STRVAR(_multiprocessing_SemLock__get_value__doc__, "_get_value($self, /)\n" "--\n" @@ -294,6 +312,10 @@ _multiprocessing_SemLock__get_value(SemLockObject *self, PyObject *Py_UNUSED(ign return _multiprocessing_SemLock__get_value_impl(self); } +#endif /* defined(HAVE_MP_SEMAPHORE) */ + +#if defined(HAVE_MP_SEMAPHORE) + PyDoc_STRVAR(_multiprocessing_SemLock__is_zero__doc__, "_is_zero($self, /)\n" "--\n" @@ -312,6 +334,10 @@ _multiprocessing_SemLock__is_zero(SemLockObject *self, PyObject *Py_UNUSED(ignor return _multiprocessing_SemLock__is_zero_impl(self); } +#endif /* defined(HAVE_MP_SEMAPHORE) */ + +#if defined(HAVE_MP_SEMAPHORE) + PyDoc_STRVAR(_multiprocessing_SemLock__after_fork__doc__, "_after_fork($self, /)\n" "--\n" @@ -330,6 +356,10 @@ _multiprocessing_SemLock__after_fork(SemLockObject *self, PyObject *Py_UNUSED(ig return _multiprocessing_SemLock__after_fork_impl(self); } +#endif /* defined(HAVE_MP_SEMAPHORE) */ + +#if defined(HAVE_MP_SEMAPHORE) + PyDoc_STRVAR(_multiprocessing_SemLock___enter____doc__, "__enter__($self, /)\n" "--\n" @@ -348,6 +378,10 @@ _multiprocessing_SemLock___enter__(SemLockObject *self, PyObject *Py_UNUSED(igno return _multiprocessing_SemLock___enter___impl(self); } +#endif /* defined(HAVE_MP_SEMAPHORE) */ + +#if defined(HAVE_MP_SEMAPHORE) + PyDoc_STRVAR(_multiprocessing_SemLock___exit____doc__, "__exit__($self, exc_type=None, exc_value=None, exc_tb=None, /)\n" "--\n" @@ -392,6 +426,8 @@ exit: return return_value; } +#endif /* defined(HAVE_MP_SEMAPHORE) */ + #ifndef _MULTIPROCESSING_SEMLOCK_ACQUIRE_METHODDEF #define _MULTIPROCESSING_SEMLOCK_ACQUIRE_METHODDEF #endif /* !defined(_MULTIPROCESSING_SEMLOCK_ACQUIRE_METHODDEF) */ @@ -399,4 +435,36 @@ exit: #ifndef _MULTIPROCESSING_SEMLOCK_RELEASE_METHODDEF #define _MULTIPROCESSING_SEMLOCK_RELEASE_METHODDEF #endif /* !defined(_MULTIPROCESSING_SEMLOCK_RELEASE_METHODDEF) */ -/*[clinic end generated code: output=e7fd938150601fe5 input=a9049054013a1b77]*/ + +#ifndef _MULTIPROCESSING_SEMLOCK__REBUILD_METHODDEF + #define _MULTIPROCESSING_SEMLOCK__REBUILD_METHODDEF +#endif /* !defined(_MULTIPROCESSING_SEMLOCK__REBUILD_METHODDEF) */ + +#ifndef _MULTIPROCESSING_SEMLOCK__COUNT_METHODDEF + #define _MULTIPROCESSING_SEMLOCK__COUNT_METHODDEF +#endif /* !defined(_MULTIPROCESSING_SEMLOCK__COUNT_METHODDEF) */ + +#ifndef _MULTIPROCESSING_SEMLOCK__IS_MINE_METHODDEF + #define _MULTIPROCESSING_SEMLOCK__IS_MINE_METHODDEF +#endif /* !defined(_MULTIPROCESSING_SEMLOCK__IS_MINE_METHODDEF) */ + +#ifndef _MULTIPROCESSING_SEMLOCK__GET_VALUE_METHODDEF + #define _MULTIPROCESSING_SEMLOCK__GET_VALUE_METHODDEF +#endif /* !defined(_MULTIPROCESSING_SEMLOCK__GET_VALUE_METHODDEF) */ + +#ifndef _MULTIPROCESSING_SEMLOCK__IS_ZERO_METHODDEF + #define _MULTIPROCESSING_SEMLOCK__IS_ZERO_METHODDEF +#endif /* !defined(_MULTIPROCESSING_SEMLOCK__IS_ZERO_METHODDEF) */ + +#ifndef _MULTIPROCESSING_SEMLOCK__AFTER_FORK_METHODDEF + #define _MULTIPROCESSING_SEMLOCK__AFTER_FORK_METHODDEF +#endif /* !defined(_MULTIPROCESSING_SEMLOCK__AFTER_FORK_METHODDEF) */ + +#ifndef _MULTIPROCESSING_SEMLOCK___ENTER___METHODDEF + #define _MULTIPROCESSING_SEMLOCK___ENTER___METHODDEF +#endif /* !defined(_MULTIPROCESSING_SEMLOCK___ENTER___METHODDEF) */ + +#ifndef _MULTIPROCESSING_SEMLOCK___EXIT___METHODDEF + #define _MULTIPROCESSING_SEMLOCK___EXIT___METHODDEF +#endif /* !defined(_MULTIPROCESSING_SEMLOCK___EXIT___METHODDEF) */ +/*[clinic end generated code: output=fde05a081b5bdba4 input=a9049054013a1b77]*/ diff --git a/Modules/_multiprocessing/multiprocessing.c b/Modules/_multiprocessing/multiprocessing.c index bec23517fca..0809c2455db 100644 --- a/Modules/_multiprocessing/multiprocessing.c +++ b/Modules/_multiprocessing/multiprocessing.c @@ -186,8 +186,7 @@ static PyMethodDef module_methods[] = { static int multiprocessing_exec(PyObject *module) { -#if defined(MS_WINDOWS) || \ - (defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)) +#ifdef HAVE_MP_SEMAPHORE /* Add _PyMp_SemLock type to module */ if (PyModule_AddType(module, &_PyMp_SemLockType) < 0) { diff --git a/Modules/_multiprocessing/multiprocessing.h b/Modules/_multiprocessing/multiprocessing.h index 277963bc157..3a8314b1db8 100644 --- a/Modules/_multiprocessing/multiprocessing.h +++ b/Modules/_multiprocessing/multiprocessing.h @@ -21,9 +21,11 @@ # endif # define SEM_HANDLE HANDLE # define SEM_VALUE_MAX LONG_MAX +# define HAVE_MP_SEMAPHORE #else # include /* O_CREAT and O_EXCL */ # if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED) +# define HAVE_MP_SEMAPHORE # include typedef sem_t *SEM_HANDLE; # endif diff --git a/Modules/_multiprocessing/semaphore.c b/Modules/_multiprocessing/semaphore.c index 9a2d1f85c92..8607476aff1 100644 --- a/Modules/_multiprocessing/semaphore.c +++ b/Modules/_multiprocessing/semaphore.c @@ -9,6 +9,8 @@ #include "multiprocessing.h" +#ifdef HAVE_MP_SEMAPHORE + enum { RECURSIVE_MUTEX, SEMAPHORE }; typedef struct { @@ -794,3 +796,5 @@ _PyMp_sem_unlink(const char *name) Py_RETURN_NONE; } + +#endif // HAVE_MP_SEMAPHORE