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. Priority inheritance<br />

L<br />

(5)<br />

S<br />

(20)<br />

H<br />

(12)<br />

Three threads at different priorities.<br />

Currently, H is running. S, a higher-priority server thread, doesn’t have anything to do<br />

right now so it’s waiting for a message and is blocked in MsgReceive(). L would like<br />

to run but is at a lower priority than H, which is running. Everything is as you’d<br />

expect, right?<br />

Now H has decided that it would like to go to sleep for 100 milliseconds — perhaps it<br />

needs to wait for some slow hardware. At this point, L is running.<br />

This is where things get interesting.<br />

As part of its normal operation, L sends a message to the server thread S, causing S to<br />

go READY and (because it’s the highest-priority thread that’s READY) to start running.<br />

Unfortunately, the message that L sent to S was “Compute pi to 5000 decimal places.”<br />

Obviously, this takes more than 100 milliseconds. Therefore, when H’s 100<br />

milliseconds are up and H goes READY, guess what? It won’t run, because S is<br />

READY and at a higher priority!<br />

What happened is that a low-priority thread prevented a higher-priority thread from<br />

running by leveraging the CPU via an even higher-priority thread. This is priority<br />

inversion.<br />

To fix it, we need to talk about priority inheritance. A simple fix is to have the server,<br />

S, inherit the priority of the client thread:<br />

SEND-blocked<br />

L<br />

(5)<br />

S<br />

(5)<br />

H<br />

(12)<br />

READY<br />

REPLY-blocked<br />

Blocked threads.<br />

April 30, 2009 Chapter 2 • Message Passing 131

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

Saved successfully!

Ooh no, something went wrong!