Skip to content

Commit 30785c6

Browse files
author
Rémy Voet (ryv)
committed
- rename bootstrapped => running
- merge wait method + is_failed: wait return False if the boostrapping has failed.
1 parent 492a5eb commit 30785c6

2 files changed

Lines changed: 28 additions & 37 deletions

File tree

Lib/threading.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ def _after_fork(self, new_ident=None):
953953
def __repr__(self):
954954
assert self._initialized, "Thread.__init__() was not called"
955955
status = "initial"
956-
if self._os_thread_handle.is_bootstrapped():
956+
if self._os_thread_handle.is_running():
957957
status = "started"
958958
if self._os_thread_handle.is_done():
959959
status = "stopped"
@@ -976,7 +976,7 @@ def start(self):
976976
if not self._initialized:
977977
raise RuntimeError("thread.__init__() not called")
978978

979-
if self._os_thread_handle.is_bootstrapped():
979+
if self._os_thread_handle.is_running():
980980
raise RuntimeError("threads can only be started once")
981981

982982
with _active_limbo_lock:
@@ -999,11 +999,10 @@ def start(self):
999999
with _active_limbo_lock:
10001000
del _limbo[self]
10011001
raise
1002-
self._os_thread_handle.wait_bootstrapped()
10031002

10041003
# It's possible that the _bootstrap(_inner) fails in the new Thread (e.g. Memory Error);
10051004
# We have to clean `_limbo` and `_active` here to avoid inconsistent state as much as possible
1006-
if self._os_thread_handle.is_failed():
1005+
if not self._os_thread_handle.wait_running():
10071006
with _active_limbo_lock:
10081007
_limbo.pop(self, None)
10091008
if self._ident:
@@ -1067,7 +1066,7 @@ def _bootstrap_inner(self):
10671066
if _HAVE_THREAD_NATIVE_ID:
10681067
self._set_native_id()
10691068
self._set_os_name()
1070-
self._os_thread_handle.set_bootstrapped()
1069+
self._os_thread_handle.set_running()
10711070
with _active_limbo_lock:
10721071
_active[self._ident] = self
10731072
del _limbo[self]
@@ -1115,7 +1114,7 @@ def join(self, timeout=None):
11151114
"""
11161115
if not self._initialized:
11171116
raise RuntimeError("Thread.__init__() not called")
1118-
if not self._os_thread_handle.is_done() and not self._os_thread_handle.is_bootstrapped():
1117+
if not self._os_thread_handle.is_done() and not self._os_thread_handle.is_running():
11191118
raise RuntimeError("cannot join thread before it is started")
11201119
if self is current_thread():
11211120
raise RuntimeError("cannot join current thread")
@@ -1178,7 +1177,7 @@ def is_alive(self):
11781177
11791178
"""
11801179
assert self._initialized, "Thread.__init__() not called"
1181-
return self._os_thread_handle.is_bootstrapped() and not self._os_thread_handle.is_done()
1180+
return self._os_thread_handle.is_running() and not self._os_thread_handle.is_done()
11821181

11831182
@property
11841183
def daemon(self):
@@ -1201,7 +1200,7 @@ def daemon(self, daemonic):
12011200
raise RuntimeError("Thread.__init__() not called")
12021201
if daemonic and not _daemon_threads_allowed():
12031202
raise RuntimeError('daemon threads are disabled in this interpreter')
1204-
if self._os_thread_handle.is_bootstrapped() or self._os_thread_handle.is_done():
1203+
if self._os_thread_handle.is_running() or self._os_thread_handle.is_done():
12051204
raise RuntimeError("cannot set daemon status of active thread")
12061205
self._daemonic = daemonic
12071206

@@ -1443,7 +1442,7 @@ def __init__(self):
14431442
_DeleteDummyThreadOnDel(self)
14441443

14451444
def is_alive(self):
1446-
if self._os_thread_handle.is_bootstrapped() and not self._os_thread_handle.is_done():
1445+
if self._os_thread_handle.is_running() and not self._os_thread_handle.is_done():
14471446
return True
14481447
raise RuntimeError("thread is not alive")
14491448

Modules/_threadmodule.c

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ typedef struct {
140140

141141
PyMutex mutex;
142142

143-
PyEvent thread_is_bootstrapped;
143+
PyEvent thread_is_running;
144144
// Set immediately before `thread_run` returns to indicate that the OS
145145
// thread is about to exit. This is used to avoid false positives when
146146
// detecting self-join attempts. See the comment in `ThreadHandle_join()`
@@ -233,7 +233,7 @@ ThreadHandle_new(void)
233233
self->os_handle = 0;
234234
self->has_os_handle = 0;
235235
self->thread_is_exiting = (PyEvent){0};
236-
self->thread_is_bootstrapped = (PyEvent){0};
236+
self->thread_is_running = (PyEvent){0};
237237
self->mutex = (PyMutex){_Py_UNLOCKED};
238238
self->once = (_PyOnceFlag){0};
239239
self->state = THREAD_HANDLE_NOT_STARTED;
@@ -326,7 +326,7 @@ _PyThread_AfterFork(struct _pythread_runtime_state *state)
326326
handle->once = (_PyOnceFlag){_Py_ONCE_INITIALIZED};
327327
handle->mutex = (PyMutex){_Py_UNLOCKED};
328328
_PyEvent_Notify(&handle->thread_is_exiting);
329-
_PyEvent_Notify(&handle->thread_is_bootstrapped);
329+
_PyEvent_Notify(&handle->thread_is_running);
330330
llist_remove(node);
331331
remove_from_shutdown_handles(handle);
332332
}
@@ -400,7 +400,7 @@ thread_run(void *boot_raw)
400400
}
401401
// Notify that bootstrap is done and failed (e.g. Memory error).
402402
set_thread_handle_state(handle, THREAD_HANDLE_FAILED);
403-
_PyEvent_Notify(&handle->thread_is_bootstrapped);
403+
_PyEvent_Notify(&handle->thread_is_running);
404404
}
405405
else {
406406
Py_DECREF(res);
@@ -719,10 +719,10 @@ PyThreadHandleObject_join(PyObject *op, PyObject *args)
719719
}
720720

