Boost C++ Libraries: Ticket #4533: timespec translation fails for times before 1970 https://svn.boost.org/trac10/ticket/4533 <p> I had a target system that had always a failed assertion with a call to condition_variable::timed_wait(); </p> <p> I found out, that pthread_cond_timedwait does not accept a timespec argument that has negative tv_nsec. </p> <p> Therefore I suggest the following patch, which works for me. </p> <p> =================================================================== --- /boost/boost_1_43_0/boost/thread/pthread/timespec.hpp (revision 1769) +++ /boost/boost_1_43_0/boost/thread/pthread/timespec.hpp (working copy) @@ -26,6 +26,13 @@ </p> <blockquote> <p> timeout.tv_sec=time_since_epoch.total_seconds(); timeout.tv_nsec=(long)(time_since_epoch.fractional_seconds()*(1000000000l/time_since_epoch.ticks_per_second())); </p> </blockquote> <p> + + if(timeout.tv_nsec &lt; 0L) + { + timeout.tv_sec -= 1L; + timeout.tv_nsec += 1000000000L; + } + </p> <blockquote> <p> return timeout; </p> </blockquote> <blockquote> <p> } </p> </blockquote> <blockquote> <p> } </p> </blockquote> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4533 Trac 1.4.3 Philipp Huber <phu@…> Thu, 12 Aug 2010 13:36:21 GMT <link>https://svn.boost.org/trac10/ticket/4533#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4533#comment:1</guid> <description> <p> by the way, the system time was 1919 and so timeout.tv_sec as well as timeout.tv_nsec became negative. tv_sec are accepted negative but not tv_nsec. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Sun, 29 Aug 2010 00:34:43 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4533#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4533#comment:2</guid> <description> <p> 1000000000L is a magic number. I suggest that it be replaced using a const long. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Wed, 17 Nov 2010 20:35:10 GMT</pubDate> <title>cc set https://svn.boost.org/trac10/ticket/4533#comment:3 https://svn.boost.org/trac10/ticket/4533#comment:3 <ul> <li><strong>cc</strong> <span class="trac-author">viboes</span> added </li> </ul> <p> I would say that in this case an exception must be thrown. </p> Ticket viboes Sun, 04 Dec 2011 00:14:49 GMT owner, status, milestone changed; cc deleted https://svn.boost.org/trac10/ticket/4533#comment:4 https://svn.boost.org/trac10/ticket/4533#comment:4 <ul> <li><strong>cc</strong> <span class="trac-author">viboes</span> removed </li> <li><strong>owner</strong> changed from <span class="trac-author">Anthony Williams</span> to <span class="trac-author">viboes</span> </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">Boost 1.44.0</span> → <span class="trac-field-new">To Be Determined</span> </li> </ul> Ticket viboes Wed, 07 Dec 2011 21:47:51 GMT <link>https://svn.boost.org/trac10/ticket/4533#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4533#comment:5</guid> <description> <p> I would like to understand how the seconds are positive and the nanoseconds are negative. Could you post an example? </p> </description> <category>Ticket</category> </item> <item> <author>phu@…</author> <pubDate>Thu, 08 Dec 2011 07:28:16 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4533#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4533#comment:6</guid> <description> <p> Hi viboes </p> <p> Well, 16 month is quite a while ago... We do use boost intensively but have fixed already 3 bugs in different parts of boost, all sent to this bug tracker tool but with little or no response. So we keep on fixing every new release... </p> <p> We used boost for embedded linux on a hardware with broken realtime clock. So the system time was often randomly startet and in this case it was 1919. </p> <p> The timeout resulted from a calculation of some time value minus another time value. let's say, we scheduled a timer to a specific absolute time. The scheduler thread inspects this time value, compares it with the current posix time and calculates how long it needs to sleep until the timer will be signaled. If the scheduler is busy, it can happen that the sleep timeout, which is basically the subtraction of two times, becomes negative. But it only happend with the low year numbers of 1919. At least, that is what I think caused the timeout to become negative. But I can't tell you for sure. After 16 Month. </p> <p> I understand that you would like to fix the cause of the negative timeout but as long as the timeout type allows negative values, you can not avoid a call with negative values in general anyway. </p> <p> What I can tell you for sure is, that the bugfix, which I suggested works well for us. Whether or not it is fixing the real cause of the bug. </p> <p> cheers phu </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Fri, 09 Dec 2011 11:13:26 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4533#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4533#comment:7</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/4533#comment:6" title="Comment 6">phu@…</a>: </p> <blockquote class="citation"> <p> Hi viboes </p> <p> Well, 16 month is quite a while ago... </p> </blockquote> <p> It is always better late than never ;-) </p> <blockquote class="citation"> <p> We do use boost intensively but have fixed already 3 bugs in different parts of boost, all sent to this bug tracker tool but with little or no response. So we keep on fixing every new release... </p> </blockquote> <p> I'm trying to change this. </p> <blockquote class="citation"> <p> We used boost for embedded linux on a hardware with broken realtime clock. So the system time was often randomly startet and in this case it was 1919. </p> <p> The timeout resulted from a calculation of some time value minus another time value. let's say, we scheduled a timer to a specific absolute time. The scheduler thread inspects this time value, compares it with the current posix time and calculates how long it needs to sleep until the timer will be signaled. If the scheduler is busy, it can happen that the sleep timeout, which is basically the subtraction of two times, becomes negative. But it only happend with the low year numbers of 1919. At least, that is what I think caused the timeout to become negative. But I can't tell you for sure. After 16 Month. </p> <p> I understand that you would like to fix the cause of the negative timeout but as long as the timeout type allows negative values, you can not avoid a call with negative values in general anyway. </p> <p> What I can tell you for sure is, that the bugfix, which I suggested works well for us. Whether or not it is fixing the real cause of the bug. </p> <p> cheers phu </p> </blockquote> <p> Thanks for the explanation. IIUC,and if you where using directly the underlying platform you will need to make sure that the time given satisfy the interface constraints and when you did a difference you will ensure that the seconds and nanoseconds will be positive, isn't it? </p> <p> I think that you will do the same when using Boost.Chrono. The best I can do is to add an assertion so that the modification doesn't add code on release mode. </p> <p> Let me know what do you think of this resolution? </p> <p> Best, Vicente </p> </description> <category>Ticket</category> </item> <item> <author>phu@…</author> <pubDate>Fri, 09 Dec 2011 13:03:44 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4533#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4533#comment:8</guid> <description> <p> Hi Vicente </p> <p> Thanks for taking up the bug. Right you are, better late than never. I stripped the answer a bit to get to the point: </p> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/4533#comment:7" title="Comment 7">viboes</a>: </p> <blockquote class="citation"> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/4533#comment:6" title="Comment 6">phu@…</a>: </p> <p> Thanks for the explanation. IIUC,and if you where using directly the underlying platform you will need to make sure that the time given satisfy the interface constraints ... </p> </blockquote> <p> Well, we have a correct linux system time. Even if it shows the year 1919, I think it perfectly satisfy the interface constraints. Well, I might be a bit naive on that point. </p> <blockquote class="citation"> <p> ... and when you did a difference you will ensure that the seconds and nanoseconds will be positive, isn't it? </p> </blockquote> <p> We subtract two ptime values. I expect the value to be usable for further operations. I would not check every subtraction for its correct value (whether its components are positive or not), that is what I expect of the operation to do. </p> <blockquote class="citation"> <p> I think that you will do the same when using Boost.Chrono. The best I can do is to add an assertion so that the modification doesn't add code on release mode. </p> </blockquote> <p> Sorry, but I might be misunderstanding this point. If we end up with an assertion instead of a working program flow, I'm not exactly happy with the solution. Then I prefer you do nothing at all and we continue the way we did so far. </p> <blockquote class="citation"> <p> Let me know what do you think of this resolution? </p> <p> Best, Vicente </p> </blockquote> <p> Thanks for looking into it anyway :-) Cheers Philipp </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Fri, 09 Dec 2011 17:44:45 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4533#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4533#comment:9</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/4533#comment:8" title="Comment 8">phu@…</a>: </p> <blockquote class="citation"> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/4533#comment:7" title="Comment 7">viboes</a>: </p> <blockquote class="citation"> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/4533#comment:6" title="Comment 6">phu@…</a>: </p> <p> Thanks for the explanation. IIUC,and if you where using directly the underlying platform you will need to make sure that the time given satisfy the interface constraints ... </p> </blockquote> <p> Well, we have a correct linux system time. Even if it shows the year 1919, I think it perfectly satisfy the interface constraints. Well, I might be a bit naive on that point. </p> <blockquote class="citation"> <p> ... and when you did a difference you will ensure that the seconds and nanoseconds will be positive, isn't it? </p> </blockquote> <p> We subtract two ptime values. I expect the value to be usable for further operations. I would not check every subtraction for its correct value (whether its components are positive or not), that is what I expect of the operation to do. </p> <blockquote class="citation"> </blockquote> </blockquote> <p> What do you use to make this difference? Boost.Date? </p> <blockquote class="citation"> <blockquote class="citation"> <p> I think that you will do the same when using Boost.Chrono. The best I can do is to add an assertion so that the modification doesn't add code on release mode. </p> </blockquote> <p> Sorry, but I might be misunderstanding this point. If we end up with an assertion instead of a working program flow, I'm not exactly happy with the solution. Then I prefer you do nothing at all and we continue the way we did so far. </p> </blockquote> <p> The question is if the ptime given as parameter must be a valid one, that is, if it is a precondition. IMO, this is the spirit of a lot of standard functions, so that the implementation is not required to test the validity of the informations received in the parameters. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sat, 10 Dec 2011 16:02:26 GMT</pubDate> <title>type changed; cc set https://svn.boost.org/trac10/ticket/4533#comment:10 https://svn.boost.org/trac10/ticket/4533#comment:10 <ul> <li><strong>cc</strong> <span class="trac-author">viboes</span> added </li> <li><strong>type</strong> <span class="trac-field-old">Bugs</span> → <span class="trac-field-new">Support Requests</span> </li> </ul> <p> Moved to support request until resolution clarified </p> Ticket viboes Sat, 31 Dec 2011 15:23:24 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/4533#comment:11 https://svn.boost.org/trac10/ticket/4533#comment:11 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">worksforme</span> </li> </ul> <p> Closed. Please answer the questions and reopen it if you consider necessary. </p> Ticket