commit 0322d44fd2e2e0a1d7b00e05c5e6a366e2b1ae69
parent 7a8004adb4b3c748264f3c7851b3e12a0224718c
Author: vaplv <vaplv@free.fr>
Date: Tue, 29 Apr 2025 18:58:47 +0200
Using POSIX threads instead of OpenMP in tests
The condition and mutex tests relied on OpenMP to create the threads
used to check their behaviour. This commit replaces OpenMP with POSIX
threads and therefore removes this unnecessary dependency. As a welcome
side-effect, all tests are now compiled and linked with the same flags.
This makes the Makefile simpler.
Diffstat:
3 files changed, 59 insertions(+), 47 deletions(-)
diff --git a/Makefile b/Makefile
@@ -262,7 +262,7 @@ $(TEST_DEP) $(TEST_OBJ): config.mk rsys-local.pc
clean_test:
rm -f $(TEST_DEP) $(TEST_OBJ) $(TEST_TGT)
- rm -f libtest_lib.so test_lib.o .test .test.ppm test_text_reader.txt test.ppm
+ rm -f libtest_lib.so test_lib.o test_text_reader.txt test.ppm
for i in $(TEST_SRC); do rm -f "$$(basename "$${i}" ".c")"; done
test_lib.o: src/test_library.c src/rsys.h config.mk
@@ -276,21 +276,22 @@ test_library: libtest_lib.so
src/test_algorithm.o \
src/test_atomic.o \
src/test_binary_heap.o \
+src/test_condition.o \
src/test_cstr.o \
-src/test_double22.o \
src/test_double2.o \
-src/test_double33.o \
+src/test_double22.o \
src/test_double3.o \
-src/test_double44.o \
+src/test_double33.o \
src/test_double4.o \
+src/test_double44.o \
src/test_dynamic_array.o \
src/test_endianness.o \
-src/test_float22.o \
src/test_float2.o \
-src/test_float33.o \
+src/test_float22.o \
src/test_float3.o \
-src/test_float44.o \
+src/test_float33.o \
src/test_float4.o \
+src/test_float44.o \
src/test_free_list.o \
src/test_func_name.o \
src/test_hash_sha256.o \
@@ -303,6 +304,7 @@ src/test_math.o \
src/test_mem_allocator.o \
src/test_misc.o \
src/test_morton.o \
+src/test_mutex.o \
src/test_quaternion.o \
src/test_ref.o \
src/test_signal.o \
@@ -314,15 +316,10 @@ src/test_vmacros.o \
:
$(CC) $(CFLAGS_TEST) -c $(@:.o=.c) -o $@
-src/test_condition.o \
-src/test_mutex.o \
-:
- $(CC) $(CFLAGS_TEST) -fopenmp -c $(@:.o=.c) -o $@
-
-
test_algorithm \
test_atomic \
-test_binary_heap\
+test_binary_heap \
+test_condition \
test_cstr \
test_double2 \
test_double22 \
@@ -350,6 +347,7 @@ test_math \
test_mem_allocator \
test_misc \
test_morton \
+test_mutex \
test_quaternion \
test_ref \
test_signal \
@@ -361,11 +359,6 @@ test_vmacros \
: config.mk rsys-local.pc $(LIBNAME)
$(CC) $(CFLAGS_TEST) -o $@ src/$@.o $(LDFLAGS_TEST)
-test_condition \
-test_mutex \
-: config.mk rsys-local.pc $(LIBNAME)
- $(CC) $(CFLAGS_TEST) -o $@ src/$@.o $(LDFLAGS_TEST) -fopenmp
-
test: tests
@err=0; \
for i in $(TEST_SRC); do \
diff --git a/src/test_condition.c b/src/test_condition.c
@@ -13,10 +13,13 @@
* You should have received a copy of the GNU General Public License
* along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */
+#define _POSIX_C_SOURCE 199506L /* POSIX threads */
+
#include "condition.h"
#include "list.h"
-#include <omp.h>
+
#include <string.h>
+#include <pthread.h>
static const char* src_str[] = {
"Rcvfbqr 1, XARR-QRRC VA GUR QRNQ:\n\
@@ -90,9 +93,10 @@ struct buff
char scratch[1024];
};
-static void
-read(struct stream* stream)
+static void*
+read(void* arg)
{
+ struct stream* stream = arg;
size_t i = 0;
ASSERT(stream);
@@ -117,11 +121,13 @@ read(struct stream* stream)
cond_broadcast(stream->cond_fill);
}
+ return NULL;
}
-static void
-write(struct stream* stream)
+static void*
+write(void* arg)
{
+ struct stream* stream = arg;
size_t i = 0;
ASSERT(stream);
@@ -147,6 +153,7 @@ write(struct stream* stream)
cond_broadcast(stream->cond_flush);
}
+ return NULL;
}
int
@@ -154,6 +161,7 @@ main(int argc, char** argv)
{
struct buff buff[2];
struct stream stream;
+ pthread_t thread;
(void)argc, (void)argv;
list_init(&stream.list_fill);
@@ -170,13 +178,10 @@ main(int argc, char** argv)
list_add(&stream.list_fill, &buff[0].node);
list_add(&stream.list_fill, &buff[1].node);
- #pragma omp parallel sections shared(stream) num_threads(2)
- {
- #pragma omp section
- read(&stream);
- #pragma omp section
- write(&stream);
- }
+ CHK(pthread_create(&thread, NULL, read, &stream) == 0); /* Sub thread */
+ write(&stream);
+ CHK(pthread_join(thread, NULL) == 0);
+
mutex_destroy(stream.mutex);
cond_destroy(stream.cond_flush);
cond_destroy(stream.cond_fill);
diff --git a/src/test_mutex.c b/src/test_mutex.c
@@ -13,11 +13,13 @@
* You should have received a copy of the GNU General Public License
* along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */
+#define _POSIX_C_SOURCE 199506L /* POSIX threads and nanosleep */
+
#include "clock_time.h"
#include "mutex.h"
#include <string.h>
-#include <omp.h>
+#include <pthread.h>
static const char src_str[] = {
'R','c','v','f','b','q','r',' ','1',',',' ','X','A','R','R',
@@ -172,23 +174,38 @@ struct string
int i;
};
-static void
-string_write(struct string* string)
+#include <unistd.h>
+
+static void*
+string_write(void* arg)
{
+ struct string* string = arg;
+ struct timespec time;
ASSERT(string);
+ time.tv_sec = 0;
+ time.tv_nsec = 10;
+
for(;;) {
+ int i = 0;
mutex_lock(string->mutex);
- if((unsigned)string->i >= sizeof(src_str)/sizeof(char) + 1) {
+
+ i = string->i;
+
+ /* Forcing thread interleaving */
+ CHK(nanosleep(&time, NULL) == 0);
+
+ if((unsigned)i >= sizeof(src_str)/sizeof(char) + 1) {
mutex_unlock(string->mutex);
break;
}
- string->str[string->i] = src_str[string->i];
- ++string->i;
+ string->str[string->i] = src_str[i];
+ string->i = i+1;
mutex_unlock(string->mutex);
}
+ return NULL;
}
static void
@@ -196,6 +213,7 @@ test_mutex(void)
{
struct string string;
struct time time_start, time_end, time_res;
+ pthread_t thread;
char dump[32];
string.str[0] = '\0';
@@ -206,16 +224,12 @@ test_mutex(void)
time_current(&time_start);
- #pragma omp parallel
- {
- #pragma omp sections
- {
- #pragma omp section
- string_write(&string);
- #pragma omp section
- string_write(&string);
- }
- }
+ /* Run a sub-thread */
+ CHK(pthread_create(&thread, NULL, string_write, &string) == 0);
+ string_write(&string);
+
+ CHK(pthread_join(thread, NULL) == 0);
+
time_current(&time_end);
time_sub(&time_res, &time_end, &time_start);