]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
term-style-control tests: Add a test with multithreading.
authorBruno Haible <bruno@clisp.org>
Mon, 18 May 2026 07:43:37 +0000 (09:43 +0200)
committerBruno Haible <bruno@clisp.org>
Mon, 18 May 2026 07:43:37 +0000 (09:43 +0200)
* tests/test-term-style-control-yes.h: New file, based on
tests/test-term-style-control-yes.c.
* tests/test-term-style-control-yes.c: Move most definitions to
tests/test-term-style-control-yes.h.
Include test-term-style-control-yes.h.
(main): Just invoke styled_yes_loop.
* tests/test-term-style-control-yes-mt.c: New file.
* modules/term-style-control-tests (Files): Add
tests/test-term-style-control-yes.h,
tests/test-term-style-control-yes-mt.c.
(Depends-on): Add thread, nanosleep.
(Makefile.am): Arrange to build test-term-style-control-yes-mt.

ChangeLog
modules/term-style-control-tests
tests/test-term-style-control-yes-mt.c [new file with mode: 0644]
tests/test-term-style-control-yes.c
tests/test-term-style-control-yes.h [new file with mode: 0644]

index 161534f35efd612d8b1ca50d4b9bb124426f0a32..fe74cffec07a13df6fd3d007c0185477a8a18ca8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2026-05-18  Bruno Haible  <bruno@clisp.org>
+
+       term-style-control tests: Add a test with multithreading.
+       * tests/test-term-style-control-yes.h: New file, based on
+       tests/test-term-style-control-yes.c.
+       * tests/test-term-style-control-yes.c: Move most definitions to
+       tests/test-term-style-control-yes.h.
+       Include test-term-style-control-yes.h.
+       (main): Just invoke styled_yes_loop.
+       * tests/test-term-style-control-yes-mt.c: New file.
+       * modules/term-style-control-tests (Files): Add
+       tests/test-term-style-control-yes.h,
+       tests/test-term-style-control-yes-mt.c.
+       (Depends-on): Add thread, nanosleep.
+       (Makefile.am): Arrange to build test-term-style-control-yes-mt.
+
 2026-05-17  Bruno Haible  <bruno@clisp.org>
 
        thread-optim: Update documentation.
index 22ae8139eb425a4b9ea515a090901b56e497b5fe..b1efef3e3eefd74cd6c186cbd99004b9ec7a74c9 100644 (file)
@@ -1,17 +1,22 @@
 Files:
 tests/test-term-style-control-hello.c
 tests/test-term-style-control-yes.c
+tests/test-term-style-control-yes-mt.c
+tests/test-term-style-control-yes.h
 
 Depends-on:
 bool
 unistd-h
 full-write
+thread
+nanosleep
 
 configure.ac:
 
 Makefile.am:
 TESTS += test-term-style-control-hello
 check_PROGRAMS += test-term-style-control-hello
-noinst_PROGRAMS += test-term-style-control-yes
+noinst_PROGRAMS += test-term-style-control-yes test-term-style-control-yes-mt
 test_term_style_control_hello_LDADD = $(LDADD) @LIBINTL@ $(LIBTHREAD)
 test_term_style_control_yes_LDADD = $(LDADD) @LIBINTL@ $(LIBTHREAD)
+test_term_style_control_yes_mt_LDADD = $(LDADD) @LIBINTL@ $(LIBMULTITHREAD) $(NANOSLEEP_LIB)
diff --git a/tests/test-term-style-control-yes-mt.c b/tests/test-term-style-control-yes-mt.c
new file mode 100644 (file)
index 0000000..e863883
--- /dev/null
@@ -0,0 +1,66 @@
+/* Interactive test program for the term-style-control module.
+   Copyright (C) 2026 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>, 2026.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include "term-style-control.h"
+
+#if USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS
+
+#include <time.h>
+#include "glthread/thread.h"
+
+#include "test-term-style-control-yes.h"
+
+/* Thread that just sleeps and is ready to receive signals.  */
+static void *
+other_thread_func (_GL_UNUSED void *arg)
+{
+  for (;;)
+    {
+      struct timespec duration;
+      duration.tv_sec = 1;
+      duration.tv_nsec = 0;
+      nanosleep (&duration, NULL);
+    }
+}
+
+int
+main ()
+{
+  /* Create another thread.  */
+  (void) gl_thread_create (other_thread_func, NULL);
+
+  styled_yes_loop ();
+}
+
+#else
+
+/* No multithreading available.  */
+
+#include <stdio.h>
+
+int
+main ()
+{
+  fputs ("Skipping test: multithreading not enabled\n", stderr);
+  return 77;
+}
+
+#endif
index 3f7fc382a617a9d08fbccee8be41841cd806755f..11e96409c9771476989ce6bc4bcd93183a12c4cf 100644 (file)
@@ -1,6 +1,5 @@
 /* Interactive test program for the term-style-control module.
    Copyright (C) 2019-2026 Free Software Foundation, Inc.
-   Written by Bruno Haible <bruno@clisp.org>, 2019.
 
    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
    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>, 2019.  */
+
 #include <config.h>
 
 /* Specification.  */
 #include "term-style-control.h"
 
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "full-write.h"
-
-/* This program outputs an endless amount of lines, each consisting of a
-   single 'y', in red color and underlined.
-   It can be used to exercise race conditions caused by
-     - simultaneous keyboard input on the terminal,
-     - pressing Ctrl-C,
-     - pressing Ctrl-Z and then "fg".  */
-
-/* ECMA-48 / ISO 6429 escape sequences.  See
-   https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
- */
-static const char set_underline_on[] = "\033[4m";
-static const char set_underline_off[] = "\033[24m";
-static const char set_foreground_color_red[] = "\033[31m";
-static const char set_foreground_color_default[] = "\033[39m";
-
-struct term_style_user_data
-{
-  /* This field is marked volatile, because it is accessed from the
-     async-safe function async_set_attributes_from_default.  */
-  bool volatile red_and_underline;
-
-  struct term_style_control_data ctrl_data;
-};
-
-static struct term_style_control_data *
-get_control_data (struct term_style_user_data *user_data)
-{
-  return &user_data->ctrl_data;
-}
-
-static void
-restore (_GL_UNUSED struct term_style_user_data *user_data)
-{
-  fputs (set_underline_off, stdout);
-  fputs (set_foreground_color_default, stdout);
-  fflush (stdout);
-}
-
-static _GL_ASYNC_SAFE void
-async_restore (_GL_UNUSED struct term_style_user_data *user_data)
-{
-  /* No <stdio.h> calls here!  */
-  full_write (STDOUT_FILENO, set_underline_off,
-              strlen (set_underline_off));
-  full_write (STDOUT_FILENO, set_foreground_color_default,
-              strlen (set_foreground_color_default));
-}
-
-static _GL_ASYNC_SAFE void
-async_set_attributes_from_default (struct term_style_user_data *user_data)
-{
-  /* No <stdio.h> calls here!  */
-  if (user_data->red_and_underline)
-    {
-      full_write (STDOUT_FILENO, set_underline_on,
-                  strlen (set_underline_on));
-      full_write (STDOUT_FILENO, set_foreground_color_red,
-                  strlen (set_foreground_color_red));
-    }
-}
-
-static const struct term_style_controller controller =
-{
-  get_control_data,
-  restore,
-  async_restore,
-  async_set_attributes_from_default
-};
+#include "test-term-style-control-yes.h"
 
 int
 main ()
 {
-  struct term_style_user_data user_data;
-
-  /* Initialization.  */
-  user_data.red_and_underline = false;
-
-  activate_term_style_controller (&controller, &user_data, STDOUT_FILENO,
-                                  TTYCTL_AUTO);
-
-  for (;;)
-    {
-      /* Before any styling, enable the non-default mode.  */
-      activate_term_non_default_mode (&controller, &user_data);
-
-      /* Set user_data.red_and_underline *before* emitting the appropriate
-         escape sequences, otherwise async_set_attributes_from_default will not
-         do its job correctly.  */
-      user_data.red_and_underline = true;
-      fputs (set_underline_on, stdout);
-      fputs (set_foreground_color_red, stdout);
-      fflush (stdout);
-
-      fputs ("y", stdout);
-      fflush (stdout);
-
-      /* Revert to the default style before emitting a newline.  */
-      user_data.red_and_underline = false;
-      fputs (set_underline_off, stdout);
-      fputs (set_foreground_color_default, stdout);
-      fflush (stdout);
-
-      /* Optional.  */
-      deactivate_term_non_default_mode (&controller, &user_data);
-
-      fputs ("\n", stdout);
-      fflush (stdout);
-    }
+  styled_yes_loop ();
 }
