Thread should wait for commplete the device request in kext programming

坚强是说给别人听的谎言 提交于 2019-12-02 12:51:25

You'll probably have to be a bit more specific than that. But in general, if you need a thread to sleep until some function of yours is called on another thread, you can use xnu's event system. As this is a device driver, I assume you're using the I/O Kit - in that case, the IO Locks or IOCommandGates are the most appropriate.

Using explicit locks, in your driver instance, you'd have something like:

IOLock* lock;
bool cleared;

You'll initialise these somewhere:

lock = IOLockAlloc();
cleared = false;

Then, when your thread initiates some I/O:

IOLockLock(lock);
cleared = false;
startYourIO(yourCallbackFunction);
while (!cleared)
{
  IOLockSleep(
    lock,
    &cleared, // using address of status variable as event
    THREAD_UNINT);
}
IOLockUnlock(lock);

In yourCallbackFunction:

IOLockLock(lock);
cleared = true;
IOLockWakeup(
  lock,
  &cleared, // this must match the event pointer used in sleep
  true); // flip to false to wake up all waiting threads, not just 1
IOLockUnlock(lock);

Obviously, if you have multiple simultaneous I/Os going, your status variable will need to be per-context, etc. And in the kernel, asynchronous design is often better than synchronous. But I assume you know all about this kind of general driver design and have weighed up the pros and cons.

The IOCommandGate sleep/wake API is very similar and should be used if you're doing thread synchronisation on your IOWorkLoop. If you're not using the I/O Kit, you may prefer to use the BSD-level lock/mutex API, which is what the IOLock is implemented on anyway.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!