@@@ fltfmt mess
[mLib] / test / tvec-timeout.h
diff --git a/test/tvec-timeout.h b/test/tvec-timeout.h
new file mode 100644 (file)
index 0000000..4c21d89
--- /dev/null
@@ -0,0 +1,103 @@
+/* -*-c-*-
+ *
+ * Test-vector framework timeout extension
+ *
+ * (c) 2024 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of the mLib utilities library.
+ *
+ * mLib is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU Library General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * mLib 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 Library General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with mLib.  If not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+ * USA.
+ */
+
+#ifndef MLIB_TVEC_TIMEOUT_H
+#define MLIB_TVEC_TIMEOUT_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <sys/time.h>
+
+#ifndef MLIB_TVEC_H
+#  include "tvec.h"
+#endif
+
+/*----- Test environment --------------------------------------------------*/
+
+struct tvec_timeoutenv {
+  /* Timeout environment definition. */
+
+  struct tvec_env _env;
+  int timer;                           /* the timer (@ITIMER_...@) */
+  double t;                            /* time to wait (in seconds) */
+  const struct tvec_env *env;          /* subsidiary environment */
+};
+
+struct tvec_timeoutctx {
+  /* Timeout environment context; private. */
+
+  const struct tvec_timeoutenv *te;    /* saved environment description */
+  int timer;                           /* timer code (as overridden) */
+  double t;                            /* time to wait (as overridden) */
+  unsigned f;                          /* flags */
+#define TVTF_SETTMO 1u                 /*   set `@timeout' */
+#define TVTF_SETTMR 2u                 /*   set `@timer' */
+#define TVTF_SETMASK (TVTF_SETTMO | TVTF_SETTMR)
+                                       /*   mask of @TVTF_SET...@ */
+  void *subctx;                                /* subordinate environment context */
+};
+
+/* --- Environment implementation --- *
+ *
+ * The following special variables are supported.
+ *
+ *   * %|@timeout|% is the duration to wait before killing the process.
+ *
+ *   * %|@timer|% is the timer to use to measure the duration.
+ *
+ * Unrecognized variables are passed to the subordinate environment, if there
+ * is one.  Other events are passed through to the subordinate environment.
+ */
+
+extern tvec_envsetupfn tvec_timeoutsetup;
+extern tvec_envfindvarfn tvec_timeoutfindvar;
+extern tvec_envbeforefn tvec_timeoutbefore;
+extern tvec_envrunfn tvec_timeoutrun;
+extern tvec_envafterfn tvec_timeoutafter;
+extern tvec_envteardownfn tvec_timeoutteardown;
+
+#define TVEC_TIMEOUTENV                                                        \
+       { sizeof(struct tvec_timeoutctx),                               \
+         tvec_timeoutsetup,                                            \
+         tvec_timeoutfindvar,                                          \
+         tvec_timeoutbefore,                                           \
+         tvec_timeoutrun,                                              \
+         tvec_timeoutafter,                                            \
+         tvec_timeoutteardown }
+#define TVEC_TIMEOUTINIT(timer, t) TVEC_TIMEOUTENV, timer, t
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif