Re: How can I get notified that a kernel thread has been terminated ?
Re: How can I get notified that a kernel thread has been terminated ?
- Subject: Re: How can I get notified that a kernel thread has been terminated ?
- From: Ken Hornstein <email@hidden>
- Date: Fri, 19 Aug 2011 10:09:26 -0400
>I have tried to use a mutex (which is what an IOLock is as far as I can see)
>and no luck. It sometimes works, sometimes it doesn't... It doesn't look
>like it is the good way to detect a thread termination.
While I agree with you about that, I think that perhaps you've got another
problem.
The filesystem I ported from Linux created what I would charitably call
an "assload" of threads. I used mutexes to wait for the threads to
finish up before the module was unloaded. I can unload my module just
fine. And I don't hear a bunch of people complaining about this
problem; if it was a serious problem then you would think it would come
up a lot (or at least it would come up at least _once_ by someone
else).
>From what I remember you never actually _verified_ that this was
the actual problem with a debugger; it was just your best guess. You also
never posted a crash log or anything else. I'm willing to believe that
your module is different enough that it might be triggering something that
doesn't come up for most people, but I think the onus is on you at this
point to prove that this is really the core issue (if my memory is wrong,
then my apologies).
And you know what? I took a longer look at this (using the kernel
source that anyone can download); it turns out that I was slightly
wrong. While there isn't an exported function that waits for a thread
to exit (there is an internal one called thread_wait()), you have a
possible option. If you look at the places where thread_wait() is
used, you've got thread_terminate() and thread_suspend().
Unfortunately, you can't use thread_terminate() on kernel threads other
than yourself, so that leaves thread_suspend().
If you try to suspend a thread that has stopped, thread_suspend() will
return KERN_TERMINATED. You don't actually want the thread to be
suspended, so what you could do is a loop where you do a
thread_suspend() and a thread_resume() until thread_suspend() (or
thread_resume()) returns KERN_TERMINATED.
I would caution that I'm not 100% sure this will work properly; this
depends on a thread being marked as not active, and I haven't convinced
myself that happens when a thread function simply exits. But that doesn't
work, then I _think_ this should work:
In your waitee thread:
- Set your mutex to indicate that you're done with whatever you need to be
done with.
- Call thread_terminate() on yourself.
In your waiter thread:
- Wait for the above mutex
- Do the aforementioned thread_suspend()/thread_resume() loop until you get
a KERN_TERMINATED return code (or an error).
--Ken
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-kernel mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden