From: Yuwen Chen <ywen.chen@foxmail.com>
To: ywen.chen@foxmail.com
Cc: akpm@linux-foundation.org, andrealmeid@igalia.com,
bigeasy@linutronix.de, colin.i.king@gmail.com, dave@stgolabs.net,
dvhart@infradead.org, edliaw@google.com, justinstitt@google.com,
kernel-team@android.com, licayy@foxmail.com,
linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
luto@mit.edu, mingo@redhat.com, morbo@google.com,
nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org,
shuah@kernel.org, tglx@kernel.org, usama.anjum@collabora.com,
wakel@google.com
Subject: [PATCH v6 1/2] selftests/futex: implement the interfaces related to threads
Date: Wed, 6 May 2026 11:25:01 +0800 [thread overview]
Message-ID: <tencent_E9CFDBF2DCBE8ABA5C8A76B833C6795C1309@qq.com> (raw)
In-Reply-To: <tencent_D6D5FA3C2F6FE52BA6ABA1A88C1B6E1CC605@qq.com>
There are timing issues in the use of threads in some unit test cases
for futex. A potentially good solution is to check whether the thread
is in the sleep state after creating it.
A file named futex_thread.h is added, in which several thread-related
functions are implemented to facilitate the solution of this problem.
Signed-off-by: Yuwen Chen <ywen.chen@foxmail.com>
---
.../selftests/futex/include/futex_thread.h | 109 ++++++++++++++++++
1 file changed, 109 insertions(+)
create mode 100644 tools/testing/selftests/futex/include/futex_thread.h
diff --git a/tools/testing/selftests/futex/include/futex_thread.h b/tools/testing/selftests/futex/include/futex_thread.h
new file mode 100644
index 0000000000000..7520ea8e8f885
--- /dev/null
+++ b/tools/testing/selftests/futex/include/futex_thread.h
@@ -0,0 +1,109 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef _FUTEX_THREAD_H
+#define _FUTEX_THREAD_H
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <pthread.h>
+#include <unistd.h>
+
+#define USEC_PER_SEC 1000000L
+#define WAIT_THREAD_RETRIES 100
+
+struct futex_thread {
+ pthread_t thread;
+ pthread_barrier_t barrier;
+ pid_t tid;
+ int (*threadfn)(void *arg);
+ void *arg;
+ int retval;
+};
+
+static int __wait_for_thread(FILE *fp, int timeout_us)
+{
+ char buf[80] = "";
+ int i;
+
+ for (i = 0; i < WAIT_THREAD_RETRIES; i++) {
+ if (!fgets(buf, sizeof(buf), fp))
+ return -EIO;
+ if (!strncmp(buf, "futex", 5))
+ return 0;
+ usleep(timeout_us / WAIT_THREAD_RETRIES);
+ rewind(fp);
+ }
+ return -ETIMEDOUT;
+}
+
+static void *__futex_thread_fn(void *arg)
+{
+ struct futex_thread *t = arg;
+
+ t->tid = gettid();
+ pthread_barrier_wait(&t->barrier);
+ t->retval = t->threadfn(t->arg);
+ return NULL;
+}
+
+/**
+ * futex_wait_for_thread - Wait for the child thread to sleep in the futex context
+ * @t: Thread handle.
+ * @timeout_us: The timeout for waiting for the thread to enter the sleep state.
+ */
+static int futex_wait_for_thread(struct futex_thread *t, int timeout_us)
+{
+ char fname[80];
+ FILE *fp;
+ int res;
+
+ snprintf(fname, sizeof(fname), "/proc/%d/wchan", t->tid);
+ fp = fopen(fname, "r");
+ if (!fp) {
+ usleep(USEC_PER_SEC / 10); /* 100ms */
+ return -EIO;
+ }
+
+ res = __wait_for_thread(fp, timeout_us);
+ if (res == -EIO)
+ usleep(USEC_PER_SEC / 10); /* 100ms */
+
+ fclose(fp);
+ return res;
+}
+
+/**
+ * futex_thread_create - Create a new thread for testing.
+ * @t: The handle of the newly created thread.
+ * @threadfn: The new thread starts execution by invoking threadfn
+ * @arg: The parameters passed to threadfn.
+ */
+static int futex_thread_create(struct futex_thread *t, int (*threadfn)(void *), void *arg)
+{
+ int ret;
+
+ pthread_barrier_init(&t->barrier, NULL, 2);
+ t->tid = 0;
+ t->threadfn = threadfn;
+ t->arg = arg;
+
+ ret = pthread_create(&t->thread, NULL, __futex_thread_fn, t);
+ if (ret)
+ return ret;
+
+ pthread_barrier_wait(&t->barrier);
+ return 0;
+}
+
+/**
+ * futex_thread_destroy - Wait for and reclaim the resources of the thread.
+ * @t: Thread handle.
+ */
+static int futex_thread_destroy(struct futex_thread *t)
+{
+ pthread_join(t->thread, NULL);
+ pthread_barrier_destroy(&t->barrier);
+ return t->retval;
+}
+
+#endif
--
2.34.1
next prev parent reply other threads:[~2026-05-06 3:25 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-06 3:23 [PATCH v6 0/2] selftests/futex: fix the failed futex_requeue test issue Yuwen Chen
2026-05-06 3:25 ` Yuwen Chen [this message]
2026-05-06 3:25 ` [PATCH v6 2/2] " Yuwen Chen
2026-05-09 0:05 ` [PATCH v6 0/2] " Andrew Morton
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=tencent_E9CFDBF2DCBE8ABA5C8A76B833C6795C1309@qq.com \
--to=ywen.chen@foxmail.com \
--cc=akpm@linux-foundation.org \
--cc=andrealmeid@igalia.com \
--cc=bigeasy@linutronix.de \
--cc=colin.i.king@gmail.com \
--cc=dave@stgolabs.net \
--cc=dvhart@infradead.org \
--cc=edliaw@google.com \
--cc=justinstitt@google.com \
--cc=kernel-team@android.com \
--cc=licayy@foxmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=luto@mit.edu \
--cc=mingo@redhat.com \
--cc=morbo@google.com \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=peterz@infradead.org \
--cc=shuah@kernel.org \
--cc=tglx@kernel.org \
--cc=usama.anjum@collabora.com \
--cc=wakel@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox