From 817c187171696ff9aa9f07164c43fb4c99a6d787 Mon Sep 17 00:00:00 2001 From: claudio Date: Tue, 25 Apr 2023 18:14:06 +0000 Subject: [PATCH] Rename ps_refcnt to ps_threadcnt in struct process and implement P_HASSIBLING() using this count. OK mvs@ mpi@ --- sys/kern/kern_exit.c | 11 ++++++----- sys/kern/kern_fork.c | 7 ++++--- sys/sys/proc.h | 9 ++++----- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/sys/kern/kern_exit.c b/sys/kern/kern_exit.c index 7860c65eeda..85c5d58dc23 100644 --- a/sys/kern/kern_exit.c +++ b/sys/kern/kern_exit.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_exit.c,v 1.210 2022/12/29 01:36:36 guenther Exp $ */ +/* $OpenBSD: kern_exit.c,v 1.211 2023/04/25 18:14:06 claudio Exp $ */ /* $NetBSD: kern_exit.c,v 1.39 1996/04/22 01:38:25 christos Exp $ */ /* @@ -159,9 +159,10 @@ exit1(struct proc *p, int xexit, int xsig, int flags) SCHED_LOCK(s); TAILQ_REMOVE(&pr->ps_threads, p, p_thr_link); SCHED_UNLOCK(s); + if ((p->p_flag & P_THREAD) == 0) { /* main thread gotta wait because it has the pid, et al */ - while (pr->ps_refcnt > 1) + while (pr->ps_threadcnt > 1) tsleep_nsec(&pr->ps_threads, PWAIT, "thrdeath", INFSLP); if (pr->ps_flags & PS_PROFIL) stopprofclock(pr); @@ -328,9 +329,9 @@ exit1(struct proc *p, int xexit, int xsig, int flags) /* just a thread? detach it from its process */ if (p->p_flag & P_THREAD) { /* scheduler_wait_hook(pr->ps_mainproc, p); XXX */ - if (--pr->ps_refcnt == 1) + if (--pr->ps_threadcnt == 1) wakeup(&pr->ps_threads); - KASSERT(pr->ps_refcnt > 0); + KASSERT(pr->ps_threadcnt > 0); } /* Release the thread's read reference of resource limit structure. */ @@ -814,7 +815,7 @@ process_zap(struct process *pr) if (otvp) vrele(otvp); - KASSERT(pr->ps_refcnt == 1); + KASSERT(pr->ps_threadcnt == 1); if (pr->ps_ptstat != NULL) free(pr->ps_ptstat, M_SUBPROC, sizeof(*pr->ps_ptstat)); pool_put(&rusage_pool, pr->ps_ru); diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c index f0f279d9f64..6c28078ebc9 100644 --- a/sys/kern/kern_fork.c +++ b/sys/kern/kern_fork.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_fork.c,v 1.246 2023/02/10 14:34:17 visa Exp $ */ +/* $OpenBSD: kern_fork.c,v 1.247 2023/04/25 18:14:06 claudio Exp $ */ /* $NetBSD: kern_fork.c,v 1.29 1996/02/09 18:59:34 christos Exp $ */ /* @@ -180,7 +180,7 @@ process_initialize(struct process *pr, struct proc *p) pr->ps_mainproc = p; TAILQ_INIT(&pr->ps_threads); TAILQ_INSERT_TAIL(&pr->ps_threads, p, p_thr_link); - pr->ps_refcnt = 1; + pr->ps_threadcnt = 1; p->p_p = pr; /* give the process the same creds as the initial thread */ @@ -539,7 +539,7 @@ thread_fork(struct proc *curp, void *stack, void *tcb, pid_t *tidptr, /* other links */ p->p_p = pr; - pr->ps_refcnt++; + pr->ps_threadcnt++; /* local copies */ p->p_fd = pr->ps_fd; @@ -560,6 +560,7 @@ thread_fork(struct proc *curp, void *stack, void *tcb, pid_t *tidptr, SCHED_LOCK(s); TAILQ_INSERT_TAIL(&pr->ps_threads, p, p_thr_link); + /* * if somebody else wants to take us to single threaded mode, * count ourselves in. diff --git a/sys/sys/proc.h b/sys/sys/proc.h index d014a558415..60003397cd8 100644 --- a/sys/sys/proc.h +++ b/sys/sys/proc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: proc.h,v 1.339 2023/01/16 07:09:11 guenther Exp $ */ +/* $OpenBSD: proc.h,v 1.340 2023/04/25 18:14:06 claudio Exp $ */ /* $NetBSD: proc.h,v 1.44 1996/04/22 01:23:21 christos Exp $ */ /*- @@ -241,8 +241,8 @@ struct process { #define BOGO_PC (u_long)-1 /* End area that is copied on creation. */ -#define ps_endcopy ps_refcnt - int ps_refcnt; /* Number of references. */ +#define ps_endcopy ps_threadcnt + u_int ps_threadcnt; /* Number of threads. */ struct timespec ps_start; /* starting uptime. */ struct timeout ps_realit_to; /* [m] ITIMER_REAL timeout */ @@ -403,8 +403,7 @@ struct proc { #define SONPROC 7 /* Thread is currently on a CPU. */ #define P_ZOMBIE(p) ((p)->p_stat == SDEAD) -#define P_HASSIBLING(p) (TAILQ_FIRST(&(p)->p_p->ps_threads) != (p) || \ - TAILQ_NEXT((p), p_thr_link) != NULL) +#define P_HASSIBLING(p) ((p)->p_p->ps_threadcnt > 1) /* * These flags are per-thread and kept in p_flag -- 2.20.1