Skip to content

Commit 54b0ce0

Browse files
committed
Remove 'daemonness' as a property of a thread.
1 parent 9d8d526 commit 54b0ce0

2 files changed

Lines changed: 5 additions & 107 deletions

File tree

Python/pylifecycle.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,7 +2014,7 @@ _Py_Finalize(_PyRuntimeState *runtime)
20142014
// Wrap up existing "threading"-module-created, non-daemon threads.
20152015
wait_for_thread_shutdown(tstate);
20162016

2017-
// Wrap up non-daemon native threads
2017+
// Wait for the interpreter's reference count to reach zero
20182018
wait_for_native_shutdown(tstate->interp);
20192019

20202020
// Make any remaining pending calls.
@@ -2433,7 +2433,7 @@ Py_EndInterpreter(PyThreadState *tstate)
24332433
// Wrap up existing "threading"-module-created, non-daemon threads.
24342434
wait_for_thread_shutdown(tstate);
24352435

2436-
// Wrap up non-daemon native threads
2436+
// Wait for the interpreter's reference count to reach zero
24372437
wait_for_native_shutdown(tstate->interp);
24382438

24392439
// Make any remaining pending calls.

Python/pystate.c

Lines changed: 3 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,8 +1610,6 @@ new_threadstate(PyInterpreterState *interp, int whence)
16101610
return NULL;
16111611
}
16121612
#endif
1613-
((PyThreadState *)tstate)->daemon = 1;
1614-
16151613
/* We serialize concurrent creation to protect global state. */
16161614
HEAD_LOCK(interp->runtime);
16171615

@@ -1823,7 +1821,7 @@ shutting_down_natives(PyInterpreterState *interp)
18231821
}
18241822

18251823
static void
1826-
decrement_daemon_count(PyInterpreterState *interp)
1824+
decref_interpreter(PyInterpreterState *interp)
18271825
{
18281826
assert(interp != NULL);
18291827
struct _Py_finalizing_threads *finalizing = &interp->threads.finalizing;
@@ -1869,10 +1867,6 @@ tstate_delete_common(PyThreadState *tstate, int release_gil)
18691867
decrement_stoptheworld_countdown(&runtime->stoptheworld);
18701868
}
18711869
}
1872-
if (tstate->daemon == 0
1873-
&& tstate != (PyThreadState *)&interp->_initial_thread) {
1874-
decrement_daemon_count(interp);
1875-
}
18761870

18771871
#if defined(Py_REF_DEBUG) && defined(Py_GIL_DISABLED)
18781872
// Add our portion of the total refcount to the interpreter's total.
@@ -3270,113 +3264,17 @@ void
32703264
PyInterpreterState_Release(PyInterpreterState *interp)
32713265
{
32723266
assert(interp != NULL);
3273-
decrement_daemon_count(interp);
3274-
}
3275-
3276-
static int
3277-
tstate_set_daemon(PyThreadState *tstate, PyInterpreterState *interp, int daemon)
3278-
{
3279-
assert(tstate != NULL);
3280-
assert(interp != NULL);
3281-
assert(tstate->interp == interp);
3282-
assert(daemon == 1 || daemon == 0);
3283-
struct _Py_finalizing_threads *finalizing = &interp->threads.finalizing;
3284-
PyMutex_Lock(&finalizing->mutex);
3285-
if (_Py_atomic_load_ssize_relaxed(&finalizing->countdown) == 0) {
3286-
PyMutex_Unlock(&finalizing->mutex);
3287-
return -1;
3288-
}
3289-
if (_PyEvent_IsSet(&finalizing->finished)) {
3290-
/* Native threads have already finalized */
3291-
PyMutex_Unlock(&finalizing->mutex);
3292-
return -1;
3293-
}
3294-
_Py_atomic_add_ssize(&finalizing->countdown, 1);
3295-
PyMutex_Unlock(&finalizing->mutex);
3296-
tstate->daemon = daemon;
3297-
return 1;
3298-
}
3299-
3300-
int
3301-
PyThreadState_SetDaemon(int daemon)
3302-
{
3303-
PyThreadState *tstate = PyThreadState_Get();
3304-
if (daemon != 0 && daemon != 1) {
3305-
Py_FatalError("daemon must be 0 or 1");
3306-
}
3307-
PyInterpreterState *interp = tstate->interp;
3308-
assert(interp != NULL);
3309-
if (tstate == (PyThreadState *)&interp->_initial_thread) {
3310-
Py_FatalError("thread cannot be the main thread");
3311-
}
3312-
if (tstate->daemon == daemon) {
3313-
return 0;
3314-
}
3315-
3316-
return tstate_set_daemon(tstate, interp, daemon);
3267+
decref_interpreter(interp);
33173268
}
33183269

33193270
int
33203271
PyThreadState_Ensure(PyInterpreterState *interp)
33213272
{
3322-
assert(interp != NULL);
3323-
_Py_ensured_tstate *entry = PyMem_RawMalloc(sizeof(_Py_ensured_tstate));
3324-
if (entry == NULL) {
3325-
decrement_daemon_count(interp);
3326-
return -1;
3327-
}
3328-
PyThreadState *save = _PyThreadState_GET();
3329-
if (save != NULL && save->interp == interp) {
3330-
entry->was_daemon = save->daemon;
3331-
entry->next = save->ensured;
3332-
entry->prior_tstate = NULL;
3333-
save->ensured = entry;
3334-
// Setting 'daemon' to 0 passes off the interpreter's reference
3335-
save->daemon = 0;
3336-
return 0;
3337-
}
3338-
3339-
PyThreadState *tstate = PyThreadState_New(interp);
3340-
if (tstate == NULL) {
3341-
PyMem_RawFree(entry);
3342-
return -1;
3343-
}
3344-
tstate->daemon = 0;
3345-
entry->was_daemon = 0;
3346-
entry->prior_tstate = save;
3347-
entry->next = NULL;
3348-
tstate->ensured = entry;
3349-
PyThreadState_Swap(tstate);
3350-
33513273
return 0;
33523274
}
33533275

33543276
void
33553277
PyThreadState_Release(void)
33563278
{
3357-
PyThreadState *tstate = _PyThreadState_GET();
3358-
_Py_EnsureTstateNotNULL(tstate);
3359-
_Py_ensured_tstate *ensured = tstate->ensured;
3360-
if (ensured == NULL) {
3361-
Py_FatalError("PyThreadState_Release() called without PyThreadState_Ensure()");
3362-
}
3363-
if (ensured->prior_tstate != NULL) {
3364-
assert(ensured->was_daemon == 0);
3365-
PyThreadState_Clear(tstate);
3366-
PyThreadState_Swap(ensured->prior_tstate);
3367-
PyMem_RawFree(ensured);
3368-
PyThreadState_Delete(tstate);
3369-
return;
3370-
}
3371-
3372-
tstate->ensured = ensured->next;
3373-
tstate->daemon = ensured->was_daemon;
3374-
PyMem_RawFree(ensured);
3375-
if (tstate->ensured == NULL) {
3376-
PyThreadState_Clear(tstate);
3377-
PyThreadState_Swap(NULL);
3378-
PyThreadState_Delete(tstate);
3379-
} else {
3380-
decrement_daemon_count(tstate->interp);
3381-
}
3279+
return 0;
33823280
}

0 commit comments

Comments
 (0)