Page 1 of 1

How to make mutex support timeout?

Posted: Sun Oct 22, 2017 2:58 pm
by yenWu
Hello,

I'm hacking ChibiOS with mutex synchronization mechanism and testing demo code in `demo/RT-Posix-simulator`. It's amazing that mutex support PIP and recursive token in such code base.

However, There is a little weird about why mutex in ChibiOS/RT doesn't support timeout? I found that semaphore and condition variable support timeout. If I want to do something like `pthread_mutex_timeoutlock()`, is it possible?

Re: How to make mutex support timeout?

Posted: Sun Oct 22, 2017 4:12 pm
by Giovanni
Hi,

It would be possible to add timeout to mutexes but it would be computationally complex because PIP, on timeout the thread would have to "recall" the priority boost that it gave to the thread(s) holding the mutex.

This is why it is not implemented, unlike all other mechanisms.

Without this "priority un-boost" thing it would be easy to add but the behavior would not be correct.

Giovanni

Re: How to make mutex support timeout?

Posted: Tue Jan 21, 2020 4:15 am
by tridge
If anyone has implemented mutex with timeout and priority inheritance then please let me know. It would be very useful for ArduPilot. As giovanni said, it does look quite complex to implement, but would be a really nice feature.