Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/thorpej-futex]: src/tests/lib/libc/sys Unit tests for eventfd(2).



details:   https://anonhg.NetBSD.org/src/rev/044299b9e2ea
branches:  thorpej-futex
changeset: 961123:044299b9e2ea
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Mon Dec 14 16:01:38 2020 +0000

description:
Unit tests for eventfd(2).

diffstat:

 tests/lib/libc/sys/Makefile    |    4 +-
 tests/lib/libc/sys/t_eventfd.c |  834 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 837 insertions(+), 1 deletions(-)

diffs (truncated from 863 to 300 lines):

diff -r a94d87163120 -r 044299b9e2ea tests/lib/libc/sys/Makefile
--- a/tests/lib/libc/sys/Makefile       Mon Dec 14 16:01:17 2020 +0000
+++ b/tests/lib/libc/sys/Makefile       Mon Dec 14 16:01:38 2020 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.68 2020/09/06 07:20:31 mrg Exp $
+# $NetBSD: Makefile,v 1.68.2.1 2020/12/14 16:01:38 thorpej Exp $
 
 MKMAN= no
 
@@ -16,6 +16,7 @@
 TESTS_C+=              t_clone
 TESTS_C+=              t_connect
 TESTS_C+=              t_dup
+TESTS_C+=              t_eventfd
 TESTS_C+=              t_fork
 TESTS_C+=              t_fsync
 TESTS_C+=              t_futex_ops
@@ -93,6 +94,7 @@
 
 SRCS.t_mprotect=       t_mprotect.c ${SRCS_EXEC_PROT} t_mprotect_helper.c
 
+LDADD.t_eventfd+=      -lpthread
 LDADD.t_getpid+=        -lpthread
 
 LDADD.t_ptrace_sigchld+=       -pthread -lm
