Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* Thread module */ |
| 33 | /* Interface to Sjoerd's portable C thread library */ |
| 34 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 35 | #include "Python.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 37 | #ifndef WITH_THREAD |
| 38 | Error! The rest of Python is not compiled with thread support. |
| 39 | Rerun configure, adding a --with-thread option. |
| 40 | #endif |
| 41 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 42 | #include "thread.h" |
| 43 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 44 | extern int _PyThread_Started; |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 45 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 46 | static PyObject *ThreadError; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 47 | |
| 48 | |
| 49 | /* Lock objects */ |
| 50 | |
| 51 | typedef struct { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 52 | PyObject_HEAD |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 53 | type_lock lock_lock; |
| 54 | } lockobject; |
| 55 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 56 | staticforward PyTypeObject Locktype; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 57 | |
| 58 | #define is_lockobject(v) ((v)->ob_type == &Locktype) |
| 59 | |
Sjoerd Mullender | ed59d20 | 1993-01-06 13:36:38 +0000 | [diff] [blame] | 60 | type_lock |
| 61 | getlocklock(lock) |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 62 | PyObject *lock; |
Sjoerd Mullender | ed59d20 | 1993-01-06 13:36:38 +0000 | [diff] [blame] | 63 | { |
| 64 | if (lock == NULL || !is_lockobject(lock)) |
| 65 | return NULL; |
| 66 | else |
| 67 | return ((lockobject *) lock)->lock_lock; |
| 68 | } |
| 69 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 70 | static lockobject * |
| 71 | newlockobject() |
| 72 | { |
| 73 | lockobject *self; |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 74 | self = PyObject_NEW(lockobject, &Locktype); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 75 | if (self == NULL) |
| 76 | return NULL; |
| 77 | self->lock_lock = allocate_lock(); |
| 78 | if (self->lock_lock == NULL) { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 79 | PyMem_DEL(self); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 80 | self = NULL; |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 81 | PyErr_SetString(ThreadError, "can't allocate lock"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 82 | } |
| 83 | return self; |
| 84 | } |
| 85 | |
| 86 | static void |
| 87 | lock_dealloc(self) |
| 88 | lockobject *self; |
| 89 | { |
| 90 | /* Unlock the lock so it's safe to free it */ |
| 91 | acquire_lock(self->lock_lock, 0); |
| 92 | release_lock(self->lock_lock); |
| 93 | |
| 94 | free_lock(self->lock_lock); |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 95 | PyMem_DEL(self); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 98 | static PyObject * |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 99 | lock_acquire_lock(self, args) |
| 100 | lockobject *self; |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 101 | PyObject *args; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 102 | { |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 103 | int i; |
| 104 | |
| 105 | if (args != NULL) { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 106 | if (!PyArg_Parse(args, "i", &i)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 107 | return NULL; |
| 108 | } |
| 109 | else |
| 110 | i = 1; |
| 111 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 112 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 113 | i = acquire_lock(self->lock_lock, i); |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 114 | Py_END_ALLOW_THREADS |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 115 | |
| 116 | if (args == NULL) { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 117 | Py_INCREF(Py_None); |
| 118 | return Py_None; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 119 | } |
| 120 | else |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 121 | return PyInt_FromLong((long)i); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 124 | static PyObject * |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 125 | lock_release_lock(self, args) |
| 126 | lockobject *self; |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 127 | PyObject *args; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 128 | { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 129 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 130 | return NULL; |
| 131 | |
| 132 | /* Sanity check: the lock must be locked */ |
| 133 | if (acquire_lock(self->lock_lock, 0)) { |
| 134 | release_lock(self->lock_lock); |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 135 | PyErr_SetString(ThreadError, "release unlocked lock"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | release_lock(self->lock_lock); |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 140 | Py_INCREF(Py_None); |
| 141 | return Py_None; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 144 | static PyObject * |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 145 | lock_locked_lock(self, args) |
| 146 | lockobject *self; |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 147 | PyObject *args; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 148 | { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 149 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 150 | return NULL; |
| 151 | |
| 152 | if (acquire_lock(self->lock_lock, 0)) { |
| 153 | release_lock(self->lock_lock); |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 154 | return PyInt_FromLong(0L); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 155 | } |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 156 | return PyInt_FromLong(1L); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 159 | static PyMethodDef lock_methods[] = { |
| 160 | {"acquire_lock", (PyCFunction)lock_acquire_lock}, |
| 161 | {"acquire", (PyCFunction)lock_acquire_lock}, |
| 162 | {"release_lock", (PyCFunction)lock_release_lock}, |
| 163 | {"release", (PyCFunction)lock_release_lock}, |
| 164 | {"locked_lock", (PyCFunction)lock_locked_lock}, |
| 165 | {"locked", (PyCFunction)lock_locked_lock}, |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 166 | {NULL, NULL} /* sentinel */ |
| 167 | }; |
| 168 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 169 | static PyObject * |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 170 | lock_getattr(self, name) |
| 171 | lockobject *self; |
| 172 | char *name; |
| 173 | { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 174 | return Py_FindMethod(lock_methods, (PyObject *)self, name); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 177 | static PyTypeObject Locktype = { |
| 178 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 179 | 0, /*ob_size*/ |
| 180 | "lock", /*tp_name*/ |
| 181 | sizeof(lockobject), /*tp_size*/ |
| 182 | 0, /*tp_itemsize*/ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 183 | /* methods */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 184 | (destructor)lock_dealloc, /*tp_dealloc*/ |
| 185 | 0, /*tp_print*/ |
| 186 | (getattrfunc)lock_getattr, /*tp_getattr*/ |
| 187 | 0, /*tp_setattr*/ |
| 188 | 0, /*tp_compare*/ |
| 189 | 0, /*tp_repr*/ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | |
| 193 | /* Module functions */ |
| 194 | |
| 195 | static void |
| 196 | t_bootstrap(args_raw) |
| 197 | void *args_raw; |
| 198 | { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 199 | PyObject *args = (PyObject *) args_raw; |
| 200 | PyObject *func, *arg, *res; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 201 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 202 | _PyThread_Started++; |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 203 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 204 | PyEval_RestoreThread((void *)NULL); |
| 205 | func = PyTuple_GetItem(args, 0); |
| 206 | arg = PyTuple_GetItem(args, 1); |
| 207 | res = PyEval_CallObject(func, arg); |
| 208 | Py_DECREF(args); /* Matches the INCREF(args) in thread_start_new_thread */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 209 | if (res == NULL) { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 210 | if (PyErr_Occurred() == PyExc_SystemExit) |
| 211 | PyErr_Clear(); |
Guido van Rossum | 385e7c6 | 1995-03-17 10:42:27 +0000 | [diff] [blame] | 212 | else { |
| 213 | fprintf(stderr, "Unhandled exception in thread:\n"); |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 214 | PyErr_Print(); /* From pythonmain.c */ |
Guido van Rossum | 385e7c6 | 1995-03-17 10:42:27 +0000 | [diff] [blame] | 215 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 216 | } |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 217 | else |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 218 | Py_DECREF(res); |
| 219 | (void) PyEval_SaveThread(); /* Should always be NULL */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 220 | exit_thread(); |
| 221 | } |
| 222 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 223 | static PyObject * |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 224 | thread_start_new_thread(self, args) |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 225 | PyObject *self; /* Not used */ |
| 226 | PyObject *args; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 227 | { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 228 | PyObject *func, *arg; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 229 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 230 | if (!PyArg_Parse(args, "(OO)", &func, &arg)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 231 | return NULL; |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 232 | Py_INCREF(args); |
Sjoerd Mullender | ed59d20 | 1993-01-06 13:36:38 +0000 | [diff] [blame] | 233 | /* Initialize the interpreter's stack save/restore mechanism */ |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 234 | PyEval_InitThreads(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 235 | if (!start_new_thread(t_bootstrap, (void*) args)) { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 236 | Py_DECREF(args); |
| 237 | PyErr_SetString(ThreadError, "can't start new thread\n"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 238 | return NULL; |
| 239 | } |
| 240 | /* Otherwise the DECREF(args) is done by t_bootstrap */ |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 241 | Py_INCREF(Py_None); |
| 242 | return Py_None; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 245 | static PyObject * |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 246 | thread_exit_thread(self, args) |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 247 | PyObject *self; /* Not used */ |
| 248 | PyObject *args; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 249 | { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 250 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 251 | return NULL; |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 252 | PyErr_SetNone(PyExc_SystemExit); |
Guido van Rossum | 385e7c6 | 1995-03-17 10:42:27 +0000 | [diff] [blame] | 253 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 256 | #ifndef NO_EXIT_PROG |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 257 | static PyObject * |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 258 | thread_exit_prog(self, args) |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 259 | PyObject *self; /* Not used */ |
| 260 | PyObject *args; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 261 | { |
| 262 | int sts; |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 263 | if (!PyArg_Parse(args, "i", &sts)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 264 | return NULL; |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 265 | Py_Exit(sts); /* Calls exit_prog(sts) or _exit_prog(sts) */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 266 | for (;;) { } /* Should not be reached */ |
| 267 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 268 | #endif |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 269 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 270 | static PyObject * |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 271 | thread_allocate_lock(self, args) |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 272 | PyObject *self; /* Not used */ |
| 273 | PyObject *args; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 274 | { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 275 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 276 | return NULL; |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 277 | return (PyObject *) newlockobject(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 280 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 281 | thread_get_ident(self, args) |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 282 | PyObject *self; /* Not used */ |
| 283 | PyObject *args; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 284 | { |
| 285 | long ident; |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 286 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 287 | return NULL; |
| 288 | ident = get_thread_ident(); |
| 289 | if (ident == -1) { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 290 | PyErr_SetString(ThreadError, "no current thread ident"); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 291 | return NULL; |
| 292 | } |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 293 | return PyInt_FromLong(ident); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 296 | static PyMethodDef thread_methods[] = { |
| 297 | {"start_new_thread", (PyCFunction)thread_start_new_thread}, |
| 298 | {"start_new", (PyCFunction)thread_start_new_thread}, |
| 299 | {"allocate_lock", (PyCFunction)thread_allocate_lock}, |
| 300 | {"allocate", (PyCFunction)thread_allocate_lock}, |
| 301 | {"exit_thread", (PyCFunction)thread_exit_thread}, |
| 302 | {"exit", (PyCFunction)thread_exit_thread}, |
| 303 | {"get_ident", (PyCFunction)thread_get_ident}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 304 | #ifndef NO_EXIT_PROG |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 305 | {"exit_prog", (PyCFunction)thread_exit_prog}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 306 | #endif |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 307 | {NULL, NULL} /* sentinel */ |
| 308 | }; |
| 309 | |
| 310 | |
| 311 | /* Initialization function */ |
| 312 | |
| 313 | void |
| 314 | initthread() |
| 315 | { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 316 | PyObject *m, *d; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 317 | |
| 318 | /* Create the module and add the functions */ |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 319 | m = Py_InitModule("thread", thread_methods); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 320 | |
| 321 | /* Add a symbolic constant */ |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 322 | d = PyModule_GetDict(m); |
| 323 | ThreadError = PyString_FromString("thread.error"); |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 324 | PyDict_SetItemString(d, "error", ThreadError); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 325 | |
| 326 | /* Check for errors */ |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 327 | if (PyErr_Occurred()) |
| 328 | Py_FatalError("can't initialize module thread"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 329 | |
| 330 | /* Initialize the C thread library */ |
| 331 | init_thread(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 332 | } |