From patchwork Wed Feb 9 09:17:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Cervesato X-Patchwork-Id: 1590260 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=HMI/tw/7; dkim=fail reason="signature verification failed" header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=Gbb1MFzo; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JtvRx0XK3z9sCD for ; Wed, 9 Feb 2022 20:18:04 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 732C33C9B6E for ; Wed, 9 Feb 2022 10:18:02 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-3.smtp.seeweb.it (in-3.smtp.seeweb.it [217.194.8.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by picard.linux.it (Postfix) with ESMTPS id DA37C3C6912 for ; Wed, 9 Feb 2022 10:18:00 +0100 (CET) Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by in-3.smtp.seeweb.it (Postfix) with ESMTPS id 0AE251A00706 for ; Wed, 9 Feb 2022 10:17:59 +0100 (CET) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 2C58321119; Wed, 9 Feb 2022 09:17:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1644398279; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=HlBBG4j/h10r2jwLTaqHVVhqcAlZIAwUjLYvifvBmQA=; b=HMI/tw/7msfnWyv4g7reEi0sFLpfHxKE53ZxcqWNYnkZ8kqIS8lI6u5OayLLwgMag6L6sw zkl3IDQRjBWtDWs5Y1vqpf0D8jmln/64LbF+xYSBytx3LJttQgMPXU9VQRgac/NF5sxPPO i5SmE0F7n3LiJMAjQEtwIYOofCcq11s= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1644398279; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=HlBBG4j/h10r2jwLTaqHVVhqcAlZIAwUjLYvifvBmQA=; b=Gbb1MFzo2dYVhGGh+ChmQl5y5Q8O9qfxuOEN9ciUga9V5ZqYrPe91UQdeBRTNX5H6BOB8k VB1jKFPkJknNCkCg== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 067AB13D1E; Wed, 9 Feb 2022 09:17:58 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id AuFDO8aGA2J+PQAAMHmgww (envelope-from ); Wed, 09 Feb 2022 09:17:58 +0000 From: Andrea Cervesato To: ltp@lists.linux.it Date: Wed, 9 Feb 2022 10:17:55 +0100 Message-Id: <20220209091756.17245-1-andrea.cervesato@suse.de> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.102.4 at in-3.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-3.smtp.seeweb.it Subject: [LTP] [PATCH v5 1/3] Add TST_THREAD_STATE_WAIT macro X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" The TST_THREAD_STATE_WAIT macro can be used to wait and check for pthread state changes. Signed-off-by: Andrea Cervesato --- 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 + */ + +#include +#include +#include +#include + +#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; +}