26.02.2014 Views

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

© 2009, <strong>QNX</strong> <strong>Software</strong> <strong>Systems</strong> GmbH & Co. KG. Advanced topics<br />

CLOCK_MONOTONIC<br />

As you can imagine, there could be a problem if we try to implement a mutex that<br />

times out in 30 seconds. Let’s go through the steps. At 11:22:00 (<strong>Neutrino</strong> time) we<br />

decide that we’re going to try and lock a mutex, but we only want to block for a<br />

maximum of 30 seconds. Since the pthread_mutex_timedlock() function takes an<br />

absolute time, we perform a calculation: we add 30 seconds to the current time, giving<br />

us 11:22:30. If we follow the example above, we would wake up at 11:22:30, which<br />

means that we would have only locked the mutex for 25 seconds, instead of the full 30.<br />

The POSIX people thought about this, and the solution they came up <strong>with</strong> was to make<br />

the pthread_mutex_timedlock() function be based on CLOCK_MONOTONIC instead<br />

of CLOCK_REALTIME. This is built in to the pthread_mutex_timedlock() function and<br />

isn’t something that you can change.<br />

They way CLOCK_MONOTONIC works is that its timebase is never adjusted. The<br />

impact of that is that regardless of what time it is in the real world, if you base a timer<br />

in CLOCK_MONOTONIC and add 30 seconds to it (and then do whatever adjustments<br />

you want to the time), the timer will expire in 30 elapsed seconds.<br />

The clock source CLOCK_MONOTONIC has the following characteristics:<br />

• always increasing count<br />

• based on real time<br />

• starts at zero<br />

The important thing about the clock starting at zero is that this is a different “epoch”<br />

(or “base”) than CLOCK_REALTIME’s epoch of Jan 1 1970, 00:00:00 GMT. So, even<br />

though both clocks run at the same rate, their values are not interchangeable.<br />

So what does CLOCK_SOFTTIME do?<br />

If we wanted to sort our clock sources by “hardness” we’d have the following<br />

ordering. You can think of CLOCK_MONOTONIC as being a freight train — it doesn’t<br />

stop for anyone. Next on the list is CLOCK_REALTIME, because it can be pushed<br />

around a bit (as we saw <strong>with</strong> the time adjustment). Finally, we have<br />

CLOCK_SOFTTIME, which we can push around a lot.<br />

The main use of CLOCK_SOFTTIME is for things that are “soft” — things that aren’t<br />

going to cause a critical failure if they don’t get done. CLOCK_SOFTTIME is “active”<br />

only when the CPU is running. (Yes, this does sound obvious :-) but wait!) When the<br />

CPU is powered down due to Power Management detecting that nothing is going to<br />

happen for a little while, CLOCK_SOFTTIME gets powered down as well!<br />

Here’s a timing chart showing the three clock sources:<br />

April 30, 2009 Chapter 3 • Clocks, Timers, and <strong>Getting</strong> a Kick Every So Often 161

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!