rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: Alice Ryhl <aliceryhl@google.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Ingo Molnar" <mingo@redhat.com>, "Will Deacon" <will@kernel.org>,
	"Waiman Long" <longman@redhat.com>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Andreas Hindborg" <a.hindborg@kernel.org>
Subject: Re: [PATCH v5] rust: add global lock support
Date: Tue, 22 Oct 2024 09:44:50 -0700	[thread overview]
Message-ID: <ZxfWglfYr52xTIO4@Boquns-Mac-mini.local> (raw)
In-Reply-To: <CAH5fLgg=Hb5NDaQQJW4SVh+hCj51bp+BzCMQs=Pg_L+_MMiZgA@mail.gmail.com>

On Tue, Oct 22, 2024 at 02:46:19PM +0200, Alice Ryhl wrote:
> On Mon, Oct 21, 2024 at 5:23 PM Boqun Feng <boqun.feng@gmail.com> wrote:
> >
> > On Mon, Oct 21, 2024 at 01:17:23PM +0000, Alice Ryhl wrote:
> > [...]
> > > +///
> > > +/// A global mutex used to protect all instances of a given struct.
> > > +///
> > > +/// ```
> > > +/// # mod ex {
> > > +/// # use kernel::prelude::*;
> > > +/// kernel::sync::global_lock! {
> > > +///     // SAFETY: Initialized in module initializer before first use.
> > > +///     unsafe(uninit) static MY_MUTEX: Mutex<(), Guard = MyGuard, LockedBy = LockedByMyMutex> = ();
> >
> > Thanks! This looks much better now ;-)
> >
> > But I still want to get rid of "LockedBy=", so I've tried and seems it
> > works, please see the below diff on top of your patch, I think it's
> > better because:
> >
> > * Users don't to pick up the names for the locked_by type ;-)
> > * It moves a significant amount of code out of macros.
> > * By having:
> >
> >     struct MyStruct {
> >         my_counter: GlobalLockedBy<MyGuard, u32>,
> >     }
> >
> >   , it's much clear for users to see which guard is used to protected
> >   `my_counter`.
> >
> > I prefer this way. Any concern about doing this?
> 
> I think I came up with an even better way of doing it. The macro can

Cool!

> generate a dummy token type for the global lock, and then we can have
> three types: GlobalLock<T>, GlobalGuard<T>, GlobalLockedBy<T> that are
> all generic over the token type. The token type is an empty enum with

Just to make sure I understand it, so let's say the token type's name is
`TK`, you mean we have GlobalLock<T, TK>, GlobalGuard<T, TK> and
GlobalLockedBy<S, TK>? Where T is the type protected by the static mutex
and S is the type protected by the locked_by type?

> no contents, but implements an unsafe trait saying that there's only
> one static using it.
> 
> This way we also do not need the helper module, as we no longer need
> to generate a struct with private fields.
> 

Sounds good to me. Do you plan to let the user name the token type? It's
fine to me, or do you want to name the token based on the static lock
name?

Regards,
Boqun

> Alice

  reply	other threads:[~2024-10-22 16:44 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-21 13:17 [PATCH v5] rust: add global lock support Alice Ryhl
2024-10-21 15:22 ` Boqun Feng
2024-10-22 12:46   ` Alice Ryhl
2024-10-22 16:44     ` Boqun Feng [this message]
2024-10-22 17:24       ` Alice Ryhl
2024-10-23 13:39         ` Alice Ryhl
2024-10-22 11:04 ` Alice Ryhl
  -- strict thread matches above, loose matches on Subject: below --
2024-10-23 13:17 Alice Ryhl
2024-10-23 13:19 ` Alice Ryhl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZxfWglfYr52xTIO4@Boquns-Mac-mini.local \
    --to=boqun.feng@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mingo@redhat.com \
    --cc=ojeda@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).