diff --git a/tests/test-term-style-control-yes.h b/tests/test-term-style-control-yes.h
new file mode 100644 (file)
index 0000000..74ed725
--- /dev/null
@@ -0,0 +1,133 @@
+/* Interactive test program for the term-style-control module.
+   Copyright (C) 2019-2026 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>, 2019.  */
+
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "full-write.h"
+
+/* This program outputs an endless amount of lines, each consisting of a
+   single 'y', in red color and underlined.
+   It can be used to exercise race conditions caused by
+     - simultaneous keyboard input on the terminal,
+     - pressing Ctrl-C,
+     - pressing Ctrl-Z and then "fg".  */
+
+/* ECMA-48 / ISO 6429 escape sequences.  See
+   https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
+ */
+static const char set_underline_on[] = "\033[4m";
+static const char set_underline_off[] = "\033[24m";
+static const char set_foreground_color_red[] = "\033[31m";
+static const char set_foreground_color_default[] = "\033[39m";
+
+struct term_style_user_data
+{
+  /* This field is marked volatile, because it is accessed from the
+     async-safe function async_set_attributes_from_default.  */
+  bool volatile red_and_underline;
+
+  struct term_style_control_data ctrl_data;
+};
+
+static struct term_style_control_data *
+get_control_data (struct term_style_user_data *user_data)
+{
+  return &user_data->ctrl_data;
+}
+
+static void
+restore (_GL_UNUSED struct term_style_user_data *user_data)
+{
+  fputs (set_underline_off, stdout);
+  fputs (set_foreground_color_default, stdout);
+  fflush (stdout);
+}
+
+static _GL_ASYNC_SAFE void
+async_restore (_GL_UNUSED struct term_style_user_data *user_data)
+{
+  /* No <stdio.h> calls here!  */
+  full_write (STDOUT_FILENO, set_underline_off,
+              strlen (set_underline_off));
+  full_write (STDOUT_FILENO, set_foreground_color_default,
+              strlen (set_foreground_color_default));
+}
+
+static _GL_ASYNC_SAFE void
+async_set_attributes_from_default (struct term_style_user_data *user_data)
+{
+  /* No <stdio.h> calls here!  */
+  if (user_data->red_and_underline)
+    {
+      full_write (STDOUT_FILENO, set_underline_on,
+                  strlen (set_underline_on));
+      full_write (STDOUT_FILENO, set_foreground_color_red,
+                  strlen (set_foreground_color_red));
+    }
+}
+
+static const struct term_style_controller controller =
+{
+  get_control_data,
+  restore,
+  async_restore,
+  async_set_attributes_from_default
+};
+
+static void
+styled_yes_loop (void)
+{
+  struct term_style_user_data user_data;
+
+  /* Initialization.  */
+  user_data.red_and_underline = false;
+
+  activate_term_style_controller (&controller, &user_data, STDOUT_FILENO,
+                                  TTYCTL_AUTO);
+
+  for (;;)
+    {
+      /* Before any styling, enable the non-default mode.  */
+      activate_term_non_default_mode (&controller, &user_data);
+
+      /* Set user_data.red_and_underline *before* emitting the appropriate
+         escape sequences, otherwise async_set_attributes_from_default will not
+         do its job correctly.  */
+      user_data.red_and_underline = true;
+      fputs (set_underline_on, stdout);
+      fputs (set_foreground_color_red, stdout);
+      fflush (stdout);
+
+      fputs ("y", stdout);
+      fflush (stdout);
+
+      /* Revert to the default style before emitting a newline.  */
+      user_data.red_and_underline = false;
+      fputs (set_underline_off, stdout);
+      fputs (set_foreground_color_default, stdout);
+      fflush (stdout);
+
+      /* Optional.  */
+      deactivate_term_non_default_mode (&controller, &user_data);
+
+      fputs ("\n", stdout);
+      fflush (stdout);
+    }
+}