]> Savannah Git Hosting - gnulib.git/commitdiff
mtx tests: Strengthen tests.
authorBruno Haible <bruno@clisp.org>
Tue, 6 Aug 2024 13:45:02 +0000 (15:45 +0200)
committerBruno Haible <bruno@clisp.org>
Tue, 6 Aug 2024 13:45:02 +0000 (15:45 +0200)
* tests/test-mtx-type.c: New file.
* modules/mtx-tests (Files): Add it.
(Makefile.am): Arrange to test test-mtx-type.

ChangeLog
modules/mtx-tests
tests/test-mtx-type.c [new file with mode: 0644]

index 9e5488a68658b122c114f96ce8d6f2f1088eb805..d9658c938bc29d3f4510f58483b9124582b02aa8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2024-08-06  Bruno Haible  <bruno@clisp.org>
+
+       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  <bruno@clisp.org>
 
        pthread-mutex tests: Strengthen tests.
index b29fb0b6ce09d818c3a9639a87f02ab3a92fc561..58101a1bd1940d5e415d5b108cf1fdacb1b43ba9 100644 (file)
@@ -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 (file)
index 0000000..9b4f089
--- /dev/null
@@ -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 <https://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2024.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <threads.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+
+#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;
+}