blob: eb05c62b0d050684b5ea260e53ea943e069339f0 [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
Antoine Pitrou23bba4c2012-04-18 20:51:15 +020054#ifdef MS_WINDOWS
55static PyObject *
56multiprocessing_closesocket(PyObject *self, PyObject *args)
57{
58 HANDLE handle;
59 int ret;
60
61 if (!PyArg_ParseTuple(args, F_HANDLE ":closesocket" , &handle))
62 return NULL;
63
64 Py_BEGIN_ALLOW_THREADS
65 ret = closesocket((SOCKET) handle);
66 Py_END_ALLOW_THREADS
67
68 if (ret)
69 return PyErr_SetExcFromWindowsErr(PyExc_IOError, WSAGetLastError());
70 Py_RETURN_NONE;
71}
72
73static PyObject *
74multiprocessing_recv(PyObject *self, PyObject *args)
75{
76 HANDLE handle;
77 int size, nread;
78 PyObject *buf;
79
80 if (!PyArg_ParseTuple(args, F_HANDLE "i:recv" , &handle, &size))
81 return NULL;
82
83 buf = PyBytes_FromStringAndSize(NULL, size);
84 if (!buf)
85 return NULL;
86
87 Py_BEGIN_ALLOW_THREADS
88 nread = recv((SOCKET) handle, PyBytes_AS_STRING(buf), size, 0);
89 Py_END_ALLOW_THREADS
90
91 if (nread < 0) {
92 Py_DECREF(buf);
93 return PyErr_SetExcFromWindowsErr(PyExc_IOError, WSAGetLastError());
94 }
95 _PyBytes_Resize(&buf, nread);
96 return buf;
97}
98
99static PyObject *
100multiprocessing_send(PyObject *self, PyObject *args)
101{
102 HANDLE handle;
103 Py_buffer buf;
104 int ret;
105
106 if (!PyArg_ParseTuple(args, F_HANDLE "y*:send" , &handle, &buf))
107 return NULL;
108
109 Py_BEGIN_ALLOW_THREADS
110 ret = send((SOCKET) handle, buf.buf, buf.len, 0);
111 Py_END_ALLOW_THREADS
112
113 PyBuffer_Release(&buf);
114 if (ret < 0)
115 return PyErr_SetExcFromWindowsErr(PyExc_IOError, WSAGetLastError());
116 return PyLong_FromLong(ret);
117}
118
119#endif
Benjamin Petersonfa268032008-06-13 19:28:21 +0000120
121/*
122 * Function table
123 */
124
125static PyMethodDef module_methods[] = {
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200126#ifdef MS_WINDOWS
127 {"closesocket", multiprocessing_closesocket, METH_VARARGS, ""},
128 {"recv", multiprocessing_recv, METH_VARARGS, ""},
129 {"send", multiprocessing_send, METH_VARARGS, ""},
130#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 {NULL}
Benjamin Petersonfa268032008-06-13 19:28:21 +0000132};
133
134
135/*
136 * Initialize
137 */
138
139static struct PyModuleDef multiprocessing_module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 PyModuleDef_HEAD_INIT,
141 "_multiprocessing",
142 NULL,
143 -1,
144 module_methods,
145 NULL,
146 NULL,
147 NULL,
148 NULL
Benjamin Petersonfa268032008-06-13 19:28:21 +0000149};
150
151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152PyMODINIT_FUNC
Benjamin Petersonfa268032008-06-13 19:28:21 +0000153PyInit__multiprocessing(void)
154{
Amaury Forgeot d'Arc7e447c82011-12-17 10:23:14 +0100155 PyObject *module, *temp, *value = NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 /* Initialize module */
158 module = PyModule_Create(&multiprocessing_module);
159 if (!module)
160 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 /* Get copy of BufferTooShort */
163 temp = PyImport_ImportModule("multiprocessing");
164 if (!temp)
165 return NULL;
166 BufferTooShort = PyObject_GetAttrString(temp, "BufferTooShort");
167 Py_XDECREF(temp);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169#if defined(MS_WINDOWS) || \
Mark Dickinsona614f042009-11-28 12:48:43 +0000170 (defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 /* Add SemLock type to module */
172 if (PyType_Ready(&SemLockType) < 0)
173 return NULL;
174 Py_INCREF(&SemLockType);
Gregory P. Smith68e01352010-10-17 02:14:36 +0000175 {
176 PyObject *py_sem_value_max;
177 /* Some systems define SEM_VALUE_MAX as an unsigned value that
178 * causes it to be negative when used as an int (NetBSD). */
179 if ((int)(SEM_VALUE_MAX) < 0)
180 py_sem_value_max = PyLong_FromLong(INT_MAX);
181 else
182 py_sem_value_max = PyLong_FromLong(SEM_VALUE_MAX);
183 if (py_sem_value_max == NULL)
184 return NULL;
185 PyDict_SetItemString(SemLockType.tp_dict, "SEM_VALUE_MAX",
186 py_sem_value_max);
187 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 PyModule_AddObject(module, "SemLock", (PyObject*)&SemLockType);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000189#endif
190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 /* Add configuration macros */
192 temp = PyDict_New();
193 if (!temp)
194 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196#define ADD_FLAG(name) \
197 value = Py_BuildValue("i", name); \
198 if (value == NULL) { Py_DECREF(temp); return NULL; } \
199 if (PyDict_SetItemString(temp, #name, value) < 0) { \
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200200 Py_DECREF(temp); Py_DECREF(value); return NULL; } \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 Py_DECREF(value)
202
Mark Dickinsona614f042009-11-28 12:48:43 +0000203#if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 ADD_FLAG(HAVE_SEM_OPEN);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000205#endif
206#ifdef HAVE_SEM_TIMEDWAIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 ADD_FLAG(HAVE_SEM_TIMEDWAIT);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000208#endif
Benjamin Petersonfa268032008-06-13 19:28:21 +0000209#ifdef HAVE_BROKEN_SEM_GETVALUE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 ADD_FLAG(HAVE_BROKEN_SEM_GETVALUE);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000211#endif
212#ifdef HAVE_BROKEN_SEM_UNLINK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 ADD_FLAG(HAVE_BROKEN_SEM_UNLINK);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000214#endif
215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 if (PyModule_AddObject(module, "flags", temp) < 0)
217 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 return module;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000220}