blob: 890b96d7dcde5d71ec32bf1f6cba1bd386d2b6a3 [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 *create_win32_namespace(void);
13
Benjamin Petersonfa268032008-06-13 19:28:21 +000014PyObject *ProcessError, *BufferTooShort;
15
16/*
17 * Function which raises exceptions based on error codes
18 */
19
20PyObject *
21mp_SetError(PyObject *Type, int num)
22{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000023 switch (num) {
Benjamin Petersonfa268032008-06-13 19:28:21 +000024#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000025 case MP_STANDARD_ERROR:
26 if (Type == NULL)
27 Type = PyExc_WindowsError;
28 PyErr_SetExcFromWindowsErr(Type, 0);
29 break;
30 case MP_SOCKET_ERROR:
31 if (Type == NULL)
32 Type = PyExc_WindowsError;
33 PyErr_SetExcFromWindowsErr(Type, WSAGetLastError());
34 break;
Benjamin Petersonfa268032008-06-13 19:28:21 +000035#else /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 case MP_STANDARD_ERROR:
37 case MP_SOCKET_ERROR:
38 if (Type == NULL)
39 Type = PyExc_OSError;
40 PyErr_SetFromErrno(Type);
41 break;
Benjamin Petersonfa268032008-06-13 19:28:21 +000042#endif /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 case MP_MEMORY_ERROR:
44 PyErr_NoMemory();
45 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 case MP_EXCEPTION_HAS_BEEN_SET:
47 break;
48 default:
49 PyErr_Format(PyExc_RuntimeError,
50 "unkown error number %d", num);
51 }
52 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +000053}
54
55
56/*
57 * Windows only
58 */
59
60#ifdef MS_WINDOWS
61
62/* On Windows we set an event to signal Ctrl-C; compare with timemodule.c */
63
64HANDLE sigint_event = NULL;
65
66static BOOL WINAPI
67ProcessingCtrlHandler(DWORD dwCtrlType)
68{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 SetEvent(sigint_event);
70 return FALSE;
Benjamin Petersonfa268032008-06-13 19:28:21 +000071}
72
Charles-François Natalidc863dd2011-09-24 20:04:29 +020073#endif /* MS_WINDOWS */
Benjamin Petersonfa268032008-06-13 19:28:21 +000074
75
76/*
77 * All platforms
78 */
79
80static PyObject*
81multiprocessing_address_of_buffer(PyObject *self, PyObject *obj)
82{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 void *buffer;
84 Py_ssize_t buffer_len;
Benjamin Petersonfa268032008-06-13 19:28:21 +000085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 if (PyObject_AsWriteBuffer(obj, &buffer, &buffer_len) < 0)
87 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +000088
Antoine Pitrou2341f9b2011-05-09 20:55:03 +020089 return Py_BuildValue("Nn",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 PyLong_FromVoidPtr(buffer), buffer_len);
Benjamin Petersonfa268032008-06-13 19:28:21 +000091}
92
93
94/*
95 * Function table
96 */
97
98static PyMethodDef module_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 {"address_of_buffer", multiprocessing_address_of_buffer, METH_O,
100 "address_of_buffer(obj) -> int\n"
101 "Return address of obj assuming obj supports buffer inteface"},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 {NULL}
Benjamin Petersonfa268032008-06-13 19:28:21 +0000103};
104
105
106/*
107 * Initialize
108 */
109
110static struct PyModuleDef multiprocessing_module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 PyModuleDef_HEAD_INIT,
112 "_multiprocessing",
113 NULL,
114 -1,
115 module_methods,
116 NULL,
117 NULL,
118 NULL,
119 NULL
Benjamin Petersonfa268032008-06-13 19:28:21 +0000120};
121
122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123PyMODINIT_FUNC
Benjamin Petersonfa268032008-06-13 19:28:21 +0000124PyInit__multiprocessing(void)
125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 PyObject *module, *temp, *value;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 /* Initialize module */
129 module = PyModule_Create(&multiprocessing_module);
130 if (!module)
131 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 /* Get copy of BufferTooShort */
134 temp = PyImport_ImportModule("multiprocessing");
135 if (!temp)
136 return NULL;
137 BufferTooShort = PyObject_GetAttrString(temp, "BufferTooShort");
138 Py_XDECREF(temp);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140#if defined(MS_WINDOWS) || \
Mark Dickinsona614f042009-11-28 12:48:43 +0000141 (defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 /* Add SemLock type to module */
143 if (PyType_Ready(&SemLockType) < 0)
144 return NULL;
145 Py_INCREF(&SemLockType);
Gregory P. Smith68e01352010-10-17 02:14:36 +0000146 {
147 PyObject *py_sem_value_max;
148 /* Some systems define SEM_VALUE_MAX as an unsigned value that
149 * causes it to be negative when used as an int (NetBSD). */
150 if ((int)(SEM_VALUE_MAX) < 0)
151 py_sem_value_max = PyLong_FromLong(INT_MAX);
152 else
153 py_sem_value_max = PyLong_FromLong(SEM_VALUE_MAX);
154 if (py_sem_value_max == NULL)
155 return NULL;
156 PyDict_SetItemString(SemLockType.tp_dict, "SEM_VALUE_MAX",
157 py_sem_value_max);
158 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 PyModule_AddObject(module, "SemLock", (PyObject*)&SemLockType);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000160#endif
161
162#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 /* Initialize win32 class and add to multiprocessing */
164 temp = create_win32_namespace();
165 if (!temp)
166 return NULL;
167 PyModule_AddObject(module, "win32", temp);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 /* Initialize the event handle used to signal Ctrl-C */
170 sigint_event = CreateEvent(NULL, TRUE, FALSE, NULL);
171 if (!sigint_event) {
172 PyErr_SetFromWindowsErr(0);
173 return NULL;
174 }
175 if (!SetConsoleCtrlHandler(ProcessingCtrlHandler, TRUE)) {
176 PyErr_SetFromWindowsErr(0);
177 return NULL;
178 }
Benjamin Petersonfa268032008-06-13 19:28:21 +0000179#endif
180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 /* Add configuration macros */
182 temp = PyDict_New();
183 if (!temp)
184 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186#define ADD_FLAG(name) \
187 value = Py_BuildValue("i", name); \
188 if (value == NULL) { Py_DECREF(temp); return NULL; } \
189 if (PyDict_SetItemString(temp, #name, value) < 0) { \
190 Py_DECREF(temp); Py_DECREF(value); return NULL; } \
191 Py_DECREF(value)
192
Mark Dickinsona614f042009-11-28 12:48:43 +0000193#if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 ADD_FLAG(HAVE_SEM_OPEN);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000195#endif
196#ifdef HAVE_SEM_TIMEDWAIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 ADD_FLAG(HAVE_SEM_TIMEDWAIT);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000198#endif
Benjamin Petersonfa268032008-06-13 19:28:21 +0000199#ifdef HAVE_BROKEN_SEM_GETVALUE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 ADD_FLAG(HAVE_BROKEN_SEM_GETVALUE);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000201#endif
202#ifdef HAVE_BROKEN_SEM_UNLINK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 ADD_FLAG(HAVE_BROKEN_SEM_UNLINK);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000204#endif
205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 if (PyModule_AddObject(module, "flags", temp) < 0)
207 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 return module;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000210}