Skip to content

Commit

Permalink
bpo-45847: Fix _multiprocessing on platforms without sem_open (GH-29833)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiran committed Nov 29, 2021
1 parent 87d5180 commit 4dc198d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 11 deletions.
86 changes: 77 additions & 9 deletions Modules/_multiprocessing/clinic/semaphore.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -52,9 +52,9 @@ _multiprocessing_SemLock_acquire(SemLockObject *self, PyObject *const *args, Py_
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"
Expand All @@ -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"
Expand Down Expand Up @@ -126,9 +126,9 @@ _multiprocessing_SemLock_acquire(SemLockObject *self, PyObject *const *args, Py_
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"
Expand All @@ -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,
Expand Down Expand Up @@ -208,6 +210,10 @@ _multiprocessing_SemLock(PyTypeObject *type, PyObject *args, PyObject *kwargs)
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"
Expand Down Expand Up @@ -240,6 +246,10 @@ _multiprocessing_SemLock__rebuild(PyTypeObject *type, PyObject *const *args, Py_
return return_value;
}

#endif /* defined(HAVE_MP_SEMAPHORE) */

#if defined(HAVE_MP_SEMAPHORE)

PyDoc_STRVAR(_multiprocessing_SemLock__count__doc__,
"_count($self, /)\n"
"--\n"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -392,11 +426,45 @@ _multiprocessing_SemLock___exit__(SemLockObject *self, PyObject *const *args, Py
return return_value;
}

#endif /* defined(HAVE_MP_SEMAPHORE) */

#ifndef _MULTIPROCESSING_SEMLOCK_ACQUIRE_METHODDEF
#define _MULTIPROCESSING_SEMLOCK_ACQUIRE_METHODDEF
#endif /* !defined(_MULTIPROCESSING_SEMLOCK_ACQUIRE_METHODDEF) */

#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]*/
3 changes: 1 addition & 2 deletions Modules/_multiprocessing/multiprocessing.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions Modules/_multiprocessing/multiprocessing.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
# endif
# define SEM_HANDLE HANDLE
# define SEM_VALUE_MAX LONG_MAX
# define HAVE_MP_SEMAPHORE
#else
# include <fcntl.h> /* O_CREAT and O_EXCL */
# if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)
# define HAVE_MP_SEMAPHORE
# include <semaphore.h>
typedef sem_t *SEM_HANDLE;
# endif
Expand Down
4 changes: 4 additions & 0 deletions Modules/_multiprocessing/semaphore.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include "multiprocessing.h"

#ifdef HAVE_MP_SEMAPHORE

enum { RECURSIVE_MUTEX, SEMAPHORE };

typedef struct {
Expand Down Expand Up @@ -794,3 +796,5 @@ _PyMp_sem_unlink(const char *name)

Py_RETURN_NONE;
}

#endif // HAVE_MP_SEMAPHORE

0 comments on commit 4dc198d

Please sign in to comment.