blob: 1981ff6bb917c90d2a0c9e70b56ff90b9dd044e6 [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 Pitrou59c44f32009-10-30 17:07:08 +000017static long nb_threads = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000018
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);
Antoine Pitrou59c44f32009-10-30 17:07:08 +0000442 nb_threads++;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000443 res = PyEval_CallObjectWithKeywords(
444 boot->func, boot->args, boot->keyw);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000445 if (res == NULL) {
Fred Drakebebc97f1998-05-28 04:35:12 +0000446 if (PyErr_ExceptionMatches(PyExc_SystemExit))
Barry Warsawd0c10421996-12-17 00:05:22 +0000447 PyErr_Clear();
Guido van Rossum385e7c61995-03-17 10:42:27 +0000448 else {
Guido van Rossum24ccca12003-04-29 19:44:05 +0000449 PyObject *file;
450 PySys_WriteStderr(
451 "Unhandled exception in thread started by ");
452 file = PySys_GetObject("stderr");
453 if (file)
454 PyFile_WriteObject(boot->func, file, 0);
455 else
456 PyObject_Print(boot->func, stderr, 0);
457 PySys_WriteStderr("\n");
Guido van Rossum40769dd1998-02-06 22:32:08 +0000458 PyErr_PrintEx(0);
Guido van Rossum385e7c61995-03-17 10:42:27 +0000459 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000460 }
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000461 else
Barry Warsawd0c10421996-12-17 00:05:22 +0000462 Py_DECREF(res);
Guido van Rossum24ccca12003-04-29 19:44:05 +0000463 Py_DECREF(boot->func);
464 Py_DECREF(boot->args);
465 Py_XDECREF(boot->keyw);
466 PyMem_DEL(boot_raw);
Antoine Pitrou59c44f32009-10-30 17:07:08 +0000467 nb_threads--;
Michael W. Hudson188d4362005-06-20 16:52:57 +0000468 PyThreadState_Clear(tstate);
469 PyThreadState_DeleteCurrent();
Guido van Rossumbcc20741998-08-04 22:53:56 +0000470 PyThread_exit_thread();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000471}
472
Barry Warsawd0c10421996-12-17 00:05:22 +0000473static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000474thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000475{
Guido van Rossum38d45b72000-09-01 20:47:58 +0000476 PyObject *func, *args, *keyw = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000477 struct bootstate *boot;
Guido van Rossum3c288632001-10-16 21:13:49 +0000478 long ident;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000479
Georg Brandl96a8c392006-05-29 21:04:52 +0000480 if (!PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3,
481 &func, &args, &keyw))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000482 return NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000483 if (!PyCallable_Check(func)) {
484 PyErr_SetString(PyExc_TypeError,
485 "first arg must be callable");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000486 return NULL;
487 }
Guido van Rossuma027efa1997-05-05 20:56:21 +0000488 if (!PyTuple_Check(args)) {
489 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38d45b72000-09-01 20:47:58 +0000490 "2nd arg must be a tuple");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000491 return NULL;
492 }
493 if (keyw != NULL && !PyDict_Check(keyw)) {
494 PyErr_SetString(PyExc_TypeError,
495 "optional 3rd arg must be a dictionary");
496 return NULL;
497 }
498 boot = PyMem_NEW(struct bootstate, 1);
499 if (boot == NULL)
500 return PyErr_NoMemory();
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000501 boot->interp = PyThreadState_GET()->interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000502 boot->func = func;
503 boot->args = args;
504 boot->keyw = keyw;
505 Py_INCREF(func);
506 Py_INCREF(args);
507 Py_XINCREF(keyw);
508 PyEval_InitThreads(); /* Start the interpreter's thread-awareness */
Guido van Rossum3c288632001-10-16 21:13:49 +0000509 ident = PyThread_start_new_thread(t_bootstrap, (void*) boot);
510 if (ident == -1) {
Guido van Rossum54c273c2005-02-20 03:02:16 +0000511 PyErr_SetString(ThreadError, "can't start new thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000512 Py_DECREF(func);
513 Py_DECREF(args);
514 Py_XDECREF(keyw);
515 PyMem_DEL(boot);
516 return NULL;
517 }
Guido van Rossum3c288632001-10-16 21:13:49 +0000518 return PyInt_FromLong(ident);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000519}
520
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000521PyDoc_STRVAR(start_new_doc,
Andrew M. Kuchling38300c62001-10-05 12:24:15 +0000522"start_new_thread(function, args[, kwargs])\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000523(start_new() is an obsolete synonym)\n\
524\n\
Guido van Rossum3c288632001-10-16 21:13:49 +0000525Start a new thread and return its identifier. The thread will call the\n\
526function with positional arguments from the tuple args and keyword arguments\n\
527taken from the optional dictionary kwargs. The thread exits when the\n\
528function returns; the return value is ignored. The thread will also exit\n\
529when the function raises an unhandled exception; a stack trace will be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000530printed unless the exception is SystemExit.\n");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000531
Barry Warsawd0c10421996-12-17 00:05:22 +0000532static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000533thread_PyThread_exit_thread(PyObject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000534{
Barry Warsawd0c10421996-12-17 00:05:22 +0000535 PyErr_SetNone(PyExc_SystemExit);
Guido van Rossum385e7c61995-03-17 10:42:27 +0000536 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000537}
538
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000539PyDoc_STRVAR(exit_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000540"exit()\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000541(PyThread_exit_thread() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000542\n\
543This is synonymous to ``raise SystemExit''. It will cause the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000544thread to exit silently unless the exception is caught.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000545
Kurt B. Kaisera11e8462003-06-13 21:59:45 +0000546static PyObject *
Kurt B. Kaisera1ad5f62003-06-16 18:51:28 +0000547thread_PyThread_interrupt_main(PyObject * self)
Kurt B. Kaisera11e8462003-06-13 21:59:45 +0000548{
549 PyErr_SetInterrupt();
550 Py_INCREF(Py_None);
551 return Py_None;
552}
553
554PyDoc_STRVAR(interrupt_doc,
555"interrupt_main()\n\
556\n\
557Raise a KeyboardInterrupt in the main thread.\n\
Kurt B. Kaisera1ad5f62003-06-16 18:51:28 +0000558A subthread can use this function to interrupt the main thread."
Kurt B. Kaisera11e8462003-06-13 21:59:45 +0000559);
560
Anthony Baxter5576b542006-04-12 04:08:46 +0000561static lockobject *newlockobject(void);
562
Barry Warsawd0c10421996-12-17 00:05:22 +0000563static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000564thread_PyThread_allocate_lock(PyObject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000565{
Barry Warsawd0c10421996-12-17 00:05:22 +0000566 return (PyObject *) newlockobject();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000567}
568
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000569PyDoc_STRVAR(allocate_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000570"allocate_lock() -> lock object\n\
571(allocate() is an obsolete synonym)\n\
572\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000573Create a new lock object. See LockType.__doc__ for information about locks.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000574
Barry Warsawd0c10421996-12-17 00:05:22 +0000575static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000576thread_get_ident(PyObject *self)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000577{
578 long ident;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000579 ident = PyThread_get_thread_ident();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000580 if (ident == -1) {
Barry Warsawd0c10421996-12-17 00:05:22 +0000581 PyErr_SetString(ThreadError, "no current thread ident");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000582 return NULL;
583 }
Barry Warsawd0c10421996-12-17 00:05:22 +0000584 return PyInt_FromLong(ident);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000585}
586
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000587PyDoc_STRVAR(get_ident_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000588"get_ident() -> integer\n\
589\n\
590Return a non-zero integer that uniquely identifies the current thread\n\
591amongst other threads that exist simultaneously.\n\
592This may be used to identify per-thread resources.\n\
593Even though on some platforms threads identities may appear to be\n\
594allocated consecutive numbers starting at 1, this behavior should not\n\
595be relied upon, and the number should be seen purely as a magic cookie.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000596A thread's identity may be reused for another thread after it exits.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000597
Andrew MacIntyre92913322006-06-13 15:04:24 +0000598static PyObject *
Antoine Pitrou59c44f32009-10-30 17:07:08 +0000599thread__count(PyObject *self)
600{
601 return PyInt_FromLong(nb_threads);
602}
603
604PyDoc_STRVAR(_count_doc,
605"_count() -> integer\n\
606\n\
Antoine Pitrou2c970a22009-10-30 22:19:09 +0000607\
608Return the number of currently running Python threads, excluding \n\
609the main thread. The returned number comprises all threads created\n\
610through `start_new_thread()` as well as `threading.Thread`, and not\n\
611yet finished.\n\
612\n\
613This function is meant for internal and specialized purposes only.\n\
614In most applications `threading.enumerate()` should be used instead.");
Antoine Pitrou59c44f32009-10-30 17:07:08 +0000615
616static PyObject *
Andrew MacIntyre92913322006-06-13 15:04:24 +0000617thread_stack_size(PyObject *self, PyObject *args)
618{
619 size_t old_size;
620 Py_ssize_t new_size = 0;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000621 int rc;
622
623 if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size))
624 return NULL;
625
626 if (new_size < 0) {
627 PyErr_SetString(PyExc_ValueError,
628 "size must be 0 or a positive value");
629 return NULL;
630 }
631
632 old_size = PyThread_get_stacksize();
633
634 rc = PyThread_set_stacksize((size_t) new_size);
635 if (rc == -1) {
636 PyErr_Format(PyExc_ValueError,
637 "size not valid: %zd bytes",
638 new_size);
639 return NULL;
640 }
641 if (rc == -2) {
642 PyErr_SetString(ThreadError,
643 "setting stack size not supported");
644 return NULL;
645 }
646
647 return PyInt_FromSsize_t((Py_ssize_t) old_size);
648}
649
650PyDoc_STRVAR(stack_size_doc,
651"stack_size([size]) -> size\n\
652\n\
653Return the thread stack size used when creating new threads. The\n\
654optional size argument specifies the stack size (in bytes) to be used\n\
655for subsequently created threads, and must be 0 (use platform or\n\
656configured default) or a positive integer value of at least 32,768 (32k).\n\
657If changing the thread stack size is unsupported, a ThreadError\n\
658exception is raised. If the specified size is invalid, a ValueError\n\
659exception is raised, and the stack size is unmodified. 32k bytes\n\
660 currently the minimum supported stack size value to guarantee\n\
661sufficient stack space for the interpreter itself.\n\
662\n\
663Note that some platforms may have particular restrictions on values for\n\
664the stack size, such as requiring a minimum stack size larger than 32kB or\n\
665requiring allocation in multiples of the system memory page size\n\
666- platform documentation should be referred to for more information\n\
667(4kB pages are common; using multiples of 4096 for the stack size is\n\
668the suggested approach in the absence of more specific information).");
669
Barry Warsawd0c10421996-12-17 00:05:22 +0000670static PyMethodDef thread_methods[] = {
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000671 {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread,
672 METH_VARARGS,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000673 start_new_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000674 {"start_new", (PyCFunction)thread_PyThread_start_new_thread,
675 METH_VARARGS,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000676 start_new_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000677 {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000678 METH_NOARGS, allocate_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000679 {"allocate", (PyCFunction)thread_PyThread_allocate_lock,
Neal Norwitz46321172002-03-26 14:52:00 +0000680 METH_NOARGS, allocate_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000681 {"exit_thread", (PyCFunction)thread_PyThread_exit_thread,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000682 METH_NOARGS, exit_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000683 {"exit", (PyCFunction)thread_PyThread_exit_thread,
Neal Norwitz46321172002-03-26 14:52:00 +0000684 METH_NOARGS, exit_doc},
Kurt B. Kaisera1ad5f62003-06-16 18:51:28 +0000685 {"interrupt_main", (PyCFunction)thread_PyThread_interrupt_main,
Kurt B. Kaisera11e8462003-06-13 21:59:45 +0000686 METH_NOARGS, interrupt_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000687 {"get_ident", (PyCFunction)thread_get_ident,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000688 METH_NOARGS, get_ident_doc},
Antoine Pitrou59c44f32009-10-30 17:07:08 +0000689 {"_count", (PyCFunction)thread__count,
690 METH_NOARGS, _count_doc},
Andrew MacIntyre92913322006-06-13 15:04:24 +0000691 {"stack_size", (PyCFunction)thread_stack_size,
692 METH_VARARGS,
693 stack_size_doc},
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000694 {NULL, NULL} /* sentinel */
695};
696
697
698/* Initialization function */
699
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000700PyDoc_STRVAR(thread_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000701"This module provides primitive operations to write multi-threaded programs.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000702The 'threading' module provides a more convenient interface.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000703
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000704PyDoc_STRVAR(lock_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000705"A lock object is a synchronization primitive. To create a lock,\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000706call the PyThread_allocate_lock() function. Methods are:\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000707\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000708acquire() -- lock the lock, possibly blocking until it can be obtained\n\
709release() -- unlock of the lock\n\
710locked() -- test whether the lock is currently locked\n\
711\n\
712A lock is not owned by the thread that locked it; another thread may\n\
713unlock it. A thread attempting to lock a lock that it has already locked\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000714will block until another thread unlocks it. Deadlocks may ensue.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000715
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000716PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000717initthread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000718{
Barry Warsawd0c10421996-12-17 00:05:22 +0000719 PyObject *m, *d;
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000720
721 /* Initialize types: */
722 if (PyType_Ready(&localtype) < 0)
723 return;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000724
725 /* Create the module and add the functions */
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000726 m = Py_InitModule3("thread", thread_methods, thread_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000727 if (m == NULL)
728 return;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000729
730 /* Add a symbolic constant */
Barry Warsawd0c10421996-12-17 00:05:22 +0000731 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000732 ThreadError = PyErr_NewException("thread.error", NULL, NULL);
Barry Warsawd0c10421996-12-17 00:05:22 +0000733 PyDict_SetItemString(d, "error", ThreadError);
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000734 Locktype.tp_doc = lock_doc;
Benjamin Petersona7724e52009-05-24 23:13:32 +0000735 if (PyType_Ready(&Locktype) < 0)
736 return;
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000737 Py_INCREF(&Locktype);
738 PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000739
Michael W. Hudson64e08142005-06-15 12:25:20 +0000740 Py_INCREF(&localtype);
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000741 if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0)
742 return;
Jim Fultond15dc062004-07-14 19:11:50 +0000743
Antoine Pitrou59c44f32009-10-30 17:07:08 +0000744 nb_threads = 0;
745
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000746 /* Initialize the C thread library */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000747 PyThread_init_thread();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000748}