Skip to content

gh-104341: Clean Up threading Module #104552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Drop interp.threads.count.
  • Loading branch information
ericsnowcurrently committed May 15, 2023
commit da85eeda70a3c051d3203b36bd9627eee66e4ebe
2 changes: 0 additions & 2 deletions Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ struct _is {
uint64_t next_unique_id;
/* The linked list of threads, newest first. */
PyThreadState *head;
/* Used in Modules/_threadmodule.c. */
long count;
/* Support for runtime thread stack size tuning.
A value of 0 means using the platform's default stack size
or the size specified by the THREAD_STACK_SIZE macro. */
Expand Down
22 changes: 12 additions & 10 deletions Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ struct module_thread {

struct module_threads {
// XXX This can replace interp->threads.count.
Py_ssize_t count;
long num_total;
long num_running;
PyThread_type_lock mutex;
struct module_thread *head;
struct module_thread *tail;
Expand All @@ -41,7 +42,8 @@ struct module_threads {
static int
module_threads_init(struct module_threads *threads)
{
threads->count = 0;
threads->num_total = 0;
threads->num_running = 0;
threads->head = NULL;
threads->tail = NULL;
threads->mutex = PyThread_allocate_lock();
Expand Down Expand Up @@ -72,7 +74,7 @@ module_threads_add(struct module_threads *threads, struct module_thread *mt)
threads->tail->next = mt;
}
threads->tail = mt;
threads->count++;
threads->num_total++;

PyThread_release_lock(threads->mutex);
}
Expand All @@ -94,7 +96,7 @@ module_threads_remove(struct module_threads *threads, struct module_thread *mt)
else {
mt->next->prev = mt->prev;
}
threads->count--;
threads->num_total--;

PyThread_release_lock(threads->mutex);
}
Expand All @@ -120,19 +122,19 @@ add_module_thread(struct module_threads *threads, PyThreadState *tstate)
}

static int
module_thread_starting(struct module_thread *mt)
module_thread_starting(struct module_threads *threads, struct module_thread *mt)
{
assert(mt->tstate == PyThreadState_Get());

mt->tstate->interp->threads.count++;
threads->num_running++;

return 0;
}

static void
module_thread_finished(struct module_thread *mt)
module_thread_finished(struct module_threads *threads, struct module_thread *mt)
{
mt->tstate->interp->threads.count--;
threads->num_running--;

// XXX We should be notifying other threads here.
}
Expand Down Expand Up @@ -1215,7 +1217,7 @@ thread_run(void *boot_raw)
_PyThreadState_Bind(tstate);
PyEval_AcquireThread(tstate);

module_thread_starting(mt);
module_thread_starting(&state->threads, mt);

PyObject *res = PyObject_Call(boot->func, boot->args, boot->kwargs);
if (res == NULL) {
Expand All @@ -1230,7 +1232,7 @@ thread_run(void *boot_raw)
Py_DECREF(res);
}

module_thread_finished(mt);
module_thread_finished(&state->threads, mt);

thread_bootstate_free(boot);
PyThreadState_Clear(tstate);
Expand Down