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