[LTP] [PATCH v3 2/3] tst_atomic: Introduce tst_atomic_t and apply it consistently

Li Wang liwang@redhat.com
Fri Jun 6 13:05:40 CEST 2025


This patch introduces a new tst_atomic_t typedef (int32_t) to replace
direct usage of int in atomic operations across the test framework.

The changes ensure:

- Consistent 32-bit atomic operations across platforms
- Clearer intent for variables used in atomic contexts
- Better maintainability through centralized type definition
- Fixed declaration consistency in atomic APIs

Signed-off-by: Li Wang <liwang@redhat.com>
---
 include/tst_atomic.h                          | 23 +++++++++++--------
 include/tst_fuzzy_sync.h                      |  6 ++---
 lib/newlib_tests/test08.c                     |  2 +-
 lib/newlib_tests/test09.c                     |  2 +-
 lib/newlib_tests/test15.c                     |  2 +-
 lib/newlib_tests/tst_fuzzy_sync01.c           |  2 +-
 lib/newlib_tests/tst_fuzzy_sync02.c           |  2 +-
 testcases/kernel/containers/pidns/pidns32.c   |  2 +-
 .../kernel/controllers/cgroup/cgroup_core03.c |  2 +-
 testcases/kernel/fs/fs_fill/fs_fill.c         |  2 +-
 testcases/kernel/fs/read_all/read_all.c       |  4 ++--
 testcases/kernel/io/ltp-aiodio/dio_read.c     |  2 +-
 testcases/kernel/mem/mtest06/mmap1.c          |  2 +-
 testcases/kernel/mem/mtest06/mmap3.c          |  2 +-
 .../kernel/syscalls/exit_group/exit_group01.c |  2 +-
 .../syscalls/futex/futex_cmp_requeue01.c      |  4 ++--
 .../syscalls/ipc/msgstress/msgstress01.c      |  2 +-
 testcases/kernel/syscalls/writev/writev03.c   |  2 +-
 .../func/sched_football/sched_football.c      |  4 ++--
 19 files changed, 36 insertions(+), 33 deletions(-)

diff --git a/include/tst_atomic.h b/include/tst_atomic.h
index 7c320c633..57b6b6bd5 100644
--- a/include/tst_atomic.h
+++ b/include/tst_atomic.h
@@ -6,23 +6,26 @@
 #ifndef TST_ATOMIC_H__
 #define TST_ATOMIC_H__
 
+#include <stdint.h>
 #include "config.h"
 
+typedef int32_t tst_atomic_t;
+
 #if HAVE_ATOMIC_MEMORY_MODEL == 1
 
 /* Use __atomic built-ins (GCC >= 4.7), with sequential consistency. */
 
-static inline int tst_atomic_add_return(int i, int *v)
+static inline int tst_atomic_add_return(int32_t i, tst_atomic_t *v)
 {
 	return __atomic_add_fetch(v, i, __ATOMIC_SEQ_CST);
 }
 
-static inline int tst_atomic_load(int *v)
+static inline int32_t tst_atomic_load(tst_atomic_t *v)
 {
 	return __atomic_load_n(v, __ATOMIC_SEQ_CST);
 }
 
-static inline void tst_atomic_store(int i, int *v)
+static inline void tst_atomic_store(int32_t i, tst_atomic_t *v)
 {
 	__atomic_store_n(v, i, __ATOMIC_SEQ_CST);
 }
@@ -31,14 +34,14 @@ static inline void tst_atomic_store(int i, int *v)
 
 /* Use __sync built-ins (GCC >= 4.1), with explicit memory barriers. */
 
-static inline int tst_atomic_add_return(int i, int *v)
+static inline int tst_atomic_add_return(int32_t i, tst_atomic_t *v)
 {
 	return __sync_add_and_fetch(v, i);
 }
 
-static inline int tst_atomic_load(int *v)
+static inline int32_t tst_atomic_load(tst_atomic_t *v)
 {
-	int ret;
+	tst_atomic_t ret;
 
 	__sync_synchronize();
 	ret = *v;
@@ -46,7 +49,7 @@ static inline int tst_atomic_load(int *v)
 	return ret;
 }
 
