Merge "Reduce flake in pthread mutex timed lock tests." into main
diff --git a/libc/private/bionic_constants.h b/libc/private/bionic_constants.h
index 6274fe2..ce484d8 100644
--- a/libc/private/bionic_constants.h
+++ b/libc/private/bionic_constants.h
@@ -16,6 +16,7 @@
#pragma once
+#define US_PER_S 1'000'000LL
#define NS_PER_S 1'000'000'000LL
// Size of the shadow call stack. This can be small because these stacks only
diff --git a/libc/private/bionic_time_conversions.h b/libc/private/bionic_time_conversions.h
index c6b3c78..ce7de0d 100644
--- a/libc/private/bionic_time_conversions.h
+++ b/libc/private/bionic_time_conversions.h
@@ -26,8 +26,7 @@
* SUCH DAMAGE.
*/
-#ifndef _BIONIC_TIME_CONVERSIONS_H
-#define _BIONIC_TIME_CONVERSIONS_H
+#pragma once
#include <errno.h>
#include <time.h>
@@ -35,20 +34,21 @@
#include "private/bionic_constants.h"
-__BEGIN_DECLS
+bool timespec_from_timeval(timespec& ts, const timeval& tv);
+void timespec_from_ms(timespec& ts, const int ms);
-__LIBC_HIDDEN__ bool timespec_from_timeval(timespec& ts, const timeval& tv);
-__LIBC_HIDDEN__ void timespec_from_ms(timespec& ts, const int ms);
+void timeval_from_timespec(timeval& tv, const timespec& ts);
-__LIBC_HIDDEN__ void timeval_from_timespec(timeval& tv, const timespec& ts);
+void monotonic_time_from_realtime_time(timespec& monotonic_time, const timespec& realtime_time);
+void realtime_time_from_monotonic_time(timespec& realtime_time, const timespec& monotonic_time);
-__LIBC_HIDDEN__ void monotonic_time_from_realtime_time(timespec& monotonic_time,
- const timespec& realtime_time);
+static inline int64_t to_ns(const timespec& ts) {
+ return ts.tv_sec * NS_PER_S + ts.tv_nsec;
+}
-__LIBC_HIDDEN__ void realtime_time_from_monotonic_time(timespec& realtime_time,
- const timespec& monotonic_time);
-
-__END_DECLS
+static inline int64_t to_us(const timeval& tv) {
+ return tv.tv_sec * US_PER_S + tv.tv_usec;
+}
static inline int check_timespec(const timespec* ts, bool null_allowed) {
if (null_allowed && ts == nullptr) {
@@ -76,5 +76,3 @@
}
}
#endif
-
-#endif
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index 2bf755b..7223784 100644
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -45,6 +45,7 @@
#include <android-base/test_utils.h>
#include "private/bionic_constants.h"
+#include "private/bionic_time_conversions.h"
#include "SignalUtils.h"
#include "utils.h"
@@ -2437,23 +2438,25 @@
ts.tv_sec = -1;
ASSERT_EQ(ETIMEDOUT, lock_function(&m, &ts));
- // check we wait long enough for the lock.
+ // Check we wait long enough for the lock before timing out...
+
+ // What time is it before we start?
ASSERT_EQ(0, clock_gettime(clock, &ts));
- const int64_t start_ns = ts.tv_sec * NS_PER_S + ts.tv_nsec;
-
- // add a second to get deadline.
+ const int64_t start_ns = to_ns(ts);
+ // Add a second to get deadline, and wait until we time out.
ts.tv_sec += 1;
-
ASSERT_EQ(ETIMEDOUT, lock_function(&m, &ts));
+ // What time is it now we've timed out?
+ timespec ts2;
+ clock_gettime(clock, &ts2);
+ const int64_t end_ns = to_ns(ts2);
+
// The timedlock must have waited at least 1 second before returning.
- clock_gettime(clock, &ts);
- const int64_t end_ns = ts.tv_sec * NS_PER_S + ts.tv_nsec;
- ASSERT_GT(end_ns - start_ns, NS_PER_S);
+ ASSERT_GE(end_ns - start_ns, NS_PER_S);
// If the mutex is unlocked, pthread_mutex_timedlock should succeed.
ASSERT_EQ(0, pthread_mutex_unlock(&m));
-
ASSERT_EQ(0, clock_gettime(clock, &ts));
ts.tv_sec += 1;
ASSERT_EQ(0, lock_function(&m, &ts));
@@ -2474,12 +2477,19 @@
#endif // __BIONIC__
}
-TEST(pthread, pthread_mutex_clocklock) {
+TEST(pthread, pthread_mutex_clocklock_MONOTONIC) {
#if defined(__BIONIC__)
pthread_mutex_timedlock_helper(
CLOCK_MONOTONIC, [](pthread_mutex_t* __mutex, const timespec* __timeout) {
return pthread_mutex_clocklock(__mutex, CLOCK_MONOTONIC, __timeout);
});
+#else // __BIONIC__
+ GTEST_SKIP() << "pthread_mutex_clocklock not available";
+#endif // __BIONIC__
+}
+
+TEST(pthread, pthread_mutex_clocklock_REALTIME) {
+#if defined(__BIONIC__)
pthread_mutex_timedlock_helper(
CLOCK_REALTIME, [](pthread_mutex_t* __mutex, const timespec* __timeout) {
return pthread_mutex_clocklock(__mutex, CLOCK_REALTIME, __timeout);
diff --git a/tests/sys_time_test.cpp b/tests/sys_time_test.cpp
index ff9271f..b0e52aa 100644
--- a/tests/sys_time_test.cpp
+++ b/tests/sys_time_test.cpp
@@ -23,6 +23,7 @@
#include <android-base/file.h>
+#include "private/bionic_time_conversions.h"
#include "utils.h"
// http://b/11383777
@@ -147,14 +148,6 @@
ASSERT_EQ(0, syscall(__NR_gettimeofday, &tv2, nullptr));
// What's the difference between the two?
- tv2.tv_sec -= tv1.tv_sec;
- tv2.tv_usec -= tv1.tv_usec;
- if (tv2.tv_usec < 0) {
- --tv2.tv_sec;
- tv2.tv_usec += 1000000;
- }
-
// To try to avoid flakiness we'll accept answers within 10,000us (0.01s).
- ASSERT_EQ(0, tv2.tv_sec);
- ASSERT_LT(tv2.tv_usec, 10'000);
+ ASSERT_LT(to_us(tv2) - to_us(tv1), 10'000);
}