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

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

问题描述

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

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套件-在这种情况下,

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

在yourCallbackFunction中:

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

很显然,如果要同时执行多个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上执行线程同步,则应使用.如果您不使用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天全站免登陆