From patchwork Wed Nov 6 14:24:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Kolosov X-Patchwork-Id: 2007582 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=dYvHukX8; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Xk6tj5gNSz1xxf for ; Thu, 7 Nov 2024 01:25:33 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 76C9C3858CDB for ; Wed, 6 Nov 2024 14:25:31 +0000 (GMT) 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 09D453858D20 for ; Wed, 6 Nov 2024 14:25:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 09D453858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none 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 09D453858D20 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=1730903116; cv=none; b=GtsielIKTSuK3J49L4DIWsOI3im1gO7AE5oBGlcfBdRzifB0X2CibUSawF/KVvFcojPr53voamIC3pQzLabjZU9i6fs8KaNovyx0MtdJo2yTB8V9yBTjK+ZYmdygrL2tV6fcKBIag1FqDVvK9pwzPVONSTSDGcz8nlvm6UNhDl4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730903116; c=relaxed/simple; bh=P3EyaJ/5dDS++/a3PEoMiVExHiVbJ00WKpfEkDYXCng=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=PJ6OmGOKQRAOdqo4oEKAcMmT2qEgoJYR4rr4zdNL1j2WMN/zR2NTdBcz9EWG+fYa+g/7mwPsVvnjkQSKXdPps5DTuXeYLcDtyQGkuCaKSYm0jALjkuS/1cHnEV0AD8O56Led3OwnQskgmnOcO23o3UkfJ1I5JoAOMRmhMG8ypv8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730903107; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=hbGqReqBSACkR41og1/qouK5d3SqPYDapfJ5qgs5qQA=; b=dYvHukX8qBeeNmVMrIUaNXwk3sam4UUY2wM/+IJ/DO20xXc6otubeMbpnnubVspHCe3XfU UNgE2/QmSzEDlnjzM9SbJZPBd0a+1E31TSlfyOn4RYuXJDqCBCOccLF06T+lNIwlb5anG8 xc7Ki1yLw2AM+YHLmfcWfKuwUVBKzyg= 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-614-u72YMvHoNX2qbycoBNQkPA-1; Wed, 06 Nov 2024 09:25:06 -0500 X-MC-Unique: u72YMvHoNX2qbycoBNQkPA-1 X-Mimecast-MFC-AGG-ID: u72YMvHoNX2qbycoBNQkPA 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 AF32D1955D4B for ; Wed, 6 Nov 2024 14:25:05 +0000 (UTC) Received: from sk-carbon-x9.redhat.com (unknown [10.45.226.64]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BE3221955F40; Wed, 6 Nov 2024 14:25:04 +0000 (UTC) From: Sergey Kolosov To: libc-alpha@sourceware.org Cc: Sergey Kolosov Subject: [PATCH v2] nptl: Add new test for pthread_spin_trylock Date: Wed, 6 Nov 2024 15:24:06 +0100 Message-ID: <20241106142457.15387-1-skolosov@redhat.com> 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: yQo5vGtrVq-Q7Mzyg-DBgcLM5o_Em3yyktz_nyhTTT0_1730903106 X-Mimecast-Originator: redhat.com 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~incoming=patchwork.ozlabs.org@sourceware.org Add a threaded test for pthread_spin_trylock attempting to lock already acquired spin lock and checking for correct return code. --- v2 change: * add missing space in comment. --- sysdeps/pthread/Makefile | 1 + sysdeps/pthread/tst-spin5.c | 82 +++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 sysdeps/pthread/tst-spin5.c diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile index 4c1dc04b20..1d0c05a4d1 100644 --- a/sysdeps/pthread/Makefile +++ b/sysdeps/pthread/Makefile @@ -267,6 +267,7 @@ tests += \ tst-spin2 \ tst-spin3 \ tst-spin4 \ + tst-spin5 \ tst-stack1 \ tst-stdio1 \ tst-stdio2 \ diff --git a/sysdeps/pthread/tst-spin5.c b/sysdeps/pthread/tst-spin5.c new file mode 100644 index 0000000000..5c23bd48ef --- /dev/null +++ b/sysdeps/pthread/tst-spin5.c @@ -0,0 +1,82 @@ +/* Threaded test the pthread_spin_trylock function. + Copyright (C) 2024 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 + +pthread_spinlock_t lock; + +void * +thread (void *arg) +{ + int ret; + int thr_id = *(int *) arg; + + ret = pthread_spin_trylock (&lock); + if (thr_id == 1) + /* thread with already acquired lock. */ + { + if (ret != EBUSY) + { + FAIL_EXIT1 ("pthread_spin_trylock should fail with EBUSY"); + } + } + else if (thr_id == 2) + /* thread with released spin lock. */ + { + if (ret != 0) + { + FAIL_EXIT1 ("pthread_spin_trylock should be able to acquire lock"); + } + } + return NULL; +} + +static int +do_test (void) +{ + pthread_t thr1, thr2; + int ret; + int thr1_id = 1, thr2_id = 2; + + pthread_spin_init (&lock, PTHREAD_PROCESS_PRIVATE); + /* lock spin in main thread. */ + ret = pthread_spin_trylock (&lock); + if (ret != 0) + { + FAIL_EXIT1 ("Main thread should be able to acquire spin lock"); + } + + /* create first thread to try locking already acquired spin lock. */ + thr1 = xpthread_create (NULL, thread, &thr1_id); + xpthread_join (thr1); + + /* release spin lock and create thread to acquire released spin lock. */ + pthread_spin_unlock (&lock); + thr2 = xpthread_create (NULL, thread, &thr2_id); + xpthread_join (thr2); + + pthread_spin_destroy (&lock); + return 0; +} + +#include