Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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
Create the thread lock in threading.py.
  • Loading branch information
ericsnowcurrently committed May 15, 2023
commit aa4d3bb0cef8a1cc7b9e6e9fac9fe3eae669c854
3 changes: 2 additions & 1 deletion 3 Lib/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,8 @@ def _set_tstate_lock(self):
Set a lock object which will be released by the interpreter when
the underlying thread state (see pystate.h) gets deleted.
"""
self._tstate_lock = _set_sentinel()
self._tstate_lock = _allocate_lock()
_set_sentinel(self._tstate_lock)
self._tstate_lock.acquire()

if not self.daemon:
Expand Down
17 changes: 6 additions & 11 deletions 17 Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1477,12 +1477,11 @@ release_sentinel(void *wr_raw)
}

static PyObject *
thread__set_sentinel(PyObject *module, PyObject *Py_UNUSED(ignored))
thread__set_sentinel(PyObject *module, PyObject *arg)
{
PyObject *wr;
PyThreadState *tstate = _PyThreadState_GET();
thread_module_state *state = get_thread_state(module);
lockobject *lock;
lockobject *lock = (lockobject *)arg;

if (tstate->on_delete_data != NULL) {
/* We must support the re-creation of the lock from a
Expand All @@ -1493,25 +1492,21 @@ thread__set_sentinel(PyObject *module, PyObject *Py_UNUSED(ignored))
tstate->on_delete_data = NULL;
Py_DECREF(wr);
}
lock = newlockobject(state);
if (lock == NULL)
return NULL;
/* The lock is owned by whoever called _set_sentinel(), but the weakref
hangs to the thread state. */
wr = PyWeakref_NewRef((PyObject *) lock, NULL);
if (wr == NULL) {
Py_DECREF(lock);
return NULL;
}
tstate->on_delete_data = (void *) wr;
tstate->on_delete = &release_sentinel;
return (PyObject *) lock;
Py_RETURN_NONE;
}

PyDoc_STRVAR(_set_sentinel_doc,
"_set_sentinel() -> lock\n\
"_set_sentinel(lock)\n\
\n\
Set a sentinel lock that will be released when the current thread\n\
Set the sentinel lock that will be released when the current thread\n\
state is finalized (after it is untied from the interpreter).\n\
\n\
This is a private API for the threading module.");
Expand Down Expand Up @@ -1740,7 +1735,7 @@ static PyMethodDef thread_methods[] = {
{"stack_size", (PyCFunction)thread_stack_size,
METH_VARARGS, stack_size_doc},
{"_set_sentinel", thread__set_sentinel,
METH_NOARGS, _set_sentinel_doc},
METH_O, _set_sentinel_doc},
{"_excepthook", thread_excepthook,
METH_O, excepthook_doc},
{NULL, NULL} /* sentinel */
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.