线程应该等待完成 kext 编程中的设备请求 [英] Thread should wait for commplete the device request in kext programming

查看:16
本文介绍了线程应该等待完成 kext 编程中的设备请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 mac 上开发设备驱动程序.我的问题是我们如何使设备请求异步到同步.就像我向设备发送一个发送封装的命令,并在收到中断管道的通知后使用获取封装的命令得到它的响应.所以我怎样才能让我的线程等到上述所有请求都没有完成(发送和获取).

I am developing a device driver on mac. my question is how can we make a device request asynchronous to synchronous. like i send a send encapsulated command to device and get it response using get encapsulated command after getting a notification on interrupt pipe. so how can i make my thread will wait until all above request is not completed (both send and get) .

推荐答案

你可能需要比这更具体一点.但一般来说,如果你需要一个线程休眠,直到你的某个函数在另一个线程上被调用,你可以使用 xnu 的事件系统.由于这是一个设备驱动程序,我假设您使用的是 I/O 套件 - 在这种情况下,IO 锁IOCommandGates 是最合适的.

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;

然后,当你的线程启动一些 I/O 时:

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);

在你的回调函数中:

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);

显然,如果您有多个同时进行的 I/O,则您的状态变量将需要针对每个上下文,等等.在内核中,异步设计通常比同步设计更好.但是我假设您对这种通用驱动程序设计了如指掌,并且已经权衡了利弊.

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.

IOCommandGate 睡眠/唤醒 API 非常相似,如果您在 IOWorkLoop 上进行线程同步,则应使用该 API.如果您不使用 I/O 工具包,您可能更喜欢使用 BSD 级别的锁/互斥 API,这就是实现 IOLock 的基础.

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.

这篇关于线程应该等待完成 kext 编程中的设备请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