From patchwork Wed Nov 6 12:30:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Kolosov X-Patchwork-Id: 2007488 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=IyMp3Pb6; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4Xk4Lb6K9mz1xxf for ; Wed, 6 Nov 2024 23:31:03 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C9DC33858D3C for ; Wed, 6 Nov 2024 12:31:01 +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 94BC13858D20 for ; Wed, 6 Nov 2024 12:30:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 94BC13858D20 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 94BC13858D20 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=1730896245; cv=none; b=lSsBtxnKIka5QFHuatV+GLw+8FuvE5il62o9ZP91e+wsusXUA0d3eTZUYnPX/1wPqql7JLtWBeAnL9fzfm3soKxlDGh+QIIxV3eiaqU3c7w1lci9k3vQtBmA63KK4yRs2rSz4slcy7nNPgrU3YjtsWoMVAbbac30szYENzDAYxA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730896245; c=relaxed/simple; bh=fbO64to3XDVyQkL7YzNGErYY2s9yj1qFLyq9WpnSQHY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=nHWkTgeAjj38fFxEQ/gcDWieKJwkScat1jRDfLicpVJVbi1h8RHg9a/Jil7Ao3+PH08UzBuVZAe1MPjY7UhH+PDmPTqzGiNy9WAnYuScyJE+MtXUZ/1LY4COu3FJwv3k9xLss5f86+38DJ4xT8WUl/komXXdPtYiPVZoTtdKMwc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730896240; 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=DpPAm0Fcn+1V09SpRcM+VGSBUcVWDHfaQ7lbuXJLhwI=; b=IyMp3Pb6sdkkGt2jhovtwU1LFoh9N/dha/3IbQleLXYiqAuS3VPo4J829WpVCcexCS1kKj pK0YWGopAoblqR7CGAXx1uK6ywxaUGvNJiEYouoJ/c1RooOpjQMXSW93TqIGmm4fn3jcYq jdQ5obFkXEV4yGmnYNLm3GMBjV/nrcM= Received: from mx-prod-mc-02.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-618-gmtjdDCMOEaeUOvGX-O1bA-1; Wed, 06 Nov 2024 07:30:35 -0500 X-MC-Unique: gmtjdDCMOEaeUOvGX-O1bA-1 X-Mimecast-MFC-AGG-ID: gmtjdDCMOEaeUOvGX-O1bA 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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A27D71954209 for ; Wed, 6 Nov 2024 12:30:34 +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 76ECF1955F40; Wed, 6 Nov 2024 12:30:33 +0000 (UTC) From: Sergey Kolosov To: libc-alpha@sourceware.org Cc: Sergey Kolosov Subject: [PATCH] nptl: Add new test for pthread_spin_trylock Date: Wed, 6 Nov 2024 13:30:20 +0100 Message-ID: <20241106123027.4498-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: -_a-mvko1YK-9yTtWYxaMA906HiEUdZycsn9gb_FZDc_1730896234 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. Reviewed-by: Florian Weimer --- 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..6918dc8592 --- /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