Fix #1474677, non-keyword argument following keyword.
[python.git] / Modules / threadmodule.c
blob9ac9881d9c19afdd3d4848014dc4fa2691fa72f6
2 /* Thread module */
3 /* Interface to Sjoerd's portable C thread library */
5 #include "Python.h"
7 #ifndef WITH_THREAD
8 #error "Error! The rest of Python is not compiled with thread support."
9 #error "Rerun configure, adding a --with-threads option."
10 #error "Then run `make clean' followed by `make'."
11 #endif
13 #include "pythread.h"
15 static PyObject *ThreadError;
18 /* Lock objects */
20 typedef struct {
21 PyObject_HEAD
22 PyThread_type_lock lock_lock;
23 } lockobject;
25 static void
26 lock_dealloc(lockobject *self)
28 /* Unlock the lock so it's safe to free it */
29 PyThread_acquire_lock(self->lock_lock, 0);
30 PyThread_release_lock(self->lock_lock);
32 PyThread_free_lock(self->lock_lock);
33 PyObject_Del(self);
36 static PyObject *
37 lock_PyThread_acquire_lock(lockobject *self, PyObject *args)
39 int i = 1;
41 if (!PyArg_ParseTuple(args, "|i:acquire", &i))
42 return NULL;
44 Py_BEGIN_ALLOW_THREADS
45 i = PyThread_acquire_lock(self->lock_lock, i);
46 Py_END_ALLOW_THREADS
48 return PyBool_FromLong((long)i);
51 PyDoc_STRVAR(acquire_doc,
52 "acquire([wait]) -> None or bool\n\
53 (acquire_lock() is an obsolete synonym)\n\
54 \n\
55 Lock the lock. Without argument, this blocks if the lock is already\n\
56 locked (even by the same thread), waiting for another thread to release\n\
57 the lock, and return None once the lock is acquired.\n\
58 With an argument, this will only block if the argument is true,\n\
59 and the return value reflects whether the lock is acquired.\n\
60 The blocking operation is not interruptible.");
62 static PyObject *
63 lock_PyThread_release_lock(lockobject *self)
65 /* Sanity check: the lock must be locked */
66 if (PyThread_acquire_lock(self->lock_lock, 0)) {
67 PyThread_release_lock(self->lock_lock);
68 PyErr_SetString(ThreadError, "release unlocked lock");
69 return NULL;
72 PyThread_release_lock(self->lock_lock);
73 Py_INCREF(Py_None);
74 return Py_None;
77 PyDoc_STRVAR(release_doc,
78 "release()\n\
79 (release_lock() is an obsolete synonym)\n\
80 \n\
81 Release the lock, allowing another thread that is blocked waiting for\n\
82 the lock to acquire the lock. The lock must be in the locked state,\n\
83 but it needn't be locked by the same thread that unlocks it.");
85 static PyObject *
86 lock_locked_lock(lockobject *self)
88 if (PyThread_acquire_lock(self->lock_lock, 0)) {
89 PyThread_release_lock(self->lock_lock);
90 return PyBool_FromLong(0L);
92 return PyBool_FromLong(1L);
95 PyDoc_STRVAR(locked_doc,
96 "locked() -> bool\n\
97 (locked_lock() is an obsolete synonym)\n\
98 \n\
99 Return whether the lock is in the locked state.");
101 static PyMethodDef lock_methods[] = {
102 {"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock,
103 METH_VARARGS, acquire_doc},
104 {"acquire", (PyCFunction)lock_PyThread_acquire_lock,
105 METH_VARARGS, acquire_doc},
106 {"release_lock", (PyCFunction)lock_PyThread_release_lock,
107 METH_NOARGS, release_doc},
108 {"release", (PyCFunction)lock_PyThread_release_lock,
109 METH_NOARGS, release_doc},
110 {"locked_lock", (PyCFunction)lock_locked_lock,
111 METH_NOARGS, locked_doc},
112 {"locked", (PyCFunction)lock_locked_lock,
113 METH_NOARGS, locked_doc},
114 {"__enter__", (PyCFunction)lock_PyThread_acquire_lock,
115 METH_VARARGS, acquire_doc},
116 {"__exit__", (PyCFunction)lock_PyThread_release_lock,
117 METH_VARARGS, release_doc},
118 {NULL, NULL} /* sentinel */
121 static PyObject *
122 lock_getattr(lockobject *self, char *name)
124 return Py_FindMethod(lock_methods, (PyObject *)self, name);
127 static PyTypeObject Locktype = {
128 PyObject_HEAD_INIT(&PyType_Type)
129 0, /*ob_size*/
130 "thread.lock", /*tp_name*/
131 sizeof(lockobject), /*tp_size*/
132 0, /*tp_itemsize*/
133 /* methods */
134 (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*/
142 static lockobject *
143 newlockobject(void)
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");
155 return self;
158 /* Thread-local objects */
160 #include "structmember.h"
162 typedef struct {
163 PyObject_HEAD
164 PyObject *key;
165 PyObject *args;
166 PyObject *kw;
167 PyObject *dict;
168 } localobject;
170 static PyObject *
171 local_new(PyTypeObject *type, PyObject *args, PyObject *kw)
173 localobject *self;
174 PyObject *tdict;
176 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;
184 self = (localobject *)type->tp_alloc(type, 0);
185 if (self == NULL)
186 return NULL;
188 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;
197 self->dict = PyDict_New();
198 if (self->dict == NULL)
199 goto err;
201 tdict = PyThreadState_GetDict();
202 if (tdict == NULL) {
203 PyErr_SetString(PyExc_SystemError,
204 "Couldn't get thread-state dictionary");
205 goto err;
208 if (PyDict_SetItem(tdict, self->key, self->dict) < 0)
209 goto err;
211 return (PyObject *)self;
213 err:
214 Py_DECREF(self);
215 return NULL;
218 static int
219 local_traverse(localobject *self, visitproc visit, void *arg)
221 Py_VISIT(self->args);
222 Py_VISIT(self->kw);
223 Py_VISIT(self->dict);
224 return 0;
227 static int
228 local_clear(localobject *self)
230 Py_CLEAR(self->key);
231 Py_CLEAR(self->args);
232 Py_CLEAR(self->kw);
233 Py_CLEAR(self->dict);
234 return 0;
237 static void
238 local_dealloc(localobject *self)
240 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);
252 local_clear(self);
253 self->ob_type->tp_free((PyObject*)self);
256 static PyObject *
257 _ldict(localobject *self)
259 PyObject *tdict, *ldict;
261 tdict = PyThreadState_GetDict();
262 if (tdict == NULL) {
263 PyErr_SetString(PyExc_SystemError,
264 "Couldn't get thread-state dictionary");
265 return NULL;
268 ldict = PyDict_GetItem(tdict, self->key);
269 if (ldict == NULL) {
270 ldict = PyDict_New(); /* we own ldict */
272 if (ldict == NULL)
273 return NULL;
274 else {
275 int i = PyDict_SetItem(tdict, self->key, ldict);
276 Py_DECREF(ldict); /* now ldict is borrowed */
277 if (i < 0)
278 return NULL;
281 Py_CLEAR(self->dict);
282 Py_INCREF(ldict);
283 self->dict = ldict; /* still borrowed */
285 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;
296 else if (self->dict != ldict) {
297 Py_CLEAR(self->dict);
298 Py_INCREF(ldict);
299 self->dict = ldict;
302 return ldict;
305 static int
306 local_setattro(localobject *self, PyObject *name, PyObject *v)
308 PyObject *ldict;
310 ldict = _ldict(self);
311 if (ldict == NULL)
312 return -1;
314 return PyObject_GenericSetAttr((PyObject *)self, name, v);
317 static PyObject *
318 local_getdict(localobject *self, void *closure)
320 if (self->dict == NULL) {
321 PyErr_SetString(PyExc_AttributeError, "__dict__");
322 return NULL;
325 Py_INCREF(self->dict);
326 return self->dict;
329 static PyGetSetDef local_getset[] = {
330 {"__dict__", (getter)local_getdict, (setter)NULL,
331 "Local-data dictionary", NULL},
332 {NULL} /* Sentinel */
335 static PyObject *local_getattro(localobject *, PyObject *);
337 static 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,
344 /* tp_print */ 0,
345 /* tp_getattr */ 0,
346 /* tp_setattr */ 0,
347 /* tp_compare */ 0,
348 /* tp_repr */ 0,
349 /* tp_as_number */ 0,
350 /* tp_as_sequence */ 0,
351 /* tp_as_mapping */ 0,
352 /* tp_hash */ 0,
353 /* tp_call */ 0,
354 /* tp_str */ 0,
355 /* 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,
359 /* tp_doc */ "Thread-local data",
360 /* tp_traverse */ (traverseproc)local_traverse,
361 /* tp_clear */ (inquiry)local_clear,
362 /* tp_richcompare */ 0,
363 /* tp_weaklistoffset */ 0,
364 /* tp_iter */ 0,
365 /* tp_iternext */ 0,
366 /* tp_methods */ 0,
367 /* tp_members */ 0,
368 /* tp_getset */ local_getset,
369 /* tp_base */ 0,
370 /* tp_dict */ 0, /* internal use */
371 /* tp_descr_get */ 0,
372 /* tp_descr_set */ 0,
373 /* tp_dictoffset */ offsetof(localobject, dict),
374 /* tp_init */ 0,
375 /* tp_alloc */ 0,
376 /* tp_new */ local_new,
377 /* tp_free */ 0, /* Low-level free-mem routine */
378 /* tp_is_gc */ 0, /* For PyObject_IS_GC */
381 static PyObject *
382 local_getattro(localobject *self, PyObject *name)
384 PyObject *ldict, *value;
386 ldict = _ldict(self);
387 if (ldict == NULL)
388 return NULL;
390 if (self->ob_type != &localtype)
391 /* use generic lookup for subtypes */
392 return PyObject_GenericGetAttr((PyObject *)self, name);
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);
400 Py_INCREF(value);
401 return value;
404 /* Module functions */
406 struct bootstate {
407 PyInterpreterState *interp;
408 PyObject *func;
409 PyObject *args;
410 PyObject *keyw;
413 static void
414 t_bootstrap(void *boot_raw)
416 struct bootstate *boot = (struct bootstate *) boot_raw;
417 PyThreadState *tstate;
418 PyObject *res;
420 tstate = PyThreadState_New(boot->interp);
422 PyEval_AcquireThread(tstate);
423 res = PyEval_CallObjectWithKeywords(
424 boot->func, boot->args, boot->keyw);
425 if (res == NULL) {
426 if (PyErr_ExceptionMatches(PyExc_SystemExit))
427 PyErr_Clear();
428 else {
429 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");
438 PyErr_PrintEx(0);
441 else
442 Py_DECREF(res);
443 Py_DECREF(boot->func);
444 Py_DECREF(boot->args);
445 Py_XDECREF(boot->keyw);
446 PyMem_DEL(boot_raw);
447 PyThreadState_Clear(tstate);
448 PyThreadState_DeleteCurrent();
449 PyThread_exit_thread();
452 static PyObject *
453 thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
455 PyObject *func, *args, *keyw = NULL;
456 struct bootstate *boot;
457 long ident;
459 if (!PyArg_ParseTuple(fargs, "OO|O:start_new_thread", &func, &args, &keyw))
460 return NULL;
461 if (!PyCallable_Check(func)) {
462 PyErr_SetString(PyExc_TypeError,
463 "first arg must be callable");
464 return NULL;
466 if (!PyTuple_Check(args)) {
467 PyErr_SetString(PyExc_TypeError,
468 "2nd arg must be a tuple");
469 return NULL;
471 if (keyw != NULL && !PyDict_Check(keyw)) {
472 PyErr_SetString(PyExc_TypeError,
473 "optional 3rd arg must be a dictionary");
474 return NULL;
476 boot = PyMem_NEW(struct bootstate, 1);
477 if (boot == NULL)
478 return PyErr_NoMemory();
479 boot->interp = PyThreadState_GET()->interp;
480 boot->func = func;
481 boot->args = args;
482 boot->keyw = keyw;
483 Py_INCREF(func);
484 Py_INCREF(args);
485 Py_XINCREF(keyw);
486 PyEval_InitThreads(); /* Start the interpreter's thread-awareness */
487 ident = PyThread_start_new_thread(t_bootstrap, (void*) boot);
488 if (ident == -1) {
489 PyErr_SetString(ThreadError, "can't start new thread");
490 Py_DECREF(func);
491 Py_DECREF(args);
492 Py_XDECREF(keyw);
493 PyMem_DEL(boot);
494 return NULL;
496 return PyInt_FromLong(ident);
499 PyDoc_STRVAR(start_new_doc,
500 "start_new_thread(function, args[, kwargs])\n\
501 (start_new() is an obsolete synonym)\n\
503 Start a new thread and return its identifier. The thread will call the\n\
504 function with positional arguments from the tuple args and keyword arguments\n\
505 taken from the optional dictionary kwargs. The thread exits when the\n\
506 function returns; the return value is ignored. The thread will also exit\n\
507 when the function raises an unhandled exception; a stack trace will be\n\
508 printed unless the exception is SystemExit.\n");
510 static PyObject *
511 thread_PyThread_exit_thread(PyObject *self)
513 PyErr_SetNone(PyExc_SystemExit);
514 return NULL;
517 PyDoc_STRVAR(exit_doc,
518 "exit()\n\
519 (PyThread_exit_thread() is an obsolete synonym)\n\
521 This is synonymous to ``raise SystemExit''. It will cause the current\n\
522 thread to exit silently unless the exception is caught.");
524 static PyObject *
525 thread_PyThread_interrupt_main(PyObject * self)
527 PyErr_SetInterrupt();
528 Py_INCREF(Py_None);
529 return Py_None;
532 PyDoc_STRVAR(interrupt_doc,
533 "interrupt_main()\n\
535 Raise a KeyboardInterrupt in the main thread.\n\
536 A subthread can use this function to interrupt the main thread."
539 #ifndef NO_EXIT_PROG
540 static PyObject *
541 thread_PyThread_exit_prog(PyObject *self, PyObject *args)
543 int sts;
544 if (!PyArg_ParseTuple(args, "i:exit_prog", &sts))
545 return NULL;
546 Py_Exit(sts); /* Calls PyThread_exit_prog(sts) or _PyThread_exit_prog(sts) */
547 for (;;) { } /* Should not be reached */
549 #endif
551 static lockobject *newlockobject(void);
553 static PyObject *
554 thread_PyThread_allocate_lock(PyObject *self)
556 return (PyObject *) newlockobject();
559 PyDoc_STRVAR(allocate_doc,
560 "allocate_lock() -> lock object\n\
561 (allocate() is an obsolete synonym)\n\
563 Create a new lock object. See LockType.__doc__ for information about locks.");
565 static PyObject *
566 thread_get_ident(PyObject *self)
568 long ident;
569 ident = PyThread_get_thread_ident();
570 if (ident == -1) {
571 PyErr_SetString(ThreadError, "no current thread ident");
572 return NULL;
574 return PyInt_FromLong(ident);
577 PyDoc_STRVAR(get_ident_doc,
578 "get_ident() -> integer\n\
580 Return a non-zero integer that uniquely identifies the current thread\n\
581 amongst other threads that exist simultaneously.\n\
582 This may be used to identify per-thread resources.\n\
583 Even though on some platforms threads identities may appear to be\n\
584 allocated consecutive numbers starting at 1, this behavior should not\n\
585 be relied upon, and the number should be seen purely as a magic cookie.\n\
586 A thread's identity may be reused for another thread after it exits.");
588 static PyMethodDef thread_methods[] = {
589 {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread,
590 METH_VARARGS,
591 start_new_doc},
592 {"start_new", (PyCFunction)thread_PyThread_start_new_thread,
593 METH_VARARGS,
594 start_new_doc},
595 {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock,
596 METH_NOARGS, allocate_doc},
597 {"allocate", (PyCFunction)thread_PyThread_allocate_lock,
598 METH_NOARGS, allocate_doc},
599 {"exit_thread", (PyCFunction)thread_PyThread_exit_thread,
600 METH_NOARGS, exit_doc},
601 {"exit", (PyCFunction)thread_PyThread_exit_thread,
602 METH_NOARGS, exit_doc},
603 {"interrupt_main", (PyCFunction)thread_PyThread_interrupt_main,
604 METH_NOARGS, interrupt_doc},
605 {"get_ident", (PyCFunction)thread_get_ident,
606 METH_NOARGS, get_ident_doc},
607 #ifndef NO_EXIT_PROG
608 {"exit_prog", (PyCFunction)thread_PyThread_exit_prog,
609 METH_VARARGS},
610 #endif
611 {NULL, NULL} /* sentinel */
615 /* Initialization function */
617 PyDoc_STRVAR(thread_doc,
618 "This module provides primitive operations to write multi-threaded programs.\n\
619 The 'threading' module provides a more convenient interface.");
621 PyDoc_STRVAR(lock_doc,
622 "A lock object is a synchronization primitive. To create a lock,\n\
623 call the PyThread_allocate_lock() function. Methods are:\n\
625 acquire() -- lock the lock, possibly blocking until it can be obtained\n\
626 release() -- unlock of the lock\n\
627 locked() -- test whether the lock is currently locked\n\
629 A lock is not owned by the thread that locked it; another thread may\n\
630 unlock it. A thread attempting to lock a lock that it has already locked\n\
631 will block until another thread unlocks it. Deadlocks may ensue.");
633 PyMODINIT_FUNC
634 initthread(void)
636 PyObject *m, *d;
638 /* Initialize types: */
639 if (PyType_Ready(&localtype) < 0)
640 return;
642 /* Create the module and add the functions */
643 m = Py_InitModule3("thread", thread_methods, thread_doc);
644 if (m == NULL)
645 return;
647 /* Add a symbolic constant */
648 d = PyModule_GetDict(m);
649 ThreadError = PyErr_NewException("thread.error", NULL, NULL);
650 PyDict_SetItemString(d, "error", ThreadError);
651 Locktype.tp_doc = lock_doc;
652 Py_INCREF(&Locktype);
653 PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype);
655 Py_INCREF(&localtype);
656 if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0)
657 return;
659 /* Initialize the C thread library */
660 PyThread_init_thread();