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