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 |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 38 | #error "Error! The rest of Python is not compiled with thread support." |
| 39 | #error "Rerun configure, adding a --with-thread option." |
| 40 | #error "Then run `make clean' followed by `make'." |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 41 | #endif |
| 42 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 43 | #include "thread.h" |
| 44 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 45 | static PyObject *ThreadError; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | /* Lock objects */ |
| 49 | |
| 50 | typedef struct { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 51 | PyObject_HEAD |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 52 | type_lock lock_lock; |
| 53 | } lockobject; |
| 54 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 55 | staticforward PyTypeObject Locktype; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 56 | |
| 57 | #define is_lockobject(v) ((v)->ob_type == &Locktype) |
| 58 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 59 | static type_lock |
Sjoerd Mullender | ed59d20 | 1993-01-06 13:36:38 +0000 | [diff] [blame] | 60 | getlocklock(lock) |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 61 | PyObject *lock; |
Sjoerd Mullender | ed59d20 | 1993-01-06 13:36:38 +0000 | [diff] [blame] | 62 | { |
| 63 | if (lock == NULL || !is_lockobject(lock)) |
| 64 | return NULL; |
| 65 | else |
| 66 | return ((lockobject *) lock)->lock_lock; |
| 67 | } |
| 68 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 69 | static lockobject * |
| 70 | newlockobject() |
| 71 | { |
| 72 | lockobject *self; |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 73 | self = PyObject_NEW(lockobject, &Locktype); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 74 | if (self == NULL) |
| 75 | return NULL; |
| 76 | self->lock_lock = allocate_lock(); |
| 77 | if (self->lock_lock == NULL) { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 78 | PyMem_DEL(self); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 79 | self = NULL; |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 80 | PyErr_SetString(ThreadError, "can't allocate lock"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 81 | } |
| 82 | return self; |
| 83 | } |
| 84 | |
| 85 | static void |
| 86 | lock_dealloc(self) |
| 87 | lockobject *self; |
| 88 | { |
| 89 | /* Unlock the lock so it's safe to free it */ |
| 90 | acquire_lock(self->lock_lock, 0); |
| 91 | release_lock(self->lock_lock); |
| 92 | |
| 93 | free_lock(self->lock_lock); |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 94 | PyMem_DEL(self); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 97 | static PyObject * |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 98 | lock_acquire_lock(self, args) |
| 99 | lockobject *self; |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 100 | PyObject *args; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 101 | { |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 102 | int i; |
| 103 | |
| 104 | if (args != NULL) { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 105 | if (!PyArg_Parse(args, "i", &i)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 106 | return NULL; |
| 107 | } |
| 108 | else |
| 109 | i = 1; |
| 110 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 111 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 112 | i = acquire_lock(self->lock_lock, i); |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 113 | Py_END_ALLOW_THREADS |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 114 | |
| 115 | if (args == NULL) { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 116 | Py_INCREF(Py_None); |
| 117 | return Py_None; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 118 | } |
| 119 | else |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 120 | return PyInt_FromLong((long)i); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 123 | static char acquire_doc[] = |
| 124 | "acquire([wait]) -> None or Boolean\n\ |
| 125 | (acquire_lock() is an obsolete synonym)\n\ |
| 126 | \n\ |
| 127 | Lock the lock. Without argument, this blocks if the lock is already\n\ |
| 128 | locked (even by the same thread), waiting for another thread to release\n\ |
| 129 | the lock, and return None when the lock is acquired.\n\ |
| 130 | With a Boolean argument, this will only block if the argument is true,\n\ |
| 131 | and the return value reflects whether the lock is acquired.\n\ |
| 132 | The blocking operation is not interruptible."; |
| 133 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 134 | static PyObject * |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 135 | lock_release_lock(self, args) |
| 136 | lockobject *self; |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 137 | PyObject *args; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 138 | { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 139 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 140 | return NULL; |
| 141 | |
| 142 | /* Sanity check: the lock must be locked */ |
| 143 | if (acquire_lock(self->lock_lock, 0)) { |
| 144 | release_lock(self->lock_lock); |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 145 | PyErr_SetString(ThreadError, "release unlocked lock"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | release_lock(self->lock_lock); |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 150 | Py_INCREF(Py_None); |
| 151 | return Py_None; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 154 | static char release_doc[] = |
| 155 | "release()\n\ |
| 156 | (release_lock() is an obsolete synonym)\n\ |
| 157 | \n\ |
| 158 | Release the lock, allowing another thread that is blocked waiting for\n\ |
| 159 | the lock to acquire the lock. The lock must be in the locked state,\n\ |
| 160 | but it needn't be locked by the same thread that unlocks it."; |
| 161 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 162 | static PyObject * |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 163 | lock_locked_lock(self, args) |
| 164 | lockobject *self; |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 165 | PyObject *args; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 166 | { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 167 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 168 | return NULL; |
| 169 | |
| 170 | if (acquire_lock(self->lock_lock, 0)) { |
| 171 | release_lock(self->lock_lock); |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 172 | return PyInt_FromLong(0L); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 173 | } |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 174 | return PyInt_FromLong(1L); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 177 | static char locked_doc[] = |
| 178 | "locked() -> Boolean\n\ |
| 179 | (locked_lock() is an obsolete synonym)\n\ |
| 180 | \n\ |
| 181 | Return whether the lock is in the locked state."; |
| 182 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 183 | static PyMethodDef lock_methods[] = { |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 184 | {"acquire_lock", (PyCFunction)lock_acquire_lock, 0, acquire_doc}, |
| 185 | {"acquire", (PyCFunction)lock_acquire_lock, 0, acquire_doc}, |
| 186 | {"release_lock", (PyCFunction)lock_release_lock, 0, release_doc}, |
| 187 | {"release", (PyCFunction)lock_release_lock, 0, release_doc}, |
| 188 | {"locked_lock", (PyCFunction)lock_locked_lock, 0, locked_doc}, |
| 189 | {"locked", (PyCFunction)lock_locked_lock, 0, locked_doc}, |
| 190 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 193 | static PyObject * |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 194 | lock_getattr(self, name) |
| 195 | lockobject *self; |
| 196 | char *name; |
| 197 | { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 198 | return Py_FindMethod(lock_methods, (PyObject *)self, name); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 201 | static PyTypeObject Locktype = { |
| 202 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 203 | 0, /*ob_size*/ |
| 204 | "lock", /*tp_name*/ |
| 205 | sizeof(lockobject), /*tp_size*/ |
| 206 | 0, /*tp_itemsize*/ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 207 | /* methods */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 208 | (destructor)lock_dealloc, /*tp_dealloc*/ |
| 209 | 0, /*tp_print*/ |
| 210 | (getattrfunc)lock_getattr, /*tp_getattr*/ |
| 211 | 0, /*tp_setattr*/ |
| 212 | 0, /*tp_compare*/ |
| 213 | 0, /*tp_repr*/ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | |
| 217 | /* Module functions */ |
| 218 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 219 | struct bootstate { |
| 220 | PyInterpreterState *interp; |
| 221 | PyObject *func; |
| 222 | PyObject *args; |
| 223 | PyObject *keyw; |
| 224 | }; |
| 225 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 226 | static void |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 227 | t_bootstrap(boot_raw) |
| 228 | void *boot_raw; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 229 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 230 | struct bootstate *boot = (struct bootstate *) boot_raw; |
Guido van Rossum | 75aa0d6 | 1997-07-18 23:57:50 +0000 | [diff] [blame] | 231 | PyThreadState *tstate; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 232 | PyObject *res; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 233 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 234 | tstate = PyThreadState_New(boot->interp); |
Guido van Rossum | 75aa0d6 | 1997-07-18 23:57:50 +0000 | [diff] [blame] | 235 | PyEval_AcquireThread(tstate); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 236 | res = PyEval_CallObjectWithKeywords( |
| 237 | boot->func, boot->args, boot->keyw); |
| 238 | Py_DECREF(boot->func); |
| 239 | Py_DECREF(boot->args); |
| 240 | Py_XDECREF(boot->keyw); |
| 241 | PyMem_DEL(boot_raw); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 242 | if (res == NULL) { |
Fred Drake | bebc97f | 1998-05-28 04:35:12 +0000 | [diff] [blame] | 243 | if (PyErr_ExceptionMatches(PyExc_SystemExit)) |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 244 | PyErr_Clear(); |
Guido van Rossum | 385e7c6 | 1995-03-17 10:42:27 +0000 | [diff] [blame] | 245 | else { |
| 246 | fprintf(stderr, "Unhandled exception in thread:\n"); |
Guido van Rossum | 40769dd | 1998-02-06 22:32:08 +0000 | [diff] [blame] | 247 | PyErr_PrintEx(0); |
Guido van Rossum | 385e7c6 | 1995-03-17 10:42:27 +0000 | [diff] [blame] | 248 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 249 | } |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 250 | else |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 251 | Py_DECREF(res); |
Guido van Rossum | b02158e | 1997-08-02 03:13:11 +0000 | [diff] [blame] | 252 | PyThreadState_Clear(tstate); |
Guido van Rossum | 75aa0d6 | 1997-07-18 23:57:50 +0000 | [diff] [blame] | 253 | PyEval_ReleaseThread(tstate); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 254 | PyThreadState_Delete(tstate); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 255 | #ifdef __BEOS__ |
| 256 | /* Dunno if this will cause problems with other ports; the BeOS thread |
| 257 | * support features only 100% renamed functions. [cjh] |
| 258 | */ |
| 259 | PyThread_exit_thread(); |
| 260 | #else |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 261 | exit_thread(); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 262 | #endif |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 265 | static PyObject * |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 266 | thread_start_new_thread(self, fargs) |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 267 | PyObject *self; /* Not used */ |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 268 | PyObject *fargs; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 269 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 270 | PyObject *func, *args = NULL, *keyw = NULL; |
| 271 | struct bootstate *boot; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 272 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 273 | if (!PyArg_ParseTuple(fargs, "OO|O", &func, &args, &keyw)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 274 | return NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 275 | if (!PyCallable_Check(func)) { |
| 276 | PyErr_SetString(PyExc_TypeError, |
| 277 | "first arg must be callable"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 278 | return NULL; |
| 279 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 280 | if (!PyTuple_Check(args)) { |
| 281 | PyErr_SetString(PyExc_TypeError, |
| 282 | "optional 2nd arg must be a tuple"); |
| 283 | return NULL; |
| 284 | } |
| 285 | if (keyw != NULL && !PyDict_Check(keyw)) { |
| 286 | PyErr_SetString(PyExc_TypeError, |
| 287 | "optional 3rd arg must be a dictionary"); |
| 288 | return NULL; |
| 289 | } |
| 290 | boot = PyMem_NEW(struct bootstate, 1); |
| 291 | if (boot == NULL) |
| 292 | return PyErr_NoMemory(); |
Guido van Rossum | b02158e | 1997-08-02 03:13:11 +0000 | [diff] [blame] | 293 | boot->interp = PyThreadState_Get()->interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 294 | boot->func = func; |
| 295 | boot->args = args; |
| 296 | boot->keyw = keyw; |
| 297 | Py_INCREF(func); |
| 298 | Py_INCREF(args); |
| 299 | Py_XINCREF(keyw); |
| 300 | PyEval_InitThreads(); /* Start the interpreter's thread-awareness */ |
| 301 | if (!start_new_thread(t_bootstrap, (void*) boot)) { |
| 302 | PyErr_SetString(ThreadError, "can't start new thread\n"); |
| 303 | Py_DECREF(func); |
| 304 | Py_DECREF(args); |
| 305 | Py_XDECREF(keyw); |
| 306 | PyMem_DEL(boot); |
| 307 | return NULL; |
| 308 | } |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 309 | Py_INCREF(Py_None); |
| 310 | return Py_None; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 313 | static char start_new_doc[] = |
| 314 | "start_new_thread(functon, args[, kwargs])\n\ |
| 315 | (start_new() is an obsolete synonym)\n\ |
| 316 | \n\ |
| 317 | Start a new thread. The thread will call the function with positional\n\ |
| 318 | arguments from the tuple args and keyword arguments taken from the optional\n\ |
| 319 | dictionary kwargs. The thread exits when the function returns; the return\n\ |
| 320 | value is ignored. The thread will also exit when the function raises an\n\ |
| 321 | unhandled exception; a stack trace will be printed unless the exception is\n\ |
| 322 | SystemExit."; |
| 323 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 324 | static PyObject * |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 325 | thread_exit_thread(self, args) |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 326 | PyObject *self; /* Not used */ |
| 327 | PyObject *args; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 328 | { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 329 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 330 | return NULL; |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 331 | PyErr_SetNone(PyExc_SystemExit); |
Guido van Rossum | 385e7c6 | 1995-03-17 10:42:27 +0000 | [diff] [blame] | 332 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 335 | static char exit_doc[] = |
| 336 | "exit()\n\ |
| 337 | (exit_thread() is an obsolete synonym)\n\ |
| 338 | \n\ |
| 339 | This is synonymous to ``raise SystemExit''. It will cause the current\n\ |
| 340 | thread to exit silently unless the exception is caught."; |
| 341 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 342 | #ifndef NO_EXIT_PROG |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 343 | static PyObject * |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 344 | thread_exit_prog(self, args) |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 345 | PyObject *self; /* Not used */ |
| 346 | PyObject *args; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 347 | { |
| 348 | int sts; |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 349 | if (!PyArg_Parse(args, "i", &sts)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 350 | return NULL; |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 351 | Py_Exit(sts); /* Calls exit_prog(sts) or _exit_prog(sts) */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 352 | for (;;) { } /* Should not be reached */ |
| 353 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 354 | #endif |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 355 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 356 | static PyObject * |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 357 | thread_allocate_lock(self, args) |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 358 | PyObject *self; /* Not used */ |
| 359 | PyObject *args; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 360 | { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 361 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 362 | return NULL; |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 363 | return (PyObject *) newlockobject(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 366 | static char allocate_doc[] = |
| 367 | "allocate_lock() -> lock object\n\ |
| 368 | (allocate() is an obsolete synonym)\n\ |
| 369 | \n\ |
| 370 | Create a new lock object. See LockType.__doc__ for information about locks."; |
| 371 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 372 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 373 | thread_get_ident(self, args) |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 374 | PyObject *self; /* Not used */ |
| 375 | PyObject *args; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 376 | { |
| 377 | long ident; |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 378 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 379 | return NULL; |
| 380 | ident = get_thread_ident(); |
| 381 | if (ident == -1) { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 382 | PyErr_SetString(ThreadError, "no current thread ident"); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 383 | return NULL; |
| 384 | } |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 385 | return PyInt_FromLong(ident); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 388 | static char get_ident_doc[] = |
| 389 | "get_ident() -> integer\n\ |
| 390 | \n\ |
| 391 | Return a non-zero integer that uniquely identifies the current thread\n\ |
| 392 | amongst other threads that exist simultaneously.\n\ |
| 393 | This may be used to identify per-thread resources.\n\ |
| 394 | Even though on some platforms threads identities may appear to be\n\ |
| 395 | allocated consecutive numbers starting at 1, this behavior should not\n\ |
| 396 | be relied upon, and the number should be seen purely as a magic cookie.\n\ |
| 397 | A thread's identity may be reused for another thread after it exits."; |
| 398 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 399 | static PyMethodDef thread_methods[] = { |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 400 | {"start_new_thread", (PyCFunction)thread_start_new_thread, 1, |
| 401 | start_new_doc}, |
| 402 | {"start_new", (PyCFunction)thread_start_new_thread, 1, |
| 403 | start_new_doc}, |
| 404 | {"allocate_lock", (PyCFunction)thread_allocate_lock, 0, |
| 405 | allocate_doc}, |
| 406 | {"allocate", (PyCFunction)thread_allocate_lock, 0, |
| 407 | allocate_doc}, |
| 408 | {"exit_thread", (PyCFunction)thread_exit_thread, 0, |
| 409 | exit_doc}, |
| 410 | {"exit", (PyCFunction)thread_exit_thread, 0, |
| 411 | exit_doc}, |
| 412 | {"get_ident", (PyCFunction)thread_get_ident, 0, |
| 413 | get_ident_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 414 | #ifndef NO_EXIT_PROG |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 415 | {"exit_prog", (PyCFunction)thread_exit_prog}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 416 | #endif |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 417 | {NULL, NULL} /* sentinel */ |
| 418 | }; |
| 419 | |
| 420 | |
| 421 | /* Initialization function */ |
| 422 | |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 423 | static char thread_doc[] = |
| 424 | "This module provides primitive operations to write multi-threaded programs.\n\ |
| 425 | The 'threading' module provides a more convenient interface."; |
| 426 | |
| 427 | static char lock_doc[] = |
| 428 | "A lock object is a synchronization primitive. To create a lock,\n\ |
| 429 | call the allocate_lock() function. Methods are:\n\ |
| 430 | \n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 431 | acquire() -- lock the lock, possibly blocking until it can be obtained\n\ |
| 432 | release() -- unlock of the lock\n\ |
| 433 | locked() -- test whether the lock is currently locked\n\ |
| 434 | \n\ |
| 435 | A lock is not owned by the thread that locked it; another thread may\n\ |
| 436 | unlock it. A thread attempting to lock a lock that it has already locked\n\ |
| 437 | will block until another thread unlocks it. Deadlocks may ensue."; |
| 438 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 439 | void |
| 440 | initthread() |
| 441 | { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 442 | PyObject *m, *d; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 443 | |
| 444 | /* Create the module and add the functions */ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 445 | m = Py_InitModule3("thread", thread_methods, thread_doc); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 446 | |
| 447 | /* Add a symbolic constant */ |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 448 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 449 | ThreadError = PyErr_NewException("thread.error", NULL, NULL); |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 450 | PyDict_SetItemString(d, "error", ThreadError); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 451 | Locktype.tp_doc = lock_doc; |
| 452 | Py_INCREF(&Locktype); |
| 453 | PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 454 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 455 | /* Initialize the C thread library */ |
| 456 | init_thread(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 457 | } |