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 { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 21 | PyObject_HEAD |
| 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 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 28 | if (self->lock_lock != NULL) { |
| 29 | /* Unlock the lock so it's safe to free it */ |
| 30 | PyThread_acquire_lock(self->lock_lock, 0); |
| 31 | PyThread_release_lock(self->lock_lock); |
| 32 | |
| 33 | PyThread_free_lock(self->lock_lock); |
| 34 | } |
| 35 | PyObject_Del(self); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 38 | static PyObject * |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 39 | lock_PyThread_acquire_lock(lockobject *self, PyObject *args) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 40 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 41 | int i = 1; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 42 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 43 | if (!PyArg_ParseTuple(args, "|i:acquire", &i)) |
| 44 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 45 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 46 | Py_BEGIN_ALLOW_THREADS |
| 47 | i = PyThread_acquire_lock(self->lock_lock, i); |
| 48 | Py_END_ALLOW_THREADS |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 49 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 50 | return PyBool_FromLong((long)i); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 53 | PyDoc_STRVAR(acquire_doc, |
Guido van Rossum | 8fdc75b | 2002-04-07 06:32:21 +0000 | [diff] [blame] | 54 | "acquire([wait]) -> None or bool\n\ |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 55 | (acquire_lock() is an obsolete synonym)\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 56 | \n\ |
| 57 | Lock the lock. Without argument, this blocks if the lock is already\n\ |
| 58 | 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] | 59 | the lock, and return None once the lock is acquired.\n\ |
| 60 | 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] | 61 | 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] | 62 | The blocking operation is not interruptible."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 63 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 64 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 65 | lock_PyThread_release_lock(lockobject *self) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 66 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 67 | /* Sanity check: the lock must be locked */ |
| 68 | if (PyThread_acquire_lock(self->lock_lock, 0)) { |
| 69 | PyThread_release_lock(self->lock_lock); |
| 70 | PyErr_SetString(ThreadError, "release unlocked lock"); |
| 71 | return NULL; |
| 72 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 73 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 74 | PyThread_release_lock(self->lock_lock); |
| 75 | Py_INCREF(Py_None); |
| 76 | return Py_None; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 79 | PyDoc_STRVAR(release_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 80 | "release()\n\ |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 81 | (release_lock() is an obsolete synonym)\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 82 | \n\ |
| 83 | Release the lock, allowing another thread that is blocked waiting for\n\ |
| 84 | 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] | 85 | 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] | 86 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 87 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 88 | lock_locked_lock(lockobject *self) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 89 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 90 | if (PyThread_acquire_lock(self->lock_lock, 0)) { |
| 91 | PyThread_release_lock(self->lock_lock); |
| 92 | return PyBool_FromLong(0L); |
| 93 | } |
| 94 | return PyBool_FromLong(1L); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 97 | PyDoc_STRVAR(locked_doc, |
Guido van Rossum | 8fdc75b | 2002-04-07 06:32:21 +0000 | [diff] [blame] | 98 | "locked() -> bool\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 99 | (locked_lock() is an obsolete synonym)\n\ |
| 100 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 101 | Return whether the lock is in the locked state."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 102 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 103 | static PyMethodDef lock_methods[] = { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 104 | {"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock, |
| 105 | METH_VARARGS, acquire_doc}, |
| 106 | {"acquire", (PyCFunction)lock_PyThread_acquire_lock, |
| 107 | METH_VARARGS, acquire_doc}, |
| 108 | {"release_lock", (PyCFunction)lock_PyThread_release_lock, |
| 109 | METH_NOARGS, release_doc}, |
| 110 | {"release", (PyCFunction)lock_PyThread_release_lock, |
| 111 | METH_NOARGS, release_doc}, |
| 112 | {"locked_lock", (PyCFunction)lock_locked_lock, |
| 113 | METH_NOARGS, locked_doc}, |
| 114 | {"locked", (PyCFunction)lock_locked_lock, |
| 115 | METH_NOARGS, locked_doc}, |
| 116 | {"__enter__", (PyCFunction)lock_PyThread_acquire_lock, |
| 117 | METH_VARARGS, acquire_doc}, |
| 118 | {"__exit__", (PyCFunction)lock_PyThread_release_lock, |
| 119 | METH_VARARGS, release_doc}, |
| 120 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 123 | static PyTypeObject Locktype = { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 124 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 125 | "_thread.lock", /*tp_name*/ |
| 126 | sizeof(lockobject), /*tp_size*/ |
| 127 | 0, /*tp_itemsize*/ |
| 128 | /* methods */ |
| 129 | (destructor)lock_dealloc, /*tp_dealloc*/ |
| 130 | 0, /*tp_print*/ |
| 131 | 0, /*tp_getattr*/ |
| 132 | 0, /*tp_setattr*/ |
| 133 | 0, /*tp_reserved*/ |
| 134 | 0, /*tp_repr*/ |
| 135 | 0, /*tp_as_number*/ |
| 136 | 0, /*tp_as_sequence*/ |
| 137 | 0, /*tp_as_mapping*/ |
| 138 | 0, /*tp_hash*/ |
| 139 | 0, /*tp_call*/ |
| 140 | 0, /*tp_str*/ |
| 141 | 0, /*tp_getattro*/ |
| 142 | 0, /*tp_setattro*/ |
| 143 | 0, /*tp_as_buffer*/ |
| 144 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 145 | 0, /*tp_doc*/ |
| 146 | 0, /*tp_traverse*/ |
| 147 | 0, /*tp_clear*/ |
| 148 | 0, /*tp_richcompare*/ |
| 149 | 0, /*tp_weaklistoffset*/ |
| 150 | 0, /*tp_iter*/ |
| 151 | 0, /*tp_iternext*/ |
| 152 | lock_methods, /*tp_methods*/ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 155 | static lockobject * |
| 156 | newlockobject(void) |
| 157 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 158 | lockobject *self; |
| 159 | self = PyObject_New(lockobject, &Locktype); |
| 160 | if (self == NULL) |
| 161 | return NULL; |
| 162 | self->lock_lock = PyThread_allocate_lock(); |
| 163 | if (self->lock_lock == NULL) { |
| 164 | Py_DECREF(self); |
| 165 | PyErr_SetString(ThreadError, "can't allocate lock"); |
| 166 | return NULL; |
| 167 | } |
| 168 | return self; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 171 | /* Thread-local objects */ |
| 172 | |
| 173 | #include "structmember.h" |
| 174 | |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 175 | /* Quick overview: |
| 176 | |
| 177 | We need to be able to reclaim reference cycles as soon as possible |
| 178 | (both when a thread is being terminated, or a thread-local object |
| 179 | becomes unreachable from user data). Constraints: |
| 180 | - it must not be possible for thread-state dicts to be involved in |
| 181 | reference cycles (otherwise the cyclic GC will refuse to consider |
| 182 | objects referenced from a reachable thread-state dict, even though |
| 183 | local_dealloc would clear them) |
| 184 | - the death of a thread-state dict must still imply destruction of the |
| 185 | corresponding local dicts in all thread-local objects. |
| 186 | |
| 187 | Our implementation uses small "localdummy" objects in order to break |
| 188 | the reference chain. These trivial objects are hashable (using the |
| 189 | default scheme of identity hashing) and weakrefable. |
| 190 | Each thread-state holds a separate localdummy for each local object |
| 191 | (as a /strong reference/), |
| 192 | and each thread-local object holds a dict mapping /weak references/ |
| 193 | of localdummies to local dicts. |
| 194 | |
| 195 | Therefore: |
| 196 | - only the thread-state dict holds a strong reference to the dummies |
| 197 | - only the thread-local object holds a strong reference to the local dicts |
| 198 | - only outside objects (application- or library-level) hold strong |
| 199 | references to the thread-local objects |
| 200 | - as soon as a thread-state dict is destroyed, the weakref callbacks of all |
| 201 | dummies attached to that thread are called, and destroy the corresponding |
| 202 | local dicts from thread-local objects |
| 203 | - as soon as a thread-local object is destroyed, its local dicts are |
| 204 | destroyed and its dummies are manually removed from all thread states |
| 205 | - the GC can do its work correctly when a thread-local object is dangling, |
| 206 | without any interference from the thread-state dicts |
| 207 | |
| 208 | As an additional optimization, each localdummy holds a borrowed reference |
| 209 | to the corresponding localdict. This borrowed reference is only used |
| 210 | by the thread-local object which has created the localdummy, which should |
| 211 | guarantee that the localdict still exists when accessed. |
| 212 | */ |
| 213 | |
| 214 | typedef struct { |
| 215 | PyObject_HEAD |
| 216 | PyObject *localdict; /* Borrowed reference! */ |
| 217 | PyObject *weakreflist; /* List of weak references to self */ |
| 218 | } localdummyobject; |
| 219 | |
| 220 | static void |
| 221 | localdummy_dealloc(localdummyobject *self) |
| 222 | { |
| 223 | if (self->weakreflist != NULL) |
| 224 | PyObject_ClearWeakRefs((PyObject *) self); |
| 225 | Py_TYPE(self)->tp_free((PyObject*)self); |
| 226 | } |
| 227 | |
| 228 | static PyTypeObject localdummytype = { |
| 229 | PyVarObject_HEAD_INIT(NULL, 0) |
| 230 | /* tp_name */ "_thread._localdummy", |
| 231 | /* tp_basicsize */ sizeof(localdummyobject), |
| 232 | /* tp_itemsize */ 0, |
| 233 | /* tp_dealloc */ (destructor)localdummy_dealloc, |
| 234 | /* tp_print */ 0, |
| 235 | /* tp_getattr */ 0, |
| 236 | /* tp_setattr */ 0, |
| 237 | /* tp_reserved */ 0, |
| 238 | /* tp_repr */ 0, |
| 239 | /* tp_as_number */ 0, |
| 240 | /* tp_as_sequence */ 0, |
| 241 | /* tp_as_mapping */ 0, |
| 242 | /* tp_hash */ 0, |
| 243 | /* tp_call */ 0, |
| 244 | /* tp_str */ 0, |
| 245 | /* tp_getattro */ 0, |
| 246 | /* tp_setattro */ 0, |
| 247 | /* tp_as_buffer */ 0, |
| 248 | /* tp_flags */ Py_TPFLAGS_DEFAULT, |
| 249 | /* tp_doc */ "Thread-local dummy", |
| 250 | /* tp_traverse */ 0, |
| 251 | /* tp_clear */ 0, |
| 252 | /* tp_richcompare */ 0, |
| 253 | /* tp_weaklistoffset */ offsetof(localdummyobject, weakreflist) |
| 254 | }; |
| 255 | |
| 256 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 257 | typedef struct { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 258 | PyObject_HEAD |
| 259 | PyObject *key; |
| 260 | PyObject *args; |
| 261 | PyObject *kw; |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 262 | /* The current thread's local dict (necessary for tp_dictoffset) */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 263 | PyObject *dict; |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 264 | PyObject *weakreflist; /* List of weak references to self */ |
| 265 | /* A {localdummy weakref -> localdict} dict */ |
| 266 | PyObject *dummies; |
| 267 | /* The callback for weakrefs to localdummies */ |
| 268 | PyObject *wr_callback; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 269 | } localobject; |
| 270 | |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 271 | /* Forward declaration */ |
| 272 | static PyObject *_ldict(localobject *self); |
| 273 | static PyObject *_localdummy_destroyed(PyObject *meth_self, PyObject *dummyweakref); |
| 274 | |
| 275 | /* Create and register the dummy for the current thread, as well as the |
| 276 | corresponding local dict */ |
| 277 | static int |
| 278 | _local_create_dummy(localobject *self) |
| 279 | { |
| 280 | PyObject *tdict, *ldict = NULL, *wr = NULL; |
| 281 | localdummyobject *dummy = NULL; |
| 282 | int r; |
| 283 | |
| 284 | tdict = PyThreadState_GetDict(); |
| 285 | if (tdict == NULL) { |
| 286 | PyErr_SetString(PyExc_SystemError, |
| 287 | "Couldn't get thread-state dictionary"); |
| 288 | goto err; |
| 289 | } |
| 290 | |
| 291 | ldict = PyDict_New(); |
| 292 | if (ldict == NULL) |
| 293 | goto err; |
| 294 | dummy = (localdummyobject *) localdummytype.tp_alloc(&localdummytype, 0); |
| 295 | if (dummy == NULL) |
| 296 | goto err; |
| 297 | dummy->localdict = ldict; |
| 298 | wr = PyWeakref_NewRef((PyObject *) dummy, self->wr_callback); |
| 299 | if (wr == NULL) |
| 300 | goto err; |
| 301 | |
| 302 | /* As a side-effect, this will cache the weakref's hash before the |
| 303 | dummy gets deleted */ |
| 304 | r = PyDict_SetItem(self->dummies, wr, ldict); |
| 305 | if (r < 0) |
| 306 | goto err; |
| 307 | Py_CLEAR(wr); |
| 308 | r = PyDict_SetItem(tdict, self->key, (PyObject *) dummy); |
| 309 | if (r < 0) |
| 310 | goto err; |
| 311 | Py_CLEAR(dummy); |
| 312 | |
| 313 | Py_CLEAR(self->dict); |
| 314 | self->dict = ldict; |
| 315 | return 0; |
| 316 | |
| 317 | err: |
| 318 | Py_XDECREF(ldict); |
| 319 | Py_XDECREF(wr); |
| 320 | Py_XDECREF(dummy); |
| 321 | return -1; |
| 322 | } |
| 323 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 324 | static PyObject * |
| 325 | local_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 326 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 327 | localobject *self; |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 328 | PyObject *wr; |
| 329 | static PyMethodDef wr_callback_def = { |
| 330 | "_localdummy_destroyed", (PyCFunction) _localdummy_destroyed, METH_O |
| 331 | }; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 332 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 333 | if (type->tp_init == PyBaseObject_Type.tp_init |
| 334 | && ((args && PyObject_IsTrue(args)) |
| 335 | || (kw && PyObject_IsTrue(kw)))) { |
| 336 | PyErr_SetString(PyExc_TypeError, |
| 337 | "Initialization arguments are not supported"); |
| 338 | return NULL; |
| 339 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 340 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 341 | self = (localobject *)type->tp_alloc(type, 0); |
| 342 | if (self == NULL) |
| 343 | return NULL; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 344 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 345 | Py_XINCREF(args); |
| 346 | self->args = args; |
| 347 | Py_XINCREF(kw); |
| 348 | self->kw = kw; |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 349 | self->key = PyUnicode_FromFormat("thread.local.%p", self); |
| 350 | if (self->key == NULL) |
| 351 | goto err; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 352 | |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 353 | self->dummies = PyDict_New(); |
| 354 | if (self->dummies == NULL) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 355 | goto err; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 356 | |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 357 | /* We use a weak reference to self in the callback closure |
| 358 | in order to avoid spurious reference cycles */ |
| 359 | wr = PyWeakref_NewRef((PyObject *) self, NULL); |
| 360 | if (wr == NULL) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 361 | goto err; |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 362 | self->wr_callback = PyCFunction_New(&wr_callback_def, wr); |
| 363 | Py_DECREF(wr); |
| 364 | if (self->wr_callback == NULL) |
| 365 | goto err; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 366 | |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 367 | if (_local_create_dummy(self) < 0) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 368 | goto err; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 369 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 370 | return (PyObject *)self; |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 371 | |
| 372 | err: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 373 | Py_DECREF(self); |
| 374 | return NULL; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | static int |
| 378 | local_traverse(localobject *self, visitproc visit, void *arg) |
| 379 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 380 | Py_VISIT(self->args); |
| 381 | Py_VISIT(self->kw); |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 382 | Py_VISIT(self->dummies); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 383 | Py_VISIT(self->dict); |
| 384 | return 0; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | static int |
| 388 | local_clear(localobject *self) |
| 389 | { |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 390 | PyThreadState *tstate; |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 391 | Py_CLEAR(self->args); |
| 392 | Py_CLEAR(self->kw); |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 393 | Py_CLEAR(self->dummies); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 394 | Py_CLEAR(self->dict); |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 395 | Py_CLEAR(self->wr_callback); |
| 396 | /* Remove all strong references to dummies from the thread states */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 397 | if (self->key |
| 398 | && (tstate = PyThreadState_Get()) |
| 399 | && tstate->interp) { |
| 400 | for(tstate = PyInterpreterState_ThreadHead(tstate->interp); |
| 401 | tstate; |
| 402 | tstate = PyThreadState_Next(tstate)) |
| 403 | if (tstate->dict && |
| 404 | PyDict_GetItem(tstate->dict, self->key)) |
| 405 | PyDict_DelItem(tstate->dict, self->key); |
| 406 | } |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 407 | return 0; |
| 408 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 409 | |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 410 | static void |
| 411 | local_dealloc(localobject *self) |
| 412 | { |
| 413 | /* Weakrefs must be invalidated right now, otherwise they can be used |
| 414 | from code called below, which is very dangerous since Py_REFCNT(self) == 0 */ |
| 415 | if (self->weakreflist != NULL) |
| 416 | PyObject_ClearWeakRefs((PyObject *) self); |
| 417 | |
| 418 | PyObject_GC_UnTrack(self); |
| 419 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 420 | local_clear(self); |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 421 | Py_XDECREF(self->key); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 422 | Py_TYPE(self)->tp_free((PyObject*)self); |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 425 | /* Returns a borrowed reference to the local dict, creating it if necessary */ |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 426 | static PyObject * |
| 427 | _ldict(localobject *self) |
| 428 | { |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 429 | PyObject *tdict, *ldict, *dummy; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 430 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 431 | tdict = PyThreadState_GetDict(); |
| 432 | if (tdict == NULL) { |
| 433 | PyErr_SetString(PyExc_SystemError, |
| 434 | "Couldn't get thread-state dictionary"); |
| 435 | return NULL; |
| 436 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 437 | |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 438 | dummy = PyDict_GetItem(tdict, self->key); |
| 439 | if (dummy == NULL) { |
| 440 | if (_local_create_dummy(self) < 0) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 441 | return NULL; |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 442 | ldict = self->dict; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 443 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 444 | if (Py_TYPE(self)->tp_init != PyBaseObject_Type.tp_init && |
| 445 | Py_TYPE(self)->tp_init((PyObject*)self, |
| 446 | self->args, self->kw) < 0) { |
| 447 | /* we need to get rid of ldict from thread so |
| 448 | we create a new one the next time we do an attr |
| 449 | acces */ |
| 450 | PyDict_DelItem(tdict, self->key); |
| 451 | return NULL; |
| 452 | } |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 453 | } |
| 454 | else { |
| 455 | assert(Py_TYPE(dummy) == &localdummytype); |
| 456 | ldict = ((localdummyobject *) dummy)->localdict; |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 457 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 458 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 459 | /* The call to tp_init above may have caused another thread to run. |
| 460 | Install our ldict again. */ |
| 461 | if (self->dict != ldict) { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 462 | Py_INCREF(ldict); |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 463 | Py_CLEAR(self->dict); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 464 | self->dict = ldict; |
| 465 | } |
| 466 | |
| 467 | return ldict; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 470 | static int |
| 471 | local_setattro(localobject *self, PyObject *name, PyObject *v) |
| 472 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 473 | PyObject *ldict; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 474 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 475 | ldict = _ldict(self); |
| 476 | if (ldict == NULL) |
| 477 | return -1; |
| 478 | |
| 479 | return PyObject_GenericSetAttr((PyObject *)self, name, v); |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | static PyObject * |
| 483 | local_getdict(localobject *self, void *closure) |
| 484 | { |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 485 | PyObject *ldict; |
| 486 | ldict = _ldict(self); |
| 487 | Py_XINCREF(ldict); |
| 488 | return ldict; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | static PyGetSetDef local_getset[] = { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 492 | {"__dict__", (getter)local_getdict, (setter)NULL, |
| 493 | "Local-data dictionary", NULL}, |
| 494 | {NULL} /* Sentinel */ |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 495 | }; |
| 496 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 497 | static PyObject *local_getattro(localobject *, PyObject *); |
| 498 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 499 | static PyTypeObject localtype = { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 500 | PyVarObject_HEAD_INIT(NULL, 0) |
| 501 | /* tp_name */ "_thread._local", |
| 502 | /* tp_basicsize */ sizeof(localobject), |
| 503 | /* tp_itemsize */ 0, |
| 504 | /* tp_dealloc */ (destructor)local_dealloc, |
| 505 | /* tp_print */ 0, |
| 506 | /* tp_getattr */ 0, |
| 507 | /* tp_setattr */ 0, |
| 508 | /* tp_reserved */ 0, |
| 509 | /* tp_repr */ 0, |
| 510 | /* tp_as_number */ 0, |
| 511 | /* tp_as_sequence */ 0, |
| 512 | /* tp_as_mapping */ 0, |
| 513 | /* tp_hash */ 0, |
| 514 | /* tp_call */ 0, |
| 515 | /* tp_str */ 0, |
| 516 | /* tp_getattro */ (getattrofunc)local_getattro, |
| 517 | /* tp_setattro */ (setattrofunc)local_setattro, |
| 518 | /* tp_as_buffer */ 0, |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 519 | /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
| 520 | | Py_TPFLAGS_HAVE_GC, |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 521 | /* tp_doc */ "Thread-local data", |
| 522 | /* tp_traverse */ (traverseproc)local_traverse, |
| 523 | /* tp_clear */ (inquiry)local_clear, |
| 524 | /* tp_richcompare */ 0, |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 525 | /* tp_weaklistoffset */ offsetof(localobject, weakreflist), |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 526 | /* tp_iter */ 0, |
| 527 | /* tp_iternext */ 0, |
| 528 | /* tp_methods */ 0, |
| 529 | /* tp_members */ 0, |
| 530 | /* tp_getset */ local_getset, |
| 531 | /* tp_base */ 0, |
| 532 | /* tp_dict */ 0, /* internal use */ |
| 533 | /* tp_descr_get */ 0, |
| 534 | /* tp_descr_set */ 0, |
| 535 | /* tp_dictoffset */ offsetof(localobject, dict), |
| 536 | /* tp_init */ 0, |
| 537 | /* tp_alloc */ 0, |
| 538 | /* tp_new */ local_new, |
| 539 | /* tp_free */ 0, /* Low-level free-mem routine */ |
| 540 | /* tp_is_gc */ 0, /* For PyObject_IS_GC */ |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 541 | }; |
| 542 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 543 | static PyObject * |
| 544 | local_getattro(localobject *self, PyObject *name) |
| 545 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 546 | PyObject *ldict, *value; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 547 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 548 | ldict = _ldict(self); |
| 549 | if (ldict == NULL) |
| 550 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 551 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 552 | if (Py_TYPE(self) != &localtype) |
| 553 | /* use generic lookup for subtypes */ |
| 554 | return PyObject_GenericGetAttr((PyObject *)self, name); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 555 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 556 | /* Optimization: just look in dict ourselves */ |
| 557 | value = PyDict_GetItem(ldict, name); |
| 558 | if (value == NULL) |
| 559 | /* Fall back on generic to get __class__ and __dict__ */ |
| 560 | return PyObject_GenericGetAttr((PyObject *)self, name); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 561 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 562 | Py_INCREF(value); |
| 563 | return value; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 564 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 565 | |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 566 | /* Called when a dummy is destroyed. */ |
| 567 | static PyObject * |
| 568 | _localdummy_destroyed(PyObject *localweakref, PyObject *dummyweakref) |
| 569 | { |
| 570 | PyObject *obj; |
| 571 | localobject *self; |
| 572 | assert(PyWeakref_CheckRef(localweakref)); |
| 573 | obj = PyWeakref_GET_OBJECT(localweakref); |
| 574 | if (obj == Py_None) |
| 575 | Py_RETURN_NONE; |
| 576 | Py_INCREF(obj); |
| 577 | assert(PyObject_TypeCheck(obj, &localtype)); |
| 578 | /* If the thread-local object is still alive and not being cleared, |
| 579 | remove the corresponding local dict */ |
| 580 | self = (localobject *) obj; |
| 581 | if (self->dummies != NULL) { |
| 582 | PyObject *ldict; |
| 583 | ldict = PyDict_GetItem(self->dummies, dummyweakref); |
| 584 | if (ldict != NULL) { |
| 585 | if (ldict == self->dict) |
| 586 | Py_CLEAR(self->dict); |
| 587 | PyDict_DelItem(self->dummies, dummyweakref); |
| 588 | } |
| 589 | if (PyErr_Occurred()) |
| 590 | PyErr_WriteUnraisable(obj); |
| 591 | } |
| 592 | Py_DECREF(obj); |
| 593 | Py_RETURN_NONE; |
| 594 | } |
| 595 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 596 | /* Module functions */ |
| 597 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 598 | struct bootstate { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 599 | PyInterpreterState *interp; |
| 600 | PyObject *func; |
| 601 | PyObject *args; |
| 602 | PyObject *keyw; |
| 603 | PyThreadState *tstate; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 604 | }; |
| 605 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 606 | static void |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 607 | t_bootstrap(void *boot_raw) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 608 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 609 | struct bootstate *boot = (struct bootstate *) boot_raw; |
| 610 | PyThreadState *tstate; |
| 611 | PyObject *res; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 612 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 613 | tstate = boot->tstate; |
| 614 | tstate->thread_id = PyThread_get_thread_ident(); |
| 615 | _PyThreadState_Init(tstate); |
| 616 | PyEval_AcquireThread(tstate); |
| 617 | res = PyEval_CallObjectWithKeywords( |
| 618 | boot->func, boot->args, boot->keyw); |
| 619 | if (res == NULL) { |
| 620 | if (PyErr_ExceptionMatches(PyExc_SystemExit)) |
| 621 | PyErr_Clear(); |
| 622 | else { |
| 623 | PyObject *file; |
| 624 | PySys_WriteStderr( |
| 625 | "Unhandled exception in thread started by "); |
| 626 | file = PySys_GetObject("stderr"); |
| 627 | if (file != NULL && file != Py_None) |
| 628 | PyFile_WriteObject(boot->func, file, 0); |
| 629 | else |
| 630 | PyObject_Print(boot->func, stderr, 0); |
| 631 | PySys_WriteStderr("\n"); |
| 632 | PyErr_PrintEx(0); |
| 633 | } |
| 634 | } |
| 635 | else |
| 636 | Py_DECREF(res); |
| 637 | Py_DECREF(boot->func); |
| 638 | Py_DECREF(boot->args); |
| 639 | Py_XDECREF(boot->keyw); |
| 640 | PyMem_DEL(boot_raw); |
| 641 | PyThreadState_Clear(tstate); |
| 642 | PyThreadState_DeleteCurrent(); |
| 643 | PyThread_exit_thread(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 646 | static PyObject * |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 647 | thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 648 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 649 | PyObject *func, *args, *keyw = NULL; |
| 650 | struct bootstate *boot; |
| 651 | long ident; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 652 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 653 | if (!PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3, |
| 654 | &func, &args, &keyw)) |
| 655 | return NULL; |
| 656 | if (!PyCallable_Check(func)) { |
| 657 | PyErr_SetString(PyExc_TypeError, |
| 658 | "first arg must be callable"); |
| 659 | return NULL; |
| 660 | } |
| 661 | if (!PyTuple_Check(args)) { |
| 662 | PyErr_SetString(PyExc_TypeError, |
| 663 | "2nd arg must be a tuple"); |
| 664 | return NULL; |
| 665 | } |
| 666 | if (keyw != NULL && !PyDict_Check(keyw)) { |
| 667 | PyErr_SetString(PyExc_TypeError, |
| 668 | "optional 3rd arg must be a dictionary"); |
| 669 | return NULL; |
| 670 | } |
| 671 | boot = PyMem_NEW(struct bootstate, 1); |
| 672 | if (boot == NULL) |
| 673 | return PyErr_NoMemory(); |
| 674 | boot->interp = PyThreadState_GET()->interp; |
| 675 | boot->func = func; |
| 676 | boot->args = args; |
| 677 | boot->keyw = keyw; |
| 678 | boot->tstate = _PyThreadState_Prealloc(boot->interp); |
| 679 | if (boot->tstate == NULL) { |
| 680 | PyMem_DEL(boot); |
| 681 | return PyErr_NoMemory(); |
| 682 | } |
| 683 | Py_INCREF(func); |
| 684 | Py_INCREF(args); |
| 685 | Py_XINCREF(keyw); |
| 686 | PyEval_InitThreads(); /* Start the interpreter's thread-awareness */ |
| 687 | ident = PyThread_start_new_thread(t_bootstrap, (void*) boot); |
| 688 | if (ident == -1) { |
| 689 | PyErr_SetString(ThreadError, "can't start new thread"); |
| 690 | Py_DECREF(func); |
| 691 | Py_DECREF(args); |
| 692 | Py_XDECREF(keyw); |
| 693 | PyThreadState_Clear(boot->tstate); |
| 694 | PyMem_DEL(boot); |
| 695 | return NULL; |
| 696 | } |
| 697 | return PyLong_FromLong(ident); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 700 | PyDoc_STRVAR(start_new_doc, |
Andrew M. Kuchling | 38300c6 | 2001-10-05 12:24:15 +0000 | [diff] [blame] | 701 | "start_new_thread(function, args[, kwargs])\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 702 | (start_new() is an obsolete synonym)\n\ |
| 703 | \n\ |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 704 | Start a new thread and return its identifier. The thread will call the\n\ |
| 705 | function with positional arguments from the tuple args and keyword arguments\n\ |
| 706 | taken from the optional dictionary kwargs. The thread exits when the\n\ |
| 707 | function returns; the return value is ignored. The thread will also exit\n\ |
| 708 | 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] | 709 | printed unless the exception is SystemExit.\n"); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 710 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 711 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 712 | thread_PyThread_exit_thread(PyObject *self) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 713 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 714 | PyErr_SetNone(PyExc_SystemExit); |
| 715 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 718 | PyDoc_STRVAR(exit_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 719 | "exit()\n\ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 720 | (PyThread_exit_thread() is an obsolete synonym)\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 721 | \n\ |
| 722 | 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] | 723 | thread to exit silently unless the exception is caught."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 724 | |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 725 | static PyObject * |
Kurt B. Kaiser | a1ad5f6 | 2003-06-16 18:51:28 +0000 | [diff] [blame] | 726 | thread_PyThread_interrupt_main(PyObject * self) |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 727 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 728 | PyErr_SetInterrupt(); |
| 729 | Py_INCREF(Py_None); |
| 730 | return Py_None; |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | PyDoc_STRVAR(interrupt_doc, |
| 734 | "interrupt_main()\n\ |
| 735 | \n\ |
| 736 | Raise a KeyboardInterrupt in the main thread.\n\ |
Kurt B. Kaiser | a1ad5f6 | 2003-06-16 18:51:28 +0000 | [diff] [blame] | 737 | A subthread can use this function to interrupt the main thread." |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 738 | ); |
| 739 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 740 | #ifndef NO_EXIT_PROG |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 741 | static PyObject * |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 742 | thread_PyThread_exit_prog(PyObject *self, PyObject *args) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 743 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 744 | int sts; |
| 745 | if (!PyArg_ParseTuple(args, "i:exit_prog", &sts)) |
| 746 | return NULL; |
| 747 | Py_Exit(sts); /* Calls PyThread_exit_prog(sts) or _PyThread_exit_prog(sts) */ |
| 748 | for (;;) { } /* Should not be reached */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 749 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 750 | #endif |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 751 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 752 | static lockobject *newlockobject(void); |
| 753 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 754 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 755 | thread_PyThread_allocate_lock(PyObject *self) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 756 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 757 | return (PyObject *) newlockobject(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 760 | PyDoc_STRVAR(allocate_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 761 | "allocate_lock() -> lock object\n\ |
| 762 | (allocate() is an obsolete synonym)\n\ |
| 763 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 764 | 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] | 765 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 766 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 767 | thread_get_ident(PyObject *self) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 768 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 769 | long ident; |
| 770 | ident = PyThread_get_thread_ident(); |
| 771 | if (ident == -1) { |
| 772 | PyErr_SetString(ThreadError, "no current thread ident"); |
| 773 | return NULL; |
| 774 | } |
| 775 | return PyLong_FromLong(ident); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 778 | PyDoc_STRVAR(get_ident_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 779 | "get_ident() -> integer\n\ |
| 780 | \n\ |
| 781 | Return a non-zero integer that uniquely identifies the current thread\n\ |
| 782 | amongst other threads that exist simultaneously.\n\ |
| 783 | This may be used to identify per-thread resources.\n\ |
| 784 | Even though on some platforms threads identities may appear to be\n\ |
| 785 | allocated consecutive numbers starting at 1, this behavior should not\n\ |
| 786 | 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] | 787 | 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] | 788 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 789 | static PyObject * |
| 790 | thread_stack_size(PyObject *self, PyObject *args) |
| 791 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 792 | size_t old_size; |
| 793 | Py_ssize_t new_size = 0; |
| 794 | int rc; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 795 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 796 | if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size)) |
| 797 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 798 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 799 | if (new_size < 0) { |
| 800 | PyErr_SetString(PyExc_ValueError, |
| 801 | "size must be 0 or a positive value"); |
| 802 | return NULL; |
| 803 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 804 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 805 | old_size = PyThread_get_stacksize(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 806 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 807 | rc = PyThread_set_stacksize((size_t) new_size); |
| 808 | if (rc == -1) { |
| 809 | PyErr_Format(PyExc_ValueError, |
| 810 | "size not valid: %zd bytes", |
| 811 | new_size); |
| 812 | return NULL; |
| 813 | } |
| 814 | if (rc == -2) { |
| 815 | PyErr_SetString(ThreadError, |
| 816 | "setting stack size not supported"); |
| 817 | return NULL; |
| 818 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 819 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 820 | return PyLong_FromSsize_t((Py_ssize_t) old_size); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | PyDoc_STRVAR(stack_size_doc, |
| 824 | "stack_size([size]) -> size\n\ |
| 825 | \n\ |
| 826 | Return the thread stack size used when creating new threads. The\n\ |
| 827 | optional size argument specifies the stack size (in bytes) to be used\n\ |
| 828 | for subsequently created threads, and must be 0 (use platform or\n\ |
| 829 | configured default) or a positive integer value of at least 32,768 (32k).\n\ |
| 830 | If changing the thread stack size is unsupported, a ThreadError\n\ |
| 831 | exception is raised. If the specified size is invalid, a ValueError\n\ |
| 832 | exception is raised, and the stack size is unmodified. 32k bytes\n\ |
| 833 | currently the minimum supported stack size value to guarantee\n\ |
| 834 | sufficient stack space for the interpreter itself.\n\ |
| 835 | \n\ |
| 836 | Note that some platforms may have particular restrictions on values for\n\ |
| 837 | the stack size, such as requiring a minimum stack size larger than 32kB or\n\ |
| 838 | requiring allocation in multiples of the system memory page size\n\ |
| 839 | - platform documentation should be referred to for more information\n\ |
| 840 | (4kB pages are common; using multiples of 4096 for the stack size is\n\ |
| 841 | the suggested approach in the absence of more specific information)."); |
| 842 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 843 | static PyMethodDef thread_methods[] = { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 844 | {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread, |
| 845 | METH_VARARGS, |
| 846 | start_new_doc}, |
| 847 | {"start_new", (PyCFunction)thread_PyThread_start_new_thread, |
| 848 | METH_VARARGS, |
| 849 | start_new_doc}, |
| 850 | {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock, |
| 851 | METH_NOARGS, allocate_doc}, |
| 852 | {"allocate", (PyCFunction)thread_PyThread_allocate_lock, |
| 853 | METH_NOARGS, allocate_doc}, |
| 854 | {"exit_thread", (PyCFunction)thread_PyThread_exit_thread, |
| 855 | METH_NOARGS, exit_doc}, |
| 856 | {"exit", (PyCFunction)thread_PyThread_exit_thread, |
| 857 | METH_NOARGS, exit_doc}, |
| 858 | {"interrupt_main", (PyCFunction)thread_PyThread_interrupt_main, |
| 859 | METH_NOARGS, interrupt_doc}, |
| 860 | {"get_ident", (PyCFunction)thread_get_ident, |
| 861 | METH_NOARGS, get_ident_doc}, |
| 862 | {"stack_size", (PyCFunction)thread_stack_size, |
| 863 | METH_VARARGS, |
| 864 | stack_size_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 865 | #ifndef NO_EXIT_PROG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 866 | {"exit_prog", (PyCFunction)thread_PyThread_exit_prog, |
| 867 | METH_VARARGS}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 868 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 869 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 870 | }; |
| 871 | |
| 872 | |
| 873 | /* Initialization function */ |
| 874 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 875 | PyDoc_STRVAR(thread_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 876 | "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] | 877 | The 'threading' module provides a more convenient interface."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 878 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 879 | PyDoc_STRVAR(lock_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 880 | "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] | 881 | call the PyThread_allocate_lock() function. Methods are:\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 882 | \n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 883 | acquire() -- lock the lock, possibly blocking until it can be obtained\n\ |
| 884 | release() -- unlock of the lock\n\ |
| 885 | locked() -- test whether the lock is currently locked\n\ |
| 886 | \n\ |
| 887 | A lock is not owned by the thread that locked it; another thread may\n\ |
| 888 | 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] | 889 | will block until another thread unlocks it. Deadlocks may ensue."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 890 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 891 | static struct PyModuleDef threadmodule = { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 892 | PyModuleDef_HEAD_INIT, |
| 893 | "_thread", |
| 894 | thread_doc, |
| 895 | -1, |
| 896 | thread_methods, |
| 897 | NULL, |
| 898 | NULL, |
| 899 | NULL, |
| 900 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 901 | }; |
| 902 | |
| 903 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 904 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 905 | PyInit__thread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 906 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 907 | PyObject *m, *d; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 908 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 909 | /* Initialize types: */ |
Antoine Pitrou | 87d5260 | 2010-08-09 22:41:38 +0000 | [diff] [blame^] | 910 | if (PyType_Ready(&localdummytype) < 0) |
| 911 | return NULL; |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 912 | if (PyType_Ready(&localtype) < 0) |
| 913 | return NULL; |
| 914 | if (PyType_Ready(&Locktype) < 0) |
| 915 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 916 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 917 | /* Create the module and add the functions */ |
| 918 | m = PyModule_Create(&threadmodule); |
| 919 | if (m == NULL) |
| 920 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 921 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 922 | /* Add a symbolic constant */ |
| 923 | d = PyModule_GetDict(m); |
| 924 | ThreadError = PyErr_NewException("_thread.error", NULL, NULL); |
| 925 | PyDict_SetItemString(d, "error", ThreadError); |
| 926 | Locktype.tp_doc = lock_doc; |
| 927 | Py_INCREF(&Locktype); |
| 928 | PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype); |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 929 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 930 | Py_INCREF(&localtype); |
| 931 | if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0) |
| 932 | return NULL; |
| 933 | |
| 934 | /* Initialize the C thread library */ |
| 935 | PyThread_init_thread(); |
| 936 | return m; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 937 | } |