Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: kernel threads



On Thursday, December 5, 2002, at 10:25 AM, Jim Magee wrote:
On Thursday, December 5, 2002, at 09:55 AM, Michael George wrote:
On Thursday, December 5, 2002, at 09:38 AM, Jim Magee wrote:

Well... but I want to be able to asynchronously kill it... What is happening is that this thread will start when our device has been idle for a while. That thread will then do Something, and Something can take quite a while (on the order of several ms). It takes long enough that we don't want this happening in our WorkLoop because it will hold all other workloop tasks up. In our case that holdup would be a Very Bad Thing.

But, if while Something is happening our device becomes active again, we want to stop the thread from doing Something and just exit. There are no resources or anything that the thread has to let go (our design is specifically to allow for this thread's activities to be interrupted at any time), it can just die.

If we set a state variable and check it from within the thread (so we'd know when to call IOExitThread()), we'd have to be constantly checking that variable. A bit of a PITA...

The terminate_thread( thread_act_t ) call is still in the system (at least until 10.2.3, eh? :) so there still exists a way to asynchronously kill a thread, it seems. However, how can I turn my IOThread/thread_t into a thread_act_t?


The thread_terminate(thread_act_t) call exists. And you can call it against kernel threads. But threads won't asynchronously terminate while running in kernel mode (they have to wait until they reach a clean point - usually when they are exiting kernel mode and returning to user-level). The thread is marked "aborted" so that any interruptible waits within the kernel will return immediately (so the thread can hurry back to its clean point). But since kernel-resident threads NEVER exit the kernel, they will never reach that particular clean point and notice that they should terminate themselves.

That's just how the kernel works. There's no getting around it. If you look at thread_terminate() code, you'll even see that even it creates an artificial clean-point if the calling thread is kernel resident and terminating itself. But there is no generic mechanism for asynchronously terminating another kernel thread.

Hmm, so the code for that is Open Source? I have a hard time remembering that parts of OS X are Open Source...

You must create your own "clean-point" for kernel-resident threads to notice that they have to exit themselves. Given this, you might as well then just use your own flag that is checked to get you into the clean point, and then have the thread terminate itself. If you need the abort semantic to get you out of prolonged waits, it is available through the thread_abort() and thread_abort_safely() API (which is used by BSD during signal delivery).

Your explanation is very helpful, thank you. I'd've preferred a different answer :-) but at least we know we have to handle it manually.

BTW, I would *strongly* recommend something to the effect of "you cannot really terminate kernel threads besides yourself -- even if you do, the thread is only marked aborted and not really terminated" Something like that would have saved me a couple hours yesterday...

Thanks!

-Michael
_______________________________________________
darwin-development mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/darwin-development
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: kernel threads (From: Jim Magee <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.