[PATCH v4 07/15] lib/igt_dummyload: Avoid pthread_cancel use by introducing an exit flag

Zbigniew Kempczyński zbigniew.kempczynski at intel.com
Thu May 15 08:15:23 UTC 2025


On Wed, May 07, 2025 at 04:01:40PM +0000, Jeevaka Prabu Badrappan wrote:
> Android build fails due to pthread_cancel unavailability.
> Replace pthread_cancel with thread exit based on an exit flag.
> 
> Signed-off-by: Jeevaka Prabu Badrappan <jeevaka.badrappan at intel.com>
> ---
>  lib/igt_dummyload.c | 6 +++---
>  lib/igt_dummyload.h | 2 ++
>  2 files changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/lib/igt_dummyload.c b/lib/igt_dummyload.c
> index 3cf80b762..38249afb7 100644
> --- a/lib/igt_dummyload.c
> +++ b/lib/igt_dummyload.c
> @@ -530,7 +530,7 @@ static void *timer_thread(void *data)
>  	/* Wait until we see the timer fire, or we get cancelled */
>  	do {
>  		read(spin->timerfd, &overruns, sizeof(overruns));
> -	} while (!overruns);
> +	} while (!overruns && !spin->exit_thread);

I've experimented a bit with this - read() in this case is a
cancellation point and it will block until the timer fires.
Instead of introducing spin->exit_thread it is better to change
timer to tv_sec = 0, tv_nsec = 1 and call timerfd_settime().

>  
>  	igt_spin_end(spin);
>  	return NULL;
> @@ -565,7 +565,7 @@ void igt_spin_set_timeout(igt_spin_t *spin, int64_t ns)
>  	timerfd = timerfd_create(CLOCK_MONOTONIC, 0);
>  	igt_assert(timerfd >= 0);
>  	spin->timerfd = timerfd;
> -
> +	spin->exit_thread = false;
>  	pthread_attr_init(&attr);
>  	pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
>  	pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
> @@ -631,7 +631,7 @@ void igt_spin_end(igt_spin_t *spin)
>  static void __igt_spin_free(int fd, igt_spin_t *spin)
>  {
>  	if (spin->timerfd >= 0) {
> -		pthread_cancel(spin->timer_thread);

I mean I would do:

#ifdef ANDROID
	tv_sec = 0
	tv_nsec = 1
	timerfd_settime()
#else
	pthread_cancel()
#endif

I also wonder to totally remove pthread_cancel() leaving only timer
setting, as from my experiments looks in this case cancelling is not
necessary anymore. But this should be verified by CI.

--
Zbigniew


> +		spin->exit_thread = true;
>  		igt_assert(pthread_join(spin->timer_thread, NULL) == 0);
>  		close(spin->timerfd);
>  	}
> diff --git a/lib/igt_dummyload.h b/lib/igt_dummyload.h
> index b771011af..07b21227d 100644
> --- a/lib/igt_dummyload.h
> +++ b/lib/igt_dummyload.h
> @@ -26,6 +26,7 @@
>  #define __IGT_DUMMYLOAD_H__
>  
>  #include <stdint.h>
> +#include <stdatomic.h>
>  #include <time.h>
>  
>  #include "drmtest.h"
> @@ -75,6 +76,7 @@ typedef struct igt_spin {
>  
>  	struct timespec last_signal;
>  	pthread_t timer_thread;
> +	atomic_bool exit_thread;
>  	int timerfd;
>  
>  	int out_fence;
> -- 
> 2.34.1
> 


More information about the igt-dev mailing list