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
Next Next commit
Make _ThreadHandle a static builtin type.
  • Loading branch information
ericsnowcurrently committed Mar 14, 2024
commit d2fb9bdc758f39c165a94571843530dfa1ca7c68
5 changes: 2 additions & 3 deletions Include/internal/pycore_pythread.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,9 @@ typedef enum {
THREAD_HANDLE_INVALID = 4,
} _PyThreadHandleState;

// XXX Make it a static type.
extern PyTypeObject * _PyThreadHandle_NewType(void);
extern PyTypeObject _PyThreadHandle_Type;

extern PyObject * _PyThreadHandle_NewObject(PyTypeObject *);
extern PyObject * _PyThreadHandle_NewObject(void);
extern _PyEventRc * _PyThreadHandle_GetExitingEvent(PyObject *);
extern void _PyThreadHandle_SetStarted(
PyObject *obj,
Expand Down
15 changes: 4 additions & 11 deletions Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ typedef struct {
PyTypeObject *lock_type;
PyTypeObject *local_type;
PyTypeObject *local_dummy_type;
PyTypeObject *thread_handle_type;
} thread_module_state;

static inline thread_module_state*
Expand Down Expand Up @@ -1545,8 +1544,6 @@ is SystemExit.\n");
static PyObject *
threadmod_start_joinable_thread(PyObject *module, PyObject *func)
{
thread_module_state *state = get_thread_state(module);

if (!PyCallable_Check(func)) {
PyErr_SetString(PyExc_TypeError,
"thread function must be callable");
Expand All @@ -1557,7 +1554,7 @@ threadmod_start_joinable_thread(PyObject *module, PyObject *func)
return NULL;
}

PyObject *hobj = _PyThreadHandle_NewObject(state->thread_handle_type);
PyObject *hobj = _PyThreadHandle_NewObject();
if (hobj == NULL) {
return NULL;
}
Expand Down Expand Up @@ -1829,11 +1826,9 @@ thread_module_exec(PyObject *module)
PyThread_init_thread();

// _ThreadHandle
state->thread_handle_type = _PyThreadHandle_NewType();
if (state->thread_handle_type == NULL) {
return -1;
}
if (PyDict_SetItemString(d, "_ThreadHandle", (PyObject *)state->thread_handle_type) < 0) {
if (PyDict_SetItemString(d, "_ThreadHandle",
(PyObject *)&_PyThreadHandle_Type) < 0)
{
return -1;
}

Expand Down Expand Up @@ -1914,7 +1909,6 @@ thread_module_traverse(PyObject *module, visitproc visit, void *arg)
Py_VISIT(state->lock_type);
Py_VISIT(state->local_type);
Py_VISIT(state->local_dummy_type);
Py_VISIT(state->thread_handle_type);
return 0;
}

Expand All @@ -1926,7 +1920,6 @@ thread_module_clear(PyObject *module)
Py_CLEAR(state->lock_type);
Py_CLEAR(state->local_type);
Py_CLEAR(state->local_dummy_type);
Py_CLEAR(state->thread_handle_type);
return 0;
}

Expand Down
1 change: 1 addition & 0 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2301,6 +2301,7 @@ static PyTypeObject* static_types[] = {
&_PyNone_Type,
&_PyNotImplemented_Type,
&_PyPositionsIterator,
&_PyThreadHandle_Type,
&_PyUnicodeASCIIIter_Type,
&_PyUnion_Type,
&_PyUOpExecutor_Type,
Expand Down
35 changes: 11 additions & 24 deletions Objects/threadhandleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ join_thread(thandleobject *hobj)
static void track_thread_handle_for_fork(thandleobject *);

PyObject *
_PyThreadHandle_NewObject(PyTypeObject *type)
_PyThreadHandle_NewObject(void)
{
_PyEventRc *event = _PyEventRc_New();
if (event == NULL) {
PyErr_NoMemory();
return NULL;
}
thandleobject *self = PyObject_New(thandleobject, type);
thandleobject *self = PyObject_New(thandleobject, &_PyThreadHandle_Type);
if (self == NULL) {
_PyEventRc_Decref(event);
return NULL;
Expand Down Expand Up @@ -261,7 +261,6 @@ ThreadHandle_dealloc(thandleobject *self)
}
_PyEventRc_Decref(self->thread_is_exiting);
PyObject_Free(self);
Py_DECREF(tp);
}


Expand All @@ -273,30 +272,18 @@ ThreadHandle_repr(thandleobject *self)
}


static PyType_Slot ThreadHandle_Type_slots[] = {
{Py_tp_dealloc, (destructor)ThreadHandle_dealloc},
{Py_tp_repr, (reprfunc)ThreadHandle_repr},
{Py_tp_getset, ThreadHandle_getsetlist},
{Py_tp_methods, ThreadHandle_methods},
{0, 0}
};

static PyType_Spec ThreadHandle_Type_spec = {
"_thread._ThreadHandle",
sizeof(thandleobject),
0,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
ThreadHandle_Type_slots,
PyTypeObject _PyThreadHandle_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "_ThreadHandle",
.tp_basicsize = sizeof(thandleobject),
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
.tp_dealloc = (destructor)ThreadHandle_dealloc,
.tp_repr = (reprfunc)ThreadHandle_repr,
.tp_getset = ThreadHandle_getsetlist,
.tp_methods = ThreadHandle_methods,
};


PyTypeObject *
_PyThreadHandle_NewType(void)
{
return (PyTypeObject *)PyType_FromSpec(&ThreadHandle_Type_spec);
}


/*************/
/* other API */
/*************/
Expand Down
0