与 IO/Kit 的同步原语
我正在 IO/Kit 中寻找等待/信号同步原语,其工作方式如下:
Thread1 : wait(myEvent) // 阻塞 thread1
Thread2 : wait(myEvent) // 阻塞 thread2
Thread3 : signal(myEvent) // 释放 thread1 之一或 thread2
这不能使用 IOLock 来完成,因为锁定/解锁操作将由不同的线程进行,根据我读过的一些文档,这是一个坏主意。
线程1、2、3可以是用户线程或内核线程。
我还希望等待操作有一个可选的超时。
感谢您的帮助 !
I'm looking for a wait/signal synchronization primitive in IO/Kit working like :
Thread1 : wait(myEvent) // Blocking thread1
Thread2 : wait(myEvent) // Blocking thread2
Thread3 : signal(myEvent) // Release one of thread1 or thread2
This can't be done using an IOLock since the lock/unlock operations would be made from different threads, which is a bad idea according to some doc I've read.
Thread1, 2, 3 can be user threads or kernel threads.
I'd also like to have an optional time out with the wait operation.
Thanks for your help !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在
中声明的函数IOLockSleepDeadline()
。在开始之前,您可以使用
IOLockAlloc()
在某处设置一个IOLock
。然后,线程 1 和 2 使用IOLockLock()
锁定 IOLock,并立即通过调用IOLockSleepDeadline()
放弃锁定并进入睡眠状态。当线程 3 准备就绪时,它会调用IOLockWakeup()
(如果您只想唤醒单个线程,则使用oneThread = true
)。这会导致线程1或2醒来并立即获取锁(因此它们需要再次解锁或睡眠)。IOLockSleep()
的工作原理类似,但没有超时。您可以使用 IOCommandGate 的 commandSleep() 方法 如果您的驱动程序已经以
IOWorkLoop
为中心,则该方法可能更合适。You want the function
IOLockSleepDeadline()
, declared in<IOKit/IOLocks.h>
.You set up a single
IOLock
somewhere withIOLockAlloc()
before you begin. Then, threads 1 and 2 lock the IOLock withIOLockLock()
and immediately relinquish the lock and go to sleep by callingIOLockSleepDeadline()
. When thread 3 is ready, it callsIOLockWakeup()
(withoneThread = true
if you only want to wake a single thread). This causes thread 1 or 2 to wake up and immediately acquire the lock (so they need to Unlock or sleep again).IOLockSleep()
works similarly, but without the timeout.You can do something similar using the IOCommandGate's commandSleep() method which may be more appropriate if your driver already is centred around an
IOWorkLoop
.方法的文档
IOLocks::IOLockLock
声明以下内容:因此它肯定会阻塞其他线程(T1 和 T2),直到持有锁的线程释放它(T3)。它似乎不支持的一件事是超时。
The documentation of method
IOLocks::IOLockLock
states the following:So it will certainly do block the other threads (T1 and T2) until the thread holding the lock releases it (T3). One thing that it doesn't seem to support is the timeout.