I am working with a system that has implemented two posix functions
mq_timedreceive() and pthread_cond_timedwait()
Both of these functions use an absolute timeout based on CLOCK_REALTIME. This clock is getting changed at various times during system startup and can go backwards or forward by 10s of seconds to hours.
Open Group says:
If the Timers option is supported, the timeout shall be based on the CLOCK_REALTIME clock; if the Timers option is not supported, the timeout shall be based on the system clock as returned by the time() function. (mq_timedreceive)
For cases when the system clock is advanced discontinuously by an operator, it is expected that implementations process any timed wait expiring at an intervening time as if that time had actually occurred. (pthread_cond_timedwait()).
However this doesn't give any guidance in the case where the clock is set backwards.
QNX solved this problem by providing
mq_timedreceive_monotonic().
The mq_timedreceive_monotonic() function is a QNX Neutrino extension; it's similar to mq_timedreceive(), but it uses CLOCK_MONOTONIC, so the timeout isn't affected by changes to the system time.
Is there a good way to achieve the QNX functionality in linux?
For mq_timedreceive() I can use mq_receive() and poll(). But for the condition variable I have not come up with a clean way. I could use itimer and signal but that seems way too complex.
The other solution, of course, is don't adjust the clock or use a different timeofday clock that is derived from CLOCK_MONOTONIC but I have no liberty to change the design.