From patchwork Mon Apr 14 16:38:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 110415 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 63369385842C for ; Mon, 14 Apr 2025 17:11:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 63369385842C 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=TUGzY9on X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 537F53857C67 for ; Mon, 14 Apr 2025 16:39:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 537F53857C67 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 537F53857C67 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1744648745; cv=none; b=mZrDZCYriSUc9XltoLOuk8VZpQ7MlF9BdwP6xLo3qd7sF0W46uFwBpu9AhJ3ulKmYu7i8dLxkEYddOFqX8PL5vhZjVzNhAXMZe4mQL4fZ2FEwnVBJEYXfig0E31Pya+AzLWmnYTaHutonHEB3DK8niLIWHGr1QOb2xjsV+Wsn1A= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1744648745; c=relaxed/simple; bh=EUXoQZuJuPmI+rC/CvCEDca94EwHee38jHl/QAxo30E=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=wyTw/o8QYeJ0nnh3CXBNQ2ltwI3bOiOcxVjQZDNe0iOhfbA7DcJ4OohBUKCCxpzkkl/JlokNsX+9S4++kgvVJ/QjkTUj6vVdeAgb/Wq/6EUdYbbfodhefpdYJDkA3VoLKXZ4//sHYL2oYXtMKEStyw0BW40jyhDIwde8lNZHzXo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 537F53857C67 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1744648745; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=71bN32XMzAvBxZ06fVqVE0edR69lDAe0dXvLlBXkTv4=; b=TUGzY9onV1zxuMYDmMFITIJKDfuYM1O/c9CnMfygNJJS/L/m+4QtIvJriLAPBTpdF+o995 XMaTJbApG7BPYnCbUSc7eQpzujC81BXle0s+24hNsJv0blBrdoLJno+6ygJwGXXIYuoKV9 d3YlZiUHQSdg9bdiHzYD6U9v+2XcDn4= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-355-5seg9HhdM42v8857xikndw-1; Mon, 14 Apr 2025 12:39:00 -0400 X-MC-Unique: 5seg9HhdM42v8857xikndw-1 X-Mimecast-MFC-AGG-ID: 5seg9HhdM42v8857xikndw_1744648739 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E7E73187DB98; Mon, 14 Apr 2025 16:38:49 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.44.32.53]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BE3521828B3D; Mon, 14 Apr 2025 16:38:46 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 53EGcggN4153979 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 14 Apr 2025 18:38:42 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 53EGcg644153978; Mon, 14 Apr 2025 18:38:42 +0200 Date: Mon, 14 Apr 2025 18:38:42 +0200 From: Jakub Jelinek To: Richard Biener , Richard Henderson Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] libatomic: Fix up libat_{,un}lock_n for mingw [PR119796] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 51gCjj8nlo0pJazCHWTrLUnGJRDrBVneg0KpGO--qmw_1744648739 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org Hi! Here is just a port of the previously posted patch to mingw which clearly has the same problems. Untested though, I don't have Windows anywhere around. 2025-04-14 Jakub Jelinek PR libstdc++/119796 * config/mingw/lock.c (libat_lock_n, libat_unlock_n): Start with computing how many locks will be needed and take into account ((uintptr_t)ptr % WATCH_SIZE). If some locks from the end of the locks array and others from the start of it will be needed, first lock the ones from the start followed by ones from the end. Jakub --- libatomic/config/mingw/lock.c.jj 2025-04-08 14:09:40.988589457 +0200 +++ libatomic/config/mingw/lock.c 2025-04-14 15:39:08.244686172 +0200 @@ -87,21 +87,30 @@ libat_lock_n (void *ptr, size_t n) { uintptr_t h = addr_hash (ptr); size_t i = 0; + size_t nlocks + = (n + ((uintptr_t)ptr % WATCH_SIZE) + WATCH_SIZE - 1) / WATCH_SIZE; /* Don't lock more than all the locks we have. */ - if (n > PAGE_SIZE) - n = PAGE_SIZE; + if (nlocks > NLOCKS) + nlocks = NLOCKS; - do + if (__builtin_expect (h + nlocks > NLOCKS, 0)) + { + size_t j = h + nlocks - NLOCKS; + for (; i < j; ++i) + { + if (!locks[i].mutex) + locks[i].mutex = CreateMutex (NULL, FALSE, NULL); + WaitForSingleObject (locks[i].mutex, INFINITE); + } + } + + for (; i < nlocks; ++i) { if (!locks[h].mutex) - locks[h].mutex = CreateMutex (NULL, FALSE, NULL); - WaitForSingleObject (locks[h].mutex, INFINITE); - if (++h == NLOCKS) - h = 0; - i += WATCH_SIZE; + locks[h].mutex = CreateMutex (NULL, FALSE, NULL); + WaitForSingleObject (locks[h++].mutex, INFINITE); } - while (i < n); } void @@ -109,17 +118,22 @@ libat_unlock_n (void *ptr, size_t n) { uintptr_t h = addr_hash (ptr); size_t i = 0; + size_t nlocks + = (n + ((uintptr_t)ptr % WATCH_SIZE) + WATCH_SIZE - 1) / WATCH_SIZE; - if (n > PAGE_SIZE) - n = PAGE_SIZE; + /* Don't lock more than all the locks we have. */ + if (nlocks > NLOCKS) + nlocks = NLOCKS; - do + if (__builtin_expect (h + nlocks > NLOCKS, 0)) { - if (locks[h].mutex) - ReleaseMutex (locks[h].mutex); - if (++h == NLOCKS) - h = 0; - i += WATCH_SIZE; + size_t j = h + nlocks - NLOCKS; + for (; i < j; ++i) + if (locks[i].mutex) + ReleaseMutex (locks[i].mutex); } - while (i < n); + + for (; i < nlocks; ++i, ++h) + if (locks[h].mutex) + ReleaseMutex (locks[h].mutex); }