IOCommandGate and IOThread
IOCommandGate and IOThread
- Subject: IOCommandGate and IOThread
- From: "Duane Murphy" <email@hidden>
- Date: Fri, 18 Apr 2008 12:03:25 -0700
As suggested, I'd should be using IOCommandGate to wait for a worker
thread to complete. I've not found any documentation or notes for how to
do this, so I am writing this up in order to document and determine if
this is the correct way to do this.
Scenario - An IOKit kext that uses a separate background thread to
process data independent of any other outside influence. When the kext
is signaled to stop this thread, how does the kext stop the thread and
wait for it to exit?
(The following code is uncompiled, untested, & unproven)
void myKext::startThread()
{
me->_threadComplete = false;
IOCreateThread( threadMain, this );
}
/* static */ void myKext::threadMain( myKext* me )
{
while ( me->_keepWorking )
{
doWork(); // ...
}
// _threadComplete is from OSMemberFunctionCast
me->_commandGate->runAction( _threadComplete, ... );
}
void myKext::threadComplete()
{
me->_commandGate->commandWakeup( &me->_threadComplete, true /* one
thread */ );
}
void myKext::stopThread()
{
_commandGate->runAction( _reallyStopThread, ... );
}
void myKext::reallyStopThread()
{
_keepWorking = false;
while( ! _threadComplete )
_commandGate->commandSleep( *_threadComplete );
}
The important point seems to be that one must be in the gate to sleep
and wake in order to be on the workloop. Hence the indirection through
IOCommandGate::runAction().
Assuming the rest of the driver hierarchy is similarly using the
workloop and command gate, this seems like it will work.
Did I get this right? Is there a better way? Are there other ways?
Thanks for the lesson,
...Duane
_______________________________________________
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