1 | n/a | /* |
---|
2 | n/a | * Extension module used by multiprocessing package |
---|
3 | n/a | * |
---|
4 | n/a | * multiprocessing.c |
---|
5 | n/a | * |
---|
6 | n/a | * Copyright (c) 2006-2008, R Oudkerk |
---|
7 | n/a | * Licensed to PSF under a Contributor Agreement. |
---|
8 | n/a | */ |
---|
9 | n/a | |
---|
10 | n/a | #include "multiprocessing.h" |
---|
11 | n/a | |
---|
12 | n/a | |
---|
13 | n/a | /* |
---|
14 | n/a | * Function which raises exceptions based on error codes |
---|
15 | n/a | */ |
---|
16 | n/a | |
---|
17 | n/a | PyObject * |
---|
18 | n/a | _PyMp_SetError(PyObject *Type, int num) |
---|
19 | n/a | { |
---|
20 | n/a | switch (num) { |
---|
21 | n/a | #ifdef MS_WINDOWS |
---|
22 | n/a | case MP_STANDARD_ERROR: |
---|
23 | n/a | if (Type == NULL) |
---|
24 | n/a | Type = PyExc_OSError; |
---|
25 | n/a | PyErr_SetExcFromWindowsErr(Type, 0); |
---|
26 | n/a | break; |
---|
27 | n/a | case MP_SOCKET_ERROR: |
---|
28 | n/a | if (Type == NULL) |
---|
29 | n/a | Type = PyExc_OSError; |
---|
30 | n/a | PyErr_SetExcFromWindowsErr(Type, WSAGetLastError()); |
---|
31 | n/a | break; |
---|
32 | n/a | #else /* !MS_WINDOWS */ |
---|
33 | n/a | case MP_STANDARD_ERROR: |
---|
34 | n/a | case MP_SOCKET_ERROR: |
---|
35 | n/a | if (Type == NULL) |
---|
36 | n/a | Type = PyExc_OSError; |
---|
37 | n/a | PyErr_SetFromErrno(Type); |
---|
38 | n/a | break; |
---|
39 | n/a | #endif /* !MS_WINDOWS */ |
---|
40 | n/a | case MP_MEMORY_ERROR: |
---|
41 | n/a | PyErr_NoMemory(); |
---|
42 | n/a | break; |
---|
43 | n/a | case MP_EXCEPTION_HAS_BEEN_SET: |
---|
44 | n/a | break; |
---|
45 | n/a | default: |
---|
46 | n/a | PyErr_Format(PyExc_RuntimeError, |
---|
47 | n/a | "unknown error number %d", num); |
---|
48 | n/a | } |
---|
49 | n/a | return NULL; |
---|
50 | n/a | } |
---|
51 | n/a | |
---|
52 | n/a | #ifdef MS_WINDOWS |
---|
53 | n/a | static PyObject * |
---|
54 | n/a | multiprocessing_closesocket(PyObject *self, PyObject *args) |
---|
55 | n/a | { |
---|
56 | n/a | HANDLE handle; |
---|
57 | n/a | int ret; |
---|
58 | n/a | |
---|
59 | n/a | if (!PyArg_ParseTuple(args, F_HANDLE ":closesocket" , &handle)) |
---|
60 | n/a | return NULL; |
---|
61 | n/a | |
---|
62 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
63 | n/a | ret = closesocket((SOCKET) handle); |
---|
64 | n/a | Py_END_ALLOW_THREADS |
---|
65 | n/a | |
---|
66 | n/a | if (ret) |
---|
67 | n/a | return PyErr_SetExcFromWindowsErr(PyExc_IOError, WSAGetLastError()); |
---|
68 | n/a | Py_RETURN_NONE; |
---|
69 | n/a | } |
---|
70 | n/a | |
---|
71 | n/a | static PyObject * |
---|
72 | n/a | multiprocessing_recv(PyObject *self, PyObject *args) |
---|
73 | n/a | { |
---|
74 | n/a | HANDLE handle; |
---|
75 | n/a | int size, nread; |
---|
76 | n/a | PyObject *buf; |
---|
77 | n/a | |
---|
78 | n/a | if (!PyArg_ParseTuple(args, F_HANDLE "i:recv" , &handle, &size)) |
---|
79 | n/a | return NULL; |
---|
80 | n/a | |
---|
81 | n/a | buf = PyBytes_FromStringAndSize(NULL, size); |
---|
82 | n/a | if (!buf) |
---|
83 | n/a | return NULL; |
---|
84 | n/a | |
---|
85 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
86 | n/a | nread = recv((SOCKET) handle, PyBytes_AS_STRING(buf), size, 0); |
---|
87 | n/a | Py_END_ALLOW_THREADS |
---|
88 | n/a | |
---|
89 | n/a | if (nread < 0) { |
---|
90 | n/a | Py_DECREF(buf); |
---|
91 | n/a | return PyErr_SetExcFromWindowsErr(PyExc_IOError, WSAGetLastError()); |
---|
92 | n/a | } |
---|
93 | n/a | _PyBytes_Resize(&buf, nread); |
---|
94 | n/a | return buf; |
---|
95 | n/a | } |
---|
96 | n/a | |
---|
97 | n/a | static PyObject * |
---|
98 | n/a | multiprocessing_send(PyObject *self, PyObject *args) |
---|
99 | n/a | { |
---|
100 | n/a | HANDLE handle; |
---|
101 | n/a | Py_buffer buf; |
---|
102 | n/a | int ret, length; |
---|
103 | n/a | |
---|
104 | n/a | if (!PyArg_ParseTuple(args, F_HANDLE "y*:send" , &handle, &buf)) |
---|
105 | n/a | return NULL; |
---|
106 | n/a | |
---|
107 | n/a | length = (int)Py_MIN(buf.len, INT_MAX); |
---|
108 | n/a | |
---|
109 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
110 | n/a | ret = send((SOCKET) handle, buf.buf, length, 0); |
---|
111 | n/a | Py_END_ALLOW_THREADS |
---|
112 | n/a | |
---|
113 | n/a | PyBuffer_Release(&buf); |
---|
114 | n/a | if (ret < 0) |
---|
115 | n/a | return PyErr_SetExcFromWindowsErr(PyExc_IOError, WSAGetLastError()); |
---|
116 | n/a | return PyLong_FromLong(ret); |
---|
117 | n/a | } |
---|
118 | n/a | |
---|
119 | n/a | #endif |
---|
120 | n/a | |
---|
121 | n/a | /* |
---|
122 | n/a | * Function table |
---|
123 | n/a | */ |
---|
124 | n/a | |
---|
125 | n/a | static PyMethodDef module_methods[] = { |
---|
126 | n/a | #ifdef MS_WINDOWS |
---|
127 | n/a | {"closesocket", multiprocessing_closesocket, METH_VARARGS, ""}, |
---|
128 | n/a | {"recv", multiprocessing_recv, METH_VARARGS, ""}, |
---|
129 | n/a | {"send", multiprocessing_send, METH_VARARGS, ""}, |
---|
130 | n/a | #endif |
---|
131 | n/a | #if !defined(POSIX_SEMAPHORES_NOT_ENABLED) && !defined(__ANDROID__) |
---|
132 | n/a | {"sem_unlink", _PyMp_sem_unlink, METH_VARARGS, ""}, |
---|
133 | n/a | #endif |
---|
134 | n/a | {NULL} |
---|
135 | n/a | }; |
---|
136 | n/a | |
---|
137 | n/a | |
---|
138 | n/a | /* |
---|
139 | n/a | * Initialize |
---|
140 | n/a | */ |
---|
141 | n/a | |
---|
142 | n/a | static struct PyModuleDef multiprocessing_module = { |
---|
143 | n/a | PyModuleDef_HEAD_INIT, |
---|
144 | n/a | "_multiprocessing", |
---|
145 | n/a | NULL, |
---|
146 | n/a | -1, |
---|
147 | n/a | module_methods, |
---|
148 | n/a | NULL, |
---|
149 | n/a | NULL, |
---|
150 | n/a | NULL, |
---|
151 | n/a | NULL |
---|
152 | n/a | }; |
---|
153 | n/a | |
---|
154 | n/a | |
---|
155 | n/a | PyMODINIT_FUNC |
---|
156 | n/a | PyInit__multiprocessing(void) |
---|
157 | n/a | { |
---|
158 | n/a | PyObject *module, *temp, *value = NULL; |
---|
159 | n/a | |
---|
160 | n/a | /* Initialize module */ |
---|
161 | n/a | module = PyModule_Create(&multiprocessing_module); |
---|
162 | n/a | if (!module) |
---|
163 | n/a | return NULL; |
---|
164 | n/a | |
---|
165 | n/a | #if defined(MS_WINDOWS) || \ |
---|
166 | n/a | (defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)) |
---|
167 | n/a | /* Add _PyMp_SemLock type to module */ |
---|
168 | n/a | if (PyType_Ready(&_PyMp_SemLockType) < 0) |
---|
169 | n/a | return NULL; |
---|
170 | n/a | Py_INCREF(&_PyMp_SemLockType); |
---|
171 | n/a | { |
---|
172 | n/a | PyObject *py_sem_value_max; |
---|
173 | n/a | /* Some systems define SEM_VALUE_MAX as an unsigned value that |
---|
174 | n/a | * causes it to be negative when used as an int (NetBSD). |
---|
175 | n/a | * |
---|
176 | n/a | * Issue #28152: Use (0) instead of 0 to fix a warning on dead code |
---|
177 | n/a | * when using clang -Wunreachable-code. */ |
---|
178 | n/a | if ((int)(SEM_VALUE_MAX) < (0)) |
---|
179 | n/a | py_sem_value_max = PyLong_FromLong(INT_MAX); |
---|
180 | n/a | else |
---|
181 | n/a | py_sem_value_max = PyLong_FromLong(SEM_VALUE_MAX); |
---|
182 | n/a | if (py_sem_value_max == NULL) |
---|
183 | n/a | return NULL; |
---|
184 | n/a | PyDict_SetItemString(_PyMp_SemLockType.tp_dict, "SEM_VALUE_MAX", |
---|
185 | n/a | py_sem_value_max); |
---|
186 | n/a | } |
---|
187 | n/a | PyModule_AddObject(module, "SemLock", (PyObject*)&_PyMp_SemLockType); |
---|
188 | n/a | #endif |
---|
189 | n/a | |
---|
190 | n/a | /* Add configuration macros */ |
---|
191 | n/a | temp = PyDict_New(); |
---|
192 | n/a | if (!temp) |
---|
193 | n/a | return NULL; |
---|
194 | n/a | |
---|
195 | n/a | #define ADD_FLAG(name) \ |
---|
196 | n/a | value = Py_BuildValue("i", name); \ |
---|
197 | n/a | if (value == NULL) { Py_DECREF(temp); return NULL; } \ |
---|
198 | n/a | if (PyDict_SetItemString(temp, #name, value) < 0) { \ |
---|
199 | n/a | Py_DECREF(temp); Py_DECREF(value); return NULL; } \ |
---|
200 | n/a | Py_DECREF(value) |
---|
201 | n/a | |
---|
202 | n/a | #if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED) |
---|
203 | n/a | ADD_FLAG(HAVE_SEM_OPEN); |
---|
204 | n/a | #endif |
---|
205 | n/a | #ifdef HAVE_SEM_TIMEDWAIT |
---|
206 | n/a | ADD_FLAG(HAVE_SEM_TIMEDWAIT); |
---|
207 | n/a | #endif |
---|
208 | n/a | #ifdef HAVE_BROKEN_SEM_GETVALUE |
---|
209 | n/a | ADD_FLAG(HAVE_BROKEN_SEM_GETVALUE); |
---|
210 | n/a | #endif |
---|
211 | n/a | #ifdef HAVE_BROKEN_SEM_UNLINK |
---|
212 | n/a | ADD_FLAG(HAVE_BROKEN_SEM_UNLINK); |
---|
213 | n/a | #endif |
---|
214 | n/a | |
---|
215 | n/a | if (PyModule_AddObject(module, "flags", temp) < 0) |
---|
216 | n/a | return NULL; |
---|
217 | n/a | |
---|
218 | n/a | return module; |
---|
219 | n/a | } |
---|