blob: c962c79f854659cdbe913cf6a7b8149eece733d1 [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
Guido van Rossumb6775db1994-08-01 11:34:53 +0000561#ifndef NO_EXIT_PROG
Barry Warsawd0c10421996-12-17 00:05:22 +0000562static PyObject *
Peter Schneider-Kamp3707efe2000-07-10 10:03:58 +0000563thread_PyThread_exit_prog(PyObject *self, PyObject *args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000564{
565 int sts;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000566 if (!PyArg_ParseTuple(args, "i:exit_prog", &sts))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000567 return NULL;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000568 Py_Exit(sts); /* Calls PyThread_exit_prog(sts) or _PyThread_exit_prog(sts) */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000569 for (;;) { } /* Should not be reached */
570}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000571#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000572
Anthony Baxter5576b542006-04-12 04:08:46 +0000573static lockobject *newlockobject(void);
574
Barry Warsawd0c10421996-12-17 00:05:22 +0000575static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000576thread_PyThread_allocate_lock(PyObject *self)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000577{
Barry Warsawd0c10421996-12-17 00:05:22 +0000578 return (PyObject *) newlockobject();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000579}
580
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000581PyDoc_STRVAR(allocate_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000582"allocate_lock() -> lock object\n\
583(allocate() is an obsolete synonym)\n\
584\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000585Create a new lock object. See LockType.__doc__ for information about locks.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000586
Barry Warsawd0c10421996-12-17 00:05:22 +0000587static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000588thread_get_ident(PyObject *self)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000589{
590 long ident;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000591 ident = PyThread_get_thread_ident();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000592 if (ident == -1) {
Barry Warsawd0c10421996-12-17 00:05:22 +0000593 PyErr_SetString(ThreadError, "no current thread ident");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000594 return NULL;
595 }
Barry Warsawd0c10421996-12-17 00:05:22 +0000596 return PyInt_FromLong(ident);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000597}
598
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000599PyDoc_STRVAR(get_ident_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000600"get_ident() -> integer\n\
601\n\
602Return a non-zero integer that uniquely identifies the current thread\n\
603amongst other threads that exist simultaneously.\n\
604This may be used to identify per-thread resources.\n\
605Even though on some platforms threads identities may appear to be\n\
606allocated consecutive numbers starting at 1, this behavior should not\n\
607be relied upon, and the number should be seen purely as a magic cookie.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000608A thread's identity may be reused for another thread after it exits.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000609
Andrew MacIntyre92913322006-06-13 15:04:24 +0000610static PyObject *
Antoine Pitrou59c44f32009-10-30 17:07:08 +0000611thread__count(PyObject *self)
612{
613 return PyInt_FromLong(nb_threads);
614}
615
616PyDoc_STRVAR(_count_doc,
617"_count() -> integer\n\
618\n\
619Return the number of currently running (sub)threads.\n\
620This excludes the main thread.");
621
622static PyObject *
Andrew MacIntyre92913322006-06-13 15:04:24 +0000623thread_stack_size(PyObject *self, PyObject *args)
624{
625 size_t old_size;
626 Py_ssize_t new_size = 0;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000627 int rc;
628
629 if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size))
630 return NULL;
631
632 if (new_size < 0) {
633 PyErr_SetString(PyExc_ValueError,
634 "size must be 0 or a positive value");
635 return NULL;
636 }
637
638 old_size = PyThread_get_stacksize();
639
640 rc = PyThread_set_stacksize((size_t) new_size);
641 if (rc == -1) {
642 PyErr_Format(PyExc_ValueError,
643 "size not valid: %zd bytes",
644 new_size);
645 return NULL;
646 }
647 if (rc == -2) {
648 PyErr_SetString(ThreadError,
649 "setting stack size not supported");
650 return NULL;
651 }
652
653 return PyInt_FromSsize_t((Py_ssize_t) old_size);
654}
655
656PyDoc_STRVAR(stack_size_doc,
657"stack_size([size]) -> size\n\
658\n\
659Return the thread stack size used when creating new threads. The\n\
660optional size argument specifies the stack size (in bytes) to be used\n\
661for subsequently created threads, and must be 0 (use platform or\n\
662configured default) or a positive integer value of at least 32,768 (32k).\n\
663If changing the thread stack size is unsupported, a ThreadError\n\
664exception is raised. If the specified size is invalid, a ValueError\n\
665exception is raised, and the stack size is unmodified. 32k bytes\n\
666 currently the minimum supported stack size value to guarantee\n\
667sufficient stack space for the interpreter itself.\n\
668\n\
669Note that some platforms may have particular restrictions on values for\n\
670the stack size, such as requiring a minimum stack size larger than 32kB or\n\
671requiring allocation in multiples of the system memory page size\n\
672- platform documentation should be referred to for more information\n\
673(4kB pages are common; using multiples of 4096 for the stack size is\n\
674the suggested approach in the absence of more specific information).");
675
Barry Warsawd0c10421996-12-17 00:05:22 +0000676static PyMethodDef thread_methods[] = {
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000677 {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread,
678 METH_VARARGS,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000679 start_new_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000680 {"start_new", (PyCFunction)thread_PyThread_start_new_thread,
681 METH_VARARGS,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000682 start_new_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000683 {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000684 METH_NOARGS, allocate_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000685 {"allocate", (PyCFunction)thread_PyThread_allocate_lock,
Neal Norwitz46321172002-03-26 14:52:00 +0000686 METH_NOARGS, allocate_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000687 {"exit_thread", (PyCFunction)thread_PyThread_exit_thread,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000688 METH_NOARGS, exit_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000689 {"exit", (PyCFunction)thread_PyThread_exit_thread,
Neal Norwitz46321172002-03-26 14:52:00 +0000690 METH_NOARGS, exit_doc},
Kurt B. Kaisera1ad5f62003-06-16 18:51:28 +0000691 {"interrupt_main", (PyCFunction)thread_PyThread_interrupt_main,
Kurt B. Kaisera11e8462003-06-13 21:59:45 +0000692 METH_NOARGS, interrupt_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000693 {"get_ident", (PyCFunction)thread_get_ident,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000694 METH_NOARGS, get_ident_doc},
Antoine Pitrou59c44f32009-10-30 17:07:08 +0000695 {"_count", (PyCFunction)thread__count,
696 METH_NOARGS, _count_doc},
Andrew MacIntyre92913322006-06-13 15:04:24 +0000697 {"stack_size", (PyCFunction)thread_stack_size,
698 METH_VARARGS,
699 stack_size_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000700#ifndef NO_EXIT_PROG
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000701 {"exit_prog", (PyCFunction)thread_PyThread_exit_prog,
702 METH_VARARGS},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000703#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000704 {NULL, NULL} /* sentinel */
705};
706
707
708/* Initialization function */
709
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000710PyDoc_STRVAR(thread_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000711"This module provides primitive operations to write multi-threaded programs.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000712The 'threading' module provides a more convenient interface.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000713
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000714PyDoc_STRVAR(lock_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000715"A lock object is a synchronization primitive. To create a lock,\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000716call the PyThread_allocate_lock() function. Methods are:\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000717\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000718acquire() -- lock the lock, possibly blocking until it can be obtained\n\
719release() -- unlock of the lock\n\
720locked() -- test whether the lock is currently locked\n\
721\n\
722A lock is not owned by the thread that locked it; another thread may\n\
723unlock it. A thread attempting to lock a lock that it has already locked\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000724will block until another thread unlocks it. Deadlocks may ensue.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000725
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000726PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000727initthread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000728{
Barry Warsawd0c10421996-12-17 00:05:22 +0000729 PyObject *m, *d;
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000730
731 /* Initialize types: */
732 if (PyType_Ready(&localtype) < 0)
733 return;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000734
735 /* Create the module and add the functions */
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000736 m = Py_InitModule3("thread", thread_methods, thread_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000737 if (m == NULL)
738 return;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000739
740 /* Add a symbolic constant */
Barry Warsawd0c10421996-12-17 00:05:22 +0000741 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000742 ThreadError = PyErr_NewException("thread.error", NULL, NULL);
Barry Warsawd0c10421996-12-17 00:05:22 +0000743 PyDict_SetItemString(d, "error", ThreadError);
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000744 Locktype.tp_doc = lock_doc;
Benjamin Petersona7724e52009-05-24 23:13:32 +0000745 if (PyType_Ready(&Locktype) < 0)
746 return;
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000747 Py_INCREF(&Locktype);
748 PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000749
Michael W. Hudson64e08142005-06-15 12:25:20 +0000750 Py_INCREF(&localtype);
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000751 if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0)
752 return;
Jim Fultond15dc062004-07-14 19:11:50 +0000753
Antoine Pitrou59c44f32009-10-30 17:07:08 +0000754 nb_threads = 0;
755
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000756 /* Initialize the C thread library */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000757 PyThread_init_thread();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000758}