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 | |
Benjamin Peterson | 965ce87 | 2009-04-05 21:24:58 +0000 | [diff] [blame] | 12 | |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 13 | PyObject *ProcessError, *BufferTooShort; |
| 14 | |
| 15 | /* |
| 16 | * Function which raises exceptions based on error codes |
| 17 | */ |
| 18 | |
| 19 | PyObject * |
| 20 | mp_SetError(PyObject *Type, int num) |
| 21 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | switch (num) { |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 23 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 24 | case MP_STANDARD_ERROR: |
| 25 | if (Type == NULL) |
| 26 | Type = PyExc_WindowsError; |
| 27 | PyErr_SetExcFromWindowsErr(Type, 0); |
| 28 | break; |
| 29 | case MP_SOCKET_ERROR: |
| 30 | if (Type == NULL) |
| 31 | Type = PyExc_WindowsError; |
| 32 | PyErr_SetExcFromWindowsErr(Type, WSAGetLastError()); |
| 33 | break; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 34 | #else /* !MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 35 | case MP_STANDARD_ERROR: |
| 36 | case MP_SOCKET_ERROR: |
| 37 | if (Type == NULL) |
| 38 | Type = PyExc_OSError; |
| 39 | PyErr_SetFromErrno(Type); |
| 40 | break; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 41 | #endif /* !MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 42 | case MP_MEMORY_ERROR: |
| 43 | PyErr_NoMemory(); |
| 44 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 45 | case MP_EXCEPTION_HAS_BEEN_SET: |
| 46 | break; |
| 47 | default: |
| 48 | PyErr_Format(PyExc_RuntimeError, |
doko@ubuntu.com | 9df891c | 2013-05-15 18:06:56 +0200 | [diff] [blame^] | 49 | "unknown error number %d", num); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 50 | } |
| 51 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 54 | #ifdef MS_WINDOWS |
| 55 | static PyObject * |
| 56 | multiprocessing_closesocket(PyObject *self, PyObject *args) |
| 57 | { |
| 58 | HANDLE handle; |
| 59 | int ret; |
| 60 | |
| 61 | if (!PyArg_ParseTuple(args, F_HANDLE ":closesocket" , &handle)) |
| 62 | return NULL; |
| 63 | |
| 64 | Py_BEGIN_ALLOW_THREADS |
| 65 | ret = closesocket((SOCKET) handle); |
| 66 | Py_END_ALLOW_THREADS |
| 67 | |
| 68 | if (ret) |
| 69 | return PyErr_SetExcFromWindowsErr(PyExc_IOError, WSAGetLastError()); |
| 70 | Py_RETURN_NONE; |
| 71 | } |
| 72 | |
| 73 | static PyObject * |
| 74 | multiprocessing_recv(PyObject *self, PyObject *args) |
| 75 | { |
| 76 | HANDLE handle; |
| 77 | int size, nread; |
| 78 | PyObject *buf; |
| 79 | |
| 80 | if (!PyArg_ParseTuple(args, F_HANDLE "i:recv" , &handle, &size)) |
| 81 | return NULL; |
| 82 | |
| 83 | buf = PyBytes_FromStringAndSize(NULL, size); |
| 84 | if (!buf) |
| 85 | return NULL; |
| 86 | |
| 87 | Py_BEGIN_ALLOW_THREADS |
| 88 | nread = recv((SOCKET) handle, PyBytes_AS_STRING(buf), size, 0); |
| 89 | Py_END_ALLOW_THREADS |
| 90 | |
| 91 | if (nread < 0) { |
| 92 | Py_DECREF(buf); |
| 93 | return PyErr_SetExcFromWindowsErr(PyExc_IOError, WSAGetLastError()); |
| 94 | } |
| 95 | _PyBytes_Resize(&buf, nread); |
| 96 | return buf; |
| 97 | } |
| 98 | |
| 99 | static PyObject * |
| 100 | multiprocessing_send(PyObject *self, PyObject *args) |
| 101 | { |
| 102 | HANDLE handle; |
| 103 | Py_buffer buf; |
| 104 | int ret; |
| 105 | |
| 106 | if (!PyArg_ParseTuple(args, F_HANDLE "y*:send" , &handle, &buf)) |
| 107 | return NULL; |
| 108 | |
| 109 | Py_BEGIN_ALLOW_THREADS |
| 110 | ret = send((SOCKET) handle, buf.buf, buf.len, 0); |
| 111 | Py_END_ALLOW_THREADS |
| 112 | |
| 113 | PyBuffer_Release(&buf); |
| 114 | if (ret < 0) |
| 115 | return PyErr_SetExcFromWindowsErr(PyExc_IOError, WSAGetLastError()); |
| 116 | return PyLong_FromLong(ret); |
| 117 | } |
| 118 | |
| 119 | #endif |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | * Function table |
| 123 | */ |
| 124 | |
| 125 | static PyMethodDef module_methods[] = { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 126 | #ifdef MS_WINDOWS |
| 127 | {"closesocket", multiprocessing_closesocket, METH_VARARGS, ""}, |
| 128 | {"recv", multiprocessing_recv, METH_VARARGS, ""}, |
| 129 | {"send", multiprocessing_send, METH_VARARGS, ""}, |
| 130 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 131 | {NULL} |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | |
| 135 | /* |
| 136 | * Initialize |
| 137 | */ |
| 138 | |
| 139 | static struct PyModuleDef multiprocessing_module = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 140 | PyModuleDef_HEAD_INIT, |
| 141 | "_multiprocessing", |
| 142 | NULL, |
| 143 | -1, |
| 144 | module_methods, |
| 145 | NULL, |
| 146 | NULL, |
| 147 | NULL, |
| 148 | NULL |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | PyMODINIT_FUNC |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 153 | PyInit__multiprocessing(void) |
| 154 | { |
Amaury Forgeot d'Arc | 7e447c8 | 2011-12-17 10:23:14 +0100 | [diff] [blame] | 155 | PyObject *module, *temp, *value = NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 156 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 157 | /* Initialize module */ |
| 158 | module = PyModule_Create(&multiprocessing_module); |
| 159 | if (!module) |
| 160 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | /* Get copy of BufferTooShort */ |
| 163 | temp = PyImport_ImportModule("multiprocessing"); |
| 164 | if (!temp) |
| 165 | return NULL; |
| 166 | BufferTooShort = PyObject_GetAttrString(temp, "BufferTooShort"); |
| 167 | Py_XDECREF(temp); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | #if defined(MS_WINDOWS) || \ |
Mark Dickinson | a614f04 | 2009-11-28 12:48:43 +0000 | [diff] [blame] | 170 | (defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 171 | /* Add SemLock type to module */ |
| 172 | if (PyType_Ready(&SemLockType) < 0) |
| 173 | return NULL; |
| 174 | Py_INCREF(&SemLockType); |
Gregory P. Smith | 68e0135 | 2010-10-17 02:14:36 +0000 | [diff] [blame] | 175 | { |
| 176 | PyObject *py_sem_value_max; |
| 177 | /* Some systems define SEM_VALUE_MAX as an unsigned value that |
| 178 | * causes it to be negative when used as an int (NetBSD). */ |
| 179 | if ((int)(SEM_VALUE_MAX) < 0) |
| 180 | py_sem_value_max = PyLong_FromLong(INT_MAX); |
| 181 | else |
| 182 | py_sem_value_max = PyLong_FromLong(SEM_VALUE_MAX); |
| 183 | if (py_sem_value_max == NULL) |
| 184 | return NULL; |
| 185 | PyDict_SetItemString(SemLockType.tp_dict, "SEM_VALUE_MAX", |
| 186 | py_sem_value_max); |
| 187 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 188 | PyModule_AddObject(module, "SemLock", (PyObject*)&SemLockType); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 189 | #endif |
| 190 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | /* Add configuration macros */ |
| 192 | temp = PyDict_New(); |
| 193 | if (!temp) |
| 194 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 196 | #define ADD_FLAG(name) \ |
| 197 | value = Py_BuildValue("i", name); \ |
| 198 | if (value == NULL) { Py_DECREF(temp); return NULL; } \ |
| 199 | if (PyDict_SetItemString(temp, #name, value) < 0) { \ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 200 | Py_DECREF(temp); Py_DECREF(value); return NULL; } \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 201 | Py_DECREF(value) |
| 202 | |
Mark Dickinson | a614f04 | 2009-11-28 12:48:43 +0000 | [diff] [blame] | 203 | #if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | ADD_FLAG(HAVE_SEM_OPEN); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 205 | #endif |
| 206 | #ifdef HAVE_SEM_TIMEDWAIT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | ADD_FLAG(HAVE_SEM_TIMEDWAIT); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 208 | #endif |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 209 | #ifdef HAVE_BROKEN_SEM_GETVALUE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | ADD_FLAG(HAVE_BROKEN_SEM_GETVALUE); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 211 | #endif |
| 212 | #ifdef HAVE_BROKEN_SEM_UNLINK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | ADD_FLAG(HAVE_BROKEN_SEM_UNLINK); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 214 | #endif |
| 215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 216 | if (PyModule_AddObject(module, "flags", temp) < 0) |
| 217 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 219 | return module; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 220 | } |