8000 gh-114271: Clean Up _threadmodule.c by ericsnowcurrently · Pull Request #116776 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-114271: Clean Up _threadmodule.c #116776

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
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
Track the data, not the objects.
  • Loading branch information
ericsnowcurrently committed Mar 14, 2024
commit f1e8fa8ec4586db3024f846afc7703ce39ea6767
2 changes: 1 addition & 1 deletion Include/internal/pycore_pythread.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct _pythread_runtime_state {
} stubs;
#endif

// Linked list of ThreadHandleObjects
// Linked list of struct _PyThread_handle_data
struct llist_node handles;
};

Expand Down
48 changes: 23 additions & 25 deletions Objects/threadhandleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
// their turn to execute or an earlier operation completes successfully. Once a
// join has completed successfully all future joins complete immediately.
struct _PyThread_handle_data {
struct llist_node fork_node; // linked list node (see _pythread_runtime_state)

// The `ident` and `handle` fields are immutable once the object is visible
// to threads other than its creator, thus they do not need to be accessed
// atomically.
Expand All @@ -44,6 +46,8 @@ struct _PyThread_handle_data {
};


static void track_thread_handle_for_fork(struct _PyThread_handle_data *);

static struct _PyThread_handle_data *
new_thread_handle_data(void)
{
Expand All @@ -62,13 +66,17 @@ new_thread_handle_data(void)
.thread_is_exiting = event,
.state = THREAD_HANDLE_INVALID,
};
// XXX Add _PyRuntime.threads.handles here.

track_thread_handle_for_fork(data);

return data;
}

static inline int get_thread_handle_state(struct _PyThread_handle_data *);
static inline void set_thread_handle_state(
struct _PyThread_handle_data *, _PyThreadHandleState);
static void untrack_thread_handle_for_fork(struct _PyThread_handle_data *);


static void
free_thread_handle_data(struct _PyThread_handle_data *data)
Expand All @@ -83,6 +91,7 @@ free_thread_handle_data(struct _PyThread_handle_data *data)
set_thread_handle_state(data, THREAD_HANDLE_DETACHED);
}
}
untrack_thread_handle_for_fork(data);
_PyEventRc_Decref(data->thread_is_exiting);
PyMem_RawFree(data);
}
Expand Down Expand Up @@ -187,14 +196,11 @@ _PyThread_GetExitingEvent(struct _PyThread_handle_data *data)

typedef struct {
PyObject_HEAD
struct llist_node fork_node; // linked list node (see _pythread_runtime_state)

struct _PyThread_handle_data *data;
} thandleobject;


static void track_thread_handle_for_fork(thandleobject *);

PyObject *
_PyThreadHandle_NewObject(void)
{
Expand All @@ -208,8 +214,6 @@ _PyThreadHandle_NewObject(void)
return NULL;
}

track_thread_handle_for_fork(self);

return (PyObject *)self;
}

Expand Down Expand Up @@ -250,14 +254,9 @@ static PyGetSetDef ThreadHandle_getsetlist[] = {

/* The _ThreadHandle class */

static void untrack_thread_handle_for_fork(thandleobject *);

static void
ThreadHandle_dealloc(thandleobject *self)
{
// Remove ourself from the global list of handles
untrack_thread_handle_for_fork(self);

// It's safe to access state non-atomically:
// 1. This is the destructor; nothing else holds a reference.
// 2. The refcount going to zero is a "synchronizes-with" event;
Expand Down Expand Up @@ -294,19 +293,19 @@ PyTypeObject _PyThreadHandle_Type = {
// XXX Track the handles instead of the objects.

static void
track_thread_handle_for_fork(thandleobject *hobj)
track_thread_handle_for_fork(struct _PyThread_handle_data *data)
{
HEAD_LOCK(&_PyRuntime);
llist_insert_tail(&_PyRuntime.threads.handles, &hobj->fork_node);
llist_insert_tail(&_PyRuntime.threads.handles, &data->fork_node);
HEAD_UNLOCK(&_PyRuntime);
}

static void
untrack_thread_handle_for_fork(thandleobject *hobj)
untrack_thread_handle_for_fork(struct _PyThread_handle_data *data)
{
HEAD_LOCK(&_PyRuntime);
if (hobj->fork_node.next != NULL) {
llist_remove(&hobj->fork_node);
if (data->fork_node.next != NULL) {
llist_remove(&data->fork_node);
}
HEAD_UNLOCK(&_PyRuntime);
}
Expand All @@ -317,16 +316,15 @@ clear_tracked_thread_handles(struct _pythread_runtime_state *state,
{
struct llist_node *node;
llist_for_each_safe(node, &state->handles) {
thandleobject *hobj = llist_data(node, thandleobject, fork_node);
if (hobj->data != NULL) {
if (hobj->data->ident == current) {
continue;
}

// Disallow calls to join() as they could crash. We are the only
// thread; it's safe to set this without an atomic.
hobj->data->state = THREAD_HANDLE_INVALID;
struct _PyThread_handle_data *data = llist_data(
node, struct _PyThread_handle_data, fork_node);
if (data->ident == current) {
continue;
}

// Disallow calls to join() as they could crash. We are the only
// thread; it's safe to set this without an atomic.
data->state = THREAD_HANDLE_INVALID;
llist_remove(node);
}
}
Expand Down
0