27 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/cpuset.h>
33 #include <sys/kthread.h>
35 #include <sys/mutex.h>
37 #include <sys/resourcevar.h>
38 #include <sys/rwlock.h>
39 #include <sys/signalvar.h>
41 #include <sys/unistd.h>
43 #include <sys/sched.h>
45 #include <vm/vm_extern.h>
47 #include <machine/stdarg.h>
60 const struct kproc_desc *kp = udata;
64 kp->global_procpp, 0, 0,
"%s", kp->arg0);
66 panic(
"kproc_start: %s: error %d", kp->arg0, error);
81 struct proc **newpp,
int flags,
int pages,
const char *fmt, ...)
89 panic(
"kproc_create called too soon");
91 error =
fork1(&thread0, RFMEM | RFFDG | RFPROC | RFSTOPPED | flags,
102 td = FIRST_THREAD_IN_PROC(p2);
103 p2->p_flag |= P_SYSTEM | P_KTHREAD;
104 td->td_pflags |= TDP_KTHREAD;
105 mtx_lock(&p2->p_sigacts->ps_mtx);
106 p2->p_sigacts->ps_flag |= PS_NOCLDWAIT;
107 mtx_unlock(&p2->p_sigacts->ps_mtx);
112 vsnprintf(p2->p_comm,
sizeof(p2->p_comm), fmt, ap);
116 vsnprintf(td->td_name,
sizeof(td->td_name), fmt, ap);
119 sched_clear_tdname(td);
123 cpu_set_fork_handler(td, func, arg);
133 if (!(flags & RFSTOPPED))
165 exit1(td, W_EXITCODE(ecode, 0));
180 if ((p->p_flag & P_KTHREAD) == 0) {
184 SIGADDSET(p->p_siglist, SIGSTOP);
186 return msleep(&p->p_siglist, &p->p_mtx, PPAUSE | PDROP,
"suspkp", timo);
197 if ((p->p_flag & P_KTHREAD) == 0) {
201 SIGDELSET(p->p_siglist, SIGSTOP);
211 while (SIGISMEMBER(p->p_siglist, SIGSTOP)) {
213 msleep(&p->p_siglist, &p->p_mtx, PPAUSE,
"kpsusp", 0);
230 const struct kthread_desc *kp = udata;
233 error =
kthread_add((
void (*)(
void *))kp->func, NULL,
234 NULL, kp->global_threadpp, 0, 0,
"%s", kp->arg0);
236 panic(
"kthread_start: %s: error %d", kp->arg0, error);
251 struct thread **newtdp,
int flags,
int pages,
const char *fmt, ...)
254 struct thread *newtd, *oldtd;
257 panic(
"kthread_add called too soon");
269 oldtd = FIRST_THREAD_IN_PROC(p);
271 bzero(&newtd->td_startzero,
272 __rangeof(
struct thread, td_startzero, td_endzero));
274 bcopy(&oldtd->td_startcopy, &newtd->td_startcopy,
275 __rangeof(
struct thread, td_startcopy, td_endcopy));
279 vsnprintf(newtd->td_name,
sizeof(newtd->td_name), fmt, ap);
287 cpu_set_upcall(newtd, oldtd);
289 cpu_set_fork_handler(newtd, func, arg);
291 newtd->td_pflags |= TDP_KTHREAD;
292 newtd->td_ucred =
crhold(p->p_ucred);
295 p->p_flag |= P_HADTHREADS;
296 newtd->td_sigmask = oldtd->td_sigmask;
301 TD_SET_CAN_RUN(newtd);
302 thread_unlock(oldtd);
311 if (!(flags & RFSTOPPED)) {
314 thread_unlock(newtd);
326 p = curthread->td_proc;
335 rw_wlock(&tidhash_lock);
337 if (p->p_numthreads == 1) {
339 rw_wunlock(&tidhash_lock);
342 LIST_REMOVE(curthread, td_hash);
343 rw_wunlock(&tidhash_lock);
364 if ((td->td_pflags & TDP_KTHREAD) == 0)
374 td->td_flags |= TDF_KTH_SUSP;
376 return (msleep(&td->td_flags, &p->p_mtx, PPAUSE | PDROP,
"suspkt",
395 if ((td->td_pflags & TDP_KTHREAD) == 0)
400 td->td_flags &= ~TDF_KTH_SUSP;
420 if ((td->td_pflags & TDP_KTHREAD) == 0)
421 panic(
"%s: curthread is not a valid kthread", __func__);
429 while (td->td_flags & TDF_KTH_SUSP) {
431 msleep(&td->td_flags, &p->p_mtx, PPAUSE,
"ktsusp", 0);
438 struct proc **procptr,
struct thread **tdptr,
439 int flags,
int pages,
const char *procname,
const char *fmt, ...)
448 procptr, flags, pages,
"%s", procname);
451 td = FIRST_THREAD_IN_PROC(*procptr);
455 vsnprintf(td->td_name,
sizeof(td->td_name), fmt, ap);
458 sched_clear_tdname(td);
466 tdptr, flags, pages,
"%s", buf);
void thread_link(struct thread *td, struct proc *p)
void sched_prio(struct thread *td, u_char prio)
void kthread_start(void *udata) const
int kthread_add(void(*func)(void *), void *arg, struct proc *p, struct thread **newtdp, int flags, int pages, const char *fmt,...)
int kproc_kthread_add(void(*func)(void *), void *arg, struct proc **procptr, struct thread **tdptr, int flags, int pages, const char *procname, const char *fmt,...)
void panic(const char *fmt,...)
int kproc_resume(struct proc *p)
void sched_fork_thread(struct thread *td, struct thread *childtd)
int vsnprintf(char *str, size_t size, const char *format, va_list ap)
int fork1(struct thread *td, int flags, int pages, struct proc **procp, int *procdescp, int pdflags)
int kproc_suspend(struct proc *p, int timo)
void sched_add(struct thread *td, int flags)
struct thread * thread_alloc(int pages)
void kproc_exit(int ecode)
void exit1(struct thread *td, int rv)
void tidhash_add(struct thread *td)
struct ucred * crhold(struct ucred *cr)
void sched_user_prio(struct thread *td, u_char prio)
void kproc_suspend_check(struct proc *p)
int kproc_create(void(*func)(void *), void *arg, struct proc **newpp, int flags, int pages, const char *fmt,...)
void kproc_start(void *udata) const
int kthread_suspend(struct thread *td, int timo)
int cpuset_setthread(lwpid_t id, cpuset_t *mask)
void proc_reparent(struct proc *child, struct proc *parent)
void kthread_suspend_check()
int kthread_resume(struct thread *td)