To reduce the boilerplate code, refactor futex_wait_timeout test to use
kselftest_harness header instead of futex's logging header.

Signed-off-by: André Almeida <andrealm...@igalia.com>
---
 .../futex/functional/futex_wait_timeout.c          | 139 +++++++++------------
 tools/testing/selftests/futex/functional/run.sh    |   2 +-
 2 files changed, 61 insertions(+), 80 deletions(-)

diff --git a/tools/testing/selftests/futex/functional/futex_wait_timeout.c 
b/tools/testing/selftests/futex/functional/futex_wait_timeout.c
index 
d183f878360bcd04c72565510c116808b3e82347..ee01e49b1b3fa62dbad87f80b6937816fcb110ac
 100644
--- a/tools/testing/selftests/futex/functional/futex_wait_timeout.c
+++ b/tools/testing/selftests/futex/functional/futex_wait_timeout.c
@@ -16,26 +16,15 @@
  *****************************************************************************/
 
 #include <pthread.h>
+
 #include "futextest.h"
 #include "futex2test.h"
-#include "logging.h"
-
-#define TEST_NAME "futex-wait-timeout"
+#include "../../kselftest_harness.h"
 
 static long timeout_ns = 100000;       /* 100us default timeout */
 static futex_t futex_pi;
 static pthread_barrier_t barrier;
 
-void usage(char *prog)
-{
-       printf("Usage: %s\n", prog);
-       printf("  -c    Use color\n");
-       printf("  -h    Display this help message\n");
-       printf("  -t N  Timeout in nanoseconds (default: 100,000)\n");
-       printf("  -v L  Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
-              VQUIET, VCRITICAL, VINFO);
-}
-
 /*
  * Get a PI lock and hold it forever, so the main thread lock_pi will block
  * and we can test the timeout
@@ -47,13 +36,13 @@ void *get_pi_lock(void *arg)
 
        ret = futex_lock_pi(&futex_pi, NULL, 0, 0);
        if (ret != 0)
-               error("futex_lock_pi failed\n", ret);
+               ksft_exit_fail_msg("futex_lock_pi failed\n");
 
        pthread_barrier_wait(&barrier);
 
        /* Blocks forever */
        ret = futex_wait(&lock, 0, NULL, 0);
-       error("futex_wait failed\n", ret);
+       ksft_exit_fail_msg("futex_wait failed\n");
 
        return NULL;
 }
@@ -61,12 +50,11 @@ void *get_pi_lock(void *arg)
 /*
  * Check if the function returned the expected error
  */
-static void test_timeout(int res, int *ret, char *test_name, int err)
+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);
-               *ret = RET_FAIL;
        } else {
                ksft_test_result_pass("%s succeeds\n", test_name);
        }
