<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - libc++ condition variables should use a steady clock"
   href="https://bugs.llvm.org/show_bug.cgi?id=39489">39489</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>libc++ condition variables should use a steady clock
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libc++
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>All Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>danalbert@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Refiling from an internal bug on behalf of tomcherry@:

libc++ condition variables have two issues with regards to how they wait with
timeout.

1) wait_for() takes a relative timeout and as per section 30.2.4.3, this
timeout should be measured with a steady clock: "The member functions whose
names end in _for take an argument that specifies a duration. These functions
produce relative timeouts. Implementations should use a steady clock to measure
time for these functions."

However, this is not the case.  These condition variables are implemented on
top of pthread condition variables and use the default initialization, which
uses CLOCK_REALTIME for all timeouts.  This clock is liable to change if the
user changes time on the device and this will cause early or delayed timeouts. 
Delayed timeouts have caused real bugs in Android.

2) wait_until() takes an absolute timeout and as per section 30.2.4.4, this
timeout should be measured with the clock specified in the timeout parameter to
the function, e.g. steady_clock (CLOCK_MONOTONIC) or system_clock
(CLOCK_REALTIME): "The member functions whose names end in _until take an
argument that specifies a time point. These functions produce absolute
timeouts. Implementations should use the clock specified in the time point to
measure time for these functions."

This is also not the case.  Unfortunately, this is not possible to do with the
pthreads API.  It is, however, possible if the pthreads API is avoided and
libc++ implements these condition variables directly with the futex system
call.

Tom also uploaded a change drafting the fix for this:
<a href="https://reviews.llvm.org/D44945">https://reviews.llvm.org/D44945</a>. Spoke with Eric about this and there's still
some work that needs to be done, so this serves as a tracking bug.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>