2008-06-13 16:28:21 -03:00
|
|
|
/*
|
|
|
|
* Extension module used by multiprocessing package
|
|
|
|
*
|
|
|
|
* multiprocessing.c
|
|
|
|
*
|
2012-04-30 08:13:55 -03:00
|
|
|
* Copyright (c) 2006-2008, R Oudkerk
|
|
|
|
* Licensed to PSF under a Contributor Agreement.
|
2008-06-13 16:28:21 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "multiprocessing.h"
|
|
|
|
|
2020-07-12 13:11:11 -03:00
|
|
|
/*[python input]
|
|
|
|
class HANDLE_converter(CConverter):
|
|
|
|
type = "HANDLE"
|
|
|
|
format_unit = '"F_HANDLE"'
|
|
|
|
|
2023-09-03 11:28:14 -03:00
|
|
|
def parse_arg(self, argname, displayname, *, limited_capi):
|
|
|
|
return self.format_code("""
|
2022-07-04 10:11:11 -03:00
|
|
|
{paramname} = PyLong_AsVoidPtr({argname});
|
|
|
|
if (!{paramname} && PyErr_Occurred()) {{{{
|
|
|
|
goto exit;
|
|
|
|
}}}}
|
2023-09-03 11:28:14 -03:00
|
|
|
""",
|
|
|
|
argname=argname)
|
2022-07-04 10:11:11 -03:00
|
|
|
|
2020-07-12 13:11:11 -03:00
|
|
|
[python start generated code]*/
|
2023-09-03 11:28:14 -03:00
|
|
|
/*[python end generated code: output=da39a3ee5e6b4b0d input=3cf0318efc6a8772]*/
|
2020-07-12 13:11:11 -03:00
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
module _multiprocessing
|
|
|
|
[clinic start generated code]*/
|
|
|
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=01e0745f380ac6e3]*/
|
|
|
|
|
|
|
|
#include "clinic/multiprocessing.c.h"
|
Merged revisions 70908,70939,71009,71022,71036 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r70908 | jesse.noller | 2009-03-31 17:20:35 -0500 (Tue, 31 Mar 2009) | 1 line
Issue 5619: Pass MS CRT debug flags into subprocesses
........
r70939 | jesse.noller | 2009-03-31 22:45:50 -0500 (Tue, 31 Mar 2009) | 1 line
Fix multiprocessing.event to match the new threading.Event API
........
r71009 | jesse.noller | 2009-04-01 19:03:28 -0500 (Wed, 01 Apr 2009) | 1 line
issue5545: Switch to Autoconf for multiprocessing; special thanks to Martin Lowis for help
........
r71022 | jesse.noller | 2009-04-01 21:32:55 -0500 (Wed, 01 Apr 2009) | 1 line
Issue 3110: Additional protection for SEM_VALUE_MAX on platforms, thanks to Martin Loewis
........
r71036 | jesse.noller | 2009-04-01 23:22:09 -0500 (Wed, 01 Apr 2009) | 1 line
Issue 3551: Raise ValueError if the size causes ERROR_NO_SYSTEM_RESOURCES
........
2009-04-05 18:24:58 -03:00
|
|
|
|
2008-06-13 16:28:21 -03:00
|
|
|
/*
|
|
|
|
* Function which raises exceptions based on error codes
|
|
|
|
*/
|
|
|
|
|
|
|
|
PyObject *
|
2012-10-07 14:08:47 -03:00
|
|
|
_PyMp_SetError(PyObject *Type, int num)
|
2008-06-13 16:28:21 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
switch (num) {
|
2008-06-13 16:28:21 -03:00
|
|
|
#ifdef MS_WINDOWS
|
2010-05-09 12:52:27 -03:00
|
|
|
case MP_STANDARD_ERROR:
|
|
|
|
if (Type == NULL)
|
2012-12-19 08:33:35 -04:00
|
|
|
Type = PyExc_OSError;
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_SetExcFromWindowsErr(Type, 0);
|
|
|
|
break;
|
|
|
|
case MP_SOCKET_ERROR:
|
|
|
|
if (Type == NULL)
|
2012-12-19 08:33:35 -04:00
|
|
|
Type = PyExc_OSError;
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_SetExcFromWindowsErr(Type, WSAGetLastError());
|
|
|
|
break;
|
2008-06-13 16:28:21 -03:00
|
|
|
#else /* !MS_WINDOWS */
|
2010-05-09 12:52:27 -03:00
|
|
|
case MP_STANDARD_ERROR:
|
|
|
|
case MP_SOCKET_ERROR:
|
|
|
|
if (Type == NULL)
|
|
|
|
Type = PyExc_OSError;
|
|
|
|
PyErr_SetFromErrno(Type);
|
|
|
|
break;
|
2008-06-13 16:28:21 -03:00
|
|
|
#endif /* !MS_WINDOWS */
|
2010-05-09 12:52:27 -03:00
|
|
|
case MP_MEMORY_ERROR:
|
|
|
|
PyErr_NoMemory();
|
|
|
|
break;
|
|
|
|
case MP_EXCEPTION_HAS_BEEN_SET:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PyErr_Format(PyExc_RuntimeError,
|
2013-05-15 13:06:56 -03:00
|
|
|
"unknown error number %d", num);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
return NULL;
|
2008-06-13 16:28:21 -03:00
|
|
|
}
|
|
|
|
|
2012-04-18 15:51:15 -03:00
|
|
|
#ifdef MS_WINDOWS
|
2020-07-12 13:11:11 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_multiprocessing.closesocket
|
|
|
|
|
|
|
|
handle: HANDLE
|
|
|
|
/
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2012-04-18 15:51:15 -03:00
|
|
|
static PyObject *
|
2020-07-12 13:11:11 -03:00
|
|
|
_multiprocessing_closesocket_impl(PyObject *module, HANDLE handle)
|
|
|
|
/*[clinic end generated code: output=214f359f900966f4 input=8a20706dd386c6cc]*/
|
2012-04-18 15:51:15 -03:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
ret = closesocket((SOCKET) handle);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
if (ret)
|
2017-04-16 04:46:38 -03:00
|
|
|
return PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
|
2012-04-18 15:51:15 -03:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2020-07-12 13:11:11 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_multiprocessing.recv
|
|
|
|
|
|
|
|
handle: HANDLE
|
|
|
|
size: int
|
|
|
|
/
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2012-04-18 15:51:15 -03:00
|
|
|
static PyObject *
|
2020-07-12 13:11:11 -03:00
|
|
|
_multiprocessing_recv_impl(PyObject *module, HANDLE handle, int size)
|
|
|
|
/*[clinic end generated code: output=92322781ba9ff598 input=6a5b0834372cee5b]*/
|
2012-04-18 15:51:15 -03:00
|
|
|
{
|
2020-07-12 13:11:11 -03:00
|
|
|
int nread;
|
2012-04-18 15:51:15 -03:00
|
|
|
PyObject *buf;
|
|
|
|
|
|
|
|
buf = PyBytes_FromStringAndSize(NULL, size);
|
|
|
|
if (!buf)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
nread = recv((SOCKET) handle, PyBytes_AS_STRING(buf), size, 0);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
if (nread < 0) {
|
|
|
|
Py_DECREF(buf);
|
2017-04-16 04:46:38 -03:00
|
|
|
return PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
|
2012-04-18 15:51:15 -03:00
|
|
|
}
|
|
|
|
_PyBytes_Resize(&buf, nread);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2020-07-12 13:11:11 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_multiprocessing.send
|
|
|
|
|
|
|
|
handle: HANDLE
|
|
|
|
buf: Py_buffer
|
|
|
|
/
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2012-04-18 15:51:15 -03:00
|
|
|
static PyObject *
|
2020-07-12 13:11:11 -03:00
|
|
|
_multiprocessing_send_impl(PyObject *module, HANDLE handle, Py_buffer *buf)
|
|
|
|
/*[clinic end generated code: output=52d7df0519c596cb input=41dce742f98d2210]*/
|
2012-04-18 15:51:15 -03:00
|
|
|
{
|
2013-09-07 13:40:45 -03:00
|
|
|
int ret, length;
|
2012-04-18 15:51:15 -03:00
|
|
|
|
2020-07-12 13:11:11 -03:00
|
|
|
length = (int)Py_MIN(buf->len, INT_MAX);
|
2013-09-07 13:40:45 -03:00
|
|
|
|
2012-04-18 15:51:15 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
2020-07-12 13:11:11 -03:00
|
|
|
ret = send((SOCKET) handle, buf->buf, length, 0);
|
2012-04-18 15:51:15 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
if (ret < 0)
|
2017-04-16 04:46:38 -03:00
|
|
|
return PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
|
2012-04-18 15:51:15 -03:00
|
|
|
return PyLong_FromLong(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2008-06-13 16:28:21 -03:00
|
|
|
|
2020-07-12 13:11:11 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_multiprocessing.sem_unlink
|
|
|
|
|
|
|
|
name: str
|
|
|
|
/
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_multiprocessing_sem_unlink_impl(PyObject *module, const char *name)
|
|
|
|
/*[clinic end generated code: output=fcbfeb1ed255e647 input=bf939aff9564f1d5]*/
|
|
|
|
{
|
|
|
|
return _PyMp_sem_unlink(name);
|
|
|
|
}
|
|
|
|
|
2008-06-13 16:28:21 -03:00
|
|
|
/*
|
|
|
|
* Function table
|
|
|
|
*/
|
|
|
|
|
|
|
|
static PyMethodDef module_methods[] = {
|
2012-04-18 15:51:15 -03:00
|
|
|
#ifdef MS_WINDOWS
|
2020-07-12 13:11:11 -03:00
|
|
|
_MULTIPROCESSING_CLOSESOCKET_METHODDEF
|
|
|
|
_MULTIPROCESSING_RECV_METHODDEF
|
|
|
|
_MULTIPROCESSING_SEND_METHODDEF
|
2012-04-18 15:51:15 -03:00
|
|
|
#endif
|
2016-05-07 15:13:50 -03:00
|
|
|
#if !defined(POSIX_SEMAPHORES_NOT_ENABLED) && !defined(__ANDROID__)
|
2020-07-12 13:11:11 -03:00
|
|
|
_MULTIPROCESSING_SEM_UNLINK_METHODDEF
|
2014-07-28 19:01:02 -03:00
|
|
|
#endif
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL}
|
2008-06-13 16:28:21 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize
|
|
|
|
*/
|
|
|
|
|
2020-08-11 07:32:35 -03:00
|
|
|
static int
|
|
|
|
multiprocessing_exec(PyObject *module)
|
2008-06-13 16:28:21 -03:00
|
|
|
{
|
2021-11-29 05:36:10 -04:00
|
|
|
#ifdef HAVE_MP_SEMAPHORE
|
2020-08-11 07:32:35 -03:00
|
|
|
|
2022-07-20 16:26:01 -03:00
|
|
|
PyTypeObject *semlock_type = (PyTypeObject *)PyType_FromModuleAndSpec(
|
|
|
|
module, &_PyMp_SemLockType_spec, NULL);
|
|
|
|
|
|
|
|
if (semlock_type == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int rc = PyModule_AddType(module, semlock_type);
|
|
|
|
Py_DECREF(semlock_type);
|
|
|
|
if (rc < 0) {
|
2020-08-11 07:32:35 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-07-20 16:26:01 -03:00
|
|
|
PyObject *py_sem_value_max;
|
|
|
|
/* Some systems define SEM_VALUE_MAX as an unsigned value that
|
|
|
|
* causes it to be negative when used as an int (NetBSD).
|
|
|
|
*
|
|
|
|
* Issue #28152: Use (0) instead of 0 to fix a warning on dead code
|
|
|
|
* when using clang -Wunreachable-code. */
|
|
|
|
if ((int)(SEM_VALUE_MAX) < (0)) {
|
|
|
|
py_sem_value_max = PyLong_FromLong(INT_MAX);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
py_sem_value_max = PyLong_FromLong(SEM_VALUE_MAX);
|
|
|
|
}
|
|
|
|
if (py_sem_value_max == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (PyDict_SetItemString(semlock_type->tp_dict, "SEM_VALUE_MAX",
|
|
|
|
py_sem_value_max) < 0) {
|
2020-08-11 07:32:35 -03:00
|
|
|
Py_DECREF(py_sem_value_max);
|
2022-07-20 16:26:01 -03:00
|
|
|
return -1;
|
2010-10-16 23:14:36 -03:00
|
|
|
}
|
2022-07-20 16:26:01 -03:00
|
|
|
Py_DECREF(py_sem_value_max);
|
2020-08-11 07:32:35 -03:00
|
|
|
|
2008-06-13 16:28:21 -03:00
|
|
|
#endif
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Add configuration macros */
|
2020-08-11 07:32:35 -03:00
|
|
|
PyObject *flags = PyDict_New();
|
|
|
|
if (!flags) {
|
|
|
|
return -1;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2020-08-11 07:32:35 -03:00
|
|
|
#define ADD_FLAG(name) \
|
|
|
|
do { \
|
|
|
|
PyObject *value = PyLong_FromLong(name); \
|
|
|
|
if (value == NULL) { \
|
|
|
|
Py_DECREF(flags); \
|
|
|
|
return -1; \
|
|
|
|
} \
|
|
|
|
if (PyDict_SetItemString(flags, #name, value) < 0) { \
|
|
|
|
Py_DECREF(flags); \
|
|
|
|
Py_DECREF(value); \
|
|
|
|
return -1; \
|
|
|
|
} \
|
|
|
|
Py_DECREF(value); \
|
|
|
|
} while (0)
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2009-11-28 08:48:43 -04:00
|
|
|
#if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)
|
2010-05-09 12:52:27 -03:00
|
|
|
ADD_FLAG(HAVE_SEM_OPEN);
|
2008-06-13 16:28:21 -03:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SEM_TIMEDWAIT
|
2010-05-09 12:52:27 -03:00
|
|
|
ADD_FLAG(HAVE_SEM_TIMEDWAIT);
|
2008-06-13 16:28:21 -03:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_BROKEN_SEM_GETVALUE
|
2010-05-09 12:52:27 -03:00
|
|
|
ADD_FLAG(HAVE_BROKEN_SEM_GETVALUE);
|
2008-06-13 16:28:21 -03:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_BROKEN_SEM_UNLINK
|
2010-05-09 12:52:27 -03:00
|
|
|
ADD_FLAG(HAVE_BROKEN_SEM_UNLINK);
|
2008-06-13 16:28:21 -03:00
|
|
|
#endif
|
|
|
|
|
2023-07-25 08:34:49 -03:00
|
|
|
if (PyModule_Add(module, "flags", flags) < 0) {
|
2020-08-11 07:32:35 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyModuleDef_Slot multiprocessing_slots[] = {
|
|
|
|
{Py_mod_exec, multiprocessing_exec},
|
2023-05-05 18:11:27 -03:00
|
|
|
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
|
2020-08-11 07:32:35 -03:00
|
|
|
{0, NULL}
|
|
|
|
};
|
2008-06-13 16:28:21 -03:00
|
|
|
|
2020-08-11 07:32:35 -03:00
|
|
|
static struct PyModuleDef multiprocessing_module = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
.m_name = "_multiprocessing",
|
2022-07-20 16:26:01 -03:00
|
|
|
.m_size = 0,
|
2020-08-11 07:32:35 -03:00
|
|
|
.m_methods = module_methods,
|
|
|
|
.m_slots = multiprocessing_slots,
|
|
|
|
};
|
|
|
|
|
|
|
|
PyMODINIT_FUNC
|
|
|
|
PyInit__multiprocessing(void)
|
|
|
|
{
|
|
|
|
return PyModuleDef_Init(&multiprocessing_module);
|
2008-06-13 16:28:21 -03:00
|
|
|
}
|