-static inline void tst_atomic_store(int i, int *v)
+static inline void tst_atomic_store(int32_t i, tst_atomic_t *v)
 {
 	__sync_synchronize();
 	*v = i;
@@ -57,14 +60,14 @@ static inline void tst_atomic_store(int i, int *v)
 # error "Your compiler does not support atomic operations (__atomic or __sync)"
 #endif
 
-static inline int tst_atomic_inc(int *v)
+static inline int tst_atomic_inc(tst_atomic_t *v)
 {
 	return tst_atomic_add_return(1, v);
 }
 
-static inline int tst_atomic_dec(int *v)
+static inline int tst_atomic_dec(tst_atomic_t *v)
 {
 	return tst_atomic_add_return(-1, v);
 }
 
-#endif	/* TST_ATOMIC_H__ */
+#endif /* TST_ATOMIC_H__ */
diff --git a/include/tst_fuzzy_sync.h b/include/tst_fuzzy_sync.h
index bef424002..b22364cab 100644
--- a/include/tst_fuzzy_sync.h
+++ b/include/tst_fuzzy_sync.h
@@ -155,11 +155,11 @@ struct tst_fzsync_pair {
 	float max_dev_ratio;
 
 	/** Internal; Atomic counter used by fzsync_pair_wait() */
-	int a_cntr;
+	tst_atomic_t a_cntr;
 	/** Internal; Atomic counter used by fzsync_pair_wait() */
-	int b_cntr;
+	tst_atomic_t b_cntr;
 	/** Internal; Used by tst_fzsync_pair_exit() and fzsync_pair_wait() */
-	int exit;
+	tst_atomic_t exit;
 	/** Internal; The test time remaining on tst_fzsync_pair_reset() */
 	float exec_time_start;
 	/**
diff --git a/lib/newlib_tests/test08.c b/lib/newlib_tests/test08.c
index 5099b08d3..d48bf29ee 100644
--- a/lib/newlib_tests/test08.c
+++ b/lib/newlib_tests/test08.c
@@ -22,7 +22,7 @@ static void setup(void)
 
 static void cleanup(void)
 {
-	static int flag;
+	static tst_atomic_t flag;
 
 	/* Avoid subsequent threads to enter the cleanup */
 	if (tst_atomic_inc(&flag) != 1)
diff --git a/lib/newlib_tests/test09.c b/lib/newlib_tests/test09.c
index 0f42bacc6..eae258e2d 100644
--- a/lib/newlib_tests/test09.c
+++ b/lib/newlib_tests/test09.c
@@ -13,7 +13,7 @@
 #define THREADS 64
 #define ITERATIONS 100000
 
-static int atomic;
+static tst_atomic_t atomic;
 
 static void *worker(void *id)
 {
diff --git a/lib/newlib_tests/test15.c b/lib/newlib_tests/test15.c
index 3a2ac362e..c63da18fc 100644
--- a/lib/newlib_tests/test15.c
+++ b/lib/newlib_tests/test15.c
@@ -39,7 +39,7 @@ struct block {
 	intptr_t filler[FILLER];
 };
 
-static int atomic;
+static tst_atomic_t atomic;
 /* Instead of storing seq_n on the stack (probably next to the atomic variable
  * above), we store it in the middle of some anonymous mapped memory and keep
  * a pointer to it. This should decrease the probability that the value of
diff --git a/lib/newlib_tests/tst_fuzzy_sync01.c b/lib/newlib_tests/tst_fuzzy_sync01.c
index 6c361e8cc..b1390f460 100644
--- a/lib/newlib_tests/tst_fuzzy_sync01.c
+++ b/lib/newlib_tests/tst_fuzzy_sync01.c
@@ -88,7 +88,7 @@ struct race {
 	const struct window b;
 };
 
-static int H;
+static tst_atomic_t H;
 static struct tst_fzsync_pair pair;
 
 static const struct race races[] = {
diff --git a/lib/newlib_tests/tst_fuzzy_sync02.c b/lib/newlib_tests/tst_fuzzy_sync02.c
index 0a595b1e2..bc079f6ff 100644
--- a/lib/newlib_tests/tst_fuzzy_sync02.c
+++ b/lib/newlib_tests/tst_fuzzy_sync02.c
@@ -61,7 +61,7 @@ struct race {
 	const struct window b;
 };
 
-static int H, D;
+static tst_atomic_t H, D;
 static struct tst_fzsync_pair pair;
 
 /**
diff --git a/testcases/kernel/containers/pidns/pidns32.c b/testcases/kernel/containers/pidns/pidns32.c
index 3798f798b..a192c128d 100644
--- a/testcases/kernel/containers/pidns/pidns32.c
+++ b/testcases/kernel/containers/pidns/pidns32.c
@@ -21,7 +21,7 @@ static const struct tst_clone_args args = {
 	.flags = CLONE_NEWPID,
 	.exit_signal = SIGCHLD,
 };
-static int *level;
+static tst_atomic_t *level;
 
 static pid_t child_func(void)
 {
diff --git a/testcases/kernel/controllers/cgroup/cgroup_core03.c b/testcases/kernel/controllers/cgroup/cgroup_core03.c
index 49b8eff40..846c00f29 100644
--- a/testcases/kernel/controllers/cgroup/cgroup_core03.c
+++ b/testcases/kernel/controllers/cgroup/cgroup_core03.c
@@ -22,7 +22,7 @@
 #define PID_NUM MIN(MAX_PID_NUM, (tst_ncpus_available() + 1))
 #define BUF_LEN (20 * PID_NUM)
 
-static int *data_ptr;
+static tst_atomic_t *data_ptr;
 static char *buf;
 static struct tst_cg_group *cg_child_test_simple;
 
diff --git a/testcases/kernel/fs/fs_fill/fs_fill.c b/testcases/kernel/fs/fs_fill/fs_fill.c
index 1662cdb50..131128db5 100644
--- a/testcases/kernel/fs/fs_fill/fs_fill.c
+++ b/testcases/kernel/fs/fs_fill/fs_fill.c
@@ -22,7 +22,7 @@
 
 static volatile int run;
 static unsigned int nthreads;
-static int enospc_cnt;
+static tst_atomic_t enospc_cnt;
 static struct worker *workers;
 
 struct worker {
diff --git a/testcases/kernel/fs/read_all/read_all.c b/testcases/kernel/fs/read_all/read_all.c
index 14588a829..e18945a34 100644
--- a/testcases/kernel/fs/read_all/read_all.c
+++ b/testcases/kernel/fs/read_all/read_all.c
@@ -57,7 +57,7 @@
 
 struct queue {
 	sem_t sem;
-	int front;
+	tst_atomic_t front;
 	int back;
 	char data[QUEUE_SIZE];
 	char popped[BUFFER_SIZE];
@@ -67,7 +67,7 @@ struct worker {
 	int i;
 	pid_t pid;
 	struct queue *q;
-	int last_seen;
+	tst_atomic_t last_seen;
 	unsigned int kill_sent:1;
 };
 
diff --git a/testcases/kernel/io/ltp-aiodio/dio_read.c b/testcases/kernel/io/ltp-aiodio/dio_read.c
index f9587ef3d..1c913cc2b 100644
--- a/testcases/kernel/io/ltp-aiodio/dio_read.c
+++ b/testcases/kernel/io/ltp-aiodio/dio_read.c
@@ -28,7 +28,7 @@ static int numchildren = 8;
 static long long writesize = 32 * 1024 * 1024;
 static long long readsize = 32 * 1024 * 1024;
 static long long filesize = 128 * 1024 * 1024;
-static int *children_completed;
+static tst_atomic_t *children_completed;
 static char *iobuf;
 static int fd;
 
diff --git a/testcases/kernel/mem/mtest06/mmap1.c b/testcases/kernel/mem/mtest06/mmap1.c
index 4e67f5fb9..5c4ffa665 100644
--- a/testcases/kernel/mem/mtest06/mmap1.c
+++ b/testcases/kernel/mem/mtest06/mmap1.c
@@ -55,7 +55,7 @@ static unsigned long data_matched;
 static unsigned long repeated_reads;
 
 /* sequence id for each map/unmap performed */
-static int mapcnt, unmapcnt;
+static tst_atomic_t mapcnt, unmapcnt;
 /* stored sequence id before making read attempt */
 static int br_map, br_unmap;
 
diff --git a/testcases/kernel/mem/mtest06/mmap3.c b/testcases/kernel/mem/mtest06/mmap3.c
index 6cebc6fbe..58127ad9a 100644
--- a/testcases/kernel/mem/mtest06/mmap3.c
+++ b/testcases/kernel/mem/mtest06/mmap3.c
@@ -28,7 +28,7 @@ static int loops = 1000;
 static int threads = 40;
 
 static volatile int sig_caught;
-static int threads_running;
+static tst_atomic_t threads_running;
 
 static int mkfile(int *size)
 {
diff --git a/testcases/kernel/syscalls/exit_group/exit_group01.c b/testcases/kernel/syscalls/exit_group/exit_group01.c
index 585bb7cdb..9005f4679 100644
--- a/testcases/kernel/syscalls/exit_group/exit_group01.c
+++ b/testcases/kernel/syscalls/exit_group/exit_group01.c
@@ -23,7 +23,7 @@ static int cpu_count;
 
 static struct worker_data {
 	pid_t tid;
-	int counter;
+	tst_atomic_t counter;
 } *workers_data;
 
 static void *worker(void *arg)
diff --git a/testcases/kernel/syscalls/futex/futex_cmp_requeue01.c b/testcases/kernel/syscalls/futex/futex_cmp_requeue01.c
index 946e4e949..51b5c6a8d 100644
--- a/testcases/kernel/syscalls/futex/futex_cmp_requeue01.c
+++ b/testcases/kernel/syscalls/futex/futex_cmp_requeue01.c
@@ -22,8 +22,8 @@
 
 struct shared_data {
 	futex_t futexes[2];
-	int spurious;
-	int test_done;
+	tst_atomic_t spurious;
+	tst_atomic_t test_done;
 };
 
 static struct shared_data *sd;
diff --git a/testcases/kernel/syscalls/ipc/msgstress/msgstress01.c b/testcases/kernel/syscalls/ipc/msgstress/msgstress01.c
index 22a2c0e7a..10c9adcb0 100644
--- a/testcases/kernel/syscalls/ipc/msgstress/msgstress01.c
+++ b/testcases/kernel/syscalls/ipc/msgstress/msgstress01.c
@@ -49,7 +49,7 @@ static int num_messages = 1000;
 static int num_iterations = MAXNREPS;
 static volatile int *stop;
 static volatile int *fail;
-static int *finished;
+static tst_atomic_t *finished;
 static int *flags;
 
 static int get_used_sysvipc(void)
diff --git a/testcases/kernel/syscalls/writev/writev03.c b/testcases/kernel/syscalls/writev/writev03.c
index f2326095e..a0b237112 100644
--- a/testcases/kernel/syscalls/writev/writev03.c
+++ b/testcases/kernel/syscalls/writev/writev03.c
@@ -33,7 +33,7 @@
 
 static unsigned char buf[BUF_SIZE], *map_ptr;
 static int mapfd = -1, writefd = -1, readfd = -1;
-static int written;
+static tst_atomic_t written;
 static struct tst_fzsync_pair fzsync_pair;
 struct iovec iov[5];
 
diff --git a/testcases/realtime/func/sched_football/sched_football.c b/testcases/realtime/func/sched_football/sched_football.c
index b89970542..1d761d43c 100644
--- a/testcases/realtime/func/sched_football/sched_football.c
+++ b/testcases/realtime/func/sched_football/sched_football.c
@@ -41,10 +41,10 @@
 #define SPIN_TIME_NS 200000000ULL
 #define SLEEP_TIME_NS 50000000ULL
 
-static int the_ball;
+static tst_atomic_t the_ball;
 static int players_per_team = 0;
 static int game_length = DEF_GAME_LENGTH;
-static int players_ready;
+static tst_atomic_t players_ready;
 
 static char *str_game_length;
 static char *str_players_per_team;
-- 
2.49.0



More information about the ltp mailing list