From 60acac06739f191bf433afbfbacbe738e5df3be2 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Fri, 28 Jun 2024 21:51:40 +0200 Subject: [PATCH] cond tests: Improve multithread-safety. * tests/test-cnd.c (cond_value, cond_timed_out): Mark as volatile. * tests/test-cond.c (cond_value, cond_timed_out): Likewise. * tests/test-pthread-cond.c (cond_value, cond_timed_out): Likewise. --- ChangeLog | 5 +++++ tests/test-cnd.c | 11 +++++++++-- tests/test-cond.c | 11 +++++++++-- tests/test-pthread-cond.c | 11 +++++++++-- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index a309c59a50..75f9d19a43 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2024-06-28 Bruno Haible + cond tests: Improve multithread-safety. + * tests/test-cnd.c (cond_value, cond_timed_out): Mark as volatile. + * tests/test-cond.c (cond_value, cond_timed_out): Likewise. + * tests/test-pthread-cond.c (cond_value, cond_timed_out): Likewise. + cond tests: Improve comments. * tests/test-cnd.c: Improve comments. (cond_value): Remove initializer. diff --git a/tests/test-cnd.c b/tests/test-cnd.c index 0aca5cdf07..730c879c49 100644 --- a/tests/test-cnd.c +++ b/tests/test-cnd.c @@ -59,7 +59,10 @@ /* * Condition check */ -static int cond_value; + +/* Marked volatile so that different threads see the same value. This is + good enough in practice, although in theory stdatomic.h should be used. */ +static int volatile cond_value; static cnd_t condtest; static mtx_t lockcond; @@ -121,7 +124,10 @@ test_cnd_wait () /* * Timed Condition check */ -static int cond_timed_out; + +/* Marked volatile so that different threads see the same value. This is + good enough in practice, although in theory stdatomic.h should be used. */ +static int volatile cond_timed_out; /* Stores in *TS the current time plus 1 second. */ static void @@ -193,6 +199,7 @@ test_cnd_timedwait (void) abort (); } + int main () { diff --git a/tests/test-cond.c b/tests/test-cond.c index eb92580e44..80d696a5d8 100644 --- a/tests/test-cond.c +++ b/tests/test-cond.c @@ -59,7 +59,10 @@ /* * Condition check */ -static int cond_value; + +/* Marked volatile so that different threads see the same value. This is + good enough in practice, although in theory stdatomic.h should be used. */ +static int volatile cond_value; gl_cond_define_initialized(static, condtest) gl_lock_define_initialized(static, lockcond) @@ -116,7 +119,10 @@ test_cond () /* * Timed Condition check */ -static int cond_timed_out; + +/* Marked volatile so that different threads see the same value. This is + good enough in practice, although in theory stdatomic.h should be used. */ +static int volatile cond_timed_out; /* Stores in *TS the current time plus 1 second. */ static void @@ -183,6 +189,7 @@ test_timedcond (void) abort (); } + int main () { diff --git a/tests/test-pthread-cond.c b/tests/test-pthread-cond.c index 40cba9e548..98c291c24c 100644 --- a/tests/test-pthread-cond.c +++ b/tests/test-pthread-cond.c @@ -64,7 +64,10 @@ /* * Condition check */ -static int cond_value; + +/* Marked volatile so that different threads see the same value. This is + good enough in practice, although in theory stdatomic.h should be used. */ +static int volatile cond_value; static pthread_cond_t condtest; static pthread_mutex_t lockcond; @@ -126,7 +129,10 @@ test_pthread_cond_wait () /* * Timed Condition check */ -static int cond_timed_out; + +/* Marked volatile so that different threads see the same value. This is + good enough in practice, although in theory stdatomic.h should be used. */ +static int volatile cond_timed_out; /* Stores in *TS the current time plus 1 second. */ static void @@ -199,6 +205,7 @@ test_pthread_cond_timedwait (void) abort (); } + int main () { -- 2.39.5