سؤال

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 !

هل كانت مفيدة؟

المحلول

You want the function IOLockSleepDeadline(), declared in <IOKit/IOLocks.h>.

You set up a single IOLock somewhere with IOLockAlloc() before you begin. Then, threads 1 and 2 lock the IOLock with IOLockLock() and immediately relinquish the lock and go to sleep by calling IOLockSleepDeadline(). When thread 3 is ready, it calls IOLockWakeup() (with oneThread = 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.

نصائح أخرى

The documentation of method IOLocks::IOLockLock states the following:

Lock the mutex. If the lock is held by any thread, block waiting for its unlock. This function may block and so should not be called from interrupt level or while a spin lock is held. Locking the mutex recursively from one thread will result in deadlock.

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top