1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2015 Cyril Hrubis <chrubis@suse.cz>
4 *
5 * Block on a futex and wait for wakeup.
6 *
7 * This tests uses private mutexes with threads.
8 */
9
10 #include "futextest.h"
11 #include "futex_utils.h"
12 #include "tst_safe_pthread.h"
13
14 static futex_t futex = FUTEX_INITIALIZER;
15
16 static struct futex_test_variants variants[] = {
17 #if (__NR_futex != __LTP__NR_INVALID_SYSCALL)
18 { .fntype = FUTEX_FN_FUTEX, .desc = "syscall with old kernel spec"},
19 #endif
20
21 #if (__NR_futex_time64 != __LTP__NR_INVALID_SYSCALL)
22 { .fntype = FUTEX_FN_FUTEX64, .desc = "syscall time64 with kernel spec"},
23 #endif
24 };
25
threaded(void * arg)26 static void *threaded(void *arg)
27 {
28 struct futex_test_variants *tv = &variants[tst_variant];
29 long ret, pid = (long)arg;
30
31 TST_PROCESS_STATE_WAIT(pid, 'S', 0);
32
33 ret = futex_wake(tv->fntype, &futex, 1, FUTEX_PRIVATE_FLAG);
34 if (ret != 1)
35 tst_res(TFAIL, "futex_wake() returned %li", ret);
36
37 return (void*)ret;
38 }
39
run(void)40 static void run(void)
41 {
42 struct futex_test_variants *tv = &variants[tst_variant];
43 long res, pid = getpid();
44 pthread_t t;
45
46 SAFE_PTHREAD_CREATE(&t, NULL, threaded, (void*)pid);
47
48 res = futex_wait(tv->fntype, &futex, futex, NULL, FUTEX_PRIVATE_FLAG);
49 if (res) {
50 tst_res(TFAIL | TERRNO, "futex_wait() failed");
51 SAFE_PTHREAD_JOIN(t, NULL);
52 return;
53 }
54
55 SAFE_PTHREAD_JOIN(t, NULL);
56 tst_res(TPASS, "futex_wait() woken up");
57 }
58
setup(void)59 static void setup(void)
60 {
61 struct futex_test_variants *tv = &variants[tst_variant];
62
63 tst_res(TINFO, "Testing variant: %s", tv->desc);
64 futex_supported_by_kernel(tv->fntype);
65 }
66
67 static struct tst_test test = {
68 .setup = setup,
69 .test_all = run,
70 .test_variants = ARRAY_SIZE(variants),
71 };
72