blob: 82eff0072b3903c274b077dd256e9db285c63c28 [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001
2/* Thread module */
3/* Interface to Sjoerd's portable C thread library */
4
Barry Warsawd0c10421996-12-17 00:05:22 +00005#include "Python.h"
Gregory P. Smith4e63d542009-08-20 09:39:38 +00006#include "structmember.h" /* offsetof */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00007
Guido van Rossumb6775db1994-08-01 11:34:53 +00008#ifndef WITH_THREAD
Guido van Rossuma027efa1997-05-05 20:56:21 +00009#error "Error! The rest of Python is not compiled with thread support."
Neal Norwitz884baa12002-09-05 21:31:04 +000010#error "Rerun configure, adding a --with-threads option."
Guido van Rossuma027efa1997-05-05 20:56:21 +000011#error "Then run `make clean' followed by `make'."
Guido van Rossumb6775db1994-08-01 11:34:53 +000012#endif
13
Guido van Rossum49b56061998-10-01 20:42:43 +000014#include "pythread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000015
Barry Warsawd0c10421996-12-17 00:05:22 +000016static PyObject *ThreadError;
Antoine Pitroua4083502010-08-28 18:29:13 +000017static PyObject *str_dict;
Antoine Pitrou59c44f32009-10-30 17:07:08 +000018static long nb_threads = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000019
20/* Lock objects */
21
22typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000023 PyObject_HEAD
24 PyThread_type_lock lock_lock;
25 PyObject *in_weakreflist;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000026} lockobject;
27
Guido van Rossum1984f1e1992-08-04 12:41:02 +000028static void
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +000029lock_dealloc(lockobject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000030{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000031 if (self->in_weakreflist != NULL)
32 PyObject_ClearWeakRefs((PyObject *) self);
33 if (self->lock_lock != NULL) {
34 /* Unlock the lock so it's safe to free it */
35 PyThread_acquire_lock(self->lock_lock, 0);
36 PyThread_release_lock(self->lock_lock);
37
38 PyThread_free_lock(self->lock_lock);
39 }
40 PyObject_Del(self);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000041}
42
Barry Warsawd0c10421996-12-17 00:05:22 +000043static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +000044lock_PyThread_acquire_lock(lockobject *self, PyObject *args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000045{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000046 int i = 1;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000047
Antoine Pitrouc83ea132010-05-09 14:46:46 +000048 if (!PyArg_ParseTuple(args, "|i:acquire", &i))
49 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000050
Antoine Pitrouc83ea132010-05-09 14:46:46 +000051 Py_BEGIN_ALLOW_THREADS
52 i = PyThread_acquire_lock(self->lock_lock, i);
53 Py_END_ALLOW_THREADS
Guido van Rossum1984f1e1992-08-04 12:41:02 +000054
Antoine Pitrouc83ea132010-05-09 14:46:46 +000055 return PyBool_FromLong((long)i);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000056}
57
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000058PyDoc_STRVAR(acquire_doc,
R David Murray381372d2013-02-04 10:29:38 -050059"acquire([wait]) -> bool\n\
Guido van Rossumf6694362006-03-10 02:28:35 +000060(acquire_lock() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +000061\n\
62Lock the lock. Without argument, this blocks if the lock is already\n\
63locked (even by the same thread), waiting for another thread to release\n\
R David Murray381372d2013-02-04 10:29:38 -050064the lock, and return True once the lock is acquired.\n\
Guido van Rossum8fdc75b2002-04-07 06:32:21 +000065With an argument, this will only block if the argument is true,\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +000066and the return value reflects whether the lock is acquired.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000067The blocking operation is not interruptible.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +000068
Barry Warsawd0c10421996-12-17 00:05:22 +000069static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +000070lock_PyThread_release_lock(lockobject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000071{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000072 /* Sanity check: the lock must be locked */
73 if (PyThread_acquire_lock(self->lock_lock, 0)) {
74 PyThread_release_lock(self->lock_lock);
75 PyErr_SetString(ThreadError, "release unlocked lock");
76 return NULL;
77 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +000078
Antoine Pitrouc83ea132010-05-09 14:46:46 +000079 PyThread_release_lock(self->lock_lock);
80 Py_INCREF(Py_None);
81 return Py_None;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000082}
83
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000084PyDoc_STRVAR(release_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +000085"release()\n\
Guido van Rossumf6694362006-03-10 02:28:35 +000086(release_lock() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +000087\n\
88Release the lock, allowing another thread that is blocked waiting for\n\
89the lock to acquire the lock. The lock must be in the locked state,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000090but it needn't be locked by the same thread that unlocks it.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +000091
Barry Warsawd0c10421996-12-17 00:05:22 +000092static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +000093lock_locked_lock(lockobject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000094{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000095 if (PyThread_acquire_lock(self->lock_lock, 0)) {
96 PyThread_release_lock(self->lock_lock);
97 return PyBool_FromLong(0L);
98 }
99 return PyBool_FromLong(1L);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000100}
101
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000102PyDoc_STRVAR(locked_doc,
Guido van Rossum8fdc75b2002-04-07 06:32:21 +0000103"locked() -> bool\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000104(locked_lock() is an obsolete synonym)\n\
105\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000106Return whether the lock is in the locked state.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000107
Barry Warsawd0c10421996-12-17 00:05:22 +0000108static PyMethodDef lock_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000109 {"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock,
110 METH_VARARGS, acquire_doc},
111 {"acquire", (PyCFunction)lock_PyThread_acquire_lock,
112 METH_VARARGS, acquire_doc},
113 {"release_lock", (PyCFunction)lock_PyThread_release_lock,
114 METH_NOARGS, release_doc},
115 {"release", (PyCFunction)lock_PyThread_release_lock,
116 METH_NOARGS, release_doc},
117 {"locked_lock", (PyCFunction)lock_locked_lock,
118 METH_NOARGS, locked_doc},
119 {"locked", (PyCFunction)lock_locked_lock,
120 METH_NOARGS, locked_doc},
121 {"__enter__", (PyCFunction)lock_PyThread_acquire_lock,
122 METH_VARARGS, acquire_doc},
123 {"__exit__", (PyCFunction)lock_PyThread_release_lock,
124 METH_VARARGS, release_doc},
125 {NULL} /* sentinel */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000126};
127
Barry Warsawd0c10421996-12-17 00:05:22 +0000128static PyTypeObject Locktype = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000129 PyVarObject_HEAD_INIT(&PyType_Type, 0)
130 "thread.lock", /*tp_name*/
131 sizeof(lockobject), /*tp_size*/
132 0, /*tp_itemsize*/
133 /* methods */
134 (destructor)lock_dealloc, /*tp_dealloc*/
135 0, /*tp_print*/
136 0, /*tp_getattr*/
137 0, /*tp_setattr*/
138 0, /*tp_compare*/
139 0, /*tp_repr*/
140 0, /* tp_as_number */
141 0, /* tp_as_sequence */
142 0, /* tp_as_mapping */
143 0, /* tp_hash */
144 0, /* tp_call */
145 0, /* tp_str */
146 0, /* tp_getattro */
147 0, /* tp_setattro */
148 0, /* tp_as_buffer */
149 Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
150 0, /* tp_doc */
151 0, /* tp_traverse */
152 0, /* tp_clear */
153 0, /* tp_richcompare */
154 offsetof(lockobject, in_weakreflist), /* tp_weaklistoffset */
155 0, /* tp_iter */
156 0, /* tp_iternext */
157 lock_methods, /* tp_methods */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000158};
159
Anthony Baxter5576b542006-04-12 04:08:46 +0000160static lockobject *
161newlockobject(void)
162{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000163 lockobject *self;
164 self = PyObject_New(lockobject, &Locktype);
165 if (self == NULL)
166 return NULL;
167 self->lock_lock = PyThread_allocate_lock();
168 self->in_weakreflist = NULL;
169 if (self->lock_lock == NULL) {
170 Py_DECREF(self);
171 PyErr_SetString(ThreadError, "can't allocate lock");
172 return NULL;
173 }
174 return self;
Anthony Baxter5576b542006-04-12 04:08:46 +0000175}
176
Jim Fultond15dc062004-07-14 19:11:50 +0000177/* Thread-local objects */
178
179#include "structmember.h"
180
Antoine Pitrou99009162010-08-09 22:51:24 +0000181/* Quick overview:
182
183 We need to be able to reclaim reference cycles as soon as possible
184 (both when a thread is being terminated, or a thread-local object
185 becomes unreachable from user data). Constraints:
186 - it must not be possible for thread-state dicts to be involved in
187 reference cycles (otherwise the cyclic GC will refuse to consider
188 objects referenced from a reachable thread-state dict, even though
189 local_dealloc would clear them)
190 - the death of a thread-state dict must still imply destruction of the
191 corresponding local dicts in all thread-local objects.
192
193 Our implementation uses small "localdummy" objects in order to break
194 the reference chain. These trivial objects are hashable (using the
195 default scheme of identity hashing) and weakrefable.
196 Each thread-state holds a separate localdummy for each local object
197 (as a /strong reference/),
198 and each thread-local object holds a dict mapping /weak references/
199 of localdummies to local dicts.
200
201 Therefore:
202 - only the thread-state dict holds a strong reference to the dummies
203 - only the thread-local object holds a strong reference to the local dicts
204 - only outside objects (application- or library-level) hold strong
205 references to the thread-local objects
206 - as soon as a thread-state dict is destroyed, the weakref callbacks of all
207 dummies attached to that thread are called, and destroy the corresponding
208 local dicts from thread-local objects
209 - as soon as a thread-local object is destroyed, its local dicts are
210 destroyed and its dummies are manually removed from all thread states
211 - the GC can do its work correctly when a thread-local object is dangling,
212 without any interference from the thread-state dicts
213
214 As an additional optimization, each localdummy holds a borrowed reference
215 to the corresponding localdict. This borrowed reference is only used
216 by the thread-local object which has created the localdummy, which should
217 guarantee that the localdict still exists when accessed.
218*/
219
220typedef struct {
221 PyObject_HEAD
222 PyObject *localdict; /* Borrowed reference! */
223 PyObject *weakreflist; /* List of weak references to self */
224} localdummyobject;
225
226static void
227localdummy_dealloc(localdummyobject *self)
228{
229 if (self->weakreflist != NULL)
230 PyObject_ClearWeakRefs((PyObject *) self);
231 Py_TYPE(self)->tp_free((PyObject*)self);
232}
233
234static PyTypeObject localdummytype = {
235 PyVarObject_HEAD_INIT(NULL, 0)
236 /* tp_name */ "_thread._localdummy",
237 /* tp_basicsize */ sizeof(localdummyobject),
238 /* tp_itemsize */ 0,
239 /* tp_dealloc */ (destructor)localdummy_dealloc,
240 /* tp_print */ 0,
241 /* tp_getattr */ 0,
242 /* tp_setattr */ 0,
243 /* tp_reserved */ 0,
244 /* tp_repr */ 0,
245 /* tp_as_number */ 0,
246 /* tp_as_sequence */ 0,
247 /* tp_as_mapping */ 0,
248 /* tp_hash */ 0,
249 /* tp_call */ 0,
250 /* tp_str */ 0,
251 /* tp_getattro */ 0,
252 /* tp_setattro */ 0,
253 /* tp_as_buffer */ 0,
254 /* tp_flags */ Py_TPFLAGS_DEFAULT,
255 /* tp_doc */ "Thread-local dummy",
256 /* tp_traverse */ 0,
257 /* tp_clear */ 0,
258 /* tp_richcompare */ 0,
259 /* tp_weaklistoffset */ offsetof(localdummyobject, weakreflist)
260};
261
262
Jim Fultond15dc062004-07-14 19:11:50 +0000263typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000264 PyObject_HEAD
265 PyObject *key;
266 PyObject *args;
267 PyObject *kw;
Antoine Pitrou99009162010-08-09 22:51:24 +0000268 PyObject *weakreflist; /* List of weak references to self */
269 /* A {localdummy weakref -> localdict} dict */
270 PyObject *dummies;
271 /* The callback for weakrefs to localdummies */
272 PyObject *wr_callback;
Jim Fultond15dc062004-07-14 19:11:50 +0000273} localobject;
274
Antoine Pitrou99009162010-08-09 22:51:24 +0000275/* Forward declaration */
276static PyObject *_ldict(localobject *self);
277static PyObject *_localdummy_destroyed(PyObject *meth_self, PyObject *dummyweakref);
278
Antoine Pitroua4083502010-08-28 18:29:13 +0000279/* Create and register the dummy for the current thread.
280 Returns a borrowed reference of the corresponding local dict */
281static PyObject *
Antoine Pitrou99009162010-08-09 22:51:24 +0000282_local_create_dummy(localobject *self)
283{
284 PyObject *tdict, *ldict = NULL, *wr = NULL;
285 localdummyobject *dummy = NULL;
286 int r;
287
288 tdict = PyThreadState_GetDict();
289 if (tdict == NULL) {
290 PyErr_SetString(PyExc_SystemError,
291 "Couldn't get thread-state dictionary");
292 goto err;
293 }
294
295 ldict = PyDict_New();
296 if (ldict == NULL)
297 goto err;
298 dummy = (localdummyobject *) localdummytype.tp_alloc(&localdummytype, 0);
299 if (dummy == NULL)
300 goto err;
301 dummy->localdict = ldict;
302 wr = PyWeakref_NewRef((PyObject *) dummy, self->wr_callback);
303 if (wr == NULL)
304 goto err;
305
306 /* As a side-effect, this will cache the weakref's hash before the
307 dummy gets deleted */
308 r = PyDict_SetItem(self->dummies, wr, ldict);
309 if (r < 0)
310 goto err;
311 Py_CLEAR(wr);
312 r = PyDict_SetItem(tdict, self->key, (PyObject *) dummy);
313 if (r < 0)
314 goto err;
315 Py_CLEAR(dummy);
316
Antoine Pitroua4083502010-08-28 18:29:13 +0000317 Py_DECREF(ldict);
318 return ldict;
Antoine Pitrou99009162010-08-09 22:51:24 +0000319
320err:
321 Py_XDECREF(ldict);
322 Py_XDECREF(wr);
323 Py_XDECREF(dummy);
Antoine Pitroua4083502010-08-28 18:29:13 +0000324 return NULL;
Antoine Pitrou99009162010-08-09 22:51:24 +0000325}
326
Jim Fultond15dc062004-07-14 19:11:50 +0000327static PyObject *
328local_new(PyTypeObject *type, PyObject *args, PyObject *kw)
329{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000330 localobject *self;
Antoine Pitrou99009162010-08-09 22:51:24 +0000331 PyObject *wr;
332 static PyMethodDef wr_callback_def = {
333 "_localdummy_destroyed", (PyCFunction) _localdummy_destroyed, METH_O
334 };
Jim Fultond15dc062004-07-14 19:11:50 +0000335
Serhiy Storchaka5127ed72015-05-30 17:45:12 +0300336 if (type->tp_init == PyBaseObject_Type.tp_init) {
337 int rc = 0;
338 if (args != NULL)
339 rc = PyObject_IsTrue(args);
340 if (rc == 0 && kw != NULL)
341 rc = PyObject_IsTrue(kw);
342 if (rc != 0) {
343 if (rc > 0)
344 PyErr_SetString(PyExc_TypeError,
345 "Initialization arguments are not supported");
346 return NULL;
347 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000348 }
Jim Fultond15dc062004-07-14 19:11:50 +0000349
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000350 self = (localobject *)type->tp_alloc(type, 0);
351 if (self == NULL)
352 return NULL;
Jim Fultond15dc062004-07-14 19:11:50 +0000353
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000354 Py_XINCREF(args);
355 self->args = args;
356 Py_XINCREF(kw);
357 self->kw = kw;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000358 self->key = PyString_FromFormat("thread.local.%p", self);
359 if (self->key == NULL)
360 goto err;
Jim Fultond15dc062004-07-14 19:11:50 +0000361
Antoine Pitrou99009162010-08-09 22:51:24 +0000362 self->dummies = PyDict_New();
363 if (self->dummies == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000364 goto err;
Jim Fultond15dc062004-07-14 19:11:50 +0000365
Antoine Pitrou99009162010-08-09 22:51:24 +0000366 /* We use a weak reference to self in the callback closure
367 in order to avoid spurious reference cycles */
368 wr = PyWeakref_NewRef((PyObject *) self, NULL);
369 if (wr == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000370 goto err;
Antoine Pitrou99009162010-08-09 22:51:24 +0000371 self->wr_callback = PyCFunction_New(&wr_callback_def, wr);
372 Py_DECREF(wr);
373 if (self->wr_callback == NULL)
374 goto err;
Jim Fultond15dc062004-07-14 19:11:50 +0000375
Antoine Pitroua4083502010-08-28 18:29:13 +0000376 if (_local_create_dummy(self) == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000377 goto err;
Jim Fultond15dc062004-07-14 19:11:50 +0000378
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000379 return (PyObject *)self;
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000380
381 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000382 Py_DECREF(self);
383 return NULL;
Jim Fultond15dc062004-07-14 19:11:50 +0000384}
385
386static int
387local_traverse(localobject *self, visitproc visit, void *arg)
388{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000389 Py_VISIT(self->args);
390 Py_VISIT(self->kw);
Antoine Pitrou99009162010-08-09 22:51:24 +0000391 Py_VISIT(self->dummies);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000392 return 0;
Jim Fultond15dc062004-07-14 19:11:50 +0000393}
394
395static int
396local_clear(localobject *self)
397{
Antoine Pitrou99009162010-08-09 22:51:24 +0000398 PyThreadState *tstate;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000399 Py_CLEAR(self->args);
400 Py_CLEAR(self->kw);
Antoine Pitrou99009162010-08-09 22:51:24 +0000401 Py_CLEAR(self->dummies);
Antoine Pitrou99009162010-08-09 22:51:24 +0000402 Py_CLEAR(self->wr_callback);
403 /* Remove all strong references to dummies from the thread states */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000404 if (self->key
405 && (tstate = PyThreadState_Get())
406 && tstate->interp) {
407 for(tstate = PyInterpreterState_ThreadHead(tstate->interp);
408 tstate;
409 tstate = PyThreadState_Next(tstate))
410 if (tstate->dict &&
411 PyDict_GetItem(tstate->dict, self->key))
412 PyDict_DelItem(tstate->dict, self->key);
413 }
Antoine Pitrou99009162010-08-09 22:51:24 +0000414 return 0;
415}
Jim Fultond15dc062004-07-14 19:11:50 +0000416
Antoine Pitrou99009162010-08-09 22:51:24 +0000417static void
418local_dealloc(localobject *self)
419{
420 /* Weakrefs must be invalidated right now, otherwise they can be used
421 from code called below, which is very dangerous since Py_REFCNT(self) == 0 */
422 if (self->weakreflist != NULL)
423 PyObject_ClearWeakRefs((PyObject *) self);
424
425 PyObject_GC_UnTrack(self);
426
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000427 local_clear(self);
Antoine Pitrou99009162010-08-09 22:51:24 +0000428 Py_XDECREF(self->key);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000429 Py_TYPE(self)->tp_free((PyObject*)self);
Jim Fultond15dc062004-07-14 19:11:50 +0000430}
431
Antoine Pitrou99009162010-08-09 22:51:24 +0000432/* Returns a borrowed reference to the local dict, creating it if necessary */
Jim Fultond15dc062004-07-14 19:11:50 +0000433static PyObject *
434_ldict(localobject *self)
435{
Antoine Pitrou99009162010-08-09 22:51:24 +0000436 PyObject *tdict, *ldict, *dummy;
Jim Fultond15dc062004-07-14 19:11:50 +0000437
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000438 tdict = PyThreadState_GetDict();
439 if (tdict == NULL) {
440 PyErr_SetString(PyExc_SystemError,
441 "Couldn't get thread-state dictionary");
442 return NULL;
443 }
Jim Fultond15dc062004-07-14 19:11:50 +0000444
Antoine Pitrou99009162010-08-09 22:51:24 +0000445 dummy = PyDict_GetItem(tdict, self->key);
446 if (dummy == NULL) {
Antoine Pitroua4083502010-08-28 18:29:13 +0000447 ldict = _local_create_dummy(self);
448 if (ldict == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000449 return NULL;
Jim Fultond15dc062004-07-14 19:11:50 +0000450
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000451 if (Py_TYPE(self)->tp_init != PyBaseObject_Type.tp_init &&
452 Py_TYPE(self)->tp_init((PyObject*)self,
453 self->args, self->kw) < 0) {
454 /* we need to get rid of ldict from thread so
455 we create a new one the next time we do an attr
Ezio Melotti24b07bc2011-03-15 18:55:01 +0200456 access */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000457 PyDict_DelItem(tdict, self->key);
458 return NULL;
459 }
Antoine Pitrou99009162010-08-09 22:51:24 +0000460 }
461 else {
462 assert(Py_TYPE(dummy) == &localdummytype);
463 ldict = ((localdummyobject *) dummy)->localdict;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000464 }
Jim Fultond15dc062004-07-14 19:11:50 +0000465
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000466 return ldict;
Jim Fultond15dc062004-07-14 19:11:50 +0000467}
468
Jim Fultond15dc062004-07-14 19:11:50 +0000469static int
470local_setattro(localobject *self, PyObject *name, PyObject *v)
471{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000472 PyObject *ldict;
Antoine Pitroua4083502010-08-28 18:29:13 +0000473 int r;
Jim Fultond15dc062004-07-14 19:11:50 +0000474
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000475 ldict = _ldict(self);
476 if (ldict == NULL)
477 return -1;
478
Antoine Pitroua4083502010-08-28 18:29:13 +0000479 r = PyObject_RichCompareBool(name, str_dict, Py_EQ);
480 if (r == 1) {
481 PyErr_Format(PyExc_AttributeError,
482 "'%.50s' object attribute '__dict__' is read-only",
483 Py_TYPE(self)->tp_name);
484 return -1;
485 }
486 if (r == -1)
487 return -1;
Jim Fultond15dc062004-07-14 19:11:50 +0000488
Antoine Pitroua4083502010-08-28 18:29:13 +0000489 return _PyObject_GenericSetAttrWithDict((PyObject *)self, name, v, ldict);
Jim Fultond15dc062004-07-14 19:11:50 +0000490}
491
Anthony Baxter5576b542006-04-12 04:08:46 +0000492static PyObject *local_getattro(localobject *, PyObject *);
493
Jim Fultond15dc062004-07-14 19:11:50 +0000494static PyTypeObject localtype = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000495 PyVarObject_HEAD_INIT(NULL, 0)
496 /* tp_name */ "thread._local",
497 /* tp_basicsize */ sizeof(localobject),
498 /* tp_itemsize */ 0,
499 /* tp_dealloc */ (destructor)local_dealloc,
500 /* tp_print */ 0,
501 /* tp_getattr */ 0,
502 /* tp_setattr */ 0,
503 /* tp_compare */ 0,
504 /* tp_repr */ 0,
505 /* tp_as_number */ 0,
506 /* tp_as_sequence */ 0,
507 /* tp_as_mapping */ 0,
508 /* tp_hash */ 0,
509 /* tp_call */ 0,
510 /* tp_str */ 0,
511 /* tp_getattro */ (getattrofunc)local_getattro,
512 /* tp_setattro */ (setattrofunc)local_setattro,
513 /* tp_as_buffer */ 0,
Antoine Pitrou99009162010-08-09 22:51:24 +0000514 /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
515 | Py_TPFLAGS_HAVE_GC,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000516 /* tp_doc */ "Thread-local data",
517 /* tp_traverse */ (traverseproc)local_traverse,
518 /* tp_clear */ (inquiry)local_clear,
519 /* tp_richcompare */ 0,
Antoine Pitrou99009162010-08-09 22:51:24 +0000520 /* tp_weaklistoffset */ offsetof(localobject, weakreflist),
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000521 /* tp_iter */ 0,
522 /* tp_iternext */ 0,
523 /* tp_methods */ 0,
524 /* tp_members */ 0,
Antoine Pitroua4083502010-08-28 18:29:13 +0000525 /* tp_getset */ 0,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000526 /* tp_base */ 0,
527 /* tp_dict */ 0, /* internal use */
528 /* tp_descr_get */ 0,
529 /* tp_descr_set */ 0,
Antoine Pitroua4083502010-08-28 18:29:13 +0000530 /* tp_dictoffset */ 0,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000531 /* tp_init */ 0,
532 /* tp_alloc */ 0,
533 /* tp_new */ local_new,
534 /* tp_free */ 0, /* Low-level free-mem routine */
535 /* tp_is_gc */ 0, /* For PyObject_IS_GC */
Jim Fultond15dc062004-07-14 19:11:50 +0000536};
537
Anthony Baxter5576b542006-04-12 04:08:46 +0000538static PyObject *
539local_getattro(localobject *self, PyObject *name)
540{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000541 PyObject *ldict, *value;
Antoine Pitroua4083502010-08-28 18:29:13 +0000542 int r;
Anthony Baxter5576b542006-04-12 04:08:46 +0000543
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000544 ldict = _ldict(self);
545 if (ldict == NULL)
546 return NULL;
Anthony Baxter5576b542006-04-12 04:08:46 +0000547
Antoine Pitroua4083502010-08-28 18:29:13 +0000548 r = PyObject_RichCompareBool(name, str_dict, Py_EQ);
549 if (r == 1) {
550 Py_INCREF(ldict);
551 return ldict;
552 }
553 if (r == -1)
554 return NULL;
555
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000556 if (Py_TYPE(self) != &localtype)
557 /* use generic lookup for subtypes */
Antoine Pitroua4083502010-08-28 18:29:13 +0000558 return _PyObject_GenericGetAttrWithDict((PyObject *)self, name, ldict);
Anthony Baxter5576b542006-04-12 04:08:46 +0000559
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000560 /* Optimization: just look in dict ourselves */
561 value = PyDict_GetItem(ldict, name);
562 if (value == NULL)
563 /* Fall back on generic to get __class__ and __dict__ */
Antoine Pitroua4083502010-08-28 18:29:13 +0000564 return _PyObject_GenericGetAttrWithDict((PyObject *)self, name, ldict);
Anthony Baxter5576b542006-04-12 04:08:46 +0000565
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000566 Py_INCREF(value);
567 return value;
Anthony Baxter5576b542006-04-12 04:08:46 +0000568}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000569
Antoine Pitrou99009162010-08-09 22:51:24 +0000570/* Called when a dummy is destroyed. */
571static PyObject *
572_localdummy_destroyed(PyObject *localweakref, PyObject *dummyweakref)
573{
574 PyObject *obj;
575 localobject *self;
576 assert(PyWeakref_CheckRef(localweakref));
577 obj = PyWeakref_GET_OBJECT(localweakref);
578 if (obj == Py_None)
579 Py_RETURN_NONE;
580 Py_INCREF(obj);
581 assert(PyObject_TypeCheck(obj, &localtype));
582 /* If the thread-local object is still alive and not being cleared,
583 remove the corresponding local dict */
584 self = (localobject *) obj;
585 if (self->dummies != NULL) {
586 PyObject *ldict;
587 ldict = PyDict_GetItem(self->dummies, dummyweakref);
588 if (ldict != NULL) {
Antoine Pitrou99009162010-08-09 22:51:24 +0000589 PyDict_DelItem(self->dummies, dummyweakref);
590 }
591 if (PyErr_Occurred())
592 PyErr_WriteUnraisable(obj);
593 }
594 Py_DECREF(obj);
595 Py_RETURN_NONE;
596}
597
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000598/* Module functions */
599
Guido van Rossuma027efa1997-05-05 20:56:21 +0000600struct bootstate {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000601 PyInterpreterState *interp;
602 PyObject *func;
603 PyObject *args;
604 PyObject *keyw;
605 PyThreadState *tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000606};
607
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000608static void
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000609t_bootstrap(void *boot_raw)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000610{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000611 struct bootstate *boot = (struct bootstate *) boot_raw;
612 PyThreadState *tstate;
613 PyObject *res;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000614
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000615 tstate = boot->tstate;
616 tstate->thread_id = PyThread_get_thread_ident();
617 _PyThreadState_Init(tstate);
618 PyEval_AcquireThread(tstate);
619 nb_threads++;
620 res = PyEval_CallObjectWithKeywords(
621 boot->func, boot->args, boot->keyw);
622 if (res == NULL) {
623 if (PyErr_ExceptionMatches(PyExc_SystemExit))
624 PyErr_Clear();
625 else {
626 PyObject *file;
Benjamin Petersonf73813a2012-04-02 11:15:17 -0400627 PyObject *exc, *value, *tb;
628 PyErr_Fetch(&exc, &value, &tb);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000629 PySys_WriteStderr(
630 "Unhandled exception in thread started by ");
631 file = PySys_GetObject("stderr");
632 if (file)
633 PyFile_WriteObject(boot->func, file, 0);
634 else
635 PyObject_Print(boot->func, stderr, 0);
636 PySys_WriteStderr("\n");
Benjamin Petersonf2359892012-04-02 11:18:18 -0400637 PyErr_Restore(exc, value, tb);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000638 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 Rossum1984f1e1992-08-04 12:41:02 +0000651}
652
Barry Warsawd0c10421996-12-17 00:05:22 +0000653static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000654thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000655{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000656 PyObject *func, *args, *keyw = NULL;
657 struct bootstate *boot;
658 long ident;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000659
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000660 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 Rossum1984f1e1992-08-04 12:41:02 +0000705}
706
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000707PyDoc_STRVAR(start_new_doc,
Andrew M. Kuchling38300c62001-10-05 12:24:15 +0000708"start_new_thread(function, args[, kwargs])\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000709(start_new() is an obsolete synonym)\n\
710\n\
Guido van Rossum3c288632001-10-16 21:13:49 +0000711Start a new thread and return its identifier. The thread will call the\n\
712function with positional arguments from the tuple args and keyword arguments\n\
713taken from the optional dictionary kwargs. The thread exits when the\n\
714function returns; the return value is ignored. The thread will also exit\n\
715when the function raises an unhandled exception; a stack trace will be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000716printed unless the exception is SystemExit.\n");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000717
Barry Warsawd0c10421996-12-17 00:05:22 +0000718static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000719thread_PyThread_exit_thread(PyObject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000720{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000721 PyErr_SetNone(PyExc_SystemExit);
722 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000723}
724
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000725PyDoc_STRVAR(exit_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000726"exit()\n\
Éric Araujof2f85032011-05-31 14:08:40 +0200727(exit_thread() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000728\n\
729This is synonymous to ``raise SystemExit''. It will cause the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000730thread to exit silently unless the exception is caught.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000731
Kurt B. Kaisera11e8462003-06-13 21:59:45 +0000732static PyObject *
Kurt B. Kaisera1ad5f62003-06-16 18:51:28 +0000733thread_PyThread_interrupt_main(PyObject * self)
Kurt B. Kaisera11e8462003-06-13 21:59:45 +0000734{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000735 PyErr_SetInterrupt();
736 Py_INCREF(Py_None);
737 return Py_None;
Kurt B. Kaisera11e8462003-06-13 21:59:45 +0000738}
739
740PyDoc_STRVAR(interrupt_doc,
741"interrupt_main()\n\
742\n\
743Raise a KeyboardInterrupt in the main thread.\n\
Kurt B. Kaisera1ad5f62003-06-16 18:51:28 +0000744A subthread can use this function to interrupt the main thread."
Kurt B. Kaisera11e8462003-06-13 21:59:45 +0000745);
746
Anthony Baxter5576b542006-04-12 04:08:46 +0000747static lockobject *newlockobject(void);
748
Barry Warsawd0c10421996-12-17 00:05:22 +0000749static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000750thread_PyThread_allocate_lock(PyObject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000751{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000752 return (PyObject *) newlockobject();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000753}
754
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000755PyDoc_STRVAR(allocate_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000756"allocate_lock() -> lock object\n\
757(allocate() is an obsolete synonym)\n\
758\n\
Alexander Belopolskyb8de9fa2010-08-16 20:30:26 +0000759Create a new lock object. See help(LockType) for information about locks.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000760
Barry Warsawd0c10421996-12-17 00:05:22 +0000761static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000762thread_get_ident(PyObject *self)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000763{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000764 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 Rossumb6775db1994-08-01 11:34:53 +0000771}
772
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000773PyDoc_STRVAR(get_ident_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000774"get_ident() -> integer\n\
775\n\
776Return a non-zero integer that uniquely identifies the current thread\n\
777amongst other threads that exist simultaneously.\n\
778This may be used to identify per-thread resources.\n\
779Even though on some platforms threads identities may appear to be\n\
780allocated consecutive numbers starting at 1, this behavior should not\n\
781be relied upon, and the number should be seen purely as a magic cookie.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000782A thread's identity may be reused for another thread after it exits.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000783
Andrew MacIntyre92913322006-06-13 15:04:24 +0000784static PyObject *
Antoine Pitrou59c44f32009-10-30 17:07:08 +0000785thread__count(PyObject *self)
786{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000787 return PyInt_FromLong(nb_threads);
Antoine Pitrou59c44f32009-10-30 17:07:08 +0000788}
789
790PyDoc_STRVAR(_count_doc,
791"_count() -> integer\n\
792\n\
Antoine Pitrou2c970a22009-10-30 22:19:09 +0000793\
794Return the number of currently running Python threads, excluding \n\
795the main thread. The returned number comprises all threads created\n\
796through `start_new_thread()` as well as `threading.Thread`, and not\n\
797yet finished.\n\
798\n\
799This function is meant for internal and specialized purposes only.\n\
800In most applications `threading.enumerate()` should be used instead.");
Antoine Pitrou59c44f32009-10-30 17:07:08 +0000801
802static PyObject *
Andrew MacIntyre92913322006-06-13 15:04:24 +0000803thread_stack_size(PyObject *self, PyObject *args)
804{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000805 size_t old_size;
806 Py_ssize_t new_size = 0;
807 int rc;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000808
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000809 if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size))
810 return NULL;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000811
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000812 if (new_size < 0) {
813 PyErr_SetString(PyExc_ValueError,
814 "size must be 0 or a positive value");
815 return NULL;
816 }
Andrew MacIntyre92913322006-06-13 15:04:24 +0000817
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000818 old_size = PyThread_get_stacksize();
Andrew MacIntyre92913322006-06-13 15:04:24 +0000819
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000820 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 MacIntyre92913322006-06-13 15:04:24 +0000832
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000833 return PyInt_FromSsize_t((Py_ssize_t) old_size);
Andrew MacIntyre92913322006-06-13 15:04:24 +0000834}
835
836PyDoc_STRVAR(stack_size_doc,
837"stack_size([size]) -> size\n\
838\n\
839Return the thread stack size used when creating new threads. The\n\
840optional size argument specifies the stack size (in bytes) to be used\n\
841for subsequently created threads, and must be 0 (use platform or\n\
842configured default) or a positive integer value of at least 32,768 (32k).\n\
843If changing the thread stack size is unsupported, a ThreadError\n\
844exception is raised. If the specified size is invalid, a ValueError\n\
845exception is raised, and the stack size is unmodified. 32k bytes\n\
846 currently the minimum supported stack size value to guarantee\n\
847sufficient stack space for the interpreter itself.\n\
848\n\
849Note that some platforms may have particular restrictions on values for\n\
850the stack size, such as requiring a minimum stack size larger than 32kB or\n\
851requiring 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\
854the suggested approach in the absence of more specific information).");
855
Barry Warsawd0c10421996-12-17 00:05:22 +0000856static PyMethodDef thread_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000857 {"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 Rossum1984f1e1992-08-04 12:41:02 +0000881};
882
883
884/* Initialization function */
885
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000886PyDoc_STRVAR(thread_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000887"This module provides primitive operations to write multi-threaded programs.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000888The 'threading' module provides a more convenient interface.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000889
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000890PyDoc_STRVAR(lock_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000891"A lock object is a synchronization primitive. To create a lock,\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000892call the PyThread_allocate_lock() function. Methods are:\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000893\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000894acquire() -- lock the lock, possibly blocking until it can be obtained\n\
895release() -- unlock of the lock\n\
896locked() -- test whether the lock is currently locked\n\
897\n\
898A lock is not owned by the thread that locked it; another thread may\n\
899unlock it. A thread attempting to lock a lock that it has already locked\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000900will block until another thread unlocks it. Deadlocks may ensue.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000901
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000902PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000903initthread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000904{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000905 PyObject *m, *d;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000906
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000907 /* Initialize types: */
Antoine Pitrou99009162010-08-09 22:51:24 +0000908 if (PyType_Ready(&localdummytype) < 0)
909 return;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000910 if (PyType_Ready(&localtype) < 0)
911 return;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000912
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000913 /* Create the module and add the functions */
914 m = Py_InitModule3("thread", thread_methods, thread_doc);
915 if (m == NULL)
916 return;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000917
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000918 /* 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 Fultond15dc062004-07-14 19:11:50 +0000927
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000928 Py_INCREF(&localtype);
929 if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0)
930 return;
Antoine Pitrou59c44f32009-10-30 17:07:08 +0000931
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000932 nb_threads = 0;
933
Antoine Pitroua4083502010-08-28 18:29:13 +0000934 str_dict = PyString_InternFromString("__dict__");
935 if (str_dict == NULL)
936 return;
937
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000938 /* Initialize the C thread library */
939 PyThread_init_thread();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000940}