Path: blob/master/tools/testing/selftests/futex/functional/futex_wait.c
50032 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) {73if (errno == ENOSYS)74ksft_exit_skip("shmget syscall not supported\n");75perror("shmget");76exit(1);77}7879shared_data = shmat(shm_id, NULL, 0);8081*shared_data = 0;82futex = shared_data;8384ksft_print_dbg_msg("Calling shared (page anon) futex_wait on futex: %p\n", futex);85if (pthread_create(&waiter, NULL, waiterfn, NULL))86ksft_exit_fail_msg("pthread_create failed\n");8788usleep(WAKE_WAIT_US);8990ksft_print_dbg_msg("Calling shared (page anon) futex_wake on futex: %p\n", futex);91res = futex_wake(futex, 1, 0);92if (res != 1) {93ksft_test_result_fail("futex_wake shared (page anon) returned: %d %s\n",94errno, strerror(errno));95} else {96ksft_test_result_pass("futex_wake shared (page anon) succeeds\n");97}9899shmdt(shared_data);100}101102TEST(file_backed)103{104u_int32_t f_private = 0;105pthread_t waiter;106int res, fd;107void *shm;108109/* Testing a file backed shared memory */110fd = open(SHM_PATH, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);111if (fd < 0)112ksft_exit_fail_msg("open\n");113114if (ftruncate(fd, sizeof(f_private)))115ksft_exit_fail_msg("ftruncate\n");116117shm = mmap(NULL, sizeof(f_private), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);118if (shm == MAP_FAILED)119ksft_exit_fail_msg("mmap\n");120121memcpy(shm, &f_private, sizeof(f_private));122123futex = shm;124125ksft_print_dbg_msg("Calling shared (file backed) futex_wait on futex: %p\n", futex);126if (pthread_create(&waiter, NULL, waiterfn, NULL))127ksft_exit_fail_msg("pthread_create failed\n");128129usleep(WAKE_WAIT_US);130131ksft_print_dbg_msg("Calling shared (file backed) futex_wake on futex: %p\n", futex);132res = futex_wake(shm, 1, 0);133if (res != 1) {134ksft_test_result_fail("futex_wake shared (file backed) returned: %d %s\n",135errno, strerror(errno));136} else {137ksft_test_result_pass("futex_wake shared (file backed) succeeds\n");138}139140munmap(shm, sizeof(f_private));141remove(SHM_PATH);142close(fd);143}144145TEST_HARNESS_MAIN146147148