mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: Alice Ryhl <aliceryhl@google.com>
Cc: "Benno Lossin" <benno.lossin@proton.me>,
	"Miguel Ojeda" <ojeda@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Andreas Hindborg" <a.hindborg@kernel.org>
Subject: Re: [PATCH v4] rust: add global lock support
Date: Thu, 10 Oct 2024 07:29:32 -0700	[thread overview]
Message-ID: <ZwfkzKz5mz6UvZfK@boqun-archlinux> (raw)
In-Reply-To: <CAH5fLgjhTWjmYqxcTRRv6FTpv7Vg9nnVCGGWbSKPqOSjJ5XyQA@mail.gmail.com>

On Thu, Oct 10, 2024 at 03:58:07PM +0200, Alice Ryhl wrote:
> On Thu, Oct 10, 2024 at 3:55 PM Boqun Feng <boqun.feng@gmail.com> wrote:
> >
> > On Thu, Oct 10, 2024 at 12:53:00PM +0200, Alice Ryhl wrote:
> > [...]
> > > > > +#[macro_export]
> > > > > +macro_rules! global_lock {
> > > > > +    {
> > > > > +        $(#[$meta:meta])* $pub:vis static $name:ident: $kind:ident<$valuety:ty> = unsafe { uninit };
> > > > > +        value: $value:expr;
> > > >
> > > > I would find it more natural to use `=` instead of `:` here, since then
> > > > it would read as a normal statement with the semicolon at the end.
> > > > Another alternative would be to use `,` instead of `;`, but that doesn't
> > > > work nicely with the static keyword above (although you could make the
> > > > user write it in another {}, but that also isn't ideal...).
> > > >
> > > > Using `=` instead of `:` makes my editor put the correct amount of
> > > > indentation there, `:` adds a lot of extra spaces.
> > >
> > > That seems sensible.
> > >
> >
> > While we are at it, how about we make the syntax:
> >
> >         global_lock!{
> >             static MY_LOCK: Mutex<u32> = unsafe { 0 };
> >         }
> >
> > or
> >
> >         global_lock!{
> >             static MY_LOCK: Mutex<u32> = unsafe { uninit { 0 } };
> >         }
> >
> > ?
> >
> > i.e. instead of a "value" field, we put it in the "initialization
> > expression". To me, this make it more clear that "value" is the
> > initialized value protected by the lock. Thoughts?
> 
> `uninit { 0 }` looks pretty terrible IMO. Can we come up with something better?
> 

I'm trying, but in the meanwhile, another idea comes to me, can we just
avoid using the `global_lock!` macro if "locked_by" is not needed? I.e.
as the global counter in your examples, we probably need a new lock type
in these cases but we don't use macros. I feel like macros don't bring
us anything in these usages. As a result, we make `global_lock!`
dedicated for locked_by usages, where values should usually be a `()`.

> > Besides, instead of a "guard" type name, could you make a
> > generic guard type over the "locked_by" type? E.g.
> >
> >         struct GlobalGuard<L: GlobalLockedBy>(Guard<...>, PhantomData<*mut L>);
> >
> > I feel like this could make the relationship between the guard type and
> > the locked_by type more obvious. But maybe there's something I'm
> > missing?
> 
> Sorry, I don't understand this. Why is the LockedBy type relevant to the guard?
> 

well, $locked_by's `as_ref()` and `as_mut()` both need a $guard
reference, instead of asking users to provide an arbitrary $guard name,
I thought it's better to just make $guard generic over $locked_by.
However, I realise that $locked_by is a generic type over T, so my
previous proposal doesn't work. How about another way around: making
$locked_by type generic over $guard:

	pub(crate) struct GlobalLockedBy<T: ?Sized, G>(::core::cell::UnsafeCell<T>, ...);

(G is a $guard type)

and if we do

	global_lock!{
	    static MY_LOCK ...;
	    guard: MyGuard;
	}

we can have a struct:

	struct MyStruct {
	    inner: GlobalLockedBy<u32, MyGuard>
	}

instead of asking users to provide a name of the $locked_by type.

Does this makes sense?

Regards,
Boqun

> Alice

  reply	other threads:[~2024-10-10 14:29 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-30 13:11 Alice Ryhl
2024-10-10 10:39 ` Benno Lossin
2024-10-10 10:53   ` Alice Ryhl
2024-10-10 13:55     ` Boqun Feng
2024-10-10 13:58       ` Alice Ryhl
2024-10-10 14:29         ` Boqun Feng [this message]
2024-10-10 16:33           ` Boqun Feng
2024-10-10 22:21             ` Benno Lossin
2024-10-10 23:06               ` Boqun Feng
2024-10-11  7:01                 ` Benno Lossin
2024-10-11 22:43                   ` Boqun Feng
2024-10-10 22:13     ` Benno Lossin
2024-10-10 14:01   ` Andreas Hindborg
2024-10-10 22:14     ` Benno Lossin
2024-10-11 14:57       ` Andreas Hindborg
2024-10-10 13:57 ` Andreas Hindborg
2024-10-10 14:01   ` Alice Ryhl
2024-10-10 14:08     ` Andreas Hindborg

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=ZwfkzKz5mz6UvZfK@boqun-archlinux \
    --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=ojeda@kernel.org \
    --cc=rust-for-linux@vger.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

all inboxes | Powered by JetHome®