blob: f6d7ee4f56eea94944ba20822a8f52112f3b7501 [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;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000017
18
19/* Lock objects */
20
21typedef struct {
Barry Warsawd0c10421996-12-17 00:05:22 +000022 PyObject_HEAD
Guido van Rossum65d5b571998-12-21 19:32:43 +000023 PyThread_type_lock lock_lock;
Gregory P. Smith4e63d542009-08-20 09:39:38 +000024 PyObject *in_weakreflist;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000025} lockobject;
26
Guido van Rossum1984f1e1992-08-04 12:41:02 +000027static void
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +000028lock_dealloc(lockobject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000029{
Neal Norwitz837ce932006-10-28 21:15:30 +000030 assert(self->lock_lock);
Gregory P. Smith4e63d542009-08-20 09:39:38 +000031 if (self->in_weakreflist != NULL)
32 PyObject_ClearWeakRefs((PyObject *) self);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000033 /* Unlock the lock so it's safe to free it */
Guido van Rossum65d5b571998-12-21 19:32:43 +000034 PyThread_acquire_lock(self->lock_lock, 0);
35 PyThread_release_lock(self->lock_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000036
Guido van Rossum65d5b571998-12-21 19:32:43 +000037 PyThread_free_lock(self->lock_lock);
Guido van Rossumb18618d2000-05-03 23:44:39 +000038 PyObject_Del(self);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000039}
40
Barry Warsawd0c10421996-12-17 00:05:22 +000041static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +000042lock_PyThread_acquire_lock(lockobject *self, PyObject *args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000043{
Neal Norwitzba3a16c2002-03-31 15:27:00 +000044 int i = 1;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000045
Neal Norwitzba3a16c2002-03-31 15:27:00 +000046 if (!PyArg_ParseTuple(args, "|i:acquire", &i))
47 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000048
Barry Warsawd0c10421996-12-17 00:05:22 +000049 Py_BEGIN_ALLOW_THREADS
Guido van Rossum65d5b571998-12-21 19:32:43 +000050 i = PyThread_acquire_lock(self->lock_lock, i);
Barry Warsawd0c10421996-12-17 00:05:22 +000051 Py_END_ALLOW_THREADS
Guido van Rossum1984f1e1992-08-04 12:41:02 +000052
Andrew M. Kuchlinga43ece92005-06-02 17:07:11 +000053 return PyBool_FromLong((long)i);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000054}
55
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000056PyDoc_STRVAR(acquire_doc,
Guido van Rossum8fdc75b2002-04-07 06:32:21 +000057"acquire([wait]) -> None or bool\n\
Guido van Rossumf6694362006-03-10 02:28:35 +000058(acquire_lock() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +000059\n\
60Lock the lock. Without argument, this blocks if the lock is already\n\
61locked (even by the same thread), waiting for another thread to release\n\
Guido van Rossum8fdc75b2002-04-07 06:32:21 +000062the lock, and return None once the lock is acquired.\n\
63With an argument, this will only block if the argument is true,\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +000064and the return value reflects whether the lock is acquired.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000065The blocking operation is not interruptible.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +000066
Barry Warsawd0c10421996-12-17 00:05:22 +000067static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +000068lock_PyThread_release_lock(lockobject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000069{
Guido van Rossum1984f1e1992-08-04 12:41:02 +000070 /* Sanity check: the lock must be locked */
Guido van Rossum65d5b571998-12-21 19:32:43 +000071 if (PyThread_acquire_lock(self->lock_lock, 0)) {
72 PyThread_release_lock(self->lock_lock);
Barry Warsawd0c10421996-12-17 00:05:22 +000073 PyErr_SetString(ThreadError, "release unlocked lock");
Guido van Rossum1984f1e1992-08-04 12:41:02 +000074 return NULL;
75 }
76
Guido van Rossum65d5b571998-12-21 19:32:43 +000077 PyThread_release_lock(self->lock_lock);
Barry Warsawd0c10421996-12-17 00:05:22 +000078 Py_INCREF(Py_None);
79 return Py_None;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000080}
81
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000082PyDoc_STRVAR(release_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +000083"release()\n\
Guido van Rossumf6694362006-03-10 02:28:35 +000084(release_lock() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +000085\n\
86Release the lock, allowing another thread that is blocked waiting for\n\
87the lock to acquire the lock. The lock must be in the locked state,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000088but it needn't be locked by the same thread that unlocks it.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +000089
Barry Warsawd0c10421996-12-17 00:05:22 +000090static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +000091lock_locked_lock(lockobject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000092{
Guido van Rossum65d5b571998-12-21 19:32:43 +000093 if (PyThread_acquire_lock(self->lock_lock, 0)) {
94 PyThread_release_lock(self->lock_lock);
Guido van Rossum8fdc75b2002-04-07 06:32:21 +000095 return PyBool_FromLong(0L);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000096 }
Guido van Rossum8fdc75b2002-04-07 06:32:21 +000097 return PyBool_FromLong(1L);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000098}
99
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000100PyDoc_STRVAR(locked_doc,
Guido van Rossum8fdc75b2002-04-07 06:32:21 +0000101"locked() -> bool\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000102(locked_lock() is an obsolete synonym)\n\
103\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000104Return whether the lock is in the locked state.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000105
Barry Warsawd0c10421996-12-17 00:05:22 +0000106static PyMethodDef lock_methods[] = {
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000107 {"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock,
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000108 METH_VARARGS, acquire_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000109 {"acquire", (PyCFunction)lock_PyThread_acquire_lock,
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000110 METH_VARARGS, acquire_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000111 {"release_lock", (PyCFunction)lock_PyThread_release_lock,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000112 METH_NOARGS, release_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000113 {"release", (PyCFunction)lock_PyThread_release_lock,
Neal Norwitz23584252002-03-25 21:05:50 +0000114 METH_NOARGS, release_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000115 {"locked_lock", (PyCFunction)lock_locked_lock,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000116 METH_NOARGS, locked_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000117 {"locked", (PyCFunction)lock_locked_lock,
Neal Norwitz23584252002-03-25 21:05:50 +0000118 METH_NOARGS, locked_doc},
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000119 {"__enter__", (PyCFunction)lock_PyThread_acquire_lock,
120 METH_VARARGS, acquire_doc},
Guido van Rossumf6694362006-03-10 02:28:35 +0000121 {"__exit__", (PyCFunction)lock_PyThread_release_lock,
122 METH_VARARGS, release_doc},
Benjamin Petersona7724e52009-05-24 23:13:32 +0000123 {NULL} /* sentinel */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000124};
125
Barry Warsawd0c10421996-12-17 00:05:22 +0000126static PyTypeObject Locktype = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000127 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000128 "thread.lock", /*tp_name*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000129 sizeof(lockobject), /*tp_size*/
130 0, /*tp_itemsize*/
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000131 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000132 (destructor)lock_dealloc, /*tp_dealloc*/
133 0, /*tp_print*/
Benjamin Petersona7724e52009-05-24 23:13:32 +0000134 0, /*tp_getattr*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000135 0, /*tp_setattr*/
136 0, /*tp_compare*/
137 0, /*tp_repr*/
Benjamin Petersona7724e52009-05-24 23:13:32 +0000138 0, /* tp_as_number */
139 0, /* tp_as_sequence */
140 0, /* tp_as_mapping */
141 0, /* tp_hash */
142 0, /* tp_call */
143 0, /* tp_str */
144 0, /* tp_getattro */
145 0, /* tp_setattro */
146 0, /* tp_as_buffer */
Gregory P. Smith4e63d542009-08-20 09:39:38 +0000147 Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
Benjamin Petersona7724e52009-05-24 23:13:32 +0000148 0, /* tp_doc */
149 0, /* tp_traverse */
150 0, /* tp_clear */
151 0, /* tp_richcompare */
Gregory P. Smith4e63d542009-08-20 09:39:38 +0000152 offsetof(lockobject, in_weakreflist), /* tp_weaklistoffset */
Benjamin Petersona7724e52009-05-24 23:13:32 +0000153 0, /* tp_iter */
154 0, /* tp_iternext */
155 lock_methods, /* tp_methods */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000156};
157
Anthony Baxter5576b542006-04-12 04:08:46 +0000158static lockobject *
159newlockobject(void)
160{
161 lockobject *self;
162 self = PyObject_New(lockobject, &Locktype);
163 if (self == NULL)
164 return NULL;
165 self->lock_lock = PyThread_allocate_lock();
Gregory P. Smith4e63d542009-08-20 09:39:38 +0000166 self->in_weakreflist = NULL;
Anthony Baxter5576b542006-04-12 04:08:46 +0000167 if (self->lock_lock == NULL) {
168 PyObject_Del(self);
169 self = NULL;
170 PyErr_SetString(ThreadError, "can't allocate lock");
171 }
172 return self;
173}
174
Jim Fultond15dc062004-07-14 19:11:50 +0000175/* Thread-local objects */
176
177#include "structmember.h"
178
179typedef struct {
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000180 PyObject_HEAD
181 PyObject *key;
182 PyObject *args;
183 PyObject *kw;
184 PyObject *dict;
Jim Fultond15dc062004-07-14 19:11:50 +0000185} localobject;
186
Jim Fultond15dc062004-07-14 19:11:50 +0000187static PyObject *
188local_new(PyTypeObject *type, PyObject *args, PyObject *kw)
189{
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000190 localobject *self;
191 PyObject *tdict;
Jim Fultond15dc062004-07-14 19:11:50 +0000192
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000193 if (type->tp_init == PyBaseObject_Type.tp_init
194 && ((args && PyObject_IsTrue(args))
195 || (kw && PyObject_IsTrue(kw)))) {
196 PyErr_SetString(PyExc_TypeError,
197 "Initialization arguments are not supported");
198 return NULL;
199 }
Jim Fultond15dc062004-07-14 19:11:50 +0000200
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000201 self = (localobject *)type->tp_alloc(type, 0);
202 if (self == NULL)
203 return NULL;
Jim Fultond15dc062004-07-14 19:11:50 +0000204
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000205 Py_XINCREF(args);
206 self->args = args;
207 Py_XINCREF(kw);
208 self->kw = kw;
209 self->dict = NULL; /* making sure */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000210 self->key = PyString_FromFormat("thread.local.%p", self);
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000211 if (self->key == NULL)
212 goto err;
Jim Fultond15dc062004-07-14 19:11:50 +0000213
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000214 self->dict = PyDict_New();
215 if (self->dict == NULL)
216 goto err;
Jim Fultond15dc062004-07-14 19:11:50 +0000217
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000218 tdict = PyThreadState_GetDict();
219 if (tdict == NULL) {
220 PyErr_SetString(PyExc_SystemError,
221 "Couldn't get thread-state dictionary");
222 goto err;
223 }
Jim Fultond15dc062004-07-14 19:11:50 +0000224
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000225 if (PyDict_SetItem(tdict, self->key, self->dict) < 0)
226 goto err;
Jim Fultond15dc062004-07-14 19:11:50 +0000227
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000228 return (PyObject *)self;
229
230 err:
231 Py_DECREF(self);
232 return NULL;
Jim Fultond15dc062004-07-14 19:11:50 +0000233}
234
235static int
236local_traverse(localobject *self, visitproc visit, void *arg)
237{
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000238 Py_VISIT(self->args);
239 Py_VISIT(self->kw);
240 Py_VISIT(self->dict);
Jim Fultond15dc062004-07-14 19:11:50 +0000241 return 0;
242}
243
244static int
245local_clear(localobject *self)
246{
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000247 Py_CLEAR(self->args);
248 Py_CLEAR(self->kw);
249 Py_CLEAR(self->dict);
250 return 0;
Jim Fultond15dc062004-07-14 19:11:50 +0000251}
252
253static void
254local_dealloc(localobject *self)
255{
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000256 PyThreadState *tstate;
257 if (self->key
258 && (tstate = PyThreadState_Get())
259 && tstate->interp) {
260 for(tstate = PyInterpreterState_ThreadHead(tstate->interp);
261 tstate;
262 tstate = PyThreadState_Next(tstate))
263 if (tstate->dict &&
264 PyDict_GetItem(tstate->dict, self->key))
265 PyDict_DelItem(tstate->dict, self->key);
266 }
Jim Fultond15dc062004-07-14 19:11:50 +0000267
Philip Jenveydbf3b252009-09-29 04:32:44 +0000268 Py_XDECREF(self->key);
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000269 local_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000270 Py_TYPE(self)->tp_free((PyObject*)self);
Jim Fultond15dc062004-07-14 19:11:50 +0000271}
272
273static PyObject *
274_ldict(localobject *self)
275{
276 PyObject *tdict, *ldict;
277
278 tdict = PyThreadState_GetDict();
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000279 if (tdict == NULL) {
280 PyErr_SetString(PyExc_SystemError,
281 "Couldn't get thread-state dictionary");
282 return NULL;
283 }
Jim Fultond15dc062004-07-14 19:11:50 +0000284
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000285 ldict = PyDict_GetItem(tdict, self->key);
286 if (ldict == NULL) {
287 ldict = PyDict_New(); /* we own ldict */
Jim Fultond15dc062004-07-14 19:11:50 +0000288
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000289 if (ldict == NULL)
290 return NULL;
291 else {
292 int i = PyDict_SetItem(tdict, self->key, ldict);
Georg Brandlf3c4ad12006-03-08 12:24:33 +0000293 Py_DECREF(ldict); /* now ldict is borrowed */
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000294 if (i < 0)
295 return NULL;
296 }
Jim Fultond15dc062004-07-14 19:11:50 +0000297
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000298 Py_CLEAR(self->dict);
299 Py_INCREF(ldict);
300 self->dict = ldict; /* still borrowed */
Jim Fultond15dc062004-07-14 19:11:50 +0000301
Christian Heimese93237d2007-12-19 02:37:44 +0000302 if (Py_TYPE(self)->tp_init != PyBaseObject_Type.tp_init &&
303 Py_TYPE(self)->tp_init((PyObject*)self,
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000304 self->args, self->kw) < 0) {
305 /* we need to get rid of ldict from thread so
306 we create a new one the next time we do an attr
307 acces */
308 PyDict_DelItem(tdict, self->key);
309 return NULL;
310 }
311
312 }
Amaury Forgeot d'Arc1f40c8a2008-06-30 22:42:40 +0000313
314 /* The call to tp_init above may have caused another thread to run.
315 Install our ldict again. */
316 if (self->dict != ldict) {
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000317 Py_CLEAR(self->dict);
318 Py_INCREF(ldict);
319 self->dict = ldict;
320 }
Jim Fultond15dc062004-07-14 19:11:50 +0000321
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000322 return ldict;
Jim Fultond15dc062004-07-14 19:11:50 +0000323}
324
Jim Fultond15dc062004-07-14 19:11:50 +0000325static int
326local_setattro(localobject *self, PyObject *name, PyObject *v)
327{
328 PyObject *ldict;
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000329
330 ldict = _ldict(self);
331 if (ldict == NULL)
332 return -1;
Jim Fultond15dc062004-07-14 19:11:50 +0000333
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000334 return PyObject_GenericSetAttr((PyObject *)self, name, v);
Jim Fultond15dc062004-07-14 19:11:50 +0000335}
336
337static PyObject *
338local_getdict(localobject *self, void *closure)
339{
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000340 if (self->dict == NULL) {
341 PyErr_SetString(PyExc_AttributeError, "__dict__");
342 return NULL;
343 }
Jim Fultond15dc062004-07-14 19:11:50 +0000344
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000345 Py_INCREF(self->dict);
346 return self->dict;
Jim Fultond15dc062004-07-14 19:11:50 +0000347}
348
349static PyGetSetDef local_getset[] = {
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000350 {"__dict__", (getter)local_getdict, (setter)NULL,
351 "Local-data dictionary", NULL},
352 {NULL} /* Sentinel */
Jim Fultond15dc062004-07-14 19:11:50 +0000353};
354
Anthony Baxter5576b542006-04-12 04:08:46 +0000355static PyObject *local_getattro(localobject *, PyObject *);
356
Jim Fultond15dc062004-07-14 19:11:50 +0000357static PyTypeObject localtype = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000358 PyVarObject_HEAD_INIT(NULL, 0)
Jim Fultond15dc062004-07-14 19:11:50 +0000359 /* tp_name */ "thread._local",
360 /* tp_basicsize */ sizeof(localobject),
361 /* tp_itemsize */ 0,
362 /* tp_dealloc */ (destructor)local_dealloc,
Georg Brandld37ac692006-03-30 11:58:57 +0000363 /* tp_print */ 0,
364 /* tp_getattr */ 0,
365 /* tp_setattr */ 0,
366 /* tp_compare */ 0,
367 /* tp_repr */ 0,
Jim Fultond15dc062004-07-14 19:11:50 +0000368 /* tp_as_number */ 0,
369 /* tp_as_sequence */ 0,
370 /* tp_as_mapping */ 0,
Georg Brandld37ac692006-03-30 11:58:57 +0000371 /* tp_hash */ 0,
372 /* tp_call */ 0,
373 /* tp_str */ 0,
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000374 /* tp_getattro */ (getattrofunc)local_getattro,
375 /* tp_setattro */ (setattrofunc)local_setattro,
376 /* tp_as_buffer */ 0,
377 /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
Jim Fultond15dc062004-07-14 19:11:50 +0000378 /* tp_doc */ "Thread-local data",
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000379 /* tp_traverse */ (traverseproc)local_traverse,
380 /* tp_clear */ (inquiry)local_clear,
Georg Brandld37ac692006-03-30 11:58:57 +0000381 /* tp_richcompare */ 0,
382 /* tp_weaklistoffset */ 0,
383 /* tp_iter */ 0,
384 /* tp_iternext */ 0,
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000385 /* tp_methods */ 0,
386 /* tp_members */ 0,
387 /* tp_getset */ local_getset,
388 /* tp_base */ 0,
389 /* tp_dict */ 0, /* internal use */
Georg Brandld37ac692006-03-30 11:58:57 +0000390 /* tp_descr_get */ 0,
391 /* tp_descr_set */ 0,
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000392 /* tp_dictoffset */ offsetof(localobject, dict),
Georg Brandld37ac692006-03-30 11:58:57 +0000393 /* tp_init */ 0,
394 /* tp_alloc */ 0,
395 /* tp_new */ local_new,
Jim Fultond15dc062004-07-14 19:11:50 +0000396 /* tp_free */ 0, /* Low-level free-mem routine */
Georg Brandld37ac692006-03-30 11:58:57 +0000397 /* tp_is_gc */ 0, /* For PyObject_IS_GC */
Jim Fultond15dc062004-07-14 19:11:50 +0000398};
399
Anthony Baxter5576b542006-04-12 04:08:46 +0000400static PyObject *
401local_getattro(localobject *self, PyObject *name)
402{
403 PyObject *ldict, *value;
404
405 ldict = _ldict(self);
406 if (ldict == NULL)
407 return NULL;
408
Christian Heimese93237d2007-12-19 02:37:44 +0000409 if (Py_TYPE(self) != &localtype)
Anthony Baxter5576b542006-04-12 04:08:46 +0000410 /* use generic lookup for subtypes */
411 return PyObject_GenericGetAttr((PyObject *)self, name);
412
413 /* Optimization: just look in dict ourselves */
414 value = PyDict_GetItem(ldict, name);
415 if (value == NULL)
416 /* Fall back on generic to get __class__ and __dict__ */
417 return PyObject_GenericGetAttr((PyObject *)self, name);
418
419 Py_INCREF(value);
420 return value;
421}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000422
423/* Module functions */
424
Guido van Rossuma027efa1997-05-05 20:56:21 +0000425struct bootstate {
426 PyInterpreterState *interp;
427 PyObject *func;
428 PyObject *args;
429 PyObject *keyw;
430};
431
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000432static void
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000433t_bootstrap(void *boot_raw)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000434{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000435 struct bootstate *boot = (struct bootstate *) boot_raw;
Michael W. Hudson188d4362005-06-20 16:52:57 +0000436 PyThreadState *tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000437 PyObject *res;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000438
Michael W. Hudson188d4362005-06-20 16:52:57 +0000439 tstate = PyThreadState_New(boot->interp);
440
441 PyEval_AcquireThread(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000442 res = PyEval_CallObjectWithKeywords(
443 boot->func, boot->args, boot->keyw);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000444 if (res == NULL) {
Fred Drakebebc97f1998-05-28 04:35:12 +0000445 if (PyErr_ExceptionMatches(PyExc_SystemExit))
Barry Warsawd0c10421996-12-17 00:05:22 +0000446 PyErr_Clear();
Guido van Rossum385e7c61995-03-17 10:42:27 +0000447 else {
Guido van Rossum24ccca12003-04-29 19:44:05 +0000448 PyObject *file;
449 PySys_WriteStderr(
450 "Unhandled exception in thread started by ");
451 file = PySys_GetObject("stderr");
452 if (file)
453 PyFile_WriteObject(boot->func, file, 0);
454 else
455 PyObject_Print(boot->func, stderr, 0);
456 PySys_WriteStderr("\n");
Guido van Rossum40769dd1998-02-06 22:32:08 +0000457 PyErr_PrintEx(0);
Guido van Rossum385e7c61995-03-17 10:42:27 +0000458 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000459 }
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000460 else
Barry Warsawd0c10421996-12-17 00:05:22 +0000461 Py_DECREF(res);
Guido van Rossum24ccca12003-04-29 19:44:05 +0000462 Py_DECREF(boot->func);
463 Py_DECREF(boot->args);
464 Py_XDECREF(boot->keyw);
465 PyMem_DEL(boot_raw);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000466 PyThreadState_Clear(tstate);
467 PyThreadState_DeleteCurrent();
Guido van Rossumbcc20741998-08-04 22:53:56 +0000468 PyThread_exit_thread();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000469}
470
Barry Warsawd0c10421996-12-17 00:05:22 +0000471static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000472thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000473{
Guido van Rossum38d45b72000-09-01 20:47:58 +0000474 PyObject *func, *args, *keyw = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000475 struct bootstate *boot;
Guido van Rossum3c288632001-10-16 21:13:49 +0000476 long ident;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000477
Georg Brandl96a8c392006-05-29 21:04:52 +0000478 if (!PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3,
479 &func, &args, &keyw))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000480 return NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000481 if (!PyCallable_Check(func)) {
482 PyErr_SetString(PyExc_TypeError,
483 "first arg must be callable");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000484 return NULL;
485 }
Guido van Rossuma027efa1997-05-05 20:56:21 +0000486 if (!PyTuple_Check(args)) {
487 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38d45b72000-09-01 20:47:58 +0000488 "2nd arg must be a tuple");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000489 return NULL;
490 }
491 if (keyw != NULL && !PyDict_Check(keyw)) {
492 PyErr_SetString(PyExc_TypeError,
493 "optional 3rd arg must be a dictionary");
494 return NULL;
495 }
496 boot = PyMem_NEW(struct bootstate, 1);
497 if (boot == NULL)
498 return PyErr_NoMemory();
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000499 boot->interp = PyThreadState_GET()->interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000500 boot->func = func;
501 boot->args = args;
502 boot->keyw = keyw;
503 Py_INCREF(func);
504 Py_INCREF(args);
505 Py_XINCREF(keyw);
506 PyEval_InitThreads(); /* Start the interpreter's thread-awareness */
Guido van Rossum3c288632001-10-16 21:13:49 +0000507 ident = PyThread_start_new_thread(t_bootstrap, (void*) boot);
508 if (ident == -1) {
Guido van Rossum54c273c2005-02-20 03:02:16 +0000509 PyErr_SetString(ThreadError, "can't start new thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000510 Py_DECREF(func);
511 Py_DECREF(args);
512 Py_XDECREF(keyw);
513 PyMem_DEL(boot);
514 return NULL;
515 }
Guido van Rossum3c288632001-10-16 21:13:49 +0000516 return PyInt_FromLong(ident);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000517}
518
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000519PyDoc_STRVAR(start_new_doc,
Andrew M. Kuchling38300c62001-10-05 12:24:15 +0000520"start_new_thread(function, args[, kwargs])\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000521(start_new() is an obsolete synonym)\n\
522\n\
Guido van Rossum3c288632001-10-16 21:13:49 +0000523Start a new thread and return its identifier. The thread will call the\n\
524function with positional arguments from the tuple args and keyword arguments\n\
525taken from the optional dictionary kwargs. The thread exits when the\n\
526function returns; the return value is ignored. The thread will also exit\n\
527when the function raises an unhandled exception; a stack trace will be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000528printed unless the exception is SystemExit.\n");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000529
Barry Warsawd0c10421996-12-17 00:05:22 +0000530static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000531thread_PyThread_exit_thread(PyObject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000532{
Barry Warsawd0c10421996-12-17 00:05:22 +0000533 PyErr_SetNone(PyExc_SystemExit);
Guido van Rossum385e7c61995-03-17 10:42:27 +0000534 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000535}
536
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000537PyDoc_STRVAR(exit_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000538"exit()\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000539(PyThread_exit_thread() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000540\n\
541This is synonymous to ``raise SystemExit''. It will cause the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000542thread to exit silently unless the exception is caught.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000543
Kurt B. Kaisera11e8462003-06-13 21:59:45 +0000544static PyObject *
Kurt B. Kaisera1ad5f62003-06-16 18:51:28 +0000545thread_PyThread_interrupt_main(PyObject * self)
Kurt B. Kaisera11e8462003-06-13 21:59:45 +0000546{
547 PyErr_SetInterrupt();
548 Py_INCREF(Py_None);
549 return Py_None;
550}
551
552PyDoc_STRVAR(interrupt_doc,
553"interrupt_main()\n\
554\n\
555Raise a KeyboardInterrupt in the main thread.\n\
Kurt B. Kaisera1ad5f62003-06-16 18:51:28 +0000556A subthread can use this function to interrupt the main thread."
Kurt B. Kaisera11e8462003-06-13 21:59:45 +0000557);
558
Guido van Rossumb6775db1994-08-01 11:34:53 +0000559#ifndef NO_EXIT_PROG
Barry Warsawd0c10421996-12-17 00:05:22 +0000560static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000561thread_PyThread_exit_prog(PyObject *self, PyObject *args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000562{
563 int sts;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000564 if (!PyArg_ParseTuple(args, "i:exit_prog", &sts))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000565 return NULL;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000566 Py_Exit(sts); /* Calls PyThread_exit_prog(sts) or _PyThread_exit_prog(sts) */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000567 for (;;) { } /* Should not be reached */
568}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000569#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000570
Anthony Baxter5576b542006-04-12 04:08:46 +0000571static lockobject *newlockobject(void);
572
Barry Warsawd0c10421996-12-17 00:05:22 +0000573static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000574thread_PyThread_allocate_lock(PyObject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000575{
Barry Warsawd0c10421996-12-17 00:05:22 +0000576 return (PyObject *) newlockobject();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000577}
578
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000579PyDoc_STRVAR(allocate_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000580"allocate_lock() -> lock object\n\
581(allocate() is an obsolete synonym)\n\
582\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000583Create a new lock object. See LockType.__doc__ for information about locks.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000584
Barry Warsawd0c10421996-12-17 00:05:22 +0000585static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000586thread_get_ident(PyObject *self)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000587{
588 long ident;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000589 ident = PyThread_get_thread_ident();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000590 if (ident == -1) {
Barry Warsawd0c10421996-12-17 00:05:22 +0000591 PyErr_SetString(ThreadError, "no current thread ident");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000592 return NULL;
593 }
Barry Warsawd0c10421996-12-17 00:05:22 +0000594 return PyInt_FromLong(ident);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000595}
596
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000597PyDoc_STRVAR(get_ident_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000598"get_ident() -> integer\n\
599\n\
600Return a non-zero integer that uniquely identifies the current thread\n\
601amongst other threads that exist simultaneously.\n\
602This may be used to identify per-thread resources.\n\
603Even though on some platforms threads identities may appear to be\n\
604allocated consecutive numbers starting at 1, this behavior should not\n\
605be relied upon, and the number should be seen purely as a magic cookie.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000606A thread's identity may be reused for another thread after it exits.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000607
Andrew MacIntyre92913322006-06-13 15:04:24 +0000608static PyObject *
609thread_stack_size(PyObject *self, PyObject *args)
610{
611 size_t old_size;
612 Py_ssize_t new_size = 0;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000613 int rc;
614
615 if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size))
616 return NULL;
617
618 if (new_size < 0) {
619 PyErr_SetString(PyExc_ValueError,
620 "size must be 0 or a positive value");
621 return NULL;
622 }
623
624 old_size = PyThread_get_stacksize();
625
626 rc = PyThread_set_stacksize((size_t) new_size);
627 if (rc == -1) {
628 PyErr_Format(PyExc_ValueError,
629 "size not valid: %zd bytes",
630 new_size);
631 return NULL;
632 }
633 if (rc == -2) {
634 PyErr_SetString(ThreadError,
635 "setting stack size not supported");
636 return NULL;
637 }
638
639 return PyInt_FromSsize_t((Py_ssize_t) old_size);
640}
641
642PyDoc_STRVAR(stack_size_doc,
643"stack_size([size]) -> size\n\
644\n\
645Return the thread stack size used when creating new threads. The\n\
646optional size argument specifies the stack size (in bytes) to be used\n\
647for subsequently created threads, and must be 0 (use platform or\n\
648configured default) or a positive integer value of at least 32,768 (32k).\n\
649If changing the thread stack size is unsupported, a ThreadError\n\
650exception is raised. If the specified size is invalid, a ValueError\n\
651exception is raised, and the stack size is unmodified. 32k bytes\n\
652 currently the minimum supported stack size value to guarantee\n\
653sufficient stack space for the interpreter itself.\n\
654\n\
655Note that some platforms may have particular restrictions on values for\n\
656the stack size, such as requiring a minimum stack size larger than 32kB or\n\
657requiring allocation in multiples of the system memory page size\n\
658- platform documentation should be referred to for more information\n\
659(4kB pages are common; using multiples of 4096 for the stack size is\n\
660the suggested approach in the absence of more specific information).");
661
Barry Warsawd0c10421996-12-17 00:05:22 +0000662static PyMethodDef thread_methods[] = {
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000663 {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread,
664 METH_VARARGS,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000665 start_new_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000666 {"start_new", (PyCFunction)thread_PyThread_start_new_thread,
667 METH_VARARGS,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000668 start_new_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000669 {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000670 METH_NOARGS, allocate_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000671 {"allocate", (PyCFunction)thread_PyThread_allocate_lock,
Neal Norwitz46321172002-03-26 14:52:00 +0000672 METH_NOARGS, allocate_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000673 {"exit_thread", (PyCFunction)thread_PyThread_exit_thread,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000674 METH_NOARGS, exit_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000675 {"exit", (PyCFunction)thread_PyThread_exit_thread,
Neal Norwitz46321172002-03-26 14:52:00 +0000676 METH_NOARGS, exit_doc},
Kurt B. Kaisera1ad5f62003-06-16 18:51:28 +0000677 {"interrupt_main", (PyCFunction)thread_PyThread_interrupt_main,
Kurt B. Kaisera11e8462003-06-13 21:59:45 +0000678 METH_NOARGS, interrupt_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000679 {"get_ident", (PyCFunction)thread_get_ident,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000680 METH_NOARGS, get_ident_doc},
Andrew MacIntyre92913322006-06-13 15:04:24 +0000681 {"stack_size", (PyCFunction)thread_stack_size,
682 METH_VARARGS,
683 stack_size_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000684#ifndef NO_EXIT_PROG
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000685 {"exit_prog", (PyCFunction)thread_PyThread_exit_prog,
686 METH_VARARGS},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000687#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000688 {NULL, NULL} /* sentinel */
689};
690
691
692/* Initialization function */
693
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000694PyDoc_STRVAR(thread_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000695"This module provides primitive operations to write multi-threaded programs.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000696The 'threading' module provides a more convenient interface.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000697
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000698PyDoc_STRVAR(lock_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000699"A lock object is a synchronization primitive. To create a lock,\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000700call the PyThread_allocate_lock() function. Methods are:\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000701\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000702acquire() -- lock the lock, possibly blocking until it can be obtained\n\
703release() -- unlock of the lock\n\
704locked() -- test whether the lock is currently locked\n\
705\n\
706A lock is not owned by the thread that locked it; another thread may\n\
707unlock it. A thread attempting to lock a lock that it has already locked\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000708will block until another thread unlocks it. Deadlocks may ensue.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000709
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000710PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000711initthread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000712{
Barry Warsawd0c10421996-12-17 00:05:22 +0000713 PyObject *m, *d;
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000714
715 /* Initialize types: */
716 if (PyType_Ready(&localtype) < 0)
717 return;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000718
719 /* Create the module and add the functions */
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000720 m = Py_InitModule3("thread", thread_methods, thread_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000721 if (m == NULL)
722 return;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000723
724 /* Add a symbolic constant */
Barry Warsawd0c10421996-12-17 00:05:22 +0000725 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000726 ThreadError = PyErr_NewException("thread.error", NULL, NULL);
Barry Warsawd0c10421996-12-17 00:05:22 +0000727 PyDict_SetItemString(d, "error", ThreadError);
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000728 Locktype.tp_doc = lock_doc;
Benjamin Petersona7724e52009-05-24 23:13:32 +0000729 if (PyType_Ready(&Locktype) < 0)
730 return;
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000731 Py_INCREF(&Locktype);
732 PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000733
Michael W. Hudson64e08142005-06-15 12:25:20 +0000734 Py_INCREF(&localtype);
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000735 if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0)
736 return;
Jim Fultond15dc062004-07-14 19:11:50 +0000737
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000738 /* Initialize the C thread library */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000739 PyThread_init_thread();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000740}