diff -r a94d87163120 -r 044299b9e2ea tests/lib/libc/sys/t_eventfd.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/sys/t_eventfd.c    Mon Dec 14 16:01:38 2020 +0000
@@ -0,0 +1,834 @@
+/* $NetBSD: t_eventfd.c,v 1.1.2.1 2020/12/14 16:01:38 thorpej Exp $ */
+
+/*-
+ * Copyright (c) 2020 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__COPYRIGHT("@(#) Copyright (c) 2020\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_eventfd.c,v 1.1.2.1 2020/12/14 16:01:38 thorpej Exp $");
+
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/eventfd.h>
+#include <sys/select.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <errno.h>
+#include <poll.h>
+#include <pthread.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+struct helper_context {
+       int     efd;
+
+       pthread_mutex_t mutex;
+       pthread_cond_t cond;
+       pthread_barrier_t barrier;
+       int     state;
+};
+
+static void
+init_helper_context(struct helper_context * const ctx)
+{
+       pthread_condattr_t condattr;
+
+       memset(ctx, 0, sizeof(*ctx));
+
+       ATF_REQUIRE(pthread_mutex_init(&ctx->mutex, NULL) == 0);
+
+       ATF_REQUIRE(pthread_condattr_init(&condattr) == 0);
+       ATF_REQUIRE(pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC) == 0);
+       ATF_REQUIRE(pthread_cond_init(&ctx->cond, &condattr) == 0);
+       ATF_REQUIRE(pthread_condattr_destroy(&condattr) == 0);
+
+       ATF_REQUIRE(pthread_barrier_init(&ctx->barrier, NULL, 2) == 0);
+}
+
+static void
+set_state(struct helper_context * const ctx, int const new)
+{
+       pthread_mutex_lock(&ctx->mutex);
+       ctx->state = new;
+       pthread_cond_signal(&ctx->cond);
+       pthread_mutex_unlock(&ctx->mutex);
+}
+
+static int
+get_state(struct helper_context * const ctx)
+{
+       int rv;
+
+       pthread_mutex_lock(&ctx->mutex);
+       rv = ctx->state;
+       pthread_mutex_unlock(&ctx->mutex);
+
+       return rv;
+}
+
+static bool
+wait_state(struct helper_context * const ctx, int const val)
+{
+       struct timespec deadline;
+       int error;
+       bool rv;
+
+       pthread_mutex_lock(&ctx->mutex);
+
+       ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &deadline) == 0);
+       deadline.tv_sec += 5;
+
+       while (ctx->state != val) {
+               error = pthread_cond_timedwait(&ctx->cond, &ctx->mutex,
+                   &deadline);
+               if (error) {
+                       break;
+               }
+       }
+       rv = ctx->state == val;
+
+       pthread_mutex_unlock(&ctx->mutex);
+
+       return rv;
+}
+
+static bool
+wait_barrier(struct helper_context * const ctx)
+{
+       int rv = pthread_barrier_wait(&ctx->barrier);
+
+       return rv == 0 || rv == PTHREAD_BARRIER_SERIAL_THREAD;
+}
+
+static int
+my_eventfd(unsigned int val, int flags)
+{
+       return syscall(SYS_eventfd, val, flags);
+}
+
+int
+eventfd_read(int efd, eventfd_t *valp)
+{
+       eventfd_t val;
+
+       switch (read(efd, &val, sizeof(val))) {
+       case -1:
+               return -1;
+
+       case sizeof(val):
+               *valp = val;
+               return 0;
+
+       default:
+               /* ?? Should never happen. */
+               errno = EIO;
+               return -1;
+       }
+}
+
+int
+eventfd_write(int efd, eventfd_t val)
+{
+
+       switch (write(efd, &val, sizeof(val))) {
+       case -1:
+               return -1;
+
+       case sizeof(val):
+               return 0;
+
+       default:
+               /* ?? Should never happen. */
+               errno = EIO;
+               return -1;
+       }
+}
+
+/*****************************************************************************/
+
+static void *
+eventfd_normal_helper(void * const v)
+{
+       struct helper_context * const ctx = v;
+       eventfd_t efd_value;
+
+       ATF_REQUIRE(wait_barrier(ctx));
+
+       /* Read the value.  This will reset it to zero. */
+       ATF_REQUIRE(get_state(ctx) == 666);
+       ATF_REQUIRE(eventfd_read(ctx->efd, &efd_value) == 0);
+
+       /* Assert the value. */
+       ATF_REQUIRE(efd_value == 0xcafebabe);
+
+       set_state(ctx, 0);
+
+       /* Wait for the main thread to prep the next test. */
+       ATF_REQUIRE(wait_barrier(ctx));
+
+       /* Read the value. */
+       ATF_REQUIRE(eventfd_read(ctx->efd, &efd_value) == 0);
+
+       /* Assert the value. */
+       ATF_REQUIRE(efd_value == 0xbeefcafe);
+
+       ATF_REQUIRE(wait_barrier(ctx));
+
+       return NULL;
+}
+
+ATF_TC(eventfd_normal);
+ATF_TC_HEAD(eventfd_normal, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "validates basic normal eventfd operation");
+}
+ATF_TC_BODY(eventfd_normal, tc)
+{
+       struct helper_context ctx;
+       pthread_t helper;
+       void *join_val;
+
+       init_helper_context(&ctx);
+
+       ATF_REQUIRE((ctx.efd = my_eventfd(0, 0)) >= 0);
+
+       ATF_REQUIRE(pthread_create(&helper, NULL,
+                                  eventfd_normal_helper, &ctx) == 0);
+
+       /*
+        * Wait for the helper to block in read().  Give it some time
+        * so that if the read fails or returns immediately, we'll
+        * notice.
+        */
+       set_state(&ctx, 666);
+       ATF_REQUIRE(wait_barrier(&ctx));
+       sleep(2);
+       ATF_REQUIRE(get_state(&ctx) == 666);
+
+       /* Write a distinct value; helper will assert it. */
+       ATF_REQUIRE(eventfd_write(ctx.efd, 0xcafebabe) == 0);
+
+       /* Wait for helper to read the value. */
+       ATF_REQUIRE(wait_state(&ctx, 0));
+
+       /* Helper is now blocked in a barrier. */
+
+       /* Test additive property of the efd value. */
+       ATF_REQUIRE(eventfd_write(ctx.efd, 0x0000cafe) == 0);
+       ATF_REQUIRE(eventfd_write(ctx.efd, 0xbeef0000) == 0);
+
+       /* Satisfy the barrier; helper will read value and assert 0xbeefcafe. */
+       ATF_REQUIRE(wait_barrier(&ctx));
+
+       /* And wait for it to finish. */
+       ATF_REQUIRE(wait_barrier(&ctx));
+
+       /* Reap the helper. */
+       ATF_REQUIRE(pthread_join(helper, &join_val) == 0);
+
+       (void) close(ctx.efd);
+}
+
+/*****************************************************************************/
+
+ATF_TC(eventfd_semaphore);
+ATF_TC_HEAD(eventfd_semaphore, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "validates semaphore and non-blocking eventfd operation");
+}
+ATF_TC_BODY(eventfd_semaphore, tc)
+{
+       eventfd_t efd_value;



Home | Main Index | Thread Index | Old Index