>From fe3c170040eed9ad3dad2030e919c6fd402bb106 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 6 Aug 2024 15:45:02 +0200 Subject: [PATCH 2/2] mtx tests: Strengthen tests. * tests/test-mtx-type.c: New file. * modules/mtx-tests (Files): Add it. (Makefile.am): Arrange to test test-mtx-type. --- ChangeLog | 7 ++++ modules/mtx-tests | 6 ++- tests/test-mtx-type.c | 93 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 tests/test-mtx-type.c diff --git a/ChangeLog b/ChangeLog index 9e5488a686..d9658c938b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2024-08-06 Bruno Haible + + mtx tests: Strengthen tests. + * tests/test-mtx-type.c: New file. + * modules/mtx-tests (Files): Add it. + (Makefile.am): Arrange to test test-mtx-type. + 2024-08-06 Bruno Haible pthread-mutex tests: Strengthen tests. diff --git a/modules/mtx-tests b/modules/mtx-tests index b29fb0b6ce..58101a1bd1 100644 --- a/modules/mtx-tests +++ b/modules/mtx-tests @@ -1,5 +1,6 @@ Files: tests/test-mtx.c +tests/test-mtx-type.c tests/atomic-int-isoc.h tests/macros.h m4/semaphore.m4 @@ -15,6 +16,7 @@ AC_CHECK_DECLS_ONCE([alarm]) AC_REQUIRE([gl_SEMAPHORE]) Makefile.am: -TESTS += test-mtx -check_PROGRAMS += test-mtx +TESTS += test-mtx test-mtx-type +check_PROGRAMS += test-mtx test-mtx-type test_mtx_LDADD = $(LDADD) @LIBSTDTHREAD@ @LIBTHREAD@ @LIB_SEMAPHORE@ +test_mtx_type_LDADD = $(LDADD) @LIBSTDTHREAD@ diff --git a/tests/test-mtx-type.c b/tests/test-mtx-type.c new file mode 100644 index 0000000000..9b4f089236 --- /dev/null +++ b/tests/test-mtx-type.c @@ -0,0 +1,93 @@ +/* Test of locking in multithreaded situations. + Copyright (C) 2024 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* Written by Bruno Haible , 2024. */ + +#include + +/* Specification. */ +#include + +#include +#include +#include + +#include "macros.h" + +/* Returns the effective type of a lock. */ +static const char * +get_effective_type (mtx_t *lock) +{ + /* Lock once. */ + ASSERT (mtx_lock (lock) == thrd_success); + + /* Try to lock a second time. */ + int err = mtx_trylock (lock); + if (err == thrd_success) + return "RECURSIVE"; + if (err == thrd_busy) + return "NORMAL"; + + return "unknown!"; +} + +int +main () +{ + /* Find the effective type of a PLAIN lock. */ + const char *type_plain; + { + mtx_t lock; + ASSERT (mtx_init (&lock, mtx_plain) == thrd_success); + type_plain = get_effective_type (&lock); + } + + /* Find the effective type of a TIMED lock. */ + const char *type_timed; + { + mtx_t lock; + ASSERT (mtx_init (&lock, mtx_timed) == thrd_success); + type_timed = get_effective_type (&lock); + } + + /* Find the effective type of a PLAIN RECURSIVE lock. */ + const char *type_plain_rec; + { + mtx_t lock; + ASSERT (mtx_init (&lock, mtx_plain | mtx_recursive) == thrd_success); + type_plain_rec = get_effective_type (&lock); + } + + /* Find the effective type of a TIMED RECURSIVE lock. */ + const char *type_timed_rec; + { + mtx_t lock; + ASSERT (mtx_init (&lock, mtx_timed | mtx_recursive) == thrd_success); + type_timed_rec = get_effective_type (&lock); + } + + printf ("PLAIN -> type = %s\n", type_plain); + printf ("TIMED -> type = %s\n", type_timed); + printf ("PLAIN RECURSIVE -> type = %s\n", type_plain_rec); + printf ("TIMED RECURSIVE -> type = %s\n", type_timed_rec); + + ASSERT (strcmp (type_plain, "NORMAL") == 0); + ASSERT (strcmp (type_timed, "NORMAL") == 0); + ASSERT (strcmp (type_plain_rec, "RECURSIVE") == 0); + ASSERT (strcmp (type_timed_rec, "RECURSIVE") == 0); + + return test_exit_status; +} -- 2.34.1