tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: rfc: kthread_join(9)



On Tue, Apr 07, 2009 at 05:17:16PM -0500, David Young wrote:

> I have written kthread_join(9).  It waits for a kernel thread to finish.
> See attachment.  The idea is that when a device driver detaches from its
> device, should indicate to its worker threads that they should stop, and
> then wait for those threads to *actually* stop.  Comments?

kthreads are created detached so cannot be joined. One needs either a flag
to kthread_create() to make them attached, or do this always and add code
to join existing kthreads that exit.

kthread_join() may create an implicit order between locks used by the
driver/component and proc_lock/p_lock. However kthread_create() already does
the same, and in the situations it's called, it's not a problem. I think we
should document this.

> +int
> +kthread_join(lwp_t *l)
> +{
> +     int error;
> +

I suggest:

        KASSERT(l->l_proc == &proc0);
        KASSERT((l->l_flag & LW_SYSTEM) != 0);
        KASSERT((l->l_pflag & LP_INTR) == 0);
        KASSERT((l->l_prflag & LPR_DETACHED) == 0);

        /* Unlocked: caller must not force a state transition to/from IDL. */
        if (l->l_stat == LSIDL) {
                lwp_exit(l);
                return;
        }

> +     mutex_enter(proc0.p_lock);
> +     error = lwp_wait1(curlwp, l->l_lid, NULL, 0);
> +     mutex_exit(proc0.p_lock);

It needs the EXITCONTROL flag to prevent interruptable waits. As a rule
kthreads don't make these. For clarity a seperate flag would be better.

Why not capture and return the value passed to kthread_exit()?


Home | Main Index | Thread Index | Old Index