blob: d5593f0db8f5628bae33fdacba90e3f8aacc1a69 [file] [log] [blame]
Benjamin Petersonfa268032008-06-13 19:28:21 +00001/*
2 * Extension module used by multiprocessing package
3 *
4 * multiprocessing.c
5 *
Richard Oudkerk3e268aa2012-04-30 12:13:55 +01006 * Copyright (c) 2006-2008, R Oudkerk
7 * Licensed to PSF under a Contributor Agreement.
Benjamin Petersonfa268032008-06-13 19:28:21 +00008 */
9
10#include "multiprocessing.h"
11
Benjamin Peterson965ce872009-04-05 21:24:58 +000012
Benjamin Petersonfa268032008-06-13 19:28:21 +000013PyObject *ProcessError, *BufferTooShort;
14
15/*
16 * Function which raises exceptions based on error codes
17 */
18
19PyObject *
20mp_SetError(PyObject *Type, int num)
21{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022 switch (num) {
Benjamin Petersonfa268032008-06-13 19:28:21 +000023#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000024 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 Petersonfa268032008-06-13 19:28:21 +000034#else /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 case MP_STANDARD_ERROR:
36 case MP_SOCKET_ERROR:
37 if (Type == NULL)
38 Type = PyExc_OSError;
39 PyErr_SetFromErrno(Type);
40 break;
Benjamin Petersonfa268032008-06-13 19:28:21 +000041#endif /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 case MP_MEMORY_ERROR:
43 PyErr_NoMemory();
44 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 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 Petersonfa268032008-06-13 19:28:21 +000052}
53
54
Benjamin Petersonfa268032008-06-13 19:28:21 +000055static PyObject*
56multiprocessing_address_of_buffer(PyObject *self, PyObject *obj)
57{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 void *buffer;
59 Py_ssize_t buffer_len;
Benjamin Petersonfa268032008-06-13 19:28:21 +000060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 if (PyObject_AsWriteBuffer(obj, &buffer, &buffer_len) < 0)
62 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +000063
Antoine Pitrou2341f9b2011-05-09 20:55:03 +020064 return Py_BuildValue("Nn",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 PyLong_FromVoidPtr(buffer), buffer_len);
Benjamin Petersonfa268032008-06-13 19:28:21 +000066}
67
Antoine Pitrou23bba4c2012-04-18 20:51:15 +020068#ifdef MS_WINDOWS
69static PyObject *
70multiprocessing_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
87static PyObject *
88multiprocessing_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
113static PyObject *
114multiprocessing_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 Petersonfa268032008-06-13 19:28:21 +0000134
135/*
136 * Function table
137 */
138
139static PyMethodDef module_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 {"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 Pitrou23bba4c2012-04-18 20:51:15 +0200143#ifdef MS_WINDOWS
144 {"closesocket", multiprocessing_closesocket, METH_VARARGS, ""},
145 {"recv", multiprocessing_recv, METH_VARARGS, ""},
146 {"send", multiprocessing_send, METH_VARARGS, ""},
147#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 {NULL}
Benjamin Petersonfa268032008-06-13 19:28:21 +0000149};
150
151
152/*
153 * Initialize
154 */
155
156static struct PyModuleDef multiprocessing_module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 PyModuleDef_HEAD_INIT,
158 "_multiprocessing",
159 NULL,
160 -1,
161 module_methods,
162 NULL,
163 NULL,
164 NULL,
165 NULL
Benjamin Petersonfa268032008-06-13 19:28:21 +0000166};
167
168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169PyMODINIT_FUNC
Benjamin Petersonfa268032008-06-13 19:28:21 +0000170PyInit__multiprocessing(void)
171{
Amaury Forgeot d'Arc7e447c82011-12-17 10:23:14 +0100172 PyObject *module, *temp, *value = NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 /* Initialize module */
175 module = PyModule_Create(&multiprocessing_module);
176 if (!module)
177 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 /* 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 Petersonfa268032008-06-13 19:28:21 +0000185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186#if defined(MS_WINDOWS) || \
Mark Dickinsona614f042009-11-28 12:48:43 +0000187 (defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 /* Add SemLock type to module */
189 if (PyType_Ready(&SemLockType) < 0)
190 return NULL;
191 Py_INCREF(&SemLockType);
Gregory P. Smith68e01352010-10-17 02:14:36 +0000192 {
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 Pitrouf95a1b32010-05-09 15:52:27 +0000205 PyModule_AddObject(module, "SemLock", (PyObject*)&SemLockType);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000206#endif
207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 /* Add configuration macros */
209 temp = PyDict_New();
210 if (!temp)
211 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213#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 Pitrou23bba4c2012-04-18 20:51:15 +0200217 Py_DECREF(temp); Py_DECREF(value); return NULL; } \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 Py_DECREF(value)
219
Mark Dickinsona614f042009-11-28 12:48:43 +0000220#if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 ADD_FLAG(HAVE_SEM_OPEN);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000222#endif
223#ifdef HAVE_SEM_TIMEDWAIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 ADD_FLAG(HAVE_SEM_TIMEDWAIT);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000225#endif
Benjamin Petersonfa268032008-06-13 19:28:21 +0000226#ifdef HAVE_BROKEN_SEM_GETVALUE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 ADD_FLAG(HAVE_BROKEN_SEM_GETVALUE);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000228#endif
229#ifdef HAVE_BROKEN_SEM_UNLINK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 ADD_FLAG(HAVE_BROKEN_SEM_UNLINK);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000231#endif
232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 if (PyModule_AddObject(module, "flags", temp) < 0)
234 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 return module;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000237}