blob: 6e39a239a69af93b94e24a7049141ba9b8e7bf81 [file] [log] [blame]
Benjamin Petersonfa268032008-06-13 19:28:21 +00001/*
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 Peterson965ce872009-04-05 21:24:58 +000011
Benjamin Petersonfa268032008-06-13 19:28:21 +000012PyObject *ProcessError, *BufferTooShort;
13
14/*
15 * Function which raises exceptions based on error codes
16 */
17
18PyObject *
19mp_SetError(PyObject *Type, int num)
20{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000021 switch (num) {
Benjamin Petersonfa268032008-06-13 19:28:21 +000022#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000023 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 Petersonfa268032008-06-13 19:28:21 +000033#else /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034 case MP_STANDARD_ERROR:
35 case MP_SOCKET_ERROR:
36 if (Type == NULL)
37 Type = PyExc_OSError;
38 PyErr_SetFromErrno(Type);
39 break;
Benjamin Petersonfa268032008-06-13 19:28:21 +000040#endif /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 case MP_MEMORY_ERROR:
42 PyErr_NoMemory();
43 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 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 Petersonfa268032008-06-13 19:28:21 +000051}
52
53
Benjamin Petersonfa268032008-06-13 19:28:21 +000054static PyObject*
55multiprocessing_address_of_buffer(PyObject *self, PyObject *obj)
56{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 void *buffer;
58 Py_ssize_t buffer_len;
Benjamin Petersonfa268032008-06-13 19:28:21 +000059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 if (PyObject_AsWriteBuffer(obj, &buffer, &buffer_len) < 0)
61 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +000062
Antoine Pitrou2341f9b2011-05-09 20:55:03 +020063 return Py_BuildValue("Nn",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 PyLong_FromVoidPtr(buffer), buffer_len);
Benjamin Petersonfa268032008-06-13 19:28:21 +000065}
66
Antoine Pitrou23bba4c2012-04-18 20:51:15 +020067#ifdef MS_WINDOWS
68static PyObject *
69multiprocessing_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
86static PyObject *
87multiprocessing_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
112static PyObject *
113multiprocessing_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 Petersonfa268032008-06-13 19:28:21 +0000133
134/*
135 * Function table
136 */
137
138static PyMethodDef module_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 {"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 Pitrou23bba4c2012-04-18 20:51:15 +0200142#ifdef MS_WINDOWS
143 {"closesocket", multiprocessing_closesocket, METH_VARARGS, ""},
144 {"recv", multiprocessing_recv, METH_VARARGS, ""},
145 {"send", multiprocessing_send, METH_VARARGS, ""},
146#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 {NULL}
Benjamin Petersonfa268032008-06-13 19:28:21 +0000148};
149
150
151/*
152 * Initialize
153 */
154
155static struct PyModuleDef multiprocessing_module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 PyModuleDef_HEAD_INIT,
157 "_multiprocessing",
158 NULL,
159 -1,
160 module_methods,
161 NULL,
162 NULL,
163 NULL,
164 NULL
Benjamin Petersonfa268032008-06-13 19:28:21 +0000165};
166
167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168PyMODINIT_FUNC
Benjamin Petersonfa268032008-06-13 19:28:21 +0000169PyInit__multiprocessing(void)
170{
Amaury Forgeot d'Arc7e447c82011-12-17 10:23:14 +0100171 PyObject *module, *temp, *value = NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 /* Initialize module */
174 module = PyModule_Create(&multiprocessing_module);
175 if (!module)
176 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 /* 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 Petersonfa268032008-06-13 19:28:21 +0000184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185#if defined(MS_WINDOWS) || \
Mark Dickinsona614f042009-11-28 12:48:43 +0000186 (defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 /* Add SemLock type to module */
188 if (PyType_Ready(&SemLockType) < 0)
189 return NULL;
190 Py_INCREF(&SemLockType);
Gregory P. Smith68e01352010-10-17 02:14:36 +0000191 {
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 Pitrouf95a1b32010-05-09 15:52:27 +0000204 PyModule_AddObject(module, "SemLock", (PyObject*)&SemLockType);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000205#endif
206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 /* Add configuration macros */
208 temp = PyDict_New();
209 if (!temp)
210 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212#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 Pitrou23bba4c2012-04-18 20:51:15 +0200216 Py_DECREF(temp); Py_DECREF(value); return NULL; } \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 Py_DECREF(value)
218
Mark Dickinsona614f042009-11-28 12:48:43 +0000219#if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 ADD_FLAG(HAVE_SEM_OPEN);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000221#endif
222#ifdef HAVE_SEM_TIMEDWAIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 ADD_FLAG(HAVE_SEM_TIMEDWAIT);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000224#endif
Benjamin Petersonfa268032008-06-13 19:28:21 +0000225#ifdef HAVE_BROKEN_SEM_GETVALUE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 ADD_FLAG(HAVE_BROKEN_SEM_GETVALUE);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000227#endif
228#ifdef HAVE_BROKEN_SEM_UNLINK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 ADD_FLAG(HAVE_BROKEN_SEM_UNLINK);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000230#endif
231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 if (PyModule_AddObject(module, "flags", temp) < 0)
233 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 return module;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000236}