blob: c9227c6d048bff24416fe590ca1fa52d809232b5 [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"
Guido van Rossum1984f1e1992-08-04 12:41:02 +00006
Guido van Rossumb6775db1994-08-01 11:34:53 +00007#ifndef WITH_THREAD
Guido van Rossuma027efa1997-05-05 20:56:21 +00008#error "Error! The rest of Python is not compiled with thread support."
Neal Norwitz884baa12002-09-05 21:31:04 +00009#error "Rerun configure, adding a --with-threads option."
Guido van Rossuma027efa1997-05-05 20:56:21 +000010#error "Then run `make clean' followed by `make'."
Guido van Rossumb6775db1994-08-01 11:34:53 +000011#endif
12
Guido van Rossum49b56061998-10-01 20:42:43 +000013#include "pythread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000014
Barry Warsawd0c10421996-12-17 00:05:22 +000015static PyObject *ThreadError;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000016
17
18/* Lock objects */
19
20typedef struct {
Barry Warsawd0c10421996-12-17 00:05:22 +000021 PyObject_HEAD
Guido van Rossum65d5b571998-12-21 19:32:43 +000022 PyThread_type_lock lock_lock;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000023} lockobject;
24
Guido van Rossum1984f1e1992-08-04 12:41:02 +000025static void
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +000026lock_dealloc(lockobject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000027{
28 /* Unlock the lock so it's safe to free it */
Guido van Rossum65d5b571998-12-21 19:32:43 +000029 PyThread_acquire_lock(self->lock_lock, 0);
30 PyThread_release_lock(self->lock_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000031
Guido van Rossum65d5b571998-12-21 19:32:43 +000032 PyThread_free_lock(self->lock_lock);
Guido van Rossumb18618d2000-05-03 23:44:39 +000033 PyObject_Del(self);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000034}
35
Barry Warsawd0c10421996-12-17 00:05:22 +000036static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +000037lock_PyThread_acquire_lock(lockobject *self, PyObject *args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000038{
Neal Norwitzba3a16c2002-03-31 15:27:00 +000039 int i = 1;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000040
Neal Norwitzba3a16c2002-03-31 15:27:00 +000041 if (!PyArg_ParseTuple(args, "|i:acquire", &i))
42 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000043
Barry Warsawd0c10421996-12-17 00:05:22 +000044 Py_BEGIN_ALLOW_THREADS
Guido van Rossum65d5b571998-12-21 19:32:43 +000045 i = PyThread_acquire_lock(self->lock_lock, i);
Barry Warsawd0c10421996-12-17 00:05:22 +000046 Py_END_ALLOW_THREADS
Guido van Rossum1984f1e1992-08-04 12:41:02 +000047
Andrew M. Kuchlinga43ece92005-06-02 17:07:11 +000048 return PyBool_FromLong((long)i);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000049}
50
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000051PyDoc_STRVAR(acquire_doc,
Guido van Rossum8fdc75b2002-04-07 06:32:21 +000052"acquire([wait]) -> None or bool\n\
Guido van Rossumf6694362006-03-10 02:28:35 +000053(acquire_lock() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +000054\n\
55Lock the lock. Without argument, this blocks if the lock is already\n\
56locked (even by the same thread), waiting for another thread to release\n\
Guido van Rossum8fdc75b2002-04-07 06:32:21 +000057the lock, and return None once the lock is acquired.\n\
58With an argument, this will only block if the argument is true,\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +000059and the return value reflects whether the lock is acquired.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000060The blocking operation is not interruptible.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +000061
Barry Warsawd0c10421996-12-17 00:05:22 +000062static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +000063lock_PyThread_release_lock(lockobject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000064{
Guido van Rossum1984f1e1992-08-04 12:41:02 +000065 /* Sanity check: the lock must be locked */
Guido van Rossum65d5b571998-12-21 19:32:43 +000066 if (PyThread_acquire_lock(self->lock_lock, 0)) {
67 PyThread_release_lock(self->lock_lock);
Barry Warsawd0c10421996-12-17 00:05:22 +000068 PyErr_SetString(ThreadError, "release unlocked lock");
Guido van Rossum1984f1e1992-08-04 12:41:02 +000069 return NULL;
70 }
71
Guido van Rossum65d5b571998-12-21 19:32:43 +000072 PyThread_release_lock(self->lock_lock);
Barry Warsawd0c10421996-12-17 00:05:22 +000073 Py_INCREF(Py_None);
74 return Py_None;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000075}
76
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000077PyDoc_STRVAR(release_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +000078"release()\n\
Guido van Rossumf6694362006-03-10 02:28:35 +000079(release_lock() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +000080\n\
81Release the lock, allowing another thread that is blocked waiting for\n\
82the lock to acquire the lock. The lock must be in the locked state,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000083but it needn't be locked by the same thread that unlocks it.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +000084
Barry Warsawd0c10421996-12-17 00:05:22 +000085static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +000086lock_locked_lock(lockobject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000087{
Guido van Rossum65d5b571998-12-21 19:32:43 +000088 if (PyThread_acquire_lock(self->lock_lock, 0)) {
89 PyThread_release_lock(self->lock_lock);
Guido van Rossum8fdc75b2002-04-07 06:32:21 +000090 return PyBool_FromLong(0L);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000091 }
Guido van Rossum8fdc75b2002-04-07 06:32:21 +000092 return PyBool_FromLong(1L);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000093}
94
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000095PyDoc_STRVAR(locked_doc,
Guido van Rossum8fdc75b2002-04-07 06:32:21 +000096"locked() -> bool\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +000097(locked_lock() is an obsolete synonym)\n\
98\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000099Return whether the lock is in the locked state.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000100
Barry Warsawd0c10421996-12-17 00:05:22 +0000101static PyMethodDef lock_methods[] = {
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000102 {"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock,
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000103 METH_VARARGS, acquire_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000104 {"acquire", (PyCFunction)lock_PyThread_acquire_lock,
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000105 METH_VARARGS, acquire_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000106 {"release_lock", (PyCFunction)lock_PyThread_release_lock,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000107 METH_NOARGS, release_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000108 {"release", (PyCFunction)lock_PyThread_release_lock,
Neal Norwitz23584252002-03-25 21:05:50 +0000109 METH_NOARGS, release_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000110 {"locked_lock", (PyCFunction)lock_locked_lock,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000111 METH_NOARGS, locked_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000112 {"locked", (PyCFunction)lock_locked_lock,
Neal Norwitz23584252002-03-25 21:05:50 +0000113 METH_NOARGS, locked_doc},
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000114 {"__enter__", (PyCFunction)lock_PyThread_acquire_lock,
115 METH_VARARGS, acquire_doc},
Guido van Rossumf6694362006-03-10 02:28:35 +0000116 {"__exit__", (PyCFunction)lock_PyThread_release_lock,
117 METH_VARARGS, release_doc},
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000118 {NULL, NULL} /* sentinel */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000119};
120
Barry Warsawd0c10421996-12-17 00:05:22 +0000121static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000122lock_getattr(lockobject *self, char *name)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000123{
Barry Warsawd0c10421996-12-17 00:05:22 +0000124 return Py_FindMethod(lock_methods, (PyObject *)self, name);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000125}
126
Barry Warsawd0c10421996-12-17 00:05:22 +0000127static PyTypeObject Locktype = {
128 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000129 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000130 "thread.lock", /*tp_name*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000131 sizeof(lockobject), /*tp_size*/
132 0, /*tp_itemsize*/
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000133 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000134 (destructor)lock_dealloc, /*tp_dealloc*/
135 0, /*tp_print*/
136 (getattrfunc)lock_getattr, /*tp_getattr*/
137 0, /*tp_setattr*/
138 0, /*tp_compare*/
139 0, /*tp_repr*/
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000140};
141
Anthony Baxter5576b542006-04-12 04:08:46 +0000142static lockobject *
143newlockobject(void)
144{
145 lockobject *self;
146 self = PyObject_New(lockobject, &Locktype);
147 if (self == NULL)
148 return NULL;
149 self->lock_lock = PyThread_allocate_lock();
150 if (self->lock_lock == NULL) {
151 PyObject_Del(self);
152 self = NULL;
153 PyErr_SetString(ThreadError, "can't allocate lock");
154 }
155 return self;
156}
157
Jim Fultond15dc062004-07-14 19:11:50 +0000158/* Thread-local objects */
159
160#include "structmember.h"
161
162typedef struct {
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000163 PyObject_HEAD
164 PyObject *key;
165 PyObject *args;
166 PyObject *kw;
167 PyObject *dict;
Jim Fultond15dc062004-07-14 19:11:50 +0000168} localobject;
169
Jim Fultond15dc062004-07-14 19:11:50 +0000170static PyObject *
171local_new(PyTypeObject *type, PyObject *args, PyObject *kw)
172{
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000173 localobject *self;
174 PyObject *tdict;
Jim Fultond15dc062004-07-14 19:11:50 +0000175
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000176 if (type->tp_init == PyBaseObject_Type.tp_init
177 && ((args && PyObject_IsTrue(args))
178 || (kw && PyObject_IsTrue(kw)))) {
179 PyErr_SetString(PyExc_TypeError,
180 "Initialization arguments are not supported");
181 return NULL;
182 }
Jim Fultond15dc062004-07-14 19:11:50 +0000183
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000184 self = (localobject *)type->tp_alloc(type, 0);
185 if (self == NULL)
186 return NULL;
Jim Fultond15dc062004-07-14 19:11:50 +0000187
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000188 Py_XINCREF(args);
189 self->args = args;
190 Py_XINCREF(kw);
191 self->kw = kw;
192 self->dict = NULL; /* making sure */
193 self->key = PyString_FromFormat("thread.local.%p", self);
194 if (self->key == NULL)
195 goto err;
Jim Fultond15dc062004-07-14 19:11:50 +0000196
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000197 self->dict = PyDict_New();
198 if (self->dict == NULL)
199 goto err;
Jim Fultond15dc062004-07-14 19:11:50 +0000200
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000201 tdict = PyThreadState_GetDict();
202 if (tdict == NULL) {
203 PyErr_SetString(PyExc_SystemError,
204 "Couldn't get thread-state dictionary");
205 goto err;
206 }
Jim Fultond15dc062004-07-14 19:11:50 +0000207
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000208 if (PyDict_SetItem(tdict, self->key, self->dict) < 0)
209 goto err;
Jim Fultond15dc062004-07-14 19:11:50 +0000210
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000211 return (PyObject *)self;
212
213 err:
214 Py_DECREF(self);
215 return NULL;
Jim Fultond15dc062004-07-14 19:11:50 +0000216}
217
218static int
219local_traverse(localobject *self, visitproc visit, void *arg)
220{
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000221 Py_VISIT(self->args);
222 Py_VISIT(self->kw);
223 Py_VISIT(self->dict);
Jim Fultond15dc062004-07-14 19:11:50 +0000224 return 0;
225}
226
227static int
228local_clear(localobject *self)
229{
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000230 Py_CLEAR(self->key);
231 Py_CLEAR(self->args);
232 Py_CLEAR(self->kw);
233 Py_CLEAR(self->dict);
234 return 0;
Jim Fultond15dc062004-07-14 19:11:50 +0000235}
236
237static void
238local_dealloc(localobject *self)
239{
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000240 PyThreadState *tstate;
241 if (self->key
242 && (tstate = PyThreadState_Get())
243 && tstate->interp) {
244 for(tstate = PyInterpreterState_ThreadHead(tstate->interp);
245 tstate;
246 tstate = PyThreadState_Next(tstate))
247 if (tstate->dict &&
248 PyDict_GetItem(tstate->dict, self->key))
249 PyDict_DelItem(tstate->dict, self->key);
250 }
Jim Fultond15dc062004-07-14 19:11:50 +0000251
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000252 local_clear(self);
253 self->ob_type->tp_free((PyObject*)self);
Jim Fultond15dc062004-07-14 19:11:50 +0000254}
255
256static PyObject *
257_ldict(localobject *self)
258{
259 PyObject *tdict, *ldict;
260
261 tdict = PyThreadState_GetDict();
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000262 if (tdict == NULL) {
263 PyErr_SetString(PyExc_SystemError,
264 "Couldn't get thread-state dictionary");
265 return NULL;
266 }
Jim Fultond15dc062004-07-14 19:11:50 +0000267
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000268 ldict = PyDict_GetItem(tdict, self->key);
269 if (ldict == NULL) {
270 ldict = PyDict_New(); /* we own ldict */
Jim Fultond15dc062004-07-14 19:11:50 +0000271
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000272 if (ldict == NULL)
273 return NULL;
274 else {
275 int i = PyDict_SetItem(tdict, self->key, ldict);
Georg Brandlf3c4ad12006-03-08 12:24:33 +0000276 Py_DECREF(ldict); /* now ldict is borrowed */
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000277 if (i < 0)
278 return NULL;
279 }
Jim Fultond15dc062004-07-14 19:11:50 +0000280
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000281 Py_CLEAR(self->dict);
282 Py_INCREF(ldict);
283 self->dict = ldict; /* still borrowed */
Jim Fultond15dc062004-07-14 19:11:50 +0000284
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000285 if (self->ob_type->tp_init != PyBaseObject_Type.tp_init &&
286 self->ob_type->tp_init((PyObject*)self,
287 self->args, self->kw) < 0) {
288 /* we need to get rid of ldict from thread so
289 we create a new one the next time we do an attr
290 acces */
291 PyDict_DelItem(tdict, self->key);
292 return NULL;
293 }
294
295 }
296 else if (self->dict != ldict) {
297 Py_CLEAR(self->dict);
298 Py_INCREF(ldict);
299 self->dict = ldict;
300 }
Jim Fultond15dc062004-07-14 19:11:50 +0000301
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000302 return ldict;
Jim Fultond15dc062004-07-14 19:11:50 +0000303}
304
Jim Fultond15dc062004-07-14 19:11:50 +0000305static int
306local_setattro(localobject *self, PyObject *name, PyObject *v)
307{
308 PyObject *ldict;
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000309
310 ldict = _ldict(self);
311 if (ldict == NULL)
312 return -1;
Jim Fultond15dc062004-07-14 19:11:50 +0000313
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000314 return PyObject_GenericSetAttr((PyObject *)self, name, v);
Jim Fultond15dc062004-07-14 19:11:50 +0000315}
316
317static PyObject *
318local_getdict(localobject *self, void *closure)
319{
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000320 if (self->dict == NULL) {
321 PyErr_SetString(PyExc_AttributeError, "__dict__");
322 return NULL;
323 }
Jim Fultond15dc062004-07-14 19:11:50 +0000324
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000325 Py_INCREF(self->dict);
326 return self->dict;
Jim Fultond15dc062004-07-14 19:11:50 +0000327}
328
329static PyGetSetDef local_getset[] = {
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000330 {"__dict__", (getter)local_getdict, (setter)NULL,
331 "Local-data dictionary", NULL},
332 {NULL} /* Sentinel */
Jim Fultond15dc062004-07-14 19:11:50 +0000333};
334
Anthony Baxter5576b542006-04-12 04:08:46 +0000335static PyObject *local_getattro(localobject *, PyObject *);
336
Jim Fultond15dc062004-07-14 19:11:50 +0000337static PyTypeObject localtype = {
338 PyObject_HEAD_INIT(NULL)
339 /* ob_size */ 0,
340 /* tp_name */ "thread._local",
341 /* tp_basicsize */ sizeof(localobject),
342 /* tp_itemsize */ 0,
343 /* tp_dealloc */ (destructor)local_dealloc,
Georg Brandld37ac692006-03-30 11:58:57 +0000344 /* tp_print */ 0,
345 /* tp_getattr */ 0,
346 /* tp_setattr */ 0,
347 /* tp_compare */ 0,
348 /* tp_repr */ 0,
Jim Fultond15dc062004-07-14 19:11:50 +0000349 /* tp_as_number */ 0,
350 /* tp_as_sequence */ 0,
351 /* tp_as_mapping */ 0,
Georg Brandld37ac692006-03-30 11:58:57 +0000352 /* tp_hash */ 0,
353 /* tp_call */ 0,
354 /* tp_str */ 0,
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000355 /* tp_getattro */ (getattrofunc)local_getattro,
356 /* tp_setattro */ (setattrofunc)local_setattro,
357 /* tp_as_buffer */ 0,
358 /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
Jim Fultond15dc062004-07-14 19:11:50 +0000359 /* tp_doc */ "Thread-local data",
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000360 /* tp_traverse */ (traverseproc)local_traverse,
361 /* tp_clear */ (inquiry)local_clear,
Georg Brandld37ac692006-03-30 11:58:57 +0000362 /* tp_richcompare */ 0,
363 /* tp_weaklistoffset */ 0,
364 /* tp_iter */ 0,
365 /* tp_iternext */ 0,
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000366 /* tp_methods */ 0,
367 /* tp_members */ 0,
368 /* tp_getset */ local_getset,
369 /* tp_base */ 0,
370 /* tp_dict */ 0, /* internal use */
Georg Brandld37ac692006-03-30 11:58:57 +0000371 /* tp_descr_get */ 0,
372 /* tp_descr_set */ 0,
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000373 /* tp_dictoffset */ offsetof(localobject, dict),
Georg Brandld37ac692006-03-30 11:58:57 +0000374 /* tp_init */ 0,
375 /* tp_alloc */ 0,
376 /* tp_new */ local_new,
Jim Fultond15dc062004-07-14 19:11:50 +0000377 /* tp_free */ 0, /* Low-level free-mem routine */
Georg Brandld37ac692006-03-30 11:58:57 +0000378 /* tp_is_gc */ 0, /* For PyObject_IS_GC */
Jim Fultond15dc062004-07-14 19:11:50 +0000379};
380
Anthony Baxter5576b542006-04-12 04:08:46 +0000381static PyObject *
382local_getattro(localobject *self, PyObject *name)
383{
384 PyObject *ldict, *value;
385
386 ldict = _ldict(self);
387 if (ldict == NULL)
388 return NULL;
389
390 if (self->ob_type != &localtype)
391 /* use generic lookup for subtypes */
392 return PyObject_GenericGetAttr((PyObject *)self, name);
393
394 /* Optimization: just look in dict ourselves */
395 value = PyDict_GetItem(ldict, name);
396 if (value == NULL)
397 /* Fall back on generic to get __class__ and __dict__ */
398 return PyObject_GenericGetAttr((PyObject *)self, name);
399
400 Py_INCREF(value);
401 return value;
402}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000403
404/* Module functions */
405
Guido van Rossuma027efa1997-05-05 20:56:21 +0000406struct bootstate {
407 PyInterpreterState *interp;
408 PyObject *func;
409 PyObject *args;
410 PyObject *keyw;
411};
412
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000413static void
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000414t_bootstrap(void *boot_raw)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000415{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000416 struct bootstate *boot = (struct bootstate *) boot_raw;
Michael W. Hudson188d4362005-06-20 16:52:57 +0000417 PyThreadState *tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000418 PyObject *res;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000419
Michael W. Hudson188d4362005-06-20 16:52:57 +0000420 tstate = PyThreadState_New(boot->interp);
421
422 PyEval_AcquireThread(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000423 res = PyEval_CallObjectWithKeywords(
424 boot->func, boot->args, boot->keyw);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000425 if (res == NULL) {
Fred Drakebebc97f1998-05-28 04:35:12 +0000426 if (PyErr_ExceptionMatches(PyExc_SystemExit))
Barry Warsawd0c10421996-12-17 00:05:22 +0000427 PyErr_Clear();
Guido van Rossum385e7c61995-03-17 10:42:27 +0000428 else {
Guido van Rossum24ccca12003-04-29 19:44:05 +0000429 PyObject *file;
430 PySys_WriteStderr(
431 "Unhandled exception in thread started by ");
432 file = PySys_GetObject("stderr");
433 if (file)
434 PyFile_WriteObject(boot->func, file, 0);
435 else
436 PyObject_Print(boot->func, stderr, 0);
437 PySys_WriteStderr("\n");
Guido van Rossum40769dd1998-02-06 22:32:08 +0000438 PyErr_PrintEx(0);
Guido van Rossum385e7c61995-03-17 10:42:27 +0000439 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000440 }
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000441 else
Barry Warsawd0c10421996-12-17 00:05:22 +0000442 Py_DECREF(res);
Guido van Rossum24ccca12003-04-29 19:44:05 +0000443 Py_DECREF(boot->func);
444 Py_DECREF(boot->args);
445 Py_XDECREF(boot->keyw);
446 PyMem_DEL(boot_raw);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000447 PyThreadState_Clear(tstate);
448 PyThreadState_DeleteCurrent();
Guido van Rossumbcc20741998-08-04 22:53:56 +0000449 PyThread_exit_thread();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000450}
451
Barry Warsawd0c10421996-12-17 00:05:22 +0000452static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000453thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000454{
Guido van Rossum38d45b72000-09-01 20:47:58 +0000455 PyObject *func, *args, *keyw = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000456 struct bootstate *boot;
Guido van Rossum3c288632001-10-16 21:13:49 +0000457 long ident;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000458
Georg Brandl96a8c392006-05-29 21:04:52 +0000459 if (!PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3,
460 &func, &args, &keyw))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000461 return NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000462 if (!PyCallable_Check(func)) {
463 PyErr_SetString(PyExc_TypeError,
464 "first arg must be callable");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000465 return NULL;
466 }
Guido van Rossuma027efa1997-05-05 20:56:21 +0000467 if (!PyTuple_Check(args)) {
468 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38d45b72000-09-01 20:47:58 +0000469 "2nd arg must be a tuple");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000470 return NULL;
471 }
472 if (keyw != NULL && !PyDict_Check(keyw)) {
473 PyErr_SetString(PyExc_TypeError,
474 "optional 3rd arg must be a dictionary");
475 return NULL;
476 }
477 boot = PyMem_NEW(struct bootstate, 1);
478 if (boot == NULL)
479 return PyErr_NoMemory();
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000480 boot->interp = PyThreadState_GET()->interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000481 boot->func = func;
482 boot->args = args;
483 boot->keyw = keyw;
484 Py_INCREF(func);
485 Py_INCREF(args);
486 Py_XINCREF(keyw);
487 PyEval_InitThreads(); /* Start the interpreter's thread-awareness */
Guido van Rossum3c288632001-10-16 21:13:49 +0000488 ident = PyThread_start_new_thread(t_bootstrap, (void*) boot);
489 if (ident == -1) {
Guido van Rossum54c273c2005-02-20 03:02:16 +0000490 PyErr_SetString(ThreadError, "can't start new thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000491 Py_DECREF(func);
492 Py_DECREF(args);
493 Py_XDECREF(keyw);
494 PyMem_DEL(boot);
495 return NULL;
496 }
Guido van Rossum3c288632001-10-16 21:13:49 +0000497 return PyInt_FromLong(ident);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000498}
499
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000500PyDoc_STRVAR(start_new_doc,
Andrew M. Kuchling38300c62001-10-05 12:24:15 +0000501"start_new_thread(function, args[, kwargs])\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000502(start_new() is an obsolete synonym)\n\
503\n\
Guido van Rossum3c288632001-10-16 21:13:49 +0000504Start a new thread and return its identifier. The thread will call the\n\
505function with positional arguments from the tuple args and keyword arguments\n\
506taken from the optional dictionary kwargs. The thread exits when the\n\
507function returns; the return value is ignored. The thread will also exit\n\
508when the function raises an unhandled exception; a stack trace will be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000509printed unless the exception is SystemExit.\n");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000510
Barry Warsawd0c10421996-12-17 00:05:22 +0000511static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000512thread_PyThread_exit_thread(PyObject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000513{
Barry Warsawd0c10421996-12-17 00:05:22 +0000514 PyErr_SetNone(PyExc_SystemExit);
Guido van Rossum385e7c61995-03-17 10:42:27 +0000515 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000516}
517
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000518PyDoc_STRVAR(exit_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000519"exit()\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000520(PyThread_exit_thread() is an obsolete synonym)\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000521\n\
522This is synonymous to ``raise SystemExit''. It will cause the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000523thread to exit silently unless the exception is caught.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000524
Kurt B. Kaisera11e8462003-06-13 21:59:45 +0000525static PyObject *
Kurt B. Kaisera1ad5f62003-06-16 18:51:28 +0000526thread_PyThread_interrupt_main(PyObject * self)
Kurt B. Kaisera11e8462003-06-13 21:59:45 +0000527{
528 PyErr_SetInterrupt();
529 Py_INCREF(Py_None);
530 return Py_None;
531}
532
533PyDoc_STRVAR(interrupt_doc,
534"interrupt_main()\n\
535\n\
536Raise a KeyboardInterrupt in the main thread.\n\
Kurt B. Kaisera1ad5f62003-06-16 18:51:28 +0000537A subthread can use this function to interrupt the main thread."
Kurt B. Kaisera11e8462003-06-13 21:59:45 +0000538);
539
Guido van Rossumb6775db1994-08-01 11:34:53 +0000540#ifndef NO_EXIT_PROG
Barry Warsawd0c10421996-12-17 00:05:22 +0000541static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000542thread_PyThread_exit_prog(PyObject *self, PyObject *args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000543{
544 int sts;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000545 if (!PyArg_ParseTuple(args, "i:exit_prog", &sts))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000546 return NULL;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000547 Py_Exit(sts); /* Calls PyThread_exit_prog(sts) or _PyThread_exit_prog(sts) */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000548 for (;;) { } /* Should not be reached */
549}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000550#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000551
Anthony Baxter5576b542006-04-12 04:08:46 +0000552static lockobject *newlockobject(void);
553
Barry Warsawd0c10421996-12-17 00:05:22 +0000554static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000555thread_PyThread_allocate_lock(PyObject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000556{
Barry Warsawd0c10421996-12-17 00:05:22 +0000557 return (PyObject *) newlockobject();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000558}
559
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000560PyDoc_STRVAR(allocate_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000561"allocate_lock() -> lock object\n\
562(allocate() is an obsolete synonym)\n\
563\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000564Create a new lock object. See LockType.__doc__ for information about locks.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000565
Barry Warsawd0c10421996-12-17 00:05:22 +0000566static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000567thread_get_ident(PyObject *self)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000568{
569 long ident;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000570 ident = PyThread_get_thread_ident();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000571 if (ident == -1) {
Barry Warsawd0c10421996-12-17 00:05:22 +0000572 PyErr_SetString(ThreadError, "no current thread ident");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000573 return NULL;
574 }
Barry Warsawd0c10421996-12-17 00:05:22 +0000575 return PyInt_FromLong(ident);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000576}
577
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000578PyDoc_STRVAR(get_ident_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000579"get_ident() -> integer\n\
580\n\
581Return a non-zero integer that uniquely identifies the current thread\n\
582amongst other threads that exist simultaneously.\n\
583This may be used to identify per-thread resources.\n\
584Even though on some platforms threads identities may appear to be\n\
585allocated consecutive numbers starting at 1, this behavior should not\n\
586be relied upon, and the number should be seen purely as a magic cookie.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000587A thread's identity may be reused for another thread after it exits.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000588
Andrew MacIntyre6539d2d2006-06-04 12:31:09 +0000589static PyObject *
590thread_stack_size(PyObject *self, PyObject *args)
591{
592 size_t old_size, new_size;
593 PyObject *set_size = NULL;
594
595 if (!PyArg_UnpackTuple(args, "stack_size", 0, 1, &set_size))
596 return NULL;
597
598 old_size = PyThread_get_stacksize();
599
600 if (set_size != NULL) {
601 if (PyInt_Check(set_size))
602 new_size = (size_t) PyInt_AsLong(set_size);
603 else {
604 PyErr_SetString(PyExc_TypeError,
605 "size must be an integer");
606 return NULL;
607 }
608 if (PyThread_set_stacksize(new_size))
609 return NULL;
610 }
611
612 return PyInt_FromLong((long) old_size);
613}
614
615PyDoc_STRVAR(stack_size_doc,
616"stack_size([size]) -> size\n\
617\n\
618Return the thread stack size used when creating new threads. The\n\
619optional size argument specifies the stack size (in bytes) to be used\n\
620for subsequently created threads, and must be 0 (use platform or\n\
621configured default) or a positive integer value of at least 32,768 (32kB).\n\
622If changing the thread stack size is unsupported, or the specified size\n\
623is invalid, a RuntimeWarning is issued and the stack size is unmodified.\n\
62432kB is currently the minimum supported stack size value, to guarantee\n\
625sufficient stack space for the interpreter itself.\n\
626\n\
627Note that some platforms may have particular restrictions on values for\n\
628the stack size, such as requiring allocation in multiples of the system\n\
629memory page size - platform documentation should be referred to for more\n\
630information (4kB pages are common; using multiples of 4096 for the\n\
631stack size is the suggested approach in the absence of more specific\n\
632information).");
633
Barry Warsawd0c10421996-12-17 00:05:22 +0000634static PyMethodDef thread_methods[] = {
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000635 {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread,
636 METH_VARARGS,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000637 start_new_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000638 {"start_new", (PyCFunction)thread_PyThread_start_new_thread,
639 METH_VARARGS,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000640 start_new_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000641 {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000642 METH_NOARGS, allocate_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000643 {"allocate", (PyCFunction)thread_PyThread_allocate_lock,
Neal Norwitz46321172002-03-26 14:52:00 +0000644 METH_NOARGS, allocate_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000645 {"exit_thread", (PyCFunction)thread_PyThread_exit_thread,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000646 METH_NOARGS, exit_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000647 {"exit", (PyCFunction)thread_PyThread_exit_thread,
Neal Norwitz46321172002-03-26 14:52:00 +0000648 METH_NOARGS, exit_doc},
Kurt B. Kaisera1ad5f62003-06-16 18:51:28 +0000649 {"interrupt_main", (PyCFunction)thread_PyThread_interrupt_main,
Kurt B. Kaisera11e8462003-06-13 21:59:45 +0000650 METH_NOARGS, interrupt_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000651 {"get_ident", (PyCFunction)thread_get_ident,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000652 METH_NOARGS, get_ident_doc},
Andrew MacIntyre6539d2d2006-06-04 12:31:09 +0000653 {"stack_size", (PyCFunction)thread_stack_size,
654 METH_VARARGS,
655 stack_size_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000656#ifndef NO_EXIT_PROG
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000657 {"exit_prog", (PyCFunction)thread_PyThread_exit_prog,
658 METH_VARARGS},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000659#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000660 {NULL, NULL} /* sentinel */
661};
662
663
664/* Initialization function */
665
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000666PyDoc_STRVAR(thread_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000667"This module provides primitive operations to write multi-threaded programs.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000668The 'threading' module provides a more convenient interface.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000669
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000670PyDoc_STRVAR(lock_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000671"A lock object is a synchronization primitive. To create a lock,\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000672call the PyThread_allocate_lock() function. Methods are:\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000673\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000674acquire() -- lock the lock, possibly blocking until it can be obtained\n\
675release() -- unlock of the lock\n\
676locked() -- test whether the lock is currently locked\n\
677\n\
678A lock is not owned by the thread that locked it; another thread may\n\
679unlock it. A thread attempting to lock a lock that it has already locked\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000680will block until another thread unlocks it. Deadlocks may ensue.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000681
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000682PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000683initthread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000684{
Barry Warsawd0c10421996-12-17 00:05:22 +0000685 PyObject *m, *d;
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000686
687 /* Initialize types: */
688 if (PyType_Ready(&localtype) < 0)
689 return;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000690
691 /* Create the module and add the functions */
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000692 m = Py_InitModule3("thread", thread_methods, thread_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000693 if (m == NULL)
694 return;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000695
696 /* Add a symbolic constant */
Barry Warsawd0c10421996-12-17 00:05:22 +0000697 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000698 ThreadError = PyErr_NewException("thread.error", NULL, NULL);
Barry Warsawd0c10421996-12-17 00:05:22 +0000699 PyDict_SetItemString(d, "error", ThreadError);
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000700 Locktype.tp_doc = lock_doc;
701 Py_INCREF(&Locktype);
702 PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000703
Michael W. Hudson64e08142005-06-15 12:25:20 +0000704 Py_INCREF(&localtype);
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000705 if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0)
706 return;
Jim Fultond15dc062004-07-14 19:11:50 +0000707
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000708 /* Initialize the C thread library */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000709 PyThread_init_thread();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000710}