Implement new clock wait functions

pthread_cond_clockwait
pthread_mutex_clocklock
pthread_rwlock_clockrdlock
pthread_rwlock_clockwrlock
sem_clockwait

Bug: 35756266
Test: new unit tests
Change-Id: I71bd25eeec6476134b368d5bdf2f729d0bba595e
This commit is contained in:
Tom Cherry 2019-05-07 20:33:05 -07:00
parent c80af7f146
commit 69010802d0
9 changed files with 351 additions and 32 deletions

View file

@ -215,6 +215,18 @@ extern "C" int pthread_cond_timedwait_monotonic_np(pthread_cond_t* cond_interfac
return __pthread_cond_timedwait(__get_internal_cond(cond_interface), mutex, false, abs_timeout);
}
int pthread_cond_clockwait(pthread_cond_t* cond_interface, pthread_mutex_t* mutex, clockid_t clock,
const struct timespec* abs_timeout) {
switch (clock) {
case CLOCK_MONOTONIC:
return pthread_cond_timedwait_monotonic_np(cond_interface, mutex, abs_timeout);
case CLOCK_REALTIME:
return pthread_cond_timedwait(cond_interface, mutex, abs_timeout);
default:
return EINVAL;
}
}
#if !defined(__LP64__)
// TODO: this exists only for backward binary compatibility on 32 bit platforms.
extern "C" int pthread_cond_timedwait_monotonic(pthread_cond_t* cond_interface,

View file

@ -989,6 +989,24 @@ int pthread_mutex_timedlock_monotonic_np(pthread_mutex_t* mutex_interface,
return __pthread_mutex_timedlock(mutex_interface, false, abs_timeout, __FUNCTION__);
}
int pthread_mutex_clocklock(pthread_mutex_t* mutex_interface, clockid_t clock,
const struct timespec* abs_timeout) {
switch (clock) {
case CLOCK_MONOTONIC:
return __pthread_mutex_timedlock(mutex_interface, false, abs_timeout, __FUNCTION__);
case CLOCK_REALTIME:
return __pthread_mutex_timedlock(mutex_interface, true, abs_timeout, __FUNCTION__);
default: {
pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface);
uint16_t old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed);
if (IsMutexDestroyed(old_state)) {
return HandleUsingDestroyedMutex(mutex_interface, __FUNCTION__);
}
return EINVAL;
}
}
}
int pthread_mutex_destroy(pthread_mutex_t* mutex_interface) {
pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface);
uint16_t old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed);

View file

@ -429,6 +429,18 @@ int pthread_rwlock_timedrdlock_monotonic_np(pthread_rwlock_t* rwlock_interface,
return __pthread_rwlock_timedrdlock(rwlock, false, abs_timeout);
}
int pthread_rwlock_clockrdlock(pthread_rwlock_t* rwlock_interface, clockid_t clock,
const struct timespec* abs_timeout) {
switch (clock) {
case CLOCK_MONOTONIC:
return pthread_rwlock_timedrdlock_monotonic_np(rwlock_interface, abs_timeout);
case CLOCK_REALTIME:
return pthread_rwlock_timedrdlock(rwlock_interface, abs_timeout);
default:
return EINVAL;
}
}
int pthread_rwlock_tryrdlock(pthread_rwlock_t* rwlock_interface) {
return __pthread_rwlock_tryrdlock(__get_internal_rwlock(rwlock_interface));
}
@ -455,6 +467,18 @@ int pthread_rwlock_timedwrlock_monotonic_np(pthread_rwlock_t* rwlock_interface,
return __pthread_rwlock_timedwrlock(rwlock, false, abs_timeout);
}
int pthread_rwlock_clockwrlock(pthread_rwlock_t* rwlock_interface, clockid_t clock,
const struct timespec* abs_timeout) {
switch (clock) {
case CLOCK_MONOTONIC:
return pthread_rwlock_timedwrlock_monotonic_np(rwlock_interface, abs_timeout);
case CLOCK_REALTIME:
return pthread_rwlock_timedwrlock(rwlock_interface, abs_timeout);
default:
return EINVAL;
}
}
int pthread_rwlock_trywrlock(pthread_rwlock_t* rwlock_interface) {
return __pthread_rwlock_trywrlock(__get_internal_rwlock(rwlock_interface));
}

