Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 27 Sep 2002 11:35:49 -0400 (EDT)
From:      John Baldwin <jhb@FreeBSD.org>
To:        Mark Murray <mark@grondar.za>
Cc:        arch@FreeBSD.org
Subject:   Re: [patch] module-with-thread exit routine.
Message-ID:  <XFMail.20020927113549.jhb@FreeBSD.org>
In-Reply-To: <200209271257.g8RCvmwf032794@grimreaper.grondar.org>

next in thread | previous in thread | raw e-mail | index | archive | help

On 27-Sep-2002 Mark Murray wrote:
>> > Attached is a patch to the kthread code. I've been running this
>> > for at least three months.
>> > 
>> > Comments?
>> 
>> You don't need it.  Look at the crash module I have.  It has a thread
>> and I committed an extra wakeup() inside of exit1() for kthread's a
>> while ago to handle just this case.  Here's an excerpt from the crash
>> module code:
> 
> This looks good. But I have some questions:
> 
>> In the kthread's main loop (it usually gets events via a sysctl from
>> userland):
>> 
>> static void
>> crash_thread(void *arg)
>> {
>>         int ev;
>> 
>>         while (1) {
>>                 mtx_lock(&event_mtx);
>>                 while ((ev = event) == 0)
>>                         cv_wait(&event_cv, &event_mtx);
>>                 event = 0;
>>                 mtx_unlock(&event_mtx);
> 
> What is the cv_wait for? For the random_kthread, can I just ignore it?

Well, the way this module works is it has a kthread that executes events
that are signaled to them via the 'event' variable.  The user can use
a sysctl to write a value that gets put in 'event' and then the sysctl
handler does a 'cv_signal'.  The unload() function is similar.  Basically,
MOD_UNLOAD needs to poke the thread somehow to tell it to commit suicide.
For the crash module, I use a special value of the 'event' variable.

>>                 ...
>>                 switch (ev) {
>>                 case -1:
>>                         mtx_lock(&Giant);
>>                         kthread_exit(0);
>>                         break;
>>                 ...
>>                 }
> 
> This looks like I can just call kthread_exit() directly with no shenanigans.
> Am I on the right track?
> 
>> Here's the unload() function that the module calls for MOD_UNLOAD:
>> 
>> static int
>> unload(void *arg)
>> {
>> 
>>         mtx_lock(&event_mtx);
>>         event = -1;
>>         cv_signal(&event_cv);
>>         msleep(kthread, &event_mtx, PWAIT, "crshun", 0);
>>         mtx_unlock(&event_mtx);
>>         mtx_destroy(&event_mtx);
>>         cv_destroy(&event_cv);
> 
> I'm not sure of the event_cv relevance. Can I not just set the event (or
> equivalent) variable and wait for event_mtx to become available?

Think of the cv as a wakeup to the kthread to tell it to go examine
'event' and see that it needs to die.  The trick here is you need to
make sure you don't miss the wakeup.  Thus, you need to atomically
signal the thread to die and then block waiting for it to die.
 
-- 

John Baldwin <jhb@FreeBSD.org>  <><  http://www.FreeBSD.org/~jhb/
"Power Users Use the Power to Serve!"  -  http://www.FreeBSD.org/

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-arch" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?XFMail.20020927113549.jhb>