summaryrefslogtreecommitdiffstats
path: root/Modules/_threadmodule.c
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2009-10-30 17:25:12 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2009-10-30 17:25:12 (GMT)
commit65c9c6426b719d885f12938a2c5ab938558406ab (patch)
tree4e4dbbf398d78a6bf2003d3a56635b9f0fa7582d /Modules/_threadmodule.c
parenta2d1fe0b843b27130d0e3194ac2f517692e89c85 (diff)
downloadcpython-65c9c6426b719d885f12938a2c5ab938558406ab.zip
cpython-65c9c6426b719d885f12938a2c5ab938558406ab.tar.gz
cpython-65c9c6426b719d885f12938a2c5ab938558406ab.tar.bz2
Merged revisions 75958 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r75958 | antoine.pitrou | 2009-10-30 18:07:08 +0100 (ven., 30 oct. 2009) | 7 lines Issue #7222: Make thread "reaping" more reliable so that reference leak-chasing test runs give sensible results. The previous method of reaping threads could return successfully while some Thread objects were still referenced. This also introduces a new private function: :func:hread._count(). ........
Diffstat (limited to 'Modules/_threadmodule.c')
-rw-r--r--Modules/_threadmodule.c20
1 files changed, 19 insertions, 1 deletions
diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c
index 8dc2d00..81e8984 100644
--- a/Modules/_threadmodule.c
+++ b/Modules/_threadmodule.c
@@ -14,7 +14,7 @@
#include "pythread.h"
static PyObject *ThreadError;
-
+static long nb_threads = 0;
/* Lock objects */
@@ -439,6 +439,7 @@ t_bootstrap(void *boot_raw)
tstate = PyThreadState_New(boot->interp);
PyEval_AcquireThread(tstate);
+ nb_threads++;
res = PyEval_CallObjectWithKeywords(
boot->func, boot->args, boot->keyw);
if (res == NULL) {
@@ -463,6 +464,7 @@ t_bootstrap(void *boot_raw)
Py_DECREF(boot->args);
Py_XDECREF(boot->keyw);
PyMem_DEL(boot_raw);
+ nb_threads--;
PyThreadState_Clear(tstate);
PyThreadState_DeleteCurrent();
PyThread_exit_thread();
@@ -606,6 +608,18 @@ be relied upon, and the number should be seen purely as a magic cookie.\n\
A thread's identity may be reused for another thread after it exits.");
static PyObject *
+thread__count(PyObject *self)
+{
+ return PyLong_FromLong(nb_threads);
+}
+
+PyDoc_STRVAR(_count_doc,
+"_count() -> integer\n\
+\n\
+Return the number of currently running (sub)threads.\n\
+This excludes the main thread.");
+
+static PyObject *
thread_stack_size(PyObject *self, PyObject *args)
{
size_t old_size;
@@ -678,6 +692,8 @@ static PyMethodDef thread_methods[] = {
METH_NOARGS, interrupt_doc},
{"get_ident", (PyCFunction)thread_get_ident,
METH_NOARGS, get_ident_doc},
+ {"_count", (PyCFunction)thread__count,
+ METH_NOARGS, _count_doc},
{"stack_size", (PyCFunction)thread_stack_size,
METH_VARARGS,
stack_size_doc},
@@ -748,6 +764,8 @@ PyInit__thread(void)
if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0)
return NULL;
+ nb_threads = 0;
+
/* Initialize the C thread library */
PyThread_init_thread();
return m;