blob: d370f9af2c415a11b0bb70c9ddac7dc52cd61cc2 [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
Benjamin Petersonfa268032008-06-13 19:28:21 +000056static PyObject*
57multiprocessing_address_of_buffer(PyObject *self, PyObject *obj)
58{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 void *buffer;
60 Py_ssize_t buffer_len;
Benjamin Petersonfa268032008-06-13 19:28:21 +000061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 if (PyObject_AsWriteBuffer(obj, &buffer, &buffer_len) < 0)
63 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +000064
Antoine Pitrou2341f9b2011-05-09 20:55:03 +020065 return Py_BuildValue("Nn",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 PyLong_FromVoidPtr(buffer), buffer_len);
Benjamin Petersonfa268032008-06-13 19:28:21 +000067}
68
69
70/*
71 * Function table
72 */
73
74static PyMethodDef module_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 {"address_of_buffer", multiprocessing_address_of_buffer, METH_O,
76 "address_of_buffer(obj) -> int\n"
77 "Return address of obj assuming obj supports buffer inteface"},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 {NULL}
Benjamin Petersonfa268032008-06-13 19:28:21 +000079};
80
81
82/*
83 * Initialize
84 */
85
86static struct PyModuleDef multiprocessing_module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 PyModuleDef_HEAD_INIT,
88 "_multiprocessing",
89 NULL,
90 -1,
91 module_methods,
92 NULL,
93 NULL,
94 NULL,
95 NULL
Benjamin Petersonfa268032008-06-13 19:28:21 +000096};
97
98
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099PyMODINIT_FUNC
Benjamin Petersonfa268032008-06-13 19:28:21 +0000100PyInit__multiprocessing(void)
101{
Amaury Forgeot d'Arc7e447c82011-12-17 10:23:14 +0100102 PyObject *module, *temp, *value = NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 /* Initialize module */
105 module = PyModule_Create(&multiprocessing_module);
106 if (!module)
107 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 /* Get copy of BufferTooShort */
110 temp = PyImport_ImportModule("multiprocessing");
111 if (!temp)
112 return NULL;
113 BufferTooShort = PyObject_GetAttrString(temp, "BufferTooShort");
114 Py_XDECREF(temp);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116#if defined(MS_WINDOWS) || \
Mark Dickinsona614f042009-11-28 12:48:43 +0000117 (defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 /* Add SemLock type to module */
119 if (PyType_Ready(&SemLockType) < 0)
120 return NULL;
121 Py_INCREF(&SemLockType);
Gregory P. Smith68e01352010-10-17 02:14:36 +0000122 {
123 PyObject *py_sem_value_max;
124 /* Some systems define SEM_VALUE_MAX as an unsigned value that
125 * causes it to be negative when used as an int (NetBSD). */
126 if ((int)(SEM_VALUE_MAX) < 0)
127 py_sem_value_max = PyLong_FromLong(INT_MAX);
128 else
129 py_sem_value_max = PyLong_FromLong(SEM_VALUE_MAX);
130 if (py_sem_value_max == NULL)
131 return NULL;
132 PyDict_SetItemString(SemLockType.tp_dict, "SEM_VALUE_MAX",
133 py_sem_value_max);
134 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 PyModule_AddObject(module, "SemLock", (PyObject*)&SemLockType);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000136#endif
137
138#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 /* Initialize win32 class and add to multiprocessing */
140 temp = create_win32_namespace();
141 if (!temp)
142 return NULL;
143 PyModule_AddObject(module, "win32", temp);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000144#endif
145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 /* Add configuration macros */
147 temp = PyDict_New();
148 if (!temp)
149 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151#define ADD_FLAG(name) \
152 value = Py_BuildValue("i", name); \
153 if (value == NULL) { Py_DECREF(temp); return NULL; } \
154 if (PyDict_SetItemString(temp, #name, value) < 0) { \
155 Py_DECREF(temp); Py_DECREF(value); return NULL; } \
156 Py_DECREF(value)
157
Mark Dickinsona614f042009-11-28 12:48:43 +0000158#if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 ADD_FLAG(HAVE_SEM_OPEN);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000160#endif
161#ifdef HAVE_SEM_TIMEDWAIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 ADD_FLAG(HAVE_SEM_TIMEDWAIT);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000163#endif
Benjamin Petersonfa268032008-06-13 19:28:21 +0000164#ifdef HAVE_BROKEN_SEM_GETVALUE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 ADD_FLAG(HAVE_BROKEN_SEM_GETVALUE);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000166#endif
167#ifdef HAVE_BROKEN_SEM_UNLINK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 ADD_FLAG(HAVE_BROKEN_SEM_UNLINK);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000169#endif
170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 if (PyModule_AddObject(module, "flags", temp) < 0)
172 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 return module;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000175}