@@ -78,10 +66,8 @@ static void test_timeout(int res, int *ret, char *test_name, 
int err)
 static int futex_get_abs_timeout(clockid_t clockid, struct timespec *to,
                                 long timeout_ns)
 {
-       if (clock_gettime(clockid, to)) {
-               error("clock_gettime failed\n", errno);
-               return errno;
-       }
+       if (clock_gettime(clockid, to))
+               ksft_exit_fail_msg("clock_gettime failed\n");
 
        to->tv_nsec += timeout_ns;
 
@@ -93,83 +79,66 @@ static int futex_get_abs_timeout(clockid_t clockid, struct 
timespec *to,
        return 0;
 }
 
-int main(int argc, char *argv[])
+TEST(wait_bitset)
 {
        futex_t f1 = FUTEX_INITIALIZER;
-       int res, ret = RET_PASS;
        struct timespec to;
-       pthread_t thread;
-       int c;
-       struct futex_waitv waitv = {
-                       .uaddr = (uintptr_t)&f1,
-                       .val = f1,
-                       .flags = FUTEX_32,
-                       .__reserved = 0
-               };
-
-       while ((c = getopt(argc, argv, "cht:v:")) != -1) {
-               switch (c) {
-               case 'c':
-                       log_color(1);
-                       break;
-               case 'h':
-                       usage(basename(argv[0]));
-                       exit(0);
-               case 't':
-                       timeout_ns = atoi(optarg);
-                       break;
-               case 'v':
-                       log_verbosity(atoi(optarg));
-                       break;
-               default:
-                       usage(basename(argv[0]));
-                       exit(1);
-               }
-       }
-
-       ksft_print_header();
-       ksft_set_plan(9);
-       ksft_print_msg("%s: Block on a futex and wait for timeout\n",
-              basename(argv[0]));
-       ksft_print_msg("\tArguments: timeout=%ldns\n", timeout_ns);
-
-       pthread_barrier_init(&barrier, NULL, 2);
-       pthread_create(&thread, NULL, get_pi_lock, NULL);
+       int res;
 
        /* initialize relative timeout */
        to.tv_sec = 0;
        to.tv_nsec = timeout_ns;
 
        res = futex_wait(&f1, f1, &to, 0);
-       test_timeout(res, &ret, "futex_wait relative", ETIMEDOUT);
+       test_timeout(res, "futex_wait relative", ETIMEDOUT);
 
        /* FUTEX_WAIT_BITSET with CLOCK_REALTIME */
        if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
-               return RET_FAIL;
+               ksft_test_result_error("get_time error");
        res = futex_wait_bitset(&f1, f1, &to, 1, FUTEX_CLOCK_REALTIME);
-       test_timeout(res, &ret, "futex_wait_bitset realtime", ETIMEDOUT);
+       test_timeout(res, "futex_wait_bitset realtime", ETIMEDOUT);
 
        /* FUTEX_WAIT_BITSET with CLOCK_MONOTONIC */
        if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
-               return RET_FAIL;
+               ksft_test_result_error("get_time error");
        res = futex_wait_bitset(&f1, f1, &to, 1, 0);
-       test_timeout(res, &ret, "futex_wait_bitset monotonic", ETIMEDOUT);
+       test_timeout(res, "futex_wait_bitset monotonic", ETIMEDOUT);
+}
+
+TEST(requeue_pi)
+{
+       futex_t f1 = FUTEX_INITIALIZER;
+       struct timespec to;
+       int res;
 
        /* FUTEX_WAIT_REQUEUE_PI with CLOCK_REALTIME */
        if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
-               return RET_FAIL;
+               ksft_test_result_error("get_time error");
        res = futex_wait_requeue_pi(&f1, f1, &futex_pi, &to, 
FUTEX_CLOCK_REALTIME);
-       test_timeout(res, &ret, "futex_wait_requeue_pi realtime", ETIMEDOUT);
+       test_timeout(res, "futex_wait_requeue_pi realtime", ETIMEDOUT);
 
        /* FUTEX_WAIT_REQUEUE_PI with CLOCK_MONOTONIC */
        if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
-               return RET_FAIL;
+               ksft_test_result_error("get_time error");
        res = futex_wait_requeue_pi(&f1, f1, &futex_pi, &to, 0);
-       test_timeout(res, &ret, "futex_wait_requeue_pi monotonic", ETIMEDOUT);
+       test_timeout(res, "futex_wait_requeue_pi monotonic", ETIMEDOUT);
+
+}
+
+TEST(lock_pi)
+{
+       struct timespec to;
+       pthread_t thread;
+       int res;
+
+       /* Create a thread that will lock forever so any waiter will timeout */
+       pthread_barrier_init(&barrier, NULL, 2);
+       pthread_create(&thread, NULL, get_pi_lock, NULL);
 
        /* Wait until the other thread calls futex_lock_pi() */
        pthread_barrier_wait(&barrier);
        pthread_barrier_destroy(&barrier);
+
        /*
         * FUTEX_LOCK_PI with CLOCK_REALTIME
         * Due to historical reasons, FUTEX_LOCK_PI supports only realtime
@@ -181,26 +150,38 @@ int main(int argc, char *argv[])
         * smaller than realtime and the syscall will timeout immediately.
         */
        if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
-               return RET_FAIL;
+               ksft_test_result_error("get_time error");
        res = futex_lock_pi(&futex_pi, &to, 0, 0);
-       test_timeout(res, &ret, "futex_lock_pi realtime", ETIMEDOUT);
+       test_timeout(res, "futex_lock_pi realtime", ETIMEDOUT);
 
        /* Test operations that don't support FUTEX_CLOCK_REALTIME */
        res = futex_lock_pi(&futex_pi, NULL, 0, FUTEX_CLOCK_REALTIME);
-       test_timeout(res, &ret, "futex_lock_pi invalid timeout flag", ENOSYS);
+       test_timeout(res, "futex_lock_pi invalid timeout flag", ENOSYS);
+}
+
+TEST(waitv)
+{
+       futex_t f1 = FUTEX_INITIALIZER;
+       struct timespec to;
+       int res;
+       struct futex_waitv waitv = {
+                       .uaddr = (uintptr_t)&f1,
+                       .val = f1,
+                       .flags = FUTEX_32,
+                       .__reserved = 0
+               };
 
        /* futex_waitv with CLOCK_MONOTONIC */
        if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
-               return RET_FAIL;
+               ksft_test_result_error("get_time error");
        res = futex_waitv(&waitv, 1, 0, &to, CLOCK_MONOTONIC);
-       test_timeout(res, &ret, "futex_waitv monotonic", ETIMEDOUT);
+       test_timeout(res, "futex_waitv monotonic", ETIMEDOUT);
 
        /* futex_waitv with CLOCK_REALTIME */
        if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
-               return RET_FAIL;
+               ksft_test_result_error("get_time error");
        res = futex_waitv(&waitv, 1, 0, &to, CLOCK_REALTIME);
-       test_timeout(res, &ret, "futex_waitv realtime", ETIMEDOUT);
-
-       ksft_print_cnts();
-       return ret;
+       test_timeout(res, "futex_waitv realtime", ETIMEDOUT);
 }
+
+TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/futex/functional/run.sh 
b/tools/testing/selftests/futex/functional/run.sh
index 
37f614e7d7fd3ce9741861e23df40f87139f572a..d42a8f4cf0a3d4361fd9d1196cb385a3ae38a04a
 100755
--- a/tools/testing/selftests/futex/functional/run.sh
+++ b/tools/testing/selftests/futex/functional/run.sh
@@ -41,7 +41,7 @@ echo
 ./futex_requeue_pi_signal_restart
 
 echo
-./futex_wait_timeout $COLOR
+./futex_wait_timeout
 
 echo
 ./futex_wait_wouldblock $COLOR

-- 
2.50.1


Reply via email to