summaryrefslogtreecommitdiff
path: root/regress/sys
diff options
context:
space:
mode:
authorMark Kettenis <kettenis@cvs.openbsd.org>2021-11-22 18:42:17 +0000
committerMark Kettenis <kettenis@cvs.openbsd.org>2021-11-22 18:42:17 +0000
commit4aa80a8fe8c9f479bfc1e32dda517f68cbdd07aa (patch)
treeed8a0ab7730acdd41b647eff58032a83f7ff76c7 /regress/sys
parentf85ea9e1997d74d59085cf7d67ad77efff7333b0 (diff)
Add regress test for futexes in shared anonymous memory.
ok mpi@
Diffstat (limited to 'regress/sys')
-rw-r--r--regress/sys/kern/futex/futex.c44
1 files changed, 43 insertions, 1 deletions
diff --git a/regress/sys/kern/futex/futex.c b/regress/sys/kern/futex/futex.c
index 018e1c6c1b9..abaca82d7d9 100644
--- a/regress/sys/kern/futex/futex.c
+++ b/regress/sys/kern/futex/futex.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: futex.c,v 1.4 2021/06/11 10:30:36 kettenis Exp $ */
+/* $OpenBSD: futex.c,v 1.5 2021/11/22 18:42:16 kettenis Exp $ */
/*
* Copyright (c) 2017 Martin Pieuchot
*
@@ -124,6 +124,48 @@ main(int argc, char *argv[])
assert(munmap(shlock, sizeof(*shlock)) == 0);
+ /* Create anonymous memory for sharing a lock. */
+ shlock = mmap(NULL, sizeof(*shlock), PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0);
+ assert(shlock != MAP_FAILED);
+
+ /* Wake another process. */
+ pid = fork();
+ assert(pid != -1);
+ if (pid == 0) {
+ usleep(50000);
+ futex_wake(shlock, -1, 0);
+ _exit(0);
+ } else {
+ assert(futex_twait(shlock, 0, 0, NULL, 0) == 0);
+ assert(waitpid(pid, &status, 0) == pid);
+ assert(WIFEXITED(status));
+ assert(WEXITSTATUS(status) == 0);
+ }
+
+ /* Cannot wake another process using a private futex. */
+ for (i = 1; i < 4; i++) {
+ pid = fork();
+ assert(pid != -1);
+ if (pid == 0) {
+ usleep(50000);
+ futex_wake(shlock, -1,
+ (i & 1) ? FUTEX_PRIVATE_FLAG : 0);
+ _exit(0);
+ } else {
+ tmo.tv_sec = 0;
+ tmo.tv_nsec = 200000000;
+ assert(futex_twait(shlock, 0, CLOCK_REALTIME, &tmo,
+ (i & 2) ? FUTEX_PRIVATE_FLAG : 0) == -1);
+ assert(errno == ETIMEDOUT);
+ assert(waitpid(pid, &status, 0) == pid);
+ assert(WIFEXITED(status));
+ assert(WEXITSTATUS(status) == 0);
+ }
+ }
+
+ assert(munmap(shlock, sizeof(*shlock)) == 0);
+
return 0;
}