Boost C++ Libraries: Ticket #8494: Added comments how to solve the priority inversion problem https://svn.boost.org/trac10/ticket/8494 <p> Hello! </p> <p> This is <em>not</em> a bug but just an added documentation: </p> <p> With Real Time OSes there does exist a general problematic scenario, called "priority inversion" (see <a class="ext-link" href="http://en.wikipedia.org/wiki/Priority_inversion"><span class="icon">​</span>http://en.wikipedia.org/wiki/Priority_inversion</a>). When using POSIX semaphores a certain default is being used by every OS if not some deviating attributes are used to instanciate a semaphore object. boost in general uses mainly the OS-defaults, which basically is fine. However for RTP's (Real Time Processes) VxWorks does use a POSIX-semaphore object which does <em>not</em> adhere to the priority inversion scenario. This could possibly lead to a deadlock! </p> <p> I added some documentation how to circumvent this problem. This is being done by changing the defaults the OS uses for semaphores, and thus does not include any changes in boost's code (and could thus not be comitted into boost) but on the contrary includes changes in the OS code itself and afterwards rebuilding the OS (which is supported for VxWorks). </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8494 Trac 1.4.3 Peter Brockamp <p.brockamp@…> Thu, 25 Apr 2013 15:18:29 GMT attachment set https://svn.boost.org/trac10/ticket/8494 https://svn.boost.org/trac10/ticket/8494 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">vxworks.hpp.diff</span> </li> </ul> <p> Added documentation to the OS configuration how to deal with the priority inversion scenario </p> Ticket anonymous Fri, 26 Apr 2013 08:34:36 GMT <link>https://svn.boost.org/trac10/ticket/8494#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8494#comment:1</guid> <description> <p> I'm not against applying this patch, but I wonder if anyone will find this information there? Is this basically an issue when using Boost.Threads? If so the thread lib docs might be a better place, or is it more general than that? </p> </description> <category>Ticket</category> </item> <item> <author>Peter Brockamp <p.brockamp@…></author> <pubDate>Fri, 26 Apr 2013 10:09:48 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8494#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8494#comment:2</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/8494#comment:1" title="Comment 1">anonymous</a>: </p> <blockquote class="citation"> <p> Is this basically an issue when using Boost.Threads? If so the thread lib docs might be a better place, or is it more general than that? </p> </blockquote> <p> No, this is <em>not</em> boost.threads specific! It may even be not considered boost specific. It is a general problem when using semaphores with an real time OS. I try to explain: </p> <p> POSIX defines that when using semaphores the OS should use a suitable default, except the creator of the semaphore explicitely uses a semaphore attribute different from the default. For detailed information you might take a look at this link: </p> <p> <a class="ext-link" href="http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread.h.html"><span class="icon">​</span>http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread.h.html</a> </p> <p> Here especially see functions <code>pthread_mutex_init()</code>, <code>pthread_mutexattr_init()</code> and <code>pthread_mutexattr_setprotocol()</code>. </p> <p> Now, the default for VxWorks Real Time Processes is <code>PTHREAD_PRIO_NONE</code>. This may be OK for certain applications, for others it will be not: If low- and high-priority tasks will race for ownership of a resource (in fact this is not limited to a mutex!) this might lead to a deadlock with the high priority task waiting for the resource ownership, owned by the low-priority task, which on the other hand is unable to release the resource because it is not being scheduled, i.e. it does not get the cpu to do its work. </p> <p> The solution for this dilema is called "priority inheritance protocol" and includes a temporary increase of the priority of the low-priority task until it releases the resource. So, if instead the mutex uses <code>PTHREAD_PRIO_INHERIT</code> this will pose an additional protocol overhead burden on the caller (the scheduler might reschedule a low priority task to a high priority), but it will not break functionality. </p> <p> Now, the problems with potentially <em>all</em> boost libraries are the following: </p> <ul><li>Several libraries of boost do use semaphores. And this might change with every new release of boost. Maybe one library stops using semaphores, maybe another starts doing it. </li><li>As they are libraries by definition you cannot know the way the user intends to use them. Maybe for the specific application <code>PTHREAD_PRIO_NONE</code> would be sufficient, maybe <code>PTHREAD_PRIO_INHERIT</code> would be required. The library has <em>no</em> way to find out. Thus it must fall back to a version which might not give optimized performance but that is secure and does not deadlock. This means is has to use <code>PTHREAD_PRIO_INHERIT</code> as a protocol. </li><li>I think the alternative of giving the library user the option to chose the semaphore protocol during instanciation of a library template is <em>not</em> an option. This will blead out very implementation and OS-specific details to the user - surely a definitive no-go for a boost library. </li></ul><p> So, two options how to solve this do remain: </p> <ol class="loweralpha"><li>Change every instanciation of semaphores inside boost. This can be done and is not very difficult. But it easily breaks when libraries change their usage of semaphores. </li><li>Change the default the underlying OS uses so that it always uses <code>PTHREAD_PRIO_INHERIT</code>. How to do this is what I added in the configuration remarks for VxWorks, in the hope that people using VxWorks will configure their boost version and take a look at the file. The problem here is that it requires you to change the OS, not boost. Wind River delivers VxWorks also as source code, so this is not a problem in itself, but it needs to be done explicitely by every user. </li></ol><p> So, to cut a long story short: </p> <ul><li>I would suggest to commit this comments to vxworks.hpp, it doesn't hurt and it could be useful if someone stumbles upon it. </li><li>If you tell me where this should belong I volunteer to add a small chapter "VxWorks specialities" to the boost documentation, containing these hints. </li><li>I suppose this problem does not only apply to VxWorks, but to other real time OS as well, depending on what defaults they use for POSIX-semaphores. E.g. QNX could potentially suffer from the very same effects, but I have no acces to this OS here, so this is just suspicion. But if so, this should be mentioned in the documentation, too, if possible with a similar solution. </li><li>And as we are in this topic already, one last remark concerning POSIX-attributes, here regarding threads: For a real time OS it would be beneficial to be able to chose the attributes used for a thread (specifically its priority). Unfortunately, boost.threads does <em>not</em> give you this option. You only have the option to change the priority of a thread that is already running (non-portable via <code>get_native_handle()</code>). But this might very well be too late for a real time OS and this fact made me set aside boost.threads, it is not very usable for a RTOS in its current implementation. I know this has been discussed in length here already and this limitations have their reason (i.e. portability), so I didn't want to restart this over again. But in fact this is a kind of nuisance, because it means setting aside <code>join()</code> and other very useful functionality as well, which is a pitty. Actually I'm thinking about whether it would be possible to create a portable interface for very OS-specific details, like thread attributes, mutex attributes, etc. If this could be done somehow it would greatly enhance functionality. </li></ul> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Fri, 26 Apr 2013 10:36:18 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8494#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8494#comment:3</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/84053" title="Apply patch from #8494. Refs #8494.">[84053]</a>) Apply patch from <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8494" title="#8494: Patches: Added comments how to solve the priority inversion problem (closed: fixed)">#8494</a>. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8494" title="#8494: Patches: Added comments how to solve the priority inversion problem (closed: fixed)">#8494</a>. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Fri, 26 Apr 2013 10:47:53 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8494#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8494#comment:4</guid> <description> <p> I've committed the patch, also added a stub Wiki entry for this here: <a class="ext-link" href="https://svn.boost.org/trac/boost/wiki/Guidelines/VxWorks"><span class="icon">​</span>https://svn.boost.org/trac/boost/wiki/Guidelines/VxWorks</a> and sent you an invitation to sign up for the Trac system which will let you edit the Wiki. Can I get you to add the above information there? </p> <p> Many thanks, John Maddock. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Peter Brockamp</dc:creator> <pubDate>Mon, 29 Apr 2013 12:08:19 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8494#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8494#comment:5</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/8494#comment:4" title="Comment 4">johnmaddock</a>: </p> <blockquote class="citation"> <p> ... Can I get you to add the above information there? </p> </blockquote> <p> I added the documentation from the file and put on some additional information how to set up everything. Please have a look at it whether this is OK! There's one link inside to wikipedia, I hope it's OK to link to outward sites? If not I will remove this again. </p> <p> Best regards </p> <p> Peter </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Thu, 13 Jun 2013 16:24:08 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/8494#comment:6 https://svn.boost.org/trac10/ticket/8494#comment:6 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Thanks! </p> Ticket