Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 1 | /* |
| 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 Peterson | 965ce87 | 2009-04-05 21:24:58 +0000 | [diff] [blame] | 11 | |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 12 | PyObject *create_win32_namespace(void); |
| 13 | |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 14 | PyObject *ProcessError, *BufferTooShort; |
| 15 | |
| 16 | /* |
| 17 | * Function which raises exceptions based on error codes |
| 18 | */ |
| 19 | |
| 20 | PyObject * |
| 21 | mp_SetError(PyObject *Type, int num) |
| 22 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 23 | switch (num) { |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 24 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 25 | 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 Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 35 | #else /* !MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 36 | case MP_STANDARD_ERROR: |
| 37 | case MP_SOCKET_ERROR: |
| 38 | if (Type == NULL) |
| 39 | Type = PyExc_OSError; |
| 40 | PyErr_SetFromErrno(Type); |
| 41 | break; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 42 | #endif /* !MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 43 | case MP_MEMORY_ERROR: |
| 44 | PyErr_NoMemory(); |
| 45 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 46 | 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 Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 56 | static PyObject* |
| 57 | multiprocessing_address_of_buffer(PyObject *self, PyObject *obj) |
| 58 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 59 | void *buffer; |
| 60 | Py_ssize_t buffer_len; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 61 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | if (PyObject_AsWriteBuffer(obj, &buffer, &buffer_len) < 0) |
| 63 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 64 | |
Antoine Pitrou | 2341f9b | 2011-05-09 20:55:03 +0200 | [diff] [blame] | 65 | return Py_BuildValue("Nn", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 66 | PyLong_FromVoidPtr(buffer), buffer_len); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | |
| 70 | /* |
| 71 | * Function table |
| 72 | */ |
| 73 | |
| 74 | static PyMethodDef module_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 75 | {"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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | {NULL} |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | |
| 82 | /* |
| 83 | * Initialize |
| 84 | */ |
| 85 | |
| 86 | static struct PyModuleDef multiprocessing_module = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 87 | PyModuleDef_HEAD_INIT, |
| 88 | "_multiprocessing", |
| 89 | NULL, |
| 90 | -1, |
| 91 | module_methods, |
| 92 | NULL, |
| 93 | NULL, |
| 94 | NULL, |
| 95 | NULL |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 99 | PyMODINIT_FUNC |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 100 | PyInit__multiprocessing(void) |
| 101 | { |
Amaury Forgeot d'Arc | 7e447c8 | 2011-12-17 10:23:14 +0100 | [diff] [blame] | 102 | PyObject *module, *temp, *value = NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 103 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | /* Initialize module */ |
| 105 | module = PyModule_Create(&multiprocessing_module); |
| 106 | if (!module) |
| 107 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | /* 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 Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 115 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 116 | #if defined(MS_WINDOWS) || \ |
Mark Dickinson | a614f04 | 2009-11-28 12:48:43 +0000 | [diff] [blame] | 117 | (defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 118 | /* Add SemLock type to module */ |
| 119 | if (PyType_Ready(&SemLockType) < 0) |
| 120 | return NULL; |
| 121 | Py_INCREF(&SemLockType); |
Gregory P. Smith | 68e0135 | 2010-10-17 02:14:36 +0000 | [diff] [blame] | 122 | { |
| 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | PyModule_AddObject(module, "SemLock", (PyObject*)&SemLockType); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 136 | #endif |
| 137 | |
| 138 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | /* 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 Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 144 | #endif |
| 145 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 146 | /* Add configuration macros */ |
| 147 | temp = PyDict_New(); |
| 148 | if (!temp) |
| 149 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 150 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | #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 Dickinson | a614f04 | 2009-11-28 12:48:43 +0000 | [diff] [blame] | 158 | #if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 159 | ADD_FLAG(HAVE_SEM_OPEN); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 160 | #endif |
| 161 | #ifdef HAVE_SEM_TIMEDWAIT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | ADD_FLAG(HAVE_SEM_TIMEDWAIT); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 163 | #endif |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 164 | #ifdef HAVE_BROKEN_SEM_GETVALUE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | ADD_FLAG(HAVE_BROKEN_SEM_GETVALUE); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 166 | #endif |
| 167 | #ifdef HAVE_BROKEN_SEM_UNLINK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | ADD_FLAG(HAVE_BROKEN_SEM_UNLINK); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 169 | #endif |
| 170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 171 | if (PyModule_AddObject(module, "flags", temp) < 0) |
| 172 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 174 | return module; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 175 | } |