Search code examples
multithreadingmacostimeoutdrivermutex

How to implement recursive mutex with timeout in the OS X kernel?


I'm porting a windows device driver to Mac OS X. The windows code uses KMUTEX. This is a recursive lock that can be acquired multiple times by the same thread. Other threads must wait to acquire it, but the acquisition will fail if the timeout occurs.

The I/O Kit's IORecursiveLock doesn't do timeouts. I can use BSD locks or Mach locks. The BSD part of the kernel offers condition variables with msleep() and wakeup().

I'm not clear if a condition variable will do what I want.

sem_timedwait(3) on BSD is in userspace.

Keith Shortridge provides a userspace set_timedwait implementation. One thread calls sem_wait() while another thread that sends a signal to the first if a timeout occurs.

I could port Shortridge's code to the kernel but I don't want to risk screwing it up. Google yields no other insight. Can you give me a clue?


Solution

  • This link might be helpful, by the documentation this has what you need, maybe the method wait_result_tlck_mtx_sleep_deadline: https://developer.apple.com/library/content/documentation/Darwin/Conceptual/KernelProgramming/synchronization/synchronization.html#//apple_ref/doc/uid/TP30000905-CH218-BEHJDFCA