Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Extension module used by multiprocessing package |
| 3 | * |
| 4 | * multiprocessing.c |
| 5 | * |
Richard Oudkerk | 3e268aa | 2012-04-30 12:13:55 +0100 | [diff] [blame] | 6 | * Copyright (c) 2006-2008, R Oudkerk |
| 7 | * Licensed to PSF under a Contributor Agreement. |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include "multiprocessing.h" |
| 11 | |
Zackery Spytz | 545b54d | 2020-07-12 10:11:11 -0600 | [diff] [blame] | 12 | /*[python input] |
| 13 | class HANDLE_converter(CConverter): |
| 14 | type = "HANDLE" |
| 15 | format_unit = '"F_HANDLE"' |
| 16 | |
| 17 | [python start generated code]*/ |
| 18 | /*[python end generated code: output=da39a3ee5e6b4b0d input=9fad6080b79ace91]*/ |
| 19 | |
| 20 | /*[clinic input] |
| 21 | module _multiprocessing |
| 22 | [clinic start generated code]*/ |
| 23 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=01e0745f380ac6e3]*/ |
| 24 | |
| 25 | #include "clinic/multiprocessing.c.h" |
Benjamin Peterson | 965ce87 | 2009-04-05 21:24:58 +0000 | [diff] [blame] | 26 | |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 27 | /* |
| 28 | * Function which raises exceptions based on error codes |
| 29 | */ |
| 30 | |
| 31 | PyObject * |
Richard Oudkerk | 8fb9f4c | 2012-10-07 18:08:47 +0100 | [diff] [blame] | 32 | _PyMp_SetError(PyObject *Type, int num) |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 33 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 34 | switch (num) { |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 35 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 36 | case MP_STANDARD_ERROR: |
| 37 | if (Type == NULL) |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 38 | Type = PyExc_OSError; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 39 | PyErr_SetExcFromWindowsErr(Type, 0); |
| 40 | break; |
| 41 | case MP_SOCKET_ERROR: |
| 42 | if (Type == NULL) |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 43 | Type = PyExc_OSError; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 44 | PyErr_SetExcFromWindowsErr(Type, WSAGetLastError()); |
| 45 | break; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 46 | #else /* !MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 47 | case MP_STANDARD_ERROR: |
| 48 | case MP_SOCKET_ERROR: |
| 49 | if (Type == NULL) |
| 50 | Type = PyExc_OSError; |
| 51 | PyErr_SetFromErrno(Type); |
| 52 | break; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 53 | #endif /* !MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 | case MP_MEMORY_ERROR: |
| 55 | PyErr_NoMemory(); |
| 56 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 57 | case MP_EXCEPTION_HAS_BEEN_SET: |
| 58 | break; |
| 59 | default: |
| 60 | PyErr_Format(PyExc_RuntimeError, |
doko@ubuntu.com | 9df891c | 2013-05-15 18:06:56 +0200 | [diff] [blame] | 61 | "unknown error number %d", num); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | } |
| 63 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 66 | #ifdef MS_WINDOWS |
Zackery Spytz | 545b54d | 2020-07-12 10:11:11 -0600 | [diff] [blame] | 67 | /*[clinic input] |
| 68 | _multiprocessing.closesocket |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 69 | |
Zackery Spytz | 545b54d | 2020-07-12 10:11:11 -0600 | [diff] [blame] | 70 | handle: HANDLE |
| 71 | / |
| 72 | |
| 73 | [clinic start generated code]*/ |
| 74 | |
| 75 | static PyObject * |
| 76 | _multiprocessing_closesocket_impl(PyObject *module, HANDLE handle) |
| 77 | /*[clinic end generated code: output=214f359f900966f4 input=8a20706dd386c6cc]*/ |
| 78 | { |
| 79 | int ret; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 80 | |
| 81 | Py_BEGIN_ALLOW_THREADS |
| 82 | ret = closesocket((SOCKET) handle); |
| 83 | Py_END_ALLOW_THREADS |
| 84 | |
| 85 | if (ret) |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 86 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError()); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 87 | Py_RETURN_NONE; |
| 88 | } |
| 89 | |
Zackery Spytz | 545b54d | 2020-07-12 10:11:11 -0600 | [diff] [blame] | 90 | /*[clinic input] |
| 91 | _multiprocessing.recv |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 92 | |
Zackery Spytz | 545b54d | 2020-07-12 10:11:11 -0600 | [diff] [blame] | 93 | handle: HANDLE |
| 94 | size: int |
| 95 | / |
| 96 | |
| 97 | [clinic start generated code]*/ |
| 98 | |
| 99 | static PyObject * |
| 100 | _multiprocessing_recv_impl(PyObject *module, HANDLE handle, int size) |
| 101 | /*[clinic end generated code: output=92322781ba9ff598 input=6a5b0834372cee5b]*/ |
| 102 | { |
| 103 | int nread; |
| 104 | PyObject *buf; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 105 | |
| 106 | buf = PyBytes_FromStringAndSize(NULL, size); |
| 107 | if (!buf) |
| 108 | return NULL; |
| 109 | |
| 110 | Py_BEGIN_ALLOW_THREADS |
| 111 | nread = recv((SOCKET) handle, PyBytes_AS_STRING(buf), size, 0); |
| 112 | Py_END_ALLOW_THREADS |
| 113 | |
| 114 | if (nread < 0) { |
| 115 | Py_DECREF(buf); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 116 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError()); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 117 | } |
| 118 | _PyBytes_Resize(&buf, nread); |
| 119 | return buf; |
| 120 | } |
| 121 | |
Zackery Spytz | 545b54d | 2020-07-12 10:11:11 -0600 | [diff] [blame] | 122 | /*[clinic input] |
| 123 | _multiprocessing.send |
| 124 | |
| 125 | handle: HANDLE |
| 126 | buf: Py_buffer |
| 127 | / |
| 128 | |
| 129 | [clinic start generated code]*/ |
| 130 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 131 | static PyObject * |
Zackery Spytz | 545b54d | 2020-07-12 10:11:11 -0600 | [diff] [blame] | 132 | _multiprocessing_send_impl(PyObject *module, HANDLE handle, Py_buffer *buf) |
| 133 | /*[clinic end generated code: output=52d7df0519c596cb input=41dce742f98d2210]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 134 | { |
Richard Oudkerk | b988ee0 | 2013-09-07 17:40:45 +0100 | [diff] [blame] | 135 | int ret, length; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 136 | |
Zackery Spytz | 545b54d | 2020-07-12 10:11:11 -0600 | [diff] [blame] | 137 | length = (int)Py_MIN(buf->len, INT_MAX); |
Richard Oudkerk | b988ee0 | 2013-09-07 17:40:45 +0100 | [diff] [blame] | 138 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 139 | Py_BEGIN_ALLOW_THREADS |
Zackery Spytz | 545b54d | 2020-07-12 10:11:11 -0600 | [diff] [blame] | 140 | ret = send((SOCKET) handle, buf->buf, length, 0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 141 | Py_END_ALLOW_THREADS |
| 142 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 143 | if (ret < 0) |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 144 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError()); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 145 | return PyLong_FromLong(ret); |
| 146 | } |
| 147 | |
| 148 | #endif |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 149 | |
Zackery Spytz | 545b54d | 2020-07-12 10:11:11 -0600 | [diff] [blame] | 150 | /*[clinic input] |
| 151 | _multiprocessing.sem_unlink |
| 152 | |
| 153 | name: str |
| 154 | / |
| 155 | |
| 156 | [clinic start generated code]*/ |
| 157 | |
| 158 | static PyObject * |
| 159 | _multiprocessing_sem_unlink_impl(PyObject *module, const char *name) |
| 160 | /*[clinic end generated code: output=fcbfeb1ed255e647 input=bf939aff9564f1d5]*/ |
| 161 | { |
| 162 | return _PyMp_sem_unlink(name); |
| 163 | } |
| 164 | |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 165 | /* |
| 166 | * Function table |
| 167 | */ |
| 168 | |
| 169 | static PyMethodDef module_methods[] = { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 170 | #ifdef MS_WINDOWS |
Zackery Spytz | 545b54d | 2020-07-12 10:11:11 -0600 | [diff] [blame] | 171 | _MULTIPROCESSING_CLOSESOCKET_METHODDEF |
| 172 | _MULTIPROCESSING_RECV_METHODDEF |
| 173 | _MULTIPROCESSING_SEND_METHODDEF |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 174 | #endif |
Berker Peksag | 1a269d0 | 2016-05-07 21:13:50 +0300 | [diff] [blame] | 175 | #if !defined(POSIX_SEMAPHORES_NOT_ENABLED) && !defined(__ANDROID__) |
Zackery Spytz | 545b54d | 2020-07-12 10:11:11 -0600 | [diff] [blame] | 176 | _MULTIPROCESSING_SEM_UNLINK_METHODDEF |
Richard Oudkerk | e0e6581 | 2014-07-28 23:01:02 +0100 | [diff] [blame] | 177 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 178 | {NULL} |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | |
| 182 | /* |
| 183 | * Initialize |
| 184 | */ |
| 185 | |
Mohamed Koubaa | 1d541c2 | 2020-08-11 05:32:35 -0500 | [diff] [blame] | 186 | static int |
| 187 | multiprocessing_exec(PyObject *module) |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 188 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | #if defined(MS_WINDOWS) || \ |
Mark Dickinson | a614f04 | 2009-11-28 12:48:43 +0000 | [diff] [blame] | 190 | (defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)) |
Mohamed Koubaa | 1d541c2 | 2020-08-11 05:32:35 -0500 | [diff] [blame] | 191 | |
Richard Oudkerk | 8fb9f4c | 2012-10-07 18:08:47 +0100 | [diff] [blame] | 192 | /* Add _PyMp_SemLock type to module */ |
Mohamed Koubaa | 1d541c2 | 2020-08-11 05:32:35 -0500 | [diff] [blame] | 193 | if (PyModule_AddType(module, &_PyMp_SemLockType) < 0) { |
| 194 | return -1; |
| 195 | } |
| 196 | |
Gregory P. Smith | 68e0135 | 2010-10-17 02:14:36 +0000 | [diff] [blame] | 197 | { |
| 198 | PyObject *py_sem_value_max; |
| 199 | /* Some systems define SEM_VALUE_MAX as an unsigned value that |
Victor Stinner | 408bfa6 | 2016-12-05 18:00:42 +0100 | [diff] [blame] | 200 | * causes it to be negative when used as an int (NetBSD). |
| 201 | * |
| 202 | * Issue #28152: Use (0) instead of 0 to fix a warning on dead code |
| 203 | * when using clang -Wunreachable-code. */ |
| 204 | if ((int)(SEM_VALUE_MAX) < (0)) |
Gregory P. Smith | 68e0135 | 2010-10-17 02:14:36 +0000 | [diff] [blame] | 205 | py_sem_value_max = PyLong_FromLong(INT_MAX); |
| 206 | else |
| 207 | py_sem_value_max = PyLong_FromLong(SEM_VALUE_MAX); |
Mohamed Koubaa | 1d541c2 | 2020-08-11 05:32:35 -0500 | [diff] [blame] | 208 | |
| 209 | if (py_sem_value_max == NULL) { |
Mohamed Koubaa | 1d541c2 | 2020-08-11 05:32:35 -0500 | [diff] [blame] | 210 | return -1; |
| 211 | } |
| 212 | if (PyDict_SetItemString(_PyMp_SemLockType.tp_dict, "SEM_VALUE_MAX", |
| 213 | py_sem_value_max) < 0) { |
| 214 | Py_DECREF(py_sem_value_max); |
| 215 | return -1; |
| 216 | } |
| 217 | Py_DECREF(py_sem_value_max); |
Gregory P. Smith | 68e0135 | 2010-10-17 02:14:36 +0000 | [diff] [blame] | 218 | } |
Mohamed Koubaa | 1d541c2 | 2020-08-11 05:32:35 -0500 | [diff] [blame] | 219 | |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 220 | #endif |
| 221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 222 | /* Add configuration macros */ |
Mohamed Koubaa | 1d541c2 | 2020-08-11 05:32:35 -0500 | [diff] [blame] | 223 | PyObject *flags = PyDict_New(); |
| 224 | if (!flags) { |
| 225 | return -1; |
| 226 | } |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 227 | |
Mohamed Koubaa | 1d541c2 | 2020-08-11 05:32:35 -0500 | [diff] [blame] | 228 | #define ADD_FLAG(name) \ |
| 229 | do { \ |
| 230 | PyObject *value = PyLong_FromLong(name); \ |
| 231 | if (value == NULL) { \ |
| 232 | Py_DECREF(flags); \ |
| 233 | return -1; \ |
| 234 | } \ |
| 235 | if (PyDict_SetItemString(flags, #name, value) < 0) { \ |
| 236 | Py_DECREF(flags); \ |
| 237 | Py_DECREF(value); \ |
| 238 | return -1; \ |
| 239 | } \ |
| 240 | Py_DECREF(value); \ |
| 241 | } while (0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | |
Mark Dickinson | a614f04 | 2009-11-28 12:48:43 +0000 | [diff] [blame] | 243 | #if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 244 | ADD_FLAG(HAVE_SEM_OPEN); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 245 | #endif |
| 246 | #ifdef HAVE_SEM_TIMEDWAIT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 247 | ADD_FLAG(HAVE_SEM_TIMEDWAIT); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 248 | #endif |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 249 | #ifdef HAVE_BROKEN_SEM_GETVALUE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | ADD_FLAG(HAVE_BROKEN_SEM_GETVALUE); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 251 | #endif |
| 252 | #ifdef HAVE_BROKEN_SEM_UNLINK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 253 | ADD_FLAG(HAVE_BROKEN_SEM_UNLINK); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 254 | #endif |
| 255 | |
Mohamed Koubaa | 1d541c2 | 2020-08-11 05:32:35 -0500 | [diff] [blame] | 256 | if (PyModule_AddObject(module, "flags", flags) < 0) { |
| 257 | Py_DECREF(flags); |
| 258 | return -1; |
| 259 | } |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 260 | |
Mohamed Koubaa | 1d541c2 | 2020-08-11 05:32:35 -0500 | [diff] [blame] | 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static PyModuleDef_Slot multiprocessing_slots[] = { |
| 265 | {Py_mod_exec, multiprocessing_exec}, |
| 266 | {0, NULL} |
| 267 | }; |
| 268 | |
| 269 | static struct PyModuleDef multiprocessing_module = { |
| 270 | PyModuleDef_HEAD_INIT, |
| 271 | .m_name = "_multiprocessing", |
| 272 | .m_methods = module_methods, |
| 273 | .m_slots = multiprocessing_slots, |
| 274 | }; |
| 275 | |
| 276 | PyMODINIT_FUNC |
| 277 | PyInit__multiprocessing(void) |
| 278 | { |
| 279 | return PyModuleDef_Init(&multiprocessing_module); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 280 | } |