Path: blob/master/tools/testing/selftests/futex/functional/futex_wait.c
29271 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Copyright Collabora Ltd., 20213*4* futex cmp requeue test by André Almeida <[email protected]>5*/67#include <pthread.h>8#include <sys/shm.h>9#include <sys/mman.h>10#include <fcntl.h>1112#include "futextest.h"13#include "../../kselftest_harness.h"1415#define timeout_ns 3000000016#define WAKE_WAIT_US 1000017#define SHM_PATH "futex_shm_file"1819void *futex;2021static void *waiterfn(void *arg)22{23struct timespec to;24unsigned int flags = 0;2526if (arg)27flags = *((unsigned int *) arg);2829to.tv_sec = 0;30to.tv_nsec = timeout_ns;3132if (futex_wait(futex, 0, &to, flags))33printf("waiter failed errno %d\n", errno);3435return NULL;36}3738TEST(private_futex)39{40unsigned int flags = FUTEX_PRIVATE_FLAG;41u_int32_t f_private = 0;42pthread_t waiter;43int res;4445futex = &f_private;4647/* Testing a private futex */48ksft_print_dbg_msg("Calling private futex_wait on futex: %p\n", futex);49if (pthread_create(&waiter, NULL, waiterfn, (void *) &flags))50ksft_exit_fail_msg("pthread_create failed\n");5152usleep(WAKE_WAIT_US);5354ksft_print_dbg_msg("Calling private futex_wake on futex: %p\n", futex);55res = futex_wake(futex, 1, FUTEX_PRIVATE_FLAG);56if (res != 1) {57ksft_test_result_fail("futex_wake private returned: %d %s\n",58errno, strerror(errno));59} else {60ksft_test_result_pass("futex_wake private succeeds\n");61}62}6364TEST(anon_page)65{66u_int32_t *shared_data;67pthread_t waiter;68int res, shm_id;6970/* Testing an anon page shared memory */71shm_id = shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0666);72if (shm_id < 0) {73perror("shmget");74exit(1);75}7677shared_data = shmat(shm_id, NULL, 0);7879*shared_data = 0;80futex = shared_data;8182ksft_print_dbg_msg("Calling shared (page anon) futex_wait on futex: %p\n", futex);83if (pthread_create(&waiter, NULL, waiterfn, NULL))84ksft_exit_fail_msg("pthread_create failed\n");8586usleep(WAKE_WAIT_US);8788ksft_print_dbg_msg("Calling shared (page anon) futex_wake on futex: %p\n", futex);89res = futex_wake(futex, 1, 0);90if (res != 1) {91ksft_test_result_fail("futex_wake shared (page anon) returned: %d %s\n",92errno, strerror(errno));93} else {94ksft_test_result_pass("futex_wake shared (page anon) succeeds\n");95}9697shmdt(shared_data);98}99100TEST(file_backed)101{102u_int32_t f_private = 0;103pthread_t waiter;104int res, fd;105void *shm;106107/* Testing a file backed shared memory */108fd = open(SHM_PATH, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);109if (fd < 0)110ksft_exit_fail_msg("open");111112if (ftruncate(fd, sizeof(f_private)))113ksft_exit_fail_msg("ftruncate");114115shm = mmap(NULL, sizeof(f_private), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);116if (shm == MAP_FAILED)117ksft_exit_fail_msg("mmap");118119memcpy(shm, &f_private, sizeof(f_private));120121futex = shm;122123ksft_print_dbg_msg("Calling shared (file backed) futex_wait on futex: %p\n", futex);124if (pthread_create(&waiter, NULL, waiterfn, NULL))125ksft_exit_fail_msg("pthread_create failed\n");126127usleep(WAKE_WAIT_US);128129ksft_print_dbg_msg("Calling shared (file backed) futex_wake on futex: %p\n", futex);130res = futex_wake(shm, 1, 0);131if (res != 1) {132ksft_test_result_fail("futex_wake shared (file backed) returned: %d %s\n",133errno, strerror(errno));134} else {135ksft_test_result_pass("futex_wake shared (file backed) succeeds\n");136}137138munmap(shm, sizeof(f_private));139remove(SHM_PATH);140close(fd);141}142143TEST_HARNESS_MAIN144145146