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