Re: [PATCH 3/3] selftests/futex: fix the issue of abnormal test results caused by thread timing

From: Licay

Date: Thu Dec 11 2025 - 23:30:39 EST


From: Licay <licayy@xxxxxxxxxxx>

Hi Ywen,

Thanks for the patch! I have a few suggestions to improve it:

1. Avoid Parsing /proc
The current approach uses get_thread_state() to read /proc/$pid/status, which isn't very reliable.
A better way would be to have waiterfn directly signal when it's ready using atomic operations.

2. Use Atomic Counting Instead of Polling Thread State
Before entering futex_wait, waiterfn can atomically increment a counter. The parent thread then just waits for this counter to reach the expected value.
This is much simpler and avoids the overhead of checking /proc repeatedly.

3. Use Standard Atomic Types
Replace the custom READ_ONCE/WRITE_ONCE macros with standard <stdatomic.h> types like atomic_int.
It's cleaner and more portable across different platforms.

Here's the basic idea:
- Add a global atomic_int ready_count variable
- In waiterfn: atomic_fetch_add(&ready_count, 1) right before futex_wait()
- Parent thread: spin-wait until atomic_load(&ready_count) reaches the expected value

This approach is much cleaner - no /proc dependency, simpler logic, and better performance.

Best regards,
Licay
Signed-off-by: Licay <licayy@xxxxxxxxxxx>