From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 13F1F2046B1; Sun, 23 Feb 2025 10:55:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.16 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740308114; cv=none; b=OnlZbQyz3QT9i5RIshmUqSZ6fVQrCAWfjH7gaAgIwlUycXKEkMSaMMj5BoVpXYyNwMKcEgdl0xId0qVrQx2wxxdkctQETCCJXHlZR53HTIBshb5x3Z3z4wRyx0lhIppT80ep1AGg87jPtMWWsm+lIuRLYa78eLHjMgNoPLRqPj0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740308114; c=relaxed/simple; bh=q+XKvnoPYXVMw3lt5027W6PxhHtQyOx1spmmaf15AQM=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=oD38SUKcikReLCCBJssblz7xkrI/x8Jf7iEj4HMqU6Cb8LDYKaXWPAcF/KYh3NcNGXDGS6fQt7am+QaUkOcYLtCQcJIBUwMlk5rN0Bq9hvZqQ4iw9cPQHYgEt96lUn2JhH+CuSifhtOYxuhJXAZ9UX9AO5R/Ay6JcJb04KAZhWQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b=TIc8+i4n; arc=none smtp.client-ip=185.70.43.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="TIc8+i4n" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1740308105; x=1740567305; bh=ftzWwZe5dgJ/2krdr+BnmHjM/Of8UIn31pqhWbkUW0w=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector:List-Unsubscribe:List-Unsubscribe-Post; b=TIc8+i4nJO4GaclByTGBjPsuhaA1wqdCDfdcFs6d22nhJbCAIDlyvsoA6yvtiS14L +LQu+NNL6H76fvUJL0IT58ZiOiCPmeY94l55YR8q7ZSY+NcrG+Uo3CJzctLQhfzIoc MBZZKpWH2KJj+ZihjO9zuJQDjucCgkAF6cVjSm86hs9lonDN9P/Y0rhwbWFfeNk03J 2Ld9HW8CY0UDhElS44ZP1LFiWPEfKFy4AkZWhR5r22xHfDSH2GAGtvPCAudURLcWh6 AYBLsEGfLJND3rneszZDnUDcAQSivOuHR6mMOZBrhlN5mATrsjFtCV4MYNq9flmbq8 sFWx//4ZhNSAQ== Date: Sun, 23 Feb 2025 10:54:59 +0000 To: Boqun Feng , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org From: Benno Lossin Cc: Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Alice Ryhl , Trevor Gross Subject: Re: [PATCH] rust: sync: lock: Add an example for Guard::lock_ref() Message-ID: In-Reply-To: <20250223072114.3715-1-boqun.feng@gmail.com> References: <20250223072114.3715-1-boqun.feng@gmail.com> Feedback-ID: 71780778:user:proton X-Pm-Message-ID: 41ca317b5d80adbd5557b5012302b0a3c5eb8c34 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 23.02.25 08:21, Boqun Feng wrote: > To provide examples on usage of `Guard::lock_ref()` along with the unit > test, an "assert a lock is held by a guard" example is added. >=20 > Signed-off-by: Boqun Feng Reviewed-by: Benno Lossin > --- > This depends on Alice's patch: >=20 > =09https://lore.kernel.org/all/20250130-guard-get-lock-v1-1-8ed87899920a@= google.com/ >=20 > I'm also OK to fold this in if Alice thinks it's fine. >=20 > rust/kernel/sync/lock.rs | 24 ++++++++++++++++++++++++ > 1 file changed, 24 insertions(+) >=20 > diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs > index 3701fac6ebf6..6d868e35b0a3 100644 > --- a/rust/kernel/sync/lock.rs > +++ b/rust/kernel/sync/lock.rs > @@ -201,6 +201,30 @@ unsafe impl Sync for G= uard<'_, T, B> {} >=20 > impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> { > /// Returns the lock that this guard originates from. > + /// > + /// # Examples > + /// > + /// The following example shows how to use [`Guard::lock_ref()`] to = assert the corresponding > + /// lock is held. > + /// > + /// ``` > + /// # use kernel::{new_spinlock, stack_pin_init, sync::lock::{Backen= d, Guard, Lock}}; > + /// > + /// fn assert_held(guard: &Guard<'_, T, B>, lock: &Lo= ck) { > + /// // Address-equal means the same lock. > + /// assert!(core::ptr::eq(guard.lock_ref(), lock)); > + /// } > + /// > + /// // Creates a new lock on stack. I would be inclined to write "new lock on the stack.", but maybe that is incorrect. --- Cheers, Benno > + /// stack_pin_init!{ > + /// let l =3D new_spinlock!(42) > + /// } > + /// > + /// let g =3D l.lock(); > + /// > + /// // `g` originates from `l`. > + /// assert_held(&g, &l); > + /// ``` > pub fn lock_ref(&self) -> &'a Lock { > self.lock > } > -- > 2.39.5 (Apple Git-154) >=20