1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) Ulrich Drepper <drepper@redhat.com> 4f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2009 5f08c3bdfSopenharmony_ci * Copyright (C) 2023 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com> 6f08c3bdfSopenharmony_ci */ 7f08c3bdfSopenharmony_ci 8f08c3bdfSopenharmony_ci/*\ 9f08c3bdfSopenharmony_ci * [Description] 10f08c3bdfSopenharmony_ci * 11f08c3bdfSopenharmony_ci * This test verifies that eventfd2 semaphore-like support is properly working. 12f08c3bdfSopenharmony_ci */ 13f08c3bdfSopenharmony_ci 14f08c3bdfSopenharmony_ci#include <fcntl.h> 15f08c3bdfSopenharmony_ci#include <stdlib.h> 16f08c3bdfSopenharmony_ci#include <sys/eventfd.h> 17f08c3bdfSopenharmony_ci#include "tst_test.h" 18f08c3bdfSopenharmony_ci#include "eventfd2.h" 19f08c3bdfSopenharmony_ci 20f08c3bdfSopenharmony_cistatic void xsem_wait(int fd) 21f08c3bdfSopenharmony_ci{ 22f08c3bdfSopenharmony_ci u_int64_t cntr; 23f08c3bdfSopenharmony_ci 24f08c3bdfSopenharmony_ci SAFE_READ(0, fd, &cntr, sizeof(cntr)); 25f08c3bdfSopenharmony_ci} 26f08c3bdfSopenharmony_ci 27f08c3bdfSopenharmony_cistatic void xsem_post(int fd, int count) 28f08c3bdfSopenharmony_ci{ 29f08c3bdfSopenharmony_ci u_int64_t cntr = count; 30f08c3bdfSopenharmony_ci 31f08c3bdfSopenharmony_ci SAFE_WRITE(0, fd, &cntr, sizeof(cntr)); 32f08c3bdfSopenharmony_ci} 33f08c3bdfSopenharmony_ci 34f08c3bdfSopenharmony_cistatic void sem_player(int fd1, int fd2) 35f08c3bdfSopenharmony_ci{ 36f08c3bdfSopenharmony_ci pid_t pid = getpid(); 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_ci tst_res(TINFO, "[%u] posting 1 on fd=%d", pid, fd1); 39f08c3bdfSopenharmony_ci xsem_post(fd1, 1); 40f08c3bdfSopenharmony_ci 41f08c3bdfSopenharmony_ci tst_res(TINFO, "[%u] waiting on fd=%d", pid, fd2); 42f08c3bdfSopenharmony_ci xsem_wait(fd2); 43f08c3bdfSopenharmony_ci 44f08c3bdfSopenharmony_ci tst_res(TINFO, "[%u] posting 5 on fd=%d", pid, fd1); 45f08c3bdfSopenharmony_ci xsem_post(fd1, 5); 46f08c3bdfSopenharmony_ci 47f08c3bdfSopenharmony_ci tst_res(TINFO, "[%u] waiting 5 times on fd=%d", pid, fd2); 48f08c3bdfSopenharmony_ci xsem_wait(fd2); 49f08c3bdfSopenharmony_ci xsem_wait(fd2); 50f08c3bdfSopenharmony_ci xsem_wait(fd2); 51f08c3bdfSopenharmony_ci xsem_wait(fd2); 52f08c3bdfSopenharmony_ci xsem_wait(fd2); 53f08c3bdfSopenharmony_ci 54f08c3bdfSopenharmony_ci tst_res(TPASS, "[%u] received all events", pid); 55f08c3bdfSopenharmony_ci} 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_cistatic void run(void) 58f08c3bdfSopenharmony_ci{ 59f08c3bdfSopenharmony_ci pid_t cpid_poster, cpid_waiter; 60f08c3bdfSopenharmony_ci int fd1, fd2; 61f08c3bdfSopenharmony_ci 62f08c3bdfSopenharmony_ci fd1 = eventfd2(0, EFD_SEMAPHORE); 63f08c3bdfSopenharmony_ci fd2 = eventfd2(0, EFD_SEMAPHORE); 64f08c3bdfSopenharmony_ci 65f08c3bdfSopenharmony_ci cpid_poster = SAFE_FORK(); 66f08c3bdfSopenharmony_ci if (!cpid_poster) { 67f08c3bdfSopenharmony_ci sem_player(fd1, fd2); 68f08c3bdfSopenharmony_ci exit(0); 69f08c3bdfSopenharmony_ci } 70f08c3bdfSopenharmony_ci 71f08c3bdfSopenharmony_ci cpid_waiter = SAFE_FORK(); 72f08c3bdfSopenharmony_ci if (!cpid_waiter) { 73f08c3bdfSopenharmony_ci sem_player(fd2, fd1); 74f08c3bdfSopenharmony_ci exit(0); 75f08c3bdfSopenharmony_ci } 76f08c3bdfSopenharmony_ci} 77f08c3bdfSopenharmony_ci 78f08c3bdfSopenharmony_cistatic struct tst_test test = { 79f08c3bdfSopenharmony_ci .test_all = run, 80f08c3bdfSopenharmony_ci .forks_child = 1, 81f08c3bdfSopenharmony_ci}; 82