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