The following issue has been SUBMITTED. ====================================================================== http://austingroupbugs.net/view.php?id=1216 ====================================================================== Reported By: mikecrowe Assigned To: ajosey ====================================================================== Project: 1003.1(2008)/Issue 7 Issue ID: 1216 Category: System Interfaces Type: Enhancement Request Severity: Comment Priority: normal Status: Under Review Name: Mike Crowe Organization: User Reference: Section: pthread Page Number: 0 Line Number: 0 Interp Status: --- Final Accepted Text: ====================================================================== Date Submitted: 2018-11-26 18:53 UTC Last Modified: 2018-11-26 18:53 UTC ====================================================================== Summary: Adding clockid parameter to functions that accept absolute struct timespec timeouts Description: POSIX contains several functions that support waiting with an absolute timeout passed as a struct timespec. This time must almost always be measured against CLOCK_REALTIME. (pthread_cond_timedwait also supports a single alternative clock specified at construction time via pthread_condattr_setclock.)
Embedded systems and desktop computers may not have a good source of accurate time, particularly at boot. This can result in CLOCK_REALTIME warping by a large amount when the real time is known. In such situations, CLOCK_REALTIME is not a good choice for expressing timeouts. A member of the Android libc team has reported[1] that this has been the cause of real world bugs in Android applications. I've worked on software at different companies where we had to work around this problem. The C++ standard provides std::condition_variable::wait_until and std::timed_mutex::try_lock_until methods which support arbitrary clocks. Current implementations that build upon POSIX convert these clocks to CLOCK_REALTIME, which can cause race conditions when CLOCK_REALTIME is warped. The C++ standard requires the clock to be specified at the time of the wait, which means that pthread_condattr_setclock isn't useful. The above problems can be solved by adding variants of the affected functions that take an extra clockid_t parameter to indicate the clock that should be used. Initially, implementations would be required to only support passing CLOCK_REALTIME which would make adding support straightforward. Support for CLOCK_MONOTONIC would be suggested, and implementations would be free to support other clocks if they wished. This proposal is the result of a thread[2] on the mailing list and my original defect report[3] only covering pthread_cond_timedwait. Various naming options for the new functions were discussed[4] and the following names are based on one of the more popular options. In all cases the clock immediately precedes the timespec timeout. int pthread_mutex_clocklock( pthread_mutex_t *restrict mutex, clockid_t clock, const struct timespec *restrict abstime) int pthread_rwlock_clockrdlock( pthread_rwlock_t *restrict rwlock, clockid_t clock, const struct timespec *restrict abstime) int pthread_rwlock_clockwrlock( pthread_rwlock_t *restrict rwlock, clockid_t clock, const struct timespec *restrict abstime) int pthread_cond_clockwait( pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, clockid_t clock, const struct timespec *restrict abstime) int sem_clockwait( sem_t *restrict sem, clockid_t clock, const struct timespec *restrict abstime) ssize_t mq_clockreceive( mqd_t mqdes, char *restrict msg_ptr, size_t msg_len, unsigned int *restrict msg_prio, clockid_t clock, const struct timespec *restrict abs_timeout) int mq_clocksend( mqd_t mqdes, const char *restrict msg_ptr, size_t msg_len, unsigned int msg_prio, clockid_t clock, const struct timespec *restrict abs_timeout) These functions all behave the same as their "timed" equivalents, but measure the timeout against the specified clock rather than CLOCK_REALTIME. If passed an unsupported clock, these functions indicate failure in the same way as their "timed" equivalents and return/set errno to ENOTSUP as required. Support for a clock by one function does not require that the clock be supported by any of the others. [1] https://www.mail-archive.com/austin-group-l@opengroup.org/msg02902.html [2] https://www.mail-archive.com/austin-group-l@opengroup.org/msg02813.html [3] http://austingroupbugs.net/view.php?id=1164 [4] https://www.mail-archive.com/austin-group-l@opengroup.org/msg03034.html Desired Action: The addition of the above functions, or ones that provide equivalent functionality. ====================================================================== Issue History Date Modified Username Field Change ====================================================================== 2018-11-26 18:53 mikecrowe New Issue 2018-11-26 18:53 mikecrowe Status New => Under Review 2018-11-26 18:53 mikecrowe Assigned To => ajosey 2018-11-26 18:53 mikecrowe Name => Mike Crowe 2018-11-26 18:53 mikecrowe Section => pthread 2018-11-26 18:53 mikecrowe Page Number => 0 2018-11-26 18:53 mikecrowe Line Number => 0 ======================================================================