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