git: 8c30192804eb - stable/13 - Add thread_reap_barrier()
Konstantin Belousov
kib at FreeBSD.org
Mon Jun 7 16:33:08 UTC 2021
The branch stable/13 has been updated by kib:
URL: https://cgit.FreeBSD.org/src/commit/?id=8c30192804eb91b2b976e92d0ca52fd034520062
commit 8c30192804eb91b2b976e92d0ca52fd034520062
Author: Konstantin Belousov <kib at FreeBSD.org>
AuthorDate: 2021-05-25 18:51:00 +0000
Commit: Konstantin Belousov <kib at FreeBSD.org>
CommitDate: 2021-06-07 16:32:42 +0000
Add thread_reap_barrier()
(cherry picked from commit f62c7e54e9cc692603081328597ba0ba0d1f21cf)
(cherry picked from commit d3f7975fcb346ea28dde079a9c04cff5ef20a8d7)
---
sys/kern/kern_thread.c | 29 +++++++++++++++++++++++++++++
sys/sys/proc.h | 1 +
2 files changed, 30 insertions(+)
diff --git a/sys/kern/kern_thread.c b/sys/kern/kern_thread.c
index 7370fd919203..48aac8e057b8 100644
--- a/sys/kern/kern_thread.c
+++ b/sys/kern/kern_thread.c
@@ -709,6 +709,35 @@ thread_reap_callout_cb(void *arg __unused)
thread_reap_callout_cb, NULL);
}
+/*
+ * Calling this function guarantees that any thread that exited before
+ * the call is reaped when the function returns. By 'exited' we mean
+ * a thread removed from the process linkage with thread_unlink().
+ * Practically this means that caller must lock/unlock corresponding
+ * process lock before the call, to synchronize with thread_exit().
+ */
+void
+thread_reap_barrier(void)
+{
+ struct task *t;
+
+ /*
+ * First do context switches to each CPU to ensure that all
+ * PCPU pc_deadthreads are moved to zombie list.
+ */
+ quiesce_all_cpus("", PDROP);
+
+ /*
+ * Second, fire the task in the same thread as normal
+ * thread_reap() is done, to serialize reaping.
+ */
+ t = malloc(sizeof(*t), M_TEMP, M_WAITOK);
+ TASK_INIT(t, 0, thread_reap_task_cb, t);
+ taskqueue_enqueue(taskqueue_thread, t);
+ taskqueue_drain(taskqueue_thread, t);
+ free(t, M_TEMP);
+}
+
/*
* Allocate a thread.
*/
diff --git a/sys/sys/proc.h b/sys/sys/proc.h
index 7a2210778a39..b0fc13a449cb 100644
--- a/sys/sys/proc.h
+++ b/sys/sys/proc.h
@@ -1177,6 +1177,7 @@ int thread_create(struct thread *td, struct rtprio *rtp,
void thread_exit(void) __dead2;
void thread_free(struct thread *td);
void thread_link(struct thread *td, struct proc *p);
+void thread_reap_barrier(void);
int thread_single(struct proc *p, int how);
void thread_single_end(struct proc *p, int how);
void thread_stash(struct thread *td);
More information about the dev-commits-src-all
mailing list