721721
static PyObject *
722-
PyThreadHandleObject_is_bootstrapped(PyObject *op, PyObject *Py_UNUSED(dummy))
722+
PyThreadHandleObject_is_running(PyObject *op, PyObject *Py_UNUSED(dummy))
723723
{
724724
PyThreadHandleObject *self = PyThreadHandleObject_CAST(op);
725-
if (_PyEvent_IsSet(&self->handle->thread_is_bootstrapped)) {
725+
if (_PyEvent_IsSet(&self->handle->thread_is_running)) {
726726
Py_RETURN_TRUE;
727727
}
728728
else {
@@ -731,33 +731,26 @@ PyThreadHandleObject_is_bootstrapped(PyObject *op, PyObject *Py_UNUSED(dummy))
731731
}
732732

733733
static PyObject *
734-
PyThreadHandleObject_wait_bootstrapped(PyObject *op, PyObject *Py_UNUSED(dummy))
734+
PyThreadHandleObject_wait_running(PyObject *op, PyObject *Py_UNUSED(dummy))
735735
{
736736
PyThreadHandleObject *self = PyThreadHandleObject_CAST(op);
737-
PyEvent_Wait(&self->handle->thread_is_bootstrapped);
738-
Py_RETURN_NONE;
737+
PyEvent_Wait(&self->handle->thread_is_running);
738+
if (get_thread_handle_state(self->handle) == THREAD_HANDLE_FAILED) {
739+
Py_RETURN_FALSE;
740+
}
741+
else {
742+
Py_RETURN_TRUE;
743+
}
739744
}
740745

741746
static PyObject *
742-
PyThreadHandleObject_set_bootstrapped(PyObject *op, PyObject *Py_UNUSED(dummy))
747+
PyThreadHandleObject_set_running(PyObject *op, PyObject *Py_UNUSED(dummy))
743748
{
744749
PyThreadHandleObject *self = PyThreadHandleObject_CAST(op);
745-
_PyEvent_Notify(&self->handle->thread_is_bootstrapped);
750+
_PyEvent_Notify(&self->handle->thread_is_running);
746751
Py_RETURN_NONE;
747752
}
748753

749-
static PyObject *
750-
PyThreadHandleObject_is_failed(PyObject *op, PyObject *Py_UNUSED(dummy))
751-
{
752-
PyThreadHandleObject *self = PyThreadHandleObject_CAST(op);
753-
if (get_thread_handle_state(self->handle) == THREAD_HANDLE_FAILED) {
754-
Py_RETURN_TRUE;
755-
}
756-
else {
757-
Py_RETURN_FALSE;
758-
}
759-
}
760-
761754
static PyObject *
762755
PyThreadHandleObject_is_done(PyObject *op, PyObject *Py_UNUSED(dummy))
763756
{
@@ -791,10 +784,9 @@ static PyGetSetDef ThreadHandle_getsetlist[] = {
791784
static PyMethodDef ThreadHandle_methods[] = {
792785
{"join", PyThreadHandleObject_join, METH_VARARGS, NULL},
793786
{"_set_done", PyThreadHandleObject_set_done, METH_NOARGS, NULL},
794-
{"wait_bootstrapped", PyThreadHandleObject_wait_bootstrapped, METH_NOARGS, NULL},
795-
{"set_bootstrapped", PyThreadHandleObject_set_bootstrapped, METH_NOARGS, NULL},
796-
{"is_bootstrapped", PyThreadHandleObject_is_bootstrapped, METH_NOARGS, NULL},
797-
{"is_failed", PyThreadHandleObject_is_failed, METH_NOARGS, NULL},
787+
{"wait_running", PyThreadHandleObject_wait_running, METH_NOARGS, NULL},
788+
{"set_running", PyThreadHandleObject_set_running, METH_NOARGS, NULL},
789+
{"is_running", PyThreadHandleObject_is_running, METH_NOARGS, NULL},
798790
{"is_done", PyThreadHandleObject_is_done, METH_NOARGS, NULL},
799791
{0, 0}
800792
};
@@ -2522,7 +2514,7 @@ thread__make_thread_handle(PyObject *module, PyObject *identobj)
25222514
hobj->handle->ident = ident;
25232515
hobj->handle->state = THREAD_HANDLE_RUNNING;
25242516
PyMutex_Unlock(&hobj->handle->mutex);
2525-
_PyEvent_Notify(&hobj->handle->thread_is_bootstrapped);
2517+
_PyEvent_Notify(&hobj->handle->thread_is_running);
25262518
return (PyObject*) hobj;
25272519
}
25282520

0 commit comments

Comments
 (0)