#include <pthread.h>
#include "futextest.h"
#include "futex2test.h"
#include "kselftest_harness.h"
static long timeout_ns = 100000;
static futex_t futex_pi;
static pthread_barrier_t barrier;
void *get_pi_lock(void *arg)
{
int ret;
volatile futex_t lock = 0;
ret = futex_lock_pi(&futex_pi, NULL, 0, 0);
if (ret != 0)
ksft_exit_fail_msg("futex_lock_pi failed\n");
pthread_barrier_wait(&barrier);
ret = futex_wait(&lock, 0, NULL, 0);
ksft_exit_fail_msg("futex_wait failed\n");
return NULL;
}
static void test_timeout(int res, char *test_name, int err)
{
if (!res || errno != err) {
ksft_test_result_fail("%s returned %d\n", test_name,
res < 0 ? errno : res);
} else {
ksft_test_result_pass("%s succeeds\n", test_name);
}
}
static int futex_get_abs_timeout(clockid_t clockid, struct timespec *to,
long timeout_ns)
{
if (clock_gettime(clockid, to))
ksft_exit_fail_msg("clock_gettime failed\n");
to->tv_nsec += timeout_ns;
if (to->tv_nsec >= 1000000000) {
to->tv_sec++;
to->tv_nsec -= 1000000000;
}
return 0;
}
TEST(wait_bitset)
{
futex_t f1 = FUTEX_INITIALIZER;
struct timespec to;
int res;
to.tv_sec = 0;
to.tv_nsec = timeout_ns;
res = futex_wait(&f1, f1, &to, 0);
test_timeout(res, "futex_wait relative", ETIMEDOUT);
if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
ksft_test_result_error("get_time error");
res = futex_wait_bitset(&f1, f1, &to, 1, FUTEX_CLOCK_REALTIME);
test_timeout(res, "futex_wait_bitset realtime", ETIMEDOUT);
if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
ksft_test_result_error("get_time error");
res = futex_wait_bitset(&f1, f1, &to, 1, 0);
test_timeout(res, "futex_wait_bitset monotonic", ETIMEDOUT);
}
TEST(requeue_pi)
{
futex_t f1 = FUTEX_INITIALIZER;
struct timespec to;
int res;
if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
ksft_test_result_error("get_time error");
res = futex_wait_requeue_pi(&f1, f1, &futex_pi, &to, FUTEX_CLOCK_REALTIME);
test_timeout(res, "futex_wait_requeue_pi realtime", ETIMEDOUT);
if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
ksft_test_result_error("get_time error");
res = futex_wait_requeue_pi(&f1, f1, &futex_pi, &to, 0);
test_timeout(res, "futex_wait_requeue_pi monotonic", ETIMEDOUT);
}
TEST(lock_pi)
{
struct timespec to;
pthread_t thread;
int res;
pthread_barrier_init(&barrier, NULL, 2);
pthread_create(&thread, NULL, get_pi_lock, NULL);
pthread_barrier_wait(&barrier);
pthread_barrier_destroy(&barrier);
if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
ksft_test_result_error("get_time error");
res = futex_lock_pi(&futex_pi, &to, 0, 0);
test_timeout(res, "futex_lock_pi realtime", ETIMEDOUT);
res = futex_lock_pi(&futex_pi, NULL, 0, FUTEX_CLOCK_REALTIME);
test_timeout(res, "futex_lock_pi invalid timeout flag", ENOSYS);
}
TEST(waitv)
{
futex_t f1 = FUTEX_INITIALIZER;
struct futex_waitv waitv = {
.uaddr = (uintptr_t)&f1,
.val = f1,
.flags = FUTEX_32,
.__reserved = 0,
};
struct timespec to;
int res;
if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
ksft_test_result_error("get_time error");
res = futex_waitv(&waitv, 1, 0, &to, CLOCK_MONOTONIC);
test_timeout(res, "futex_waitv monotonic", ETIMEDOUT);
if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
ksft_test_result_error("get_time error");
res = futex_waitv(&waitv, 1, 0, &to, CLOCK_REALTIME);
test_timeout(res, "futex_waitv realtime", ETIMEDOUT);
}
TEST_HARNESS_MAIN