From patchwork Tue Dec 9 17:31:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 126277 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 17CAD4BA2E06 for ; Tue, 9 Dec 2025 17:32:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 17CAD4BA2E06 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=KNPZJTlL X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 4CA7E4BA2E39 for ; Tue, 9 Dec 2025 17:31:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4CA7E4BA2E39 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4CA7E4BA2E39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1765301518; cv=none; b=BcLFAhxMGF2vK00QudVYzciHtcccLGoRY513zrDYp2Fx4Ht6WIGGvFEPQABNi/dV6Xsscw4NmgA1LZkC+kXwaRAUyawayI89vFHXxm9R0QxU/kUaskQHXd5MFb6h9sm5vyVJrqVG+zExVgIAW2zDSrKKQQl+XIf3l7/vBisP3wk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1765301518; c=relaxed/simple; bh=QVg+tUP5aJ4fyZjEso4Pz35Ln/+hlzCvfYTEPvxJHUE=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=kpS0TDjl0FgmkxHe596htl1D2bWQzqX96XGwP0hz7++sH8ZMM3HQ50PSqS1cZ3omnDQjfEir/A2EjQ3+RwnDCcgHO94frB3n8S9rryB4l3KR7wNjI1rz4yC1ibJWRUP8cJc4yzVN1o8byYsQwQNG8XKJ+ZmYz4zpnxi7p37HGs0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4CA7E4BA2E39 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1765301518; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=UiZkn5E8hpANFZYUawkVb7dXVvIKL1L3P7zq5SO8D+Y=; b=KNPZJTlLjXBK9wi8zIw4rDxjPKJm94s7QTlOi809iBThOMmQ3ChVbVXCgqxeZ/WdFDFUmL yoXTNNy0nomk7KEaDgGaJ9e9z7kG7q+KtH7Era5YRHH+25dRByMGBydWyZCYy3nzEm7fSw BRdctA24GzqgUxsEvlfQUDjN/eKhZgk= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-230-Viq4oN2fND2xeb7EobSJ-g-1; Tue, 09 Dec 2025 12:31:56 -0500 X-MC-Unique: Viq4oN2fND2xeb7EobSJ-g-1 X-Mimecast-MFC-AGG-ID: Viq4oN2fND2xeb7EobSJ-g_1765301516 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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 mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id DADEA19560A7 for ; Tue, 9 Dec 2025 17:31:55 +0000 (UTC) Received: from fweimer-oldenburg.csb.redhat.com (unknown [10.45.226.70]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1786C1956095 for ; Tue, 9 Dec 2025 17:31:54 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 5/5] nptl: Add test nptl/tst-pthread-perthread-inherit In-Reply-To: Message-ID: <33cbcfd4a91b89742123aa389b06c0614e38bf50.1765301308.git.fweimer@redhat.com> References: X-From-Line: 33cbcfd4a91b89742123aa389b06c0614e38bf50 Mon Sep 17 00:00:00 2001 Date: Tue, 09 Dec 2025 18:31:52 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: EKBdWpcsfON9zL8_6qpLnGITXvlMo9PQ_n_caPnf4KM_1765301516 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS, SPF_NONE, TXREP, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org This test checks the inheritance behavior of the per-thread flags. It is also a regular (non-xtest) for pthread_attr_setperthreadids_np and pthread_attr_getperthreadids_np. --- nptl/Makefile | 1 + nptl/tst-pthread-perthread-inherit.c | 220 +++++++++++++++++++++++++++ 2 files changed, 221 insertions(+) create mode 100644 nptl/tst-pthread-perthread-inherit.c diff --git a/nptl/Makefile b/nptl/Makefile index 3d1848ebea..eff3cecff8 100644 --- a/nptl/Makefile +++ b/nptl/Makefile @@ -321,6 +321,7 @@ tests = \ tst-pthread-gdb-attach-static \ tst-pthread-getcpuclockid-invalid \ tst-pthread-key1-static \ + tst-pthread-perthread-inherit \ tst-pthread-perthreadfs \ tst-pthread-perthreadfs-chroot \ tst-pthread-timedlock-lockloop \ diff --git a/nptl/tst-pthread-perthread-inherit.c b/nptl/tst-pthread-perthread-inherit.c new file mode 100644 index 0000000000..99feb01dc9 --- /dev/null +++ b/nptl/tst-pthread-perthread-inherit.c @@ -0,0 +1,220 @@ +/* Test inheritance of per-thread attributes. + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include + +/* Controls how an attribute is applied. */ +enum kind { null_attribute, default_attribute, attribute_per_process, + attribute_per_thread }; + +/* Determine the combined per-thread/per-process status from the two + requested attributes. */ +static int +combine_kind (enum kind a, enum kind b) +{ + if (a == attribute_per_thread || b == attribute_per_thread) + return PTHREAD_PER_THREAD_NP; + else + return PTHREAD_PER_PROCESS_NP; +} + +/* Convert one kind to the corresponding PTHREAD_PER_PROCESS_NP, + PTHREAD_PER_THREAD_NP flag. */ +static int +convert_kind (enum kind k) +{ + if (k == attribute_per_thread) + return PTHREAD_PER_THREAD_NP; + else + return PTHREAD_PER_PROCESS_NP; +} + +/* Return either PTHREAD_PER_PROCESS_NP or PTHREAD_PER_THREAD_NP. */ +static int +scope (pthread_t thr, int (*getter) (const pthread_attr_t *, int *)) +{ + pthread_attr_t attr; + int ret = pthread_getattr_np (thr, &attr); + if (ret != 0) + { + errno = ret; + FAIL_EXIT1 ("pthread_getattr_np: %m"); + } + int flag = -1; + TEST_COMPARE (getter (&attr, &flag), 0); + if (flag != PTHREAD_PER_THREAD_NP) + TEST_COMPARE (flag, PTHREAD_PER_PROCESS_NP); + xpthread_attr_destroy (&attr); + return flag; +} + +/* The loop in do_test iterates through the Cartesian product of all + possible values. */ +static enum kind outer_perthreadfs; +static enum kind inner_perthreadfs; +static enum kind outer_perthreadids; +static enum kind inner_perthreadids; + +/* Used to prevent premature thread exit, so that we can safely call + pthread_getattr_np. */ +static pthread_barrier_t barrier; + +static void * +inner_thread (void *closure) +{ + TEST_COMPARE (scope (pthread_self (), + pthread_attr_getperthreadfs_np), + combine_kind (outer_perthreadfs, inner_perthreadfs)); + TEST_COMPARE (scope (pthread_self (), + pthread_attr_getperthreadids_np), + combine_kind (outer_perthreadids, inner_perthreadids)); + /* Delay exit of the thread until the outer thread has read the + attributes. */ + xpthread_barrier_wait (&barrier); + return NULL; +} + +static void * +outer_thread (void *attr) +{ + TEST_COMPARE (scope (pthread_self (), + pthread_attr_getperthreadfs_np), + convert_kind (outer_perthreadfs)); + TEST_COMPARE (scope (pthread_self (), + pthread_attr_getperthreadids_np), + convert_kind (outer_perthreadids)); + pthread_t thr = xpthread_create (attr, inner_thread, NULL); + TEST_COMPARE (scope (thr, pthread_attr_getperthreadfs_np), + combine_kind (outer_perthreadfs, inner_perthreadfs)); + TEST_COMPARE (scope (thr, pthread_attr_getperthreadids_np), + combine_kind (outer_perthreadids, inner_perthreadids)); + /* Delay exit of the thread until the main thread has read the + attributes. */ + xpthread_barrier_wait (&barrier); + return xpthread_join (thr); +} + +/* Apply the attribute KIND according to SETTER to ATTR. */ +static void +attribute_apply (pthread_attr_t *attr, enum kind kind, + int (*setter) (pthread_attr_t *attr, int)) +{ + switch (kind) + { + case default_attribute: + return; + + case attribute_per_process: + case attribute_per_thread: + TEST_COMPARE (setter (attr, convert_kind (kind)), 0); + return; + + case null_attribute: + /* Report failure below. */ + ; + } + + FAIL_EXIT1 ("invalid kind: %d", (int) kind); +} + +/* Create a new attribute according to both kinds. */ +static pthread_attr_t * +make_attribute (enum kind perthreadfs, enum kind perthreadids) +{ + if (perthreadfs == null_attribute) + { + TEST_COMPARE (perthreadids, null_attribute); + return NULL; + } + pthread_attr_t *result = xmalloc (sizeof (*result)); + xpthread_attr_init (result); + attribute_apply (result, perthreadfs, pthread_attr_setperthreadfs_np); + attribute_apply (result, perthreadids, pthread_attr_setperthreadids_np); + return result; +} + +/* Deallocate the attribute pointer returned from make_attribute. */ +static void +free_attribute (pthread_attr_t *attr) +{ + if (attr != NULL) + { + xpthread_attr_destroy (attr); + free (attr); + } +} + +static int +do_test (void) +{ + /* Used to delay thread exit, so that pthread_getattr_np works + reliably. Inner thread, outer thread, and main thread are + waiting. */ + xpthread_barrier_init (&barrier, NULL, 3); + + for (outer_perthreadfs = null_attribute; + outer_perthreadfs <= attribute_per_thread; + ++outer_perthreadfs) + for (inner_perthreadfs = null_attribute; + inner_perthreadfs <= attribute_per_thread; + ++inner_perthreadfs) + for (outer_perthreadids = null_attribute; + outer_perthreadids <= attribute_per_thread; + ++outer_perthreadids) + for (inner_perthreadids = null_attribute; + inner_perthreadids <= attribute_per_thread; + ++inner_perthreadids) + { + /* Some combinations are impossible. If we must pass a + NULL attribute at one level, then that is true for both + kinds of properties. */ + if ((outer_perthreadfs == null_attribute) + != (outer_perthreadids == null_attribute)) + continue; + if ((inner_perthreadfs == null_attribute) + != (inner_perthreadids == null_attribute)) + continue; + + pthread_attr_t *outer_attr + = make_attribute (outer_perthreadfs, outer_perthreadids); + pthread_attr_t *inner_attr + = make_attribute (inner_perthreadfs, inner_perthreadids); + pthread_t thr = xpthread_create (outer_attr, + outer_thread, inner_attr); + TEST_COMPARE (scope (thr, pthread_attr_getperthreadfs_np), + convert_kind (outer_perthreadfs)); + TEST_COMPARE (scope (thr, pthread_attr_getperthreadids_np), + convert_kind (outer_perthreadids)); + /* The threads may now exit, after we have checked the + attributes. */ + xpthread_barrier_wait (&barrier); + xpthread_join (thr); + free_attribute (inner_attr); + free_attribute (outer_attr); + } + + xpthread_barrier_destroy (&barrier); + return 0; +} + +#include