Linux Test Project
 help / color / mirror / Atom feed
* [LTP] [PATCH v5 1/3] Add TST_THREAD_STATE_WAIT macro
@ 2022-02-09  9:17 Andrea Cervesato
  2022-02-09  9:17 ` [LTP] [PATCH v5 3/3] Add futex_waitv testing suite Andrea Cervesato
  0 siblings, 1 reply; 3+ messages in thread
From: Andrea Cervesato @ 2022-02-09  9:17 UTC (permalink / raw)
  To: ltp

The TST_THREAD_STATE_WAIT macro can be used to wait and check for
pthread state changes.

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
In v5 TST_THREAD_STATE_WAIT has been added in the tst_process_state.h header.

 include/tst_process_state.h | 19 +++++++++++++++++++
 lib/tst_thread_state.c      | 38 +++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)
 create mode 100644 lib/tst_thread_state.c

diff --git a/include/tst_process_state.h b/include/tst_process_state.h
index 81532524a..b1d83e109 100644
--- a/include/tst_process_state.h
+++ b/include/tst_process_state.h
@@ -36,6 +36,25 @@
 #define TST_PROCESS_EXIT_WAIT(pid, msec_timeout) \
 	tst_process_exit_wait((pid), (msec_timeout))
 
+/*
+ * Waits for thread state change.
+ *
+ * The state is one of the following:
+ *
+ * R - running
+ * S - sleeping
+ * D - disk sleep
+ * T - stopped
+ * t - tracing stopped
+ * Z - zombie
+ * X - dead
+ */
+#define TST_THREAD_STATE_WAIT(tid, state, msec_timeout) \
+	tst_thread_state_wait((tid), (state), (msec_timeout))
+
+int tst_thread_state_wait(pid_t tid, const char state,
+				unsigned int msec_timeout);
+
 #else
 /*
  * The same as above but does not use tst_brkm() interface.
diff --git a/lib/tst_thread_state.c b/lib/tst_thread_state.c
new file mode 100644
index 000000000..6562dfaf0
--- /dev/null
+++ b/lib/tst_thread_state.c
@@ -0,0 +1,38 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+
+#include "tst_safe_file_ops.h"
+#include "tst_process_state.h"
+
+int tst_thread_state_wait(pid_t tid, const char state,
+			  unsigned int msec_timeout)
+{
+	char proc_path[128], cur_state;
+	unsigned int msecs = 0;
+
+	snprintf(proc_path, sizeof(proc_path), "/proc/self/task/%i/stat", tid);
+
+	for (;;) {
+		SAFE_FILE_SCANF(proc_path, "%*i %*s %c", &cur_state);
+
+		if (state == cur_state)
+			break;
+
+		usleep(1000);
+		msecs += 1;
+
+		if (msec_timeout && msecs >= msec_timeout) {
+			errno = ETIMEDOUT;
+			return -1;
+		}
+	}
+
+	return 0;
+}
-- 
2.35.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-02-10 14:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-02-09  9:17 [LTP] [PATCH v5 1/3] Add TST_THREAD_STATE_WAIT macro Andrea Cervesato
2022-02-09  9:17 ` [LTP] [PATCH v5 3/3] Add futex_waitv testing suite Andrea Cervesato
2022-02-10 14:18   ` Cyril Hrubis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox