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