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