Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1 | |
| 2 | /* Thread module */ |
| 3 | /* Interface to Sjoerd's portable C thread library */ |
| 4 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 5 | #include "Python.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7 | #ifndef WITH_THREAD |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 8 | #error "Error! The rest of Python is not compiled with thread support." |
Neal Norwitz | 884baa1 | 2002-09-05 21:31:04 +0000 | [diff] [blame] | 9 | #error "Rerun configure, adding a --with-threads option." |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 10 | #error "Then run `make clean' followed by `make'." |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11 | #endif |
| 12 | |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 13 | #include "pythread.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 14 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 15 | static PyObject *ThreadError; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 16 | |
| 17 | |
| 18 | /* Lock objects */ |
| 19 | |
| 20 | typedef struct { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 21 | PyObject_HEAD |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 22 | PyThread_type_lock lock_lock; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 23 | } lockobject; |
| 24 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 25 | static void |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 26 | lock_dealloc(lockobject *self) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 27 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 28 | assert(self->lock_lock); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 29 | /* Unlock the lock so it's safe to free it */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 30 | PyThread_acquire_lock(self->lock_lock, 0); |
| 31 | PyThread_release_lock(self->lock_lock); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 33 | PyThread_free_lock(self->lock_lock); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 34 | PyObject_Del(self); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 37 | static PyObject * |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 38 | lock_PyThread_acquire_lock(lockobject *self, PyObject *args) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 39 | { |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 40 | int i = 1; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 41 | |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 42 | if (!PyArg_ParseTuple(args, "|i:acquire", &i)) |
| 43 | return NULL; |
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 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 46 | i = PyThread_acquire_lock(self->lock_lock, i); |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 47 | Py_END_ALLOW_THREADS |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 48 | |
Andrew M. Kuchling | a43ece9 | 2005-06-02 17:07:11 +0000 | [diff] [blame] | 49 | return PyBool_FromLong((long)i); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 52 | PyDoc_STRVAR(acquire_doc, |
Guido van Rossum | 8fdc75b | 2002-04-07 06:32:21 +0000 | [diff] [blame] | 53 | "acquire([wait]) -> None or bool\n\ |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 54 | (acquire_lock() is an obsolete synonym)\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 55 | \n\ |
| 56 | Lock the lock. Without argument, this blocks if the lock is already\n\ |
| 57 | locked (even by the same thread), waiting for another thread to release\n\ |
Guido van Rossum | 8fdc75b | 2002-04-07 06:32:21 +0000 | [diff] [blame] | 58 | the lock, and return None once the lock is acquired.\n\ |
| 59 | With an argument, this will only block if the argument is true,\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 60 | and the return value reflects whether the lock is acquired.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 61 | The blocking operation is not interruptible."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 62 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 63 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 64 | lock_PyThread_release_lock(lockobject *self) |
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 | /* Sanity check: the lock must be locked */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 67 | if (PyThread_acquire_lock(self->lock_lock, 0)) { |
| 68 | PyThread_release_lock(self->lock_lock); |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 69 | PyErr_SetString(ThreadError, "release unlocked lock"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 70 | return NULL; |
| 71 | } |
| 72 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 73 | PyThread_release_lock(self->lock_lock); |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 74 | Py_INCREF(Py_None); |
| 75 | return Py_None; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 78 | PyDoc_STRVAR(release_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 79 | "release()\n\ |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 80 | (release_lock() is an obsolete synonym)\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 81 | \n\ |
| 82 | Release the lock, allowing another thread that is blocked waiting for\n\ |
| 83 | the lock to acquire the lock. The lock must be in the locked state,\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 84 | but it needn't be locked by the same thread that unlocks it."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 85 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 86 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 87 | lock_locked_lock(lockobject *self) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 88 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 89 | if (PyThread_acquire_lock(self->lock_lock, 0)) { |
| 90 | PyThread_release_lock(self->lock_lock); |
Guido van Rossum | 8fdc75b | 2002-04-07 06:32:21 +0000 | [diff] [blame] | 91 | return PyBool_FromLong(0L); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 92 | } |
Guido van Rossum | 8fdc75b | 2002-04-07 06:32:21 +0000 | [diff] [blame] | 93 | return PyBool_FromLong(1L); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 96 | PyDoc_STRVAR(locked_doc, |
Guido van Rossum | 8fdc75b | 2002-04-07 06:32:21 +0000 | [diff] [blame] | 97 | "locked() -> bool\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 98 | (locked_lock() is an obsolete synonym)\n\ |
| 99 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 100 | Return whether the lock is in the locked state."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 101 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 102 | static PyMethodDef lock_methods[] = { |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 103 | {"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock, |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 104 | METH_VARARGS, acquire_doc}, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 105 | {"acquire", (PyCFunction)lock_PyThread_acquire_lock, |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 106 | METH_VARARGS, acquire_doc}, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 107 | {"release_lock", (PyCFunction)lock_PyThread_release_lock, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 108 | METH_NOARGS, release_doc}, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 109 | {"release", (PyCFunction)lock_PyThread_release_lock, |
Neal Norwitz | 2358425 | 2002-03-25 21:05:50 +0000 | [diff] [blame] | 110 | METH_NOARGS, release_doc}, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 111 | {"locked_lock", (PyCFunction)lock_locked_lock, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 112 | METH_NOARGS, locked_doc}, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 113 | {"locked", (PyCFunction)lock_locked_lock, |
Neal Norwitz | 2358425 | 2002-03-25 21:05:50 +0000 | [diff] [blame] | 114 | METH_NOARGS, locked_doc}, |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 115 | {"__enter__", (PyCFunction)lock_PyThread_acquire_lock, |
| 116 | METH_VARARGS, acquire_doc}, |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 117 | {"__exit__", (PyCFunction)lock_PyThread_release_lock, |
| 118 | METH_VARARGS, release_doc}, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 119 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 122 | static PyTypeObject Locktype = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 123 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 124 | "_thread.lock", /*tp_name*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 125 | sizeof(lockobject), /*tp_size*/ |
| 126 | 0, /*tp_itemsize*/ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 127 | /* methods */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 128 | (destructor)lock_dealloc, /*tp_dealloc*/ |
| 129 | 0, /*tp_print*/ |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 130 | 0, /*tp_getattr*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 131 | 0, /*tp_setattr*/ |
| 132 | 0, /*tp_compare*/ |
| 133 | 0, /*tp_repr*/ |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 134 | 0, /*tp_as_number*/ |
| 135 | 0, /*tp_as_sequence*/ |
| 136 | 0, /*tp_as_mapping*/ |
| 137 | 0, /*tp_hash*/ |
| 138 | 0, /*tp_call*/ |
| 139 | 0, /*tp_str*/ |
| 140 | 0, /*tp_getattro*/ |
| 141 | 0, /*tp_setattro*/ |
| 142 | 0, /*tp_as_buffer*/ |
| 143 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 144 | 0, /*tp_doc*/ |
| 145 | 0, /*tp_traverse*/ |
| 146 | 0, /*tp_clear*/ |
| 147 | 0, /*tp_richcompare*/ |
| 148 | 0, /*tp_weaklistoffset*/ |
| 149 | 0, /*tp_iter*/ |
| 150 | 0, /*tp_iternext*/ |
| 151 | lock_methods, /*tp_methods*/ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 154 | static lockobject * |
| 155 | newlockobject(void) |
| 156 | { |
| 157 | lockobject *self; |
| 158 | self = PyObject_New(lockobject, &Locktype); |
| 159 | if (self == NULL) |
| 160 | return NULL; |
| 161 | self->lock_lock = PyThread_allocate_lock(); |
| 162 | if (self->lock_lock == NULL) { |
| 163 | PyObject_Del(self); |
| 164 | self = NULL; |
| 165 | PyErr_SetString(ThreadError, "can't allocate lock"); |
| 166 | } |
| 167 | return self; |
| 168 | } |
| 169 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 170 | /* Thread-local objects */ |
| 171 | |
| 172 | #include "structmember.h" |
| 173 | |
| 174 | typedef struct { |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 175 | PyObject_HEAD |
| 176 | PyObject *key; |
| 177 | PyObject *args; |
| 178 | PyObject *kw; |
| 179 | PyObject *dict; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 180 | } localobject; |
| 181 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 182 | static PyObject * |
| 183 | local_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 184 | { |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 185 | localobject *self; |
| 186 | PyObject *tdict; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 187 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 188 | if (type->tp_init == PyBaseObject_Type.tp_init |
| 189 | && ((args && PyObject_IsTrue(args)) |
| 190 | || (kw && PyObject_IsTrue(kw)))) { |
| 191 | PyErr_SetString(PyExc_TypeError, |
| 192 | "Initialization arguments are not supported"); |
| 193 | return NULL; |
| 194 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 195 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 196 | self = (localobject *)type->tp_alloc(type, 0); |
| 197 | if (self == NULL) |
| 198 | return NULL; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 199 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 200 | Py_XINCREF(args); |
| 201 | self->args = args; |
| 202 | Py_XINCREF(kw); |
| 203 | self->kw = kw; |
| 204 | self->dict = NULL; /* making sure */ |
Walter Dörwald | 4ee631e | 2007-06-20 14:55:01 +0000 | [diff] [blame] | 205 | self->key = PyUnicode_FromFormat("thread.local.%p", self); |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 206 | if (self->key == NULL) |
| 207 | goto err; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 208 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 209 | self->dict = PyDict_New(); |
| 210 | if (self->dict == NULL) |
| 211 | goto err; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 212 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 213 | tdict = PyThreadState_GetDict(); |
| 214 | if (tdict == NULL) { |
| 215 | PyErr_SetString(PyExc_SystemError, |
| 216 | "Couldn't get thread-state dictionary"); |
| 217 | goto err; |
| 218 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 219 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 220 | if (PyDict_SetItem(tdict, self->key, self->dict) < 0) |
| 221 | goto err; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 222 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 223 | return (PyObject *)self; |
| 224 | |
| 225 | err: |
| 226 | Py_DECREF(self); |
| 227 | return NULL; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static int |
| 231 | local_traverse(localobject *self, visitproc visit, void *arg) |
| 232 | { |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 233 | Py_VISIT(self->args); |
| 234 | Py_VISIT(self->kw); |
| 235 | Py_VISIT(self->dict); |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static int |
| 240 | local_clear(localobject *self) |
| 241 | { |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 242 | Py_CLEAR(self->key); |
| 243 | Py_CLEAR(self->args); |
| 244 | Py_CLEAR(self->kw); |
| 245 | Py_CLEAR(self->dict); |
| 246 | return 0; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static void |
| 250 | local_dealloc(localobject *self) |
| 251 | { |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 252 | PyThreadState *tstate; |
| 253 | if (self->key |
| 254 | && (tstate = PyThreadState_Get()) |
| 255 | && tstate->interp) { |
| 256 | for(tstate = PyInterpreterState_ThreadHead(tstate->interp); |
| 257 | tstate; |
| 258 | tstate = PyThreadState_Next(tstate)) |
| 259 | if (tstate->dict && |
| 260 | PyDict_GetItem(tstate->dict, self->key)) |
| 261 | PyDict_DelItem(tstate->dict, self->key); |
| 262 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 263 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 264 | local_clear(self); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 265 | Py_TYPE(self)->tp_free((PyObject*)self); |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static PyObject * |
| 269 | _ldict(localobject *self) |
| 270 | { |
| 271 | PyObject *tdict, *ldict; |
| 272 | |
| 273 | tdict = PyThreadState_GetDict(); |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 274 | if (tdict == NULL) { |
| 275 | PyErr_SetString(PyExc_SystemError, |
| 276 | "Couldn't get thread-state dictionary"); |
| 277 | return NULL; |
| 278 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 279 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 280 | ldict = PyDict_GetItem(tdict, self->key); |
| 281 | if (ldict == NULL) { |
| 282 | ldict = PyDict_New(); /* we own ldict */ |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 283 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 284 | if (ldict == NULL) |
| 285 | return NULL; |
| 286 | else { |
| 287 | int i = PyDict_SetItem(tdict, self->key, ldict); |
Georg Brandl | f3c4ad1 | 2006-03-08 12:24:33 +0000 | [diff] [blame] | 288 | Py_DECREF(ldict); /* now ldict is borrowed */ |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 289 | if (i < 0) |
| 290 | return NULL; |
| 291 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 292 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 293 | Py_CLEAR(self->dict); |
| 294 | Py_INCREF(ldict); |
| 295 | self->dict = ldict; /* still borrowed */ |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 296 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 297 | if (Py_TYPE(self)->tp_init != PyBaseObject_Type.tp_init && |
| 298 | Py_TYPE(self)->tp_init((PyObject*)self, |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 299 | self->args, self->kw) < 0) { |
| 300 | /* we need to get rid of ldict from thread so |
| 301 | we create a new one the next time we do an attr |
| 302 | acces */ |
| 303 | PyDict_DelItem(tdict, self->key); |
| 304 | return NULL; |
| 305 | } |
| 306 | |
| 307 | } |
Benjamin Peterson | 8a250ae | 2008-06-30 23:30:24 +0000 | [diff] [blame] | 308 | |
| 309 | /* The call to tp_init above may have caused another thread to run. |
| 310 | Install our ldict again. */ |
| 311 | if (self->dict != ldict) { |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 312 | Py_CLEAR(self->dict); |
| 313 | Py_INCREF(ldict); |
| 314 | self->dict = ldict; |
| 315 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 316 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 317 | return ldict; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 320 | static int |
| 321 | local_setattro(localobject *self, PyObject *name, PyObject *v) |
| 322 | { |
| 323 | PyObject *ldict; |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 324 | |
| 325 | ldict = _ldict(self); |
| 326 | if (ldict == NULL) |
| 327 | return -1; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 328 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 329 | return PyObject_GenericSetAttr((PyObject *)self, name, v); |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | static PyObject * |
| 333 | local_getdict(localobject *self, void *closure) |
| 334 | { |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 335 | if (self->dict == NULL) { |
| 336 | PyErr_SetString(PyExc_AttributeError, "__dict__"); |
| 337 | return NULL; |
| 338 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 339 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 340 | Py_INCREF(self->dict); |
| 341 | return self->dict; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static PyGetSetDef local_getset[] = { |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 345 | {"__dict__", (getter)local_getdict, (setter)NULL, |
| 346 | "Local-data dictionary", NULL}, |
| 347 | {NULL} /* Sentinel */ |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 348 | }; |
| 349 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 350 | static PyObject *local_getattro(localobject *, PyObject *); |
| 351 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 352 | static PyTypeObject localtype = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 353 | PyVarObject_HEAD_INIT(NULL, 0) |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 354 | /* tp_name */ "_thread._local", |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 355 | /* tp_basicsize */ sizeof(localobject), |
| 356 | /* tp_itemsize */ 0, |
| 357 | /* tp_dealloc */ (destructor)local_dealloc, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 358 | /* tp_print */ 0, |
| 359 | /* tp_getattr */ 0, |
| 360 | /* tp_setattr */ 0, |
| 361 | /* tp_compare */ 0, |
| 362 | /* tp_repr */ 0, |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 363 | /* tp_as_number */ 0, |
| 364 | /* tp_as_sequence */ 0, |
| 365 | /* tp_as_mapping */ 0, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 366 | /* tp_hash */ 0, |
| 367 | /* tp_call */ 0, |
| 368 | /* tp_str */ 0, |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 369 | /* tp_getattro */ (getattrofunc)local_getattro, |
| 370 | /* tp_setattro */ (setattrofunc)local_setattro, |
| 371 | /* tp_as_buffer */ 0, |
| 372 | /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 373 | /* tp_doc */ "Thread-local data", |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 374 | /* tp_traverse */ (traverseproc)local_traverse, |
| 375 | /* tp_clear */ (inquiry)local_clear, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 376 | /* tp_richcompare */ 0, |
| 377 | /* tp_weaklistoffset */ 0, |
| 378 | /* tp_iter */ 0, |
| 379 | /* tp_iternext */ 0, |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 380 | /* tp_methods */ 0, |
| 381 | /* tp_members */ 0, |
| 382 | /* tp_getset */ local_getset, |
| 383 | /* tp_base */ 0, |
| 384 | /* tp_dict */ 0, /* internal use */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 385 | /* tp_descr_get */ 0, |
| 386 | /* tp_descr_set */ 0, |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 387 | /* tp_dictoffset */ offsetof(localobject, dict), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 388 | /* tp_init */ 0, |
| 389 | /* tp_alloc */ 0, |
| 390 | /* tp_new */ local_new, |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 391 | /* tp_free */ 0, /* Low-level free-mem routine */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 392 | /* tp_is_gc */ 0, /* For PyObject_IS_GC */ |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 393 | }; |
| 394 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 395 | static PyObject * |
| 396 | local_getattro(localobject *self, PyObject *name) |
| 397 | { |
| 398 | PyObject *ldict, *value; |
| 399 | |
| 400 | ldict = _ldict(self); |
| 401 | if (ldict == NULL) |
| 402 | return NULL; |
| 403 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 404 | if (Py_TYPE(self) != &localtype) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 405 | /* use generic lookup for subtypes */ |
| 406 | return PyObject_GenericGetAttr((PyObject *)self, name); |
| 407 | |
| 408 | /* Optimization: just look in dict ourselves */ |
| 409 | value = PyDict_GetItem(ldict, name); |
| 410 | if (value == NULL) |
| 411 | /* Fall back on generic to get __class__ and __dict__ */ |
| 412 | return PyObject_GenericGetAttr((PyObject *)self, name); |
| 413 | |
| 414 | Py_INCREF(value); |
| 415 | return value; |
| 416 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 417 | |
| 418 | /* Module functions */ |
| 419 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 420 | struct bootstate { |
| 421 | PyInterpreterState *interp; |
| 422 | PyObject *func; |
| 423 | PyObject *args; |
| 424 | PyObject *keyw; |
| 425 | }; |
| 426 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 427 | static void |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 428 | t_bootstrap(void *boot_raw) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 429 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 430 | struct bootstate *boot = (struct bootstate *) boot_raw; |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 431 | PyThreadState *tstate; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 432 | PyObject *res; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 433 | |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 434 | tstate = PyThreadState_New(boot->interp); |
| 435 | |
| 436 | PyEval_AcquireThread(tstate); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 437 | res = PyEval_CallObjectWithKeywords( |
| 438 | boot->func, boot->args, boot->keyw); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 439 | if (res == NULL) { |
Fred Drake | bebc97f | 1998-05-28 04:35:12 +0000 | [diff] [blame] | 440 | if (PyErr_ExceptionMatches(PyExc_SystemExit)) |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 441 | PyErr_Clear(); |
Guido van Rossum | 385e7c6 | 1995-03-17 10:42:27 +0000 | [diff] [blame] | 442 | else { |
Guido van Rossum | 24ccca1 | 2003-04-29 19:44:05 +0000 | [diff] [blame] | 443 | PyObject *file; |
| 444 | PySys_WriteStderr( |
| 445 | "Unhandled exception in thread started by "); |
| 446 | file = PySys_GetObject("stderr"); |
Christian Heimes | 2be0373 | 2007-11-15 02:26:46 +0000 | [diff] [blame] | 447 | if (file != NULL && file != Py_None) |
Guido van Rossum | 24ccca1 | 2003-04-29 19:44:05 +0000 | [diff] [blame] | 448 | PyFile_WriteObject(boot->func, file, 0); |
| 449 | else |
| 450 | PyObject_Print(boot->func, stderr, 0); |
| 451 | PySys_WriteStderr("\n"); |
Guido van Rossum | 40769dd | 1998-02-06 22:32:08 +0000 | [diff] [blame] | 452 | PyErr_PrintEx(0); |
Guido van Rossum | 385e7c6 | 1995-03-17 10:42:27 +0000 | [diff] [blame] | 453 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 454 | } |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 455 | else |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 456 | Py_DECREF(res); |
Guido van Rossum | 24ccca1 | 2003-04-29 19:44:05 +0000 | [diff] [blame] | 457 | Py_DECREF(boot->func); |
| 458 | Py_DECREF(boot->args); |
| 459 | Py_XDECREF(boot->keyw); |
| 460 | PyMem_DEL(boot_raw); |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 461 | PyThreadState_Clear(tstate); |
| 462 | PyThreadState_DeleteCurrent(); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 463 | PyThread_exit_thread(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 466 | static PyObject * |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 467 | thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 468 | { |
Guido van Rossum | 38d45b7 | 2000-09-01 20:47:58 +0000 | [diff] [blame] | 469 | PyObject *func, *args, *keyw = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 470 | struct bootstate *boot; |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 471 | long ident; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 472 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 473 | if (!PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3, |
| 474 | &func, &args, &keyw)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 475 | return NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 476 | if (!PyCallable_Check(func)) { |
| 477 | PyErr_SetString(PyExc_TypeError, |
| 478 | "first arg must be callable"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 479 | return NULL; |
| 480 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 481 | if (!PyTuple_Check(args)) { |
| 482 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 38d45b7 | 2000-09-01 20:47:58 +0000 | [diff] [blame] | 483 | "2nd arg must be a tuple"); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 484 | return NULL; |
| 485 | } |
| 486 | if (keyw != NULL && !PyDict_Check(keyw)) { |
| 487 | PyErr_SetString(PyExc_TypeError, |
| 488 | "optional 3rd arg must be a dictionary"); |
| 489 | return NULL; |
| 490 | } |
| 491 | boot = PyMem_NEW(struct bootstate, 1); |
| 492 | if (boot == NULL) |
| 493 | return PyErr_NoMemory(); |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 494 | boot->interp = PyThreadState_GET()->interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 495 | boot->func = func; |
| 496 | boot->args = args; |
| 497 | boot->keyw = keyw; |
| 498 | Py_INCREF(func); |
| 499 | Py_INCREF(args); |
| 500 | Py_XINCREF(keyw); |
| 501 | PyEval_InitThreads(); /* Start the interpreter's thread-awareness */ |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 502 | ident = PyThread_start_new_thread(t_bootstrap, (void*) boot); |
| 503 | if (ident == -1) { |
Guido van Rossum | 54c273c | 2005-02-20 03:02:16 +0000 | [diff] [blame] | 504 | PyErr_SetString(ThreadError, "can't start new thread"); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 505 | Py_DECREF(func); |
| 506 | Py_DECREF(args); |
| 507 | Py_XDECREF(keyw); |
| 508 | PyMem_DEL(boot); |
| 509 | return NULL; |
| 510 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 511 | return PyLong_FromLong(ident); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 514 | PyDoc_STRVAR(start_new_doc, |
Andrew M. Kuchling | 38300c6 | 2001-10-05 12:24:15 +0000 | [diff] [blame] | 515 | "start_new_thread(function, args[, kwargs])\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 516 | (start_new() is an obsolete synonym)\n\ |
| 517 | \n\ |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 518 | Start a new thread and return its identifier. The thread will call the\n\ |
| 519 | function with positional arguments from the tuple args and keyword arguments\n\ |
| 520 | taken from the optional dictionary kwargs. The thread exits when the\n\ |
| 521 | function returns; the return value is ignored. The thread will also exit\n\ |
| 522 | when the function raises an unhandled exception; a stack trace will be\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 523 | printed unless the exception is SystemExit.\n"); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 524 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 525 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 526 | thread_PyThread_exit_thread(PyObject *self) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 527 | { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 528 | PyErr_SetNone(PyExc_SystemExit); |
Guido van Rossum | 385e7c6 | 1995-03-17 10:42:27 +0000 | [diff] [blame] | 529 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 532 | PyDoc_STRVAR(exit_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 533 | "exit()\n\ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 534 | (PyThread_exit_thread() is an obsolete synonym)\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 535 | \n\ |
| 536 | This is synonymous to ``raise SystemExit''. It will cause the current\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 537 | thread to exit silently unless the exception is caught."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 538 | |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 539 | static PyObject * |
Kurt B. Kaiser | a1ad5f6 | 2003-06-16 18:51:28 +0000 | [diff] [blame] | 540 | thread_PyThread_interrupt_main(PyObject * self) |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 541 | { |
| 542 | PyErr_SetInterrupt(); |
| 543 | Py_INCREF(Py_None); |
| 544 | return Py_None; |
| 545 | } |
| 546 | |
| 547 | PyDoc_STRVAR(interrupt_doc, |
| 548 | "interrupt_main()\n\ |
| 549 | \n\ |
| 550 | Raise a KeyboardInterrupt in the main thread.\n\ |
Kurt B. Kaiser | a1ad5f6 | 2003-06-16 18:51:28 +0000 | [diff] [blame] | 551 | A subthread can use this function to interrupt the main thread." |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 552 | ); |
| 553 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 554 | #ifndef NO_EXIT_PROG |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 555 | static PyObject * |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 556 | thread_PyThread_exit_prog(PyObject *self, PyObject *args) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 557 | { |
| 558 | int sts; |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 559 | if (!PyArg_ParseTuple(args, "i:exit_prog", &sts)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 560 | return NULL; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 561 | 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] | 562 | for (;;) { } /* Should not be reached */ |
| 563 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 564 | #endif |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 565 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 566 | static lockobject *newlockobject(void); |
| 567 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 568 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 569 | thread_PyThread_allocate_lock(PyObject *self) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 570 | { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 571 | return (PyObject *) newlockobject(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 574 | PyDoc_STRVAR(allocate_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 575 | "allocate_lock() -> lock object\n\ |
| 576 | (allocate() is an obsolete synonym)\n\ |
| 577 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 578 | Create a new lock object. See LockType.__doc__ for information about locks."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 579 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 580 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 581 | thread_get_ident(PyObject *self) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 582 | { |
| 583 | long ident; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 584 | ident = PyThread_get_thread_ident(); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 585 | if (ident == -1) { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 586 | PyErr_SetString(ThreadError, "no current thread ident"); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 587 | return NULL; |
| 588 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 589 | return PyLong_FromLong(ident); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 592 | PyDoc_STRVAR(get_ident_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 593 | "get_ident() -> integer\n\ |
| 594 | \n\ |
| 595 | Return a non-zero integer that uniquely identifies the current thread\n\ |
| 596 | amongst other threads that exist simultaneously.\n\ |
| 597 | This may be used to identify per-thread resources.\n\ |
| 598 | Even though on some platforms threads identities may appear to be\n\ |
| 599 | allocated consecutive numbers starting at 1, this behavior should not\n\ |
| 600 | be relied upon, and the number should be seen purely as a magic cookie.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 601 | A thread's identity may be reused for another thread after it exits."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 602 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 603 | static PyObject * |
| 604 | thread_stack_size(PyObject *self, PyObject *args) |
| 605 | { |
| 606 | size_t old_size; |
| 607 | Py_ssize_t new_size = 0; |
| 608 | int rc; |
| 609 | |
| 610 | if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size)) |
| 611 | return NULL; |
| 612 | |
| 613 | if (new_size < 0) { |
| 614 | PyErr_SetString(PyExc_ValueError, |
| 615 | "size must be 0 or a positive value"); |
| 616 | return NULL; |
| 617 | } |
| 618 | |
| 619 | old_size = PyThread_get_stacksize(); |
| 620 | |
| 621 | rc = PyThread_set_stacksize((size_t) new_size); |
| 622 | if (rc == -1) { |
| 623 | PyErr_Format(PyExc_ValueError, |
| 624 | "size not valid: %zd bytes", |
| 625 | new_size); |
| 626 | return NULL; |
| 627 | } |
| 628 | if (rc == -2) { |
| 629 | PyErr_SetString(ThreadError, |
| 630 | "setting stack size not supported"); |
| 631 | return NULL; |
| 632 | } |
| 633 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 634 | return PyLong_FromSsize_t((Py_ssize_t) old_size); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | PyDoc_STRVAR(stack_size_doc, |
| 638 | "stack_size([size]) -> size\n\ |
| 639 | \n\ |
| 640 | Return the thread stack size used when creating new threads. The\n\ |
| 641 | optional size argument specifies the stack size (in bytes) to be used\n\ |
| 642 | for subsequently created threads, and must be 0 (use platform or\n\ |
| 643 | configured default) or a positive integer value of at least 32,768 (32k).\n\ |
| 644 | If changing the thread stack size is unsupported, a ThreadError\n\ |
| 645 | exception is raised. If the specified size is invalid, a ValueError\n\ |
| 646 | exception is raised, and the stack size is unmodified. 32k bytes\n\ |
| 647 | currently the minimum supported stack size value to guarantee\n\ |
| 648 | sufficient stack space for the interpreter itself.\n\ |
| 649 | \n\ |
| 650 | Note that some platforms may have particular restrictions on values for\n\ |
| 651 | the stack size, such as requiring a minimum stack size larger than 32kB or\n\ |
| 652 | requiring allocation in multiples of the system memory page size\n\ |
| 653 | - platform documentation should be referred to for more information\n\ |
| 654 | (4kB pages are common; using multiples of 4096 for the stack size is\n\ |
| 655 | the suggested approach in the absence of more specific information)."); |
| 656 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 657 | static PyMethodDef thread_methods[] = { |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 658 | {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread, |
| 659 | METH_VARARGS, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 660 | start_new_doc}, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 661 | {"start_new", (PyCFunction)thread_PyThread_start_new_thread, |
| 662 | METH_VARARGS, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 663 | start_new_doc}, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 664 | {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 665 | METH_NOARGS, allocate_doc}, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 666 | {"allocate", (PyCFunction)thread_PyThread_allocate_lock, |
Neal Norwitz | 4632117 | 2002-03-26 14:52:00 +0000 | [diff] [blame] | 667 | METH_NOARGS, allocate_doc}, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 668 | {"exit_thread", (PyCFunction)thread_PyThread_exit_thread, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 669 | METH_NOARGS, exit_doc}, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 670 | {"exit", (PyCFunction)thread_PyThread_exit_thread, |
Neal Norwitz | 4632117 | 2002-03-26 14:52:00 +0000 | [diff] [blame] | 671 | METH_NOARGS, exit_doc}, |
Kurt B. Kaiser | a1ad5f6 | 2003-06-16 18:51:28 +0000 | [diff] [blame] | 672 | {"interrupt_main", (PyCFunction)thread_PyThread_interrupt_main, |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 673 | METH_NOARGS, interrupt_doc}, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 674 | {"get_ident", (PyCFunction)thread_get_ident, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 675 | METH_NOARGS, get_ident_doc}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 676 | {"stack_size", (PyCFunction)thread_stack_size, |
| 677 | METH_VARARGS, |
| 678 | stack_size_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 679 | #ifndef NO_EXIT_PROG |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 680 | {"exit_prog", (PyCFunction)thread_PyThread_exit_prog, |
| 681 | METH_VARARGS}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 682 | #endif |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 683 | {NULL, NULL} /* sentinel */ |
| 684 | }; |
| 685 | |
| 686 | |
| 687 | /* Initialization function */ |
| 688 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 689 | PyDoc_STRVAR(thread_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 690 | "This module provides primitive operations to write multi-threaded programs.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 691 | The 'threading' module provides a more convenient interface."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 692 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 693 | PyDoc_STRVAR(lock_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 694 | "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] | 695 | call the PyThread_allocate_lock() function. Methods are:\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 696 | \n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 697 | acquire() -- lock the lock, possibly blocking until it can be obtained\n\ |
| 698 | release() -- unlock of the lock\n\ |
| 699 | locked() -- test whether the lock is currently locked\n\ |
| 700 | \n\ |
| 701 | A lock is not owned by the thread that locked it; another thread may\n\ |
| 702 | unlock it. A thread attempting to lock a lock that it has already locked\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 703 | will block until another thread unlocks it. Deadlocks may ensue."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 704 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 705 | static struct PyModuleDef threadmodule = { |
| 706 | PyModuleDef_HEAD_INIT, |
| 707 | "_thread", |
| 708 | thread_doc, |
| 709 | -1, |
| 710 | thread_methods, |
| 711 | NULL, |
| 712 | NULL, |
| 713 | NULL, |
| 714 | NULL |
| 715 | }; |
| 716 | |
| 717 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 718 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 719 | PyInit__thread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 720 | { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 721 | PyObject *m, *d; |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 722 | |
| 723 | /* Initialize types: */ |
| 724 | if (PyType_Ready(&localtype) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 725 | return NULL; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 726 | if (PyType_Ready(&Locktype) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 727 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 728 | |
| 729 | /* Create the module and add the functions */ |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 730 | m = PyModule_Create(&threadmodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 731 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 732 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 733 | |
| 734 | /* Add a symbolic constant */ |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 735 | d = PyModule_GetDict(m); |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 736 | ThreadError = PyErr_NewException("_thread.error", NULL, NULL); |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 737 | PyDict_SetItemString(d, "error", ThreadError); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 738 | Locktype.tp_doc = lock_doc; |
| 739 | Py_INCREF(&Locktype); |
| 740 | PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 741 | |
Michael W. Hudson | 64e0814 | 2005-06-15 12:25:20 +0000 | [diff] [blame] | 742 | Py_INCREF(&localtype); |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 743 | if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 744 | return NULL; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 745 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 746 | /* Initialize the C thread library */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 747 | PyThread_init_thread(); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 748 | return m; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 749 | } |