blob: 1aaf3605714eda3b9d1b6ad8b5e4013029d0b38c [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 +000013/*
14 * Function which raises exceptions based on error codes
15 */
16
17PyObject *
Richard Oudkerk8fb9f4c2012-10-07 18:08:47 +010018_PyMp_SetError(PyObject *Type, int num)
Benjamin Petersonfa268032008-06-13 19:28:21 +000019{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 switch (num) {
Benjamin Petersonfa268032008-06-13 19:28:21 +000021#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022 case MP_STANDARD_ERROR:
23 if (Type == NULL)
Andrew Svetlov2606a6f2012-12-19 14:33:35 +020024 Type = PyExc_OSError;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000025 PyErr_SetExcFromWindowsErr(Type, 0);
26 break;
27 case MP_SOCKET_ERROR:
28 if (Type == NULL)
Andrew Svetlov2606a6f2012-12-19 14:33:35 +020029 Type = PyExc_OSError;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 PyErr_SetExcFromWindowsErr(Type, WSAGetLastError());
31 break;
Benjamin Petersonfa268032008-06-13 19:28:21 +000032#else /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 case MP_STANDARD_ERROR:
34 case MP_SOCKET_ERROR:
35 if (Type == NULL)
36 Type = PyExc_OSError;
37 PyErr_SetFromErrno(Type);
38 break;
Benjamin Petersonfa268032008-06-13 19:28:21 +000039#endif /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 case MP_MEMORY_ERROR:
41 PyErr_NoMemory();
42 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 case MP_EXCEPTION_HAS_BEEN_SET:
44 break;
45 default:
46 PyErr_Format(PyExc_RuntimeError,
doko@ubuntu.com9df891c2013-05-15 18:06:56 +020047 "unknown error number %d", num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 }
49 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +000050}
51
Antoine Pitrou23bba4c2012-04-18 20:51:15 +020052#ifdef MS_WINDOWS
53static PyObject *
54multiprocessing_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
71static PyObject *
72multiprocessing_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
97static PyObject *
98multiprocessing_send(PyObject *self, PyObject *args)
99{
100 HANDLE handle;
101 Py_buffer buf;
Richard Oudkerkb988ee02013-09-07 17:40:45 +0100102 int ret, length;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200103
104 if (!PyArg_ParseTuple(args, F_HANDLE "y*:send" , &handle, &buf))
105 return NULL;
106
Richard Oudkerkb988ee02013-09-07 17:40:45 +0100107 length = (int)Py_MIN(buf.len, INT_MAX);
108
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200109 Py_BEGIN_ALLOW_THREADS
Richard Oudkerkb988ee02013-09-07 17:40:45 +0100110 ret = send((SOCKET) handle, buf.buf, length, 0);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200111 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
Richard Oudkerk84ed9a62013-08-14 15:35:41 +0100131 {"sem_unlink", _PyMp_sem_unlink, METH_VARARGS, ""},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 {NULL}
Benjamin Petersonfa268032008-06-13 19:28:21 +0000133};
134
135
136/*
137 * Initialize
138 */
139
140static struct PyModuleDef multiprocessing_module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 PyModuleDef_HEAD_INIT,
142 "_multiprocessing",
143 NULL,
144 -1,
145 module_methods,
146 NULL,
147 NULL,
148 NULL,
149 NULL
Benjamin Petersonfa268032008-06-13 19:28:21 +0000150};
151
152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153PyMODINIT_FUNC
Benjamin Petersonfa268032008-06-13 19:28:21 +0000154PyInit__multiprocessing(void)
155{
Amaury Forgeot d'Arc7e447c82011-12-17 10:23:14 +0100156 PyObject *module, *temp, *value = NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 /* Initialize module */
159 module = PyModule_Create(&multiprocessing_module);
160 if (!module)
161 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163#if defined(MS_WINDOWS) || \
Mark Dickinsona614f042009-11-28 12:48:43 +0000164 (defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED))
Richard Oudkerk8fb9f4c2012-10-07 18:08:47 +0100165 /* Add _PyMp_SemLock type to module */
166 if (PyType_Ready(&_PyMp_SemLockType) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 return NULL;
Richard Oudkerk8fb9f4c2012-10-07 18:08:47 +0100168 Py_INCREF(&_PyMp_SemLockType);
Gregory P. Smith68e01352010-10-17 02:14:36 +0000169 {
170 PyObject *py_sem_value_max;
171 /* Some systems define SEM_VALUE_MAX as an unsigned value that
172 * causes it to be negative when used as an int (NetBSD). */
173 if ((int)(SEM_VALUE_MAX) < 0)
174 py_sem_value_max = PyLong_FromLong(INT_MAX);
175 else
176 py_sem_value_max = PyLong_FromLong(SEM_VALUE_MAX);
177 if (py_sem_value_max == NULL)
178 return NULL;
Richard Oudkerk8fb9f4c2012-10-07 18:08:47 +0100179 PyDict_SetItemString(_PyMp_SemLockType.tp_dict, "SEM_VALUE_MAX",
Gregory P. Smith68e01352010-10-17 02:14:36 +0000180 py_sem_value_max);
181 }
Richard Oudkerk8fb9f4c2012-10-07 18:08:47 +0100182 PyModule_AddObject(module, "SemLock", (PyObject*)&_PyMp_SemLockType);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000183#endif
184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 /* Add configuration macros */
186 temp = PyDict_New();
187 if (!temp)
188 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190#define ADD_FLAG(name) \
191 value = Py_BuildValue("i", name); \
192 if (value == NULL) { Py_DECREF(temp); return NULL; } \
193 if (PyDict_SetItemString(temp, #name, value) < 0) { \
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200194 Py_DECREF(temp); Py_DECREF(value); return NULL; } \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 Py_DECREF(value)
196
Mark Dickinsona614f042009-11-28 12:48:43 +0000197#if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 ADD_FLAG(HAVE_SEM_OPEN);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000199#endif
200#ifdef HAVE_SEM_TIMEDWAIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 ADD_FLAG(HAVE_SEM_TIMEDWAIT);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000202#endif
Benjamin Petersonfa268032008-06-13 19:28:21 +0000203#ifdef HAVE_BROKEN_SEM_GETVALUE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 ADD_FLAG(HAVE_BROKEN_SEM_GETVALUE);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000205#endif
206#ifdef HAVE_BROKEN_SEM_UNLINK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 ADD_FLAG(HAVE_BROKEN_SEM_UNLINK);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000208#endif
209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 if (PyModule_AddObject(module, "flags", temp) < 0)
211 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 return module;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000214}