blob: fe63c4b6f23f25ea0464b02b4abe49ffc8dd76b5 [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"
Benjamin Petersonbec4d572009-10-10 01:16:07 +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 Pitrou65c9c642009-10-30 17:25:12 +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;
Benjamin Petersonbec4d572009-10-10 01:16:07 +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{
Thomas Wouters89f507f2006-12-13 04:49:30 +000030 assert(self->lock_lock);
Benjamin Petersonbec4d572009-10-10 01:16:07 +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},
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000123 {NULL, 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öwis9f2e3462007-07-21 17:22:18 +0000127 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Georg Brandl0a7ac7d2008-05-26 10:29:35 +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*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000134 0, /*tp_getattr*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000135 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000136 0, /*tp_reserved*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000137 0, /*tp_repr*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +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*/
Benjamin Petersonfe51a1f2009-10-09 20:36:25 +0000147 Py_TPFLAGS_DEFAULT, /*tp_flags*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000148 0, /*tp_doc*/
149 0, /*tp_traverse*/
150 0, /*tp_clear*/
151 0, /*tp_richcompare*/
Benjamin Petersonbec4d572009-10-10 01:16:07 +0000152 offsetof(lockobject, in_weakreflist), /*tp_weaklistoffset*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000153 0, /*tp_iter*/
154 0, /*tp_iternext*/
155 lock_methods, /*tp_methods*/
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000156};
157
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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();
Benjamin Petersonbec4d572009-10-10 01:16:07 +0000166 self->in_weakreflist = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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 */
Walter Dörwald4ee631e2007-06-20 14:55:01 +0000210 self->key = PyUnicode_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 Jenvey26713ca2009-09-29 04:57:18 +0000268 Py_XDECREF(self->key);
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000269 local_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +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 Heimes90aa7642007-12-19 02:45:37 +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 }
Benjamin Peterson8a250ae2008-06-30 23:30:24 +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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000355static PyObject *local_getattro(localobject *, PyObject *);
356
Jim Fultond15dc062004-07-14 19:11:50 +0000357static PyTypeObject localtype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000358 PyVarObject_HEAD_INIT(NULL, 0)
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000359 /* tp_name */ "_thread._local",
Jim Fultond15dc062004-07-14 19:11:50 +0000360 /* tp_basicsize */ sizeof(localobject),
361 /* tp_itemsize */ 0,
362 /* tp_dealloc */ (destructor)local_dealloc,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000363 /* tp_print */ 0,
364 /* tp_getattr */ 0,
365 /* tp_setattr */ 0,
Mark Dickinsone94c6792009-02-02 20:36:42 +0000366 /* tp_reserved */ 0,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000367 /* 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,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000390 /* tp_descr_get */ 0,
391 /* tp_descr_set */ 0,
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000392 /* tp_dictoffset */ offsetof(localobject, dict),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000397 /* tp_is_gc */ 0, /* For PyObject_IS_GC */
Jim Fultond15dc062004-07-14 19:11:50 +0000398};
399
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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 Heimes90aa7642007-12-19 02:45:37 +0000409 if (Py_TYPE(self) != &localtype)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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 Pitrou65c9c642009-10-30 17:25:12 +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");
Christian Heimes2be03732007-11-15 02:26:46 +0000453 if (file != NULL && file != Py_None)
Guido van Rossum24ccca12003-04-29 19:44:05 +0000454 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 Pitrou65c9c642009-10-30 17:25:12 +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
Thomas Wouters4d70c3d2006-06-08 14:42:34 +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 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000518 return PyLong_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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000596 return PyLong_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
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000610static PyObject *
Antoine Pitrou65c9c642009-10-30 17:25:12 +0000611thread__count(PyObject *self)
612{
613 return PyLong_FromLong(nb_threads);
614}
615
616PyDoc_STRVAR(_count_doc,
617"_count() -> integer\n\
618\n\
Antoine Pitrou9257f5e2009-10-30 22:23:02 +0000619\
620Return the number of currently running Python threads, excluding \n\
621the main thread. The returned number comprises all threads created\n\
622through `start_new_thread()` as well as `threading.Thread`, and not\n\
623yet finished.\n\
624\n\
625This function is meant for internal and specialized purposes only.\n\
626In most applications `threading.enumerate()` should be used instead.");
Antoine Pitrou65c9c642009-10-30 17:25:12 +0000627
628static PyObject *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000629thread_stack_size(PyObject *self, PyObject *args)
630{
631 size_t old_size;
632 Py_ssize_t new_size = 0;
633 int rc;
634
635 if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size))
636 return NULL;
637
638 if (new_size < 0) {
639 PyErr_SetString(PyExc_ValueError,
640 "size must be 0 or a positive value");
641 return NULL;
642 }
643
644 old_size = PyThread_get_stacksize();
645
646 rc = PyThread_set_stacksize((size_t) new_size);
647 if (rc == -1) {
648 PyErr_Format(PyExc_ValueError,
649 "size not valid: %zd bytes",
650 new_size);
651 return NULL;
652 }
653 if (rc == -2) {
654 PyErr_SetString(ThreadError,
655 "setting stack size not supported");
656 return NULL;
657 }
658
Christian Heimes217cfd12007-12-02 14:31:20 +0000659 return PyLong_FromSsize_t((Py_ssize_t) old_size);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000660}
661
662PyDoc_STRVAR(stack_size_doc,
663"stack_size([size]) -> size\n\
664\n\
665Return the thread stack size used when creating new threads. The\n\
666optional size argument specifies the stack size (in bytes) to be used\n\
667for subsequently created threads, and must be 0 (use platform or\n\
668configured default) or a positive integer value of at least 32,768 (32k).\n\
669If changing the thread stack size is unsupported, a ThreadError\n\
670exception is raised. If the specified size is invalid, a ValueError\n\
671exception is raised, and the stack size is unmodified. 32k bytes\n\
672 currently the minimum supported stack size value to guarantee\n\
673sufficient stack space for the interpreter itself.\n\
674\n\
675Note that some platforms may have particular restrictions on values for\n\
676the stack size, such as requiring a minimum stack size larger than 32kB or\n\
677requiring allocation in multiples of the system memory page size\n\
678- platform documentation should be referred to for more information\n\
679(4kB pages are common; using multiples of 4096 for the stack size is\n\
680the suggested approach in the absence of more specific information).");
681
Barry Warsawd0c10421996-12-17 00:05:22 +0000682static PyMethodDef thread_methods[] = {
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000683 {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread,
684 METH_VARARGS,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000685 start_new_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000686 {"start_new", (PyCFunction)thread_PyThread_start_new_thread,
687 METH_VARARGS,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000688 start_new_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000689 {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000690 METH_NOARGS, allocate_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000691 {"allocate", (PyCFunction)thread_PyThread_allocate_lock,
Neal Norwitz46321172002-03-26 14:52:00 +0000692 METH_NOARGS, allocate_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000693 {"exit_thread", (PyCFunction)thread_PyThread_exit_thread,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000694 METH_NOARGS, exit_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000695 {"exit", (PyCFunction)thread_PyThread_exit_thread,
Neal Norwitz46321172002-03-26 14:52:00 +0000696 METH_NOARGS, exit_doc},
Kurt B. Kaisera1ad5f62003-06-16 18:51:28 +0000697 {"interrupt_main", (PyCFunction)thread_PyThread_interrupt_main,
Kurt B. Kaisera11e8462003-06-13 21:59:45 +0000698 METH_NOARGS, interrupt_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000699 {"get_ident", (PyCFunction)thread_get_ident,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000700 METH_NOARGS, get_ident_doc},
Antoine Pitrou65c9c642009-10-30 17:25:12 +0000701 {"_count", (PyCFunction)thread__count,
702 METH_NOARGS, _count_doc},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000703 {"stack_size", (PyCFunction)thread_stack_size,
704 METH_VARARGS,
705 stack_size_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000706#ifndef NO_EXIT_PROG
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000707 {"exit_prog", (PyCFunction)thread_PyThread_exit_prog,
708 METH_VARARGS},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000709#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000710 {NULL, NULL} /* sentinel */
711};
712
713
714/* Initialization function */
715
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000716PyDoc_STRVAR(thread_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000717"This module provides primitive operations to write multi-threaded programs.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000718The 'threading' module provides a more convenient interface.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000719
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000720PyDoc_STRVAR(lock_doc,
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000721"A lock object is a synchronization primitive. To create a lock,\n\
Guido van Rossum65d5b571998-12-21 19:32:43 +0000722call the PyThread_allocate_lock() function. Methods are:\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000723\n\
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000724acquire() -- lock the lock, possibly blocking until it can be obtained\n\
725release() -- unlock of the lock\n\
726locked() -- test whether the lock is currently locked\n\
727\n\
728A lock is not owned by the thread that locked it; another thread may\n\
729unlock it. A thread attempting to lock a lock that it has already locked\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000730will block until another thread unlocks it. Deadlocks may ensue.");
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000731
Martin v. Löwis1a214512008-06-11 05:26:20 +0000732static struct PyModuleDef threadmodule = {
733 PyModuleDef_HEAD_INIT,
734 "_thread",
735 thread_doc,
736 -1,
737 thread_methods,
738 NULL,
739 NULL,
740 NULL,
741 NULL
742};
743
744
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000745PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000746PyInit__thread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000747{
Barry Warsawd0c10421996-12-17 00:05:22 +0000748 PyObject *m, *d;
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000749
750 /* Initialize types: */
751 if (PyType_Ready(&localtype) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000752 return NULL;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000753 if (PyType_Ready(&Locktype) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000754 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000755
756 /* Create the module and add the functions */
Martin v. Löwis1a214512008-06-11 05:26:20 +0000757 m = PyModule_Create(&threadmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000758 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000759 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000760
761 /* Add a symbolic constant */
Barry Warsawd0c10421996-12-17 00:05:22 +0000762 d = PyModule_GetDict(m);
Georg Brandl2067bfd2008-05-25 13:05:15 +0000763 ThreadError = PyErr_NewException("_thread.error", NULL, NULL);
Barry Warsawd0c10421996-12-17 00:05:22 +0000764 PyDict_SetItemString(d, "error", ThreadError);
Guido van Rossum75e9fc31998-06-27 18:21:06 +0000765 Locktype.tp_doc = lock_doc;
766 Py_INCREF(&Locktype);
767 PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000768
Michael W. Hudson64e08142005-06-15 12:25:20 +0000769 Py_INCREF(&localtype);
Michael W. Hudson2368b3c2005-06-15 12:48:40 +0000770 if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000771 return NULL;
Jim Fultond15dc062004-07-14 19:11:50 +0000772
Antoine Pitrou65c9c642009-10-30 17:25:12 +0000773 nb_threads = 0;
774
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000775 /* Initialize the C thread library */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000776 PyThread_init_thread();
Martin v. Löwis1a214512008-06-11 05:26:20 +0000777 return m;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000778}