View file

@ -275,6 +275,17 @@ int sem_timedwait_monotonic_np(sem_t* sem, const timespec* abs_timeout) {
return __sem_timedwait(sem, abs_timeout, false);
}
int sem_clockwait(sem_t* sem, clockid_t clock, const timespec* abs_timeout) {
switch (clock) {
case CLOCK_MONOTONIC:
return sem_timedwait_monotonic_np(sem, abs_timeout);
case CLOCK_REALTIME:
return sem_timedwait(sem, abs_timeout);
default:
return EINVAL;
}
}
int sem_post(sem_t* sem) {
atomic_uint* sem_count_ptr = SEM_TO_ATOMIC_POINTER(sem);
unsigned int shared = SEM_GET_SHARED(sem_count_ptr);

View file

@ -121,6 +121,8 @@ int pthread_condattr_setclock(pthread_condattr_t* __attr, clockid_t __clock) __I
int pthread_condattr_setpshared(pthread_condattr_t* __attr, int __shared);
int pthread_cond_broadcast(pthread_cond_t* __cond);
int pthread_cond_clockwait(pthread_cond_t* __cond, pthread_mutex_t* __mutex, clockid_t __clock,
const struct timespec* __timeout) __INTRODUCED_IN(30);
int pthread_cond_destroy(pthread_cond_t* __cond);
int pthread_cond_init(pthread_cond_t* __cond, const pthread_condattr_t* __attr);
int pthread_cond_signal(pthread_cond_t* __cond);
@ -130,8 +132,10 @@ int pthread_cond_timedwait(pthread_cond_t* __cond, pthread_mutex_t* __mutex, con
* typically inappropriate, since that clock can change dramatically, causing the timeout to
* either expire earlier or much later than intended.
* Condition variables have an initialization option to use CLOCK_MONOTONIC, and in addition,
* Android provides this API to use CLOCK_MONOTONIC on a condition variable for this single wait
* no matter how it was initialized.
* Android provides pthread_cond_timedwait_monotonic_np to use CLOCK_MONOTONIC on a condition
* variable for this single wait no matter how it was initialized.
* Note that pthread_cond_clockwait() allows specifying an arbitrary clock and has superseded this
* function.
*/
int pthread_cond_timedwait_monotonic_np(pthread_cond_t* __cond, pthread_mutex_t* __mutex,
const struct timespec* __timeout) __INTRODUCED_IN_64(28);
@ -181,6 +185,8 @@ int pthread_mutexattr_setpshared(pthread_mutexattr_t* __attr, int __shared);
int pthread_mutexattr_settype(pthread_mutexattr_t* __attr, int __type);
int pthread_mutexattr_setprotocol(pthread_mutexattr_t* __attr, int __protocol) __INTRODUCED_IN(28);
int pthread_mutex_clocklock(pthread_mutex_t* __mutex, clockid_t __clock,
const struct timespec* __abstime) __INTRODUCED_IN(30);
int pthread_mutex_destroy(pthread_mutex_t* __mutex);
int pthread_mutex_init(pthread_mutex_t* __mutex, const pthread_mutexattr_t* __attr);
int pthread_mutex_lock(pthread_mutex_t* __mutex);
@ -188,11 +194,13 @@ int pthread_mutex_timedlock(pthread_mutex_t* __mutex, const struct timespec* __t
__INTRODUCED_IN(21);
/*
* POSIX only supports using pthread_mutex_timedlock() with CLOCK_REALTIME, however that is
* typically inappropriate, since that clock can change dramatically, causing the timeout to
* POSIX historically only supported using pthread_mutex_timedlock() with CLOCK_REALTIME, however
* that is typically inappropriate, since that clock can change dramatically, causing the timeout to
* either expire earlier or much later than intended.
* This function is added to use a timespec based on CLOCK_MONOTONIC that does not suffer
* from this issue.
* Note that pthread_mutex_clocklock() allows specifying an arbitrary clock and has superseded this
* function.
*/
int pthread_mutex_timedlock_monotonic_np(pthread_mutex_t* __mutex, const struct timespec* __timeout)
__INTRODUCED_IN(28);
@ -226,6 +234,10 @@ int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t* __attr, int* __kin
__INTRODUCED_IN(23);
int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t* __attr, int __kind) __INTRODUCED_IN(23);
int pthread_rwlock_clockrdlock(pthread_rwlock_t* __rwlock, clockid_t __clock,
const struct timespec* __timeout) __INTRODUCED_IN(30);
int pthread_rwlock_clockwrlock(pthread_rwlock_t* __rwlock, clockid_t __clock,
const struct timespec* __timeout) __INTRODUCED_IN(30);
int pthread_rwlock_destroy(pthread_rwlock_t* __rwlock);
int pthread_rwlock_init(pthread_rwlock_t* __rwlock, const pthread_rwlockattr_t* __attr);
int pthread_rwlock_rdlock(pthread_rwlock_t* __rwlock);

View file

@ -30,6 +30,7 @@
#define _SEMAPHORE_H
#include <sys/cdefs.h>
#include <sys/types.h>
__BEGIN_DECLS
@ -44,16 +45,19 @@ typedef struct {
#define SEM_FAILED __BIONIC_CAST(reinterpret_cast, sem_t*, 0)
int sem_clockwait(sem_t* __sem, clockid_t __clock, const struct timespec* __ts) __INTRODUCED_IN(30);
int sem_destroy(sem_t* __sem);
int sem_getvalue(sem_t* __sem, int* __value);
int sem_init(sem_t* __sem, int __shared, unsigned int __value);
int sem_post(sem_t* __sem);
int sem_timedwait(sem_t* __sem, const struct timespec* __ts);
/*
* POSIX only supports using sem_timedwait() with CLOCK_REALTIME, however that is typically
* inappropriate, since that clock can change dramatically, causing the timeout to either
* POSIX historically only supported using sem_timedwait() with CLOCK_REALTIME, however that is
* typically inappropriate, since that clock can change dramatically, causing the timeout to either
* expire earlier or much later than intended. This function is added to use a timespec based
* on CLOCK_MONOTONIC that does not suffer from this issue.
* Note that sem_clockwait() allows specifying an arbitrary clock and has superseded this
* function.
*/
int sem_timedwait_monotonic_np(sem_t* __sem, const struct timespec* __ts) __INTRODUCED_IN(28);
int sem_trywait(sem_t* __sem);

View file

@ -1498,6 +1498,11 @@ LIBC_R { # introduced=R
mtx_timedlock;
mtx_trylock;
mtx_unlock;
pthread_cond_clockwait;
pthread_mutex_clocklock;
pthread_rwlock_clockrdlock;
pthread_rwlock_clockwrlock;
sem_clockwait;
thrd_create;
thrd_current;
thrd_detach;

View file

@ -990,6 +990,25 @@ TEST(pthread, pthread_rwlock_reader_wakeup_writer_timedwait_monotonic_np) {
#endif // __BIONIC__
}
TEST(pthread, pthread_rwlock_reader_wakeup_writer_clockwait) {
#if defined(__BIONIC__)
timespec ts;
ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
ts.tv_sec += 1;
test_pthread_rwlock_reader_wakeup_writer([&](pthread_rwlock_t* lock) {
return pthread_rwlock_clockwrlock(lock, CLOCK_MONOTONIC, &ts);
});
ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
ts.tv_sec += 1;
test_pthread_rwlock_reader_wakeup_writer([&](pthread_rwlock_t* lock) {
return pthread_rwlock_clockwrlock(lock, CLOCK_REALTIME, &ts);
});
#else // __BIONIC__
GTEST_SKIP() << "pthread_rwlock_clockwrlock not available";
#endif // __BIONIC__
}
static void test_pthread_rwlock_writer_wakeup_reader(std::function<int (pthread_rwlock_t*)> lock_function) {
RwlockWakeupHelperArg wakeup_arg;
ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr));
@ -1038,6 +1057,25 @@ TEST(pthread, pthread_rwlock_writer_wakeup_reader_timedwait_monotonic_np) {
#endif // __BIONIC__
}
TEST(pthread, pthread_rwlock_writer_wakeup_reader_clockwait) {
#if defined(__BIONIC__)
timespec ts;
ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
ts.tv_sec += 1;
test_pthread_rwlock_writer_wakeup_reader([&](pthread_rwlock_t* lock) {
return pthread_rwlock_clockrdlock(lock, CLOCK_MONOTONIC, &ts);
});
ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
ts.tv_sec += 1;
test_pthread_rwlock_writer_wakeup_reader([&](pthread_rwlock_t* lock) {
return pthread_rwlock_clockrdlock(lock, CLOCK_REALTIME, &ts);
});
#else // __BIONIC__
GTEST_SKIP() << "pthread_rwlock_clockrdlock not available";
#endif // __BIONIC__
}
static void pthread_rwlock_wakeup_timeout_helper(RwlockWakeupHelperArg* arg) {
arg->tid = gettid();
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
@ -1098,6 +1136,38 @@ TEST(pthread, pthread_rwlock_timedrdlock_monotonic_np_timeout) {
#endif // __BIONIC__
}
TEST(pthread, pthread_rwlock_clockrdlock_monotonic_timeout) {
#if defined(__BIONIC__)
pthread_rwlock_timedrdlock_timeout_helper(
CLOCK_MONOTONIC, [](pthread_rwlock_t* __rwlock, const timespec* __timeout) {
return pthread_rwlock_clockrdlock(__rwlock, CLOCK_MONOTONIC, __timeout);
});
#else // __BIONIC__
GTEST_SKIP() << "pthread_rwlock_clockrdlock not available";
#endif // __BIONIC__
}
TEST(pthread, pthread_rwlock_clockrdlock_realtime_timeout) {
#if defined(__BIONIC__)
pthread_rwlock_timedrdlock_timeout_helper(
CLOCK_REALTIME, [](pthread_rwlock_t* __rwlock, const timespec* __timeout) {
return pthread_rwlock_clockrdlock(__rwlock, CLOCK_REALTIME, __timeout);
});
#else // __BIONIC__
GTEST_SKIP() << "pthread_rwlock_clockrdlock not available";
#endif // __BIONIC__
}
TEST(pthread, pthread_rwlock_clockrdlock_invalid) {
#if defined(__BIONIC__)
pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
timespec ts;
EXPECT_EQ(EINVAL, pthread_rwlock_clockrdlock(&lock, CLOCK_PROCESS_CPUTIME_ID, &ts));
#else // __BIONIC__
GTEST_SKIP() << "pthread_rwlock_clockrdlock not available";
#endif // __BIONIC__
}
static void pthread_rwlock_timedwrlock_timeout_helper(
clockid_t clock, int (*lock_function)(pthread_rwlock_t* __rwlock, const timespec* __timeout)) {
RwlockWakeupHelperArg wakeup_arg;
@ -1134,6 +1204,38 @@ TEST(pthread, pthread_rwlock_timedwrlock_monotonic_np_timeout) {
#endif // __BIONIC__
}
TEST(pthread, pthread_rwlock_clockwrlock_monotonic_timeout) {
#if defined(__BIONIC__)
pthread_rwlock_timedwrlock_timeout_helper(
CLOCK_MONOTONIC, [](pthread_rwlock_t* __rwlock, const timespec* __timeout) {
return pthread_rwlock_clockwrlock(__rwlock, CLOCK_MONOTONIC, __timeout);
});
#else // __BIONIC__
GTEST_SKIP() << "pthread_rwlock_clockwrlock not available";
#endif // __BIONIC__
}
TEST(pthread, pthread_rwlock_clockwrlock_realtime_timeout) {
#if defined(__BIONIC__)
pthread_rwlock_timedwrlock_timeout_helper(
CLOCK_REALTIME, [](pthread_rwlock_t* __rwlock, const timespec* __timeout) {
return pthread_rwlock_clockwrlock(__rwlock, CLOCK_REALTIME, __timeout);
});
#else // __BIONIC__
GTEST_SKIP() << "pthread_rwlock_clockwrlock not available";
#endif // __BIONIC__
}
TEST(pthread, pthread_rwlock_clockwrlock_invalid) {
#if defined(__BIONIC__)
pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
timespec ts;
EXPECT_EQ(EINVAL, pthread_rwlock_clockwrlock(&lock, CLOCK_PROCESS_CPUTIME_ID, &ts));
#else // __BIONIC__
GTEST_SKIP() << "pthread_rwlock_clockrwlock not available";
#endif // __BIONIC__
}
class RwlockKindTestHelper {
private:
struct ThreadArg {
@ -1392,16 +1494,43 @@ class pthread_CondWakeupTest : public ::testing::Test {
ASSERT_EQ(0, pthread_condattr_destroy(&attr));
}
void StartWaitingThread(std::function<int (pthread_cond_t* cond, pthread_mutex_t* mutex)> wait_function) {
void StartWaitingThread(
std::function<int(pthread_cond_t* cond, pthread_mutex_t* mutex)> wait_function) {
progress = INITIALIZED;
this->wait_function = wait_function;
ASSERT_EQ(0, pthread_create(&thread, nullptr, reinterpret_cast<void* (*)(void*)>(WaitThreadFn), this));
ASSERT_EQ(0, pthread_create(&thread, nullptr, reinterpret_cast<void* (*)(void*)>(WaitThreadFn),
this));
while (progress != WAITING) {
usleep(5000);
}
usleep(5000);
}
void RunTimedTest(
clockid_t clock,
std::function<int(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* timeout)>
wait_function) {
timespec ts;
ASSERT_EQ(0, clock_gettime(clock, &ts));
ts.tv_sec += 1;
StartWaitingThread([&wait_function, &ts](pthread_cond_t* cond, pthread_mutex_t* mutex) {
return wait_function(cond, mutex, &ts);
});
progress = SIGNALED;
ASSERT_EQ(0, pthread_cond_signal(&cond));
}
void RunTimedTest(clockid_t clock, std::function<int(pthread_cond_t* cond, pthread_mutex_t* mutex,
clockid_t clock, const timespec* timeout)>
wait_function) {
RunTimedTest(clock, [clock, &wait_function](pthread_cond_t* cond, pthread_mutex_t* mutex,
const timespec* timeout) {
return wait_function(cond, mutex, clock, timeout);
});
}
void TearDown() override {
ASSERT_EQ(0, pthread_join(thread, nullptr));
ASSERT_EQ(FINISHED, progress);
@ -1442,44 +1571,59 @@ TEST_F(pthread_CondWakeupTest, broadcast_wait) {
TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_REALTIME) {
InitCond(CLOCK_REALTIME);
timespec ts;
ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
ts.tv_sec += 1;
StartWaitingThread([&](pthread_cond_t* cond, pthread_mutex_t* mutex) {
return pthread_cond_timedwait(cond, mutex, &ts);
});
progress = SIGNALED;
ASSERT_EQ(0, pthread_cond_signal(&cond));
RunTimedTest(CLOCK_REALTIME, pthread_cond_timedwait);
}
TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_MONOTONIC) {
InitCond(CLOCK_MONOTONIC);
timespec ts;
ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
ts.tv_sec += 1;
StartWaitingThread([&](pthread_cond_t* cond, pthread_mutex_t* mutex) {
return pthread_cond_timedwait(cond, mutex, &ts);
});
progress = SIGNALED;
ASSERT_EQ(0, pthread_cond_signal(&cond));
RunTimedTest(CLOCK_MONOTONIC, pthread_cond_timedwait);
}
TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_MONOTONIC_np) {
#if defined(__BIONIC__)
InitCond(CLOCK_REALTIME);
timespec ts;
ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
ts.tv_sec += 1;
StartWaitingThread([&](pthread_cond_t* cond, pthread_mutex_t* mutex) {
return pthread_cond_timedwait_monotonic_np(cond, mutex, &ts);
});
progress = SIGNALED;
ASSERT_EQ(0, pthread_cond_signal(&cond));
RunTimedTest(CLOCK_MONOTONIC, pthread_cond_timedwait_monotonic_np);
#else // __BIONIC__
GTEST_SKIP() << "pthread_cond_timedwait_monotonic_np not available";
#endif // __BIONIC__
}
TEST_F(pthread_CondWakeupTest, signal_clockwait_monotonic_monotonic) {
#if defined(__BIONIC__)
InitCond(CLOCK_MONOTONIC);
RunTimedTest(CLOCK_MONOTONIC, pthread_cond_clockwait);
#else // __BIONIC__
GTEST_SKIP() << "pthread_cond_clockwait not available";
#endif // __BIONIC__
}
TEST_F(pthread_CondWakeupTest, signal_clockwait_monotonic_realtime) {
#if defined(__BIONIC__)
InitCond(CLOCK_MONOTONIC);
RunTimedTest(CLOCK_REALTIME, pthread_cond_clockwait);
#else // __BIONIC__
GTEST_SKIP() << "pthread_cond_clockwait not available";
#endif // __BIONIC__
}
TEST_F(pthread_CondWakeupTest, signal_clockwait_realtime_monotonic) {
#if defined(__BIONIC__)
InitCond(CLOCK_REALTIME);
RunTimedTest(CLOCK_MONOTONIC, pthread_cond_clockwait);
#else // __BIONIC__
GTEST_SKIP() << "pthread_cond_clockwait not available";
#endif // __BIONIC__
}
TEST_F(pthread_CondWakeupTest, signal_clockwait_realtime_realtime) {
#if defined(__BIONIC__)
InitCond(CLOCK_REALTIME);
RunTimedTest(CLOCK_REALTIME, pthread_cond_clockwait);
#else // __BIONIC__
GTEST_SKIP() << "pthread_cond_clockwait not available";
#endif // __BIONIC__
}
static void pthread_cond_timedwait_timeout_helper(clockid_t clock,
int (*wait_function)(pthread_cond_t* __cond,
pthread_mutex_t* __mutex,
@ -1515,6 +1659,35 @@ TEST(pthread, pthread_cond_timedwait_monotonic_np_timeout) {
#endif // __BIONIC__
}
TEST(pthread, pthread_cond_clockwait_timeout) {
#if defined(__BIONIC__)
pthread_cond_timedwait_timeout_helper(
CLOCK_MONOTONIC,
[](pthread_cond_t* __cond, pthread_mutex_t* __mutex, const timespec* __timeout) {
return pthread_cond_clockwait(__cond, __mutex, CLOCK_MONOTONIC, __timeout);
});
pthread_cond_timedwait_timeout_helper(
CLOCK_REALTIME,
[](pthread_cond_t* __cond, pthread_mutex_t* __mutex, const timespec* __timeout) {
return pthread_cond_clockwait(__cond, __mutex, CLOCK_REALTIME, __timeout);
});
#else // __BIONIC__
GTEST_SKIP() << "pthread_cond_clockwait not available";
#endif // __BIONIC__
}
TEST(pthread, pthread_cond_clockwait_invalid) {
#if defined(__BIONIC__)
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
timespec ts;
EXPECT_EQ(EINVAL, pthread_cond_clockwait(&cond, &mutex, CLOCK_PROCESS_CPUTIME_ID, &ts));
#else // __BIONIC__
GTEST_SKIP() << "pthread_cond_clockwait not available";
#endif // __BIONIC__
}
TEST(pthread, pthread_attr_getstack__main_thread) {
// This test is only meaningful for the main thread, so make sure we're running on it!
ASSERT_EQ(getpid(), syscall(__NR_gettid));
@ -2180,6 +2353,21 @@ TEST(pthread, pthread_mutex_timedlock_monotonic_np) {
#endif // __BIONIC__
}
TEST(pthread, pthread_mutex_clocklock) {
#if defined(__BIONIC__)
pthread_mutex_timedlock_helper(
CLOCK_MONOTONIC, [](pthread_mutex_t* __mutex, const timespec* __timeout) {
return pthread_mutex_clocklock(__mutex, CLOCK_MONOTONIC, __timeout);
});
pthread_mutex_timedlock_helper(
CLOCK_REALTIME, [](pthread_mutex_t* __mutex, const timespec* __timeout) {
return pthread_mutex_clocklock(__mutex, CLOCK_REALTIME, __timeout);
});
#else // __BIONIC__
GTEST_SKIP() << "pthread_mutex_clocklock not available";
#endif // __BIONIC__
}
static void pthread_mutex_timedlock_pi_helper(clockid_t clock,
int (*lock_function)(pthread_mutex_t* __mutex,
const timespec* __timeout)) {
@ -2231,6 +2419,31 @@ TEST(pthread, pthread_mutex_timedlock_monotonic_np_pi) {
#endif // __BIONIC__
}
TEST(pthread, pthread_mutex_clocklock_pi) {
#if defined(__BIONIC__)
pthread_mutex_timedlock_pi_helper(
CLOCK_MONOTONIC, [](pthread_mutex_t* __mutex, const timespec* __timeout) {
return pthread_mutex_clocklock(__mutex, CLOCK_MONOTONIC, __timeout);
});
pthread_mutex_timedlock_pi_helper(
CLOCK_REALTIME, [](pthread_mutex_t* __mutex, const timespec* __timeout) {
return pthread_mutex_clocklock(__mutex, CLOCK_REALTIME, __timeout);
});
#else // __BIONIC__
GTEST_SKIP() << "pthread_mutex_clocklock not available";
#endif // __BIONIC__
}
TEST(pthread, pthread_mutex_clocklock_invalid) {
#if defined(__BIONIC__)
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
timespec ts;
EXPECT_EQ(EINVAL, pthread_mutex_clocklock(&mutex, CLOCK_PROCESS_CPUTIME_ID, &ts));
#else // __BIONIC__
GTEST_SKIP() << "pthread_mutex_clocklock not available";
#endif // __BIONIC__
}
TEST(pthread, pthread_mutex_using_destroyed_mutex) {
#if defined(__BIONIC__)
pthread_mutex_t m;
@ -2247,6 +2460,13 @@ TEST(pthread, pthread_mutex_using_destroyed_mutex) {
"pthread_mutex_timedlock called on a destroyed mutex");
ASSERT_EXIT(pthread_mutex_timedlock_monotonic_np(&m, &ts), ::testing::KilledBySignal(SIGABRT),
"pthread_mutex_timedlock_monotonic_np called on a destroyed mutex");
ASSERT_EXIT(pthread_mutex_clocklock(&m, CLOCK_MONOTONIC, &ts), ::testing::KilledBySignal(SIGABRT),
"pthread_mutex_clocklock called on a destroyed mutex");
ASSERT_EXIT(pthread_mutex_clocklock(&m, CLOCK_REALTIME, &ts), ::testing::KilledBySignal(SIGABRT),
"pthread_mutex_clocklock called on a destroyed mutex");
ASSERT_EXIT(pthread_mutex_clocklock(&m, CLOCK_PROCESS_CPUTIME_ID, &ts),
::testing::KilledBySignal(SIGABRT),
"pthread_mutex_clocklock called on a destroyed mutex");
ASSERT_EXIT(pthread_mutex_destroy(&m), ::testing::KilledBySignal(SIGABRT),
"pthread_mutex_destroy called on a destroyed mutex");
#else

View file

@ -145,6 +145,19 @@ TEST(semaphore, sem_timedwait_monotonic_np) {
#endif // __BIONIC__
}
TEST(semaphore, sem_clockwait) {
#if defined(__BIONIC__)
sem_timedwait_helper(CLOCK_MONOTONIC, [](sem_t* __sem, const timespec* __ts) {
return sem_clockwait(__sem, CLOCK_MONOTONIC, __ts);
});
sem_timedwait_helper(CLOCK_REALTIME, [](sem_t* __sem, const timespec* __ts) {
return sem_clockwait(__sem, CLOCK_REALTIME, __ts);
});
#else // __BIONIC__
GTEST_SKIP() << "sem_clockwait is only supported on bionic";
#endif // __BIONIC__
}
TEST(semaphore_DeathTest, sem_timedwait_null_timeout) {
sem_t s;
ASSERT_EQ(0, sem_init(&s, 0, 0));