diff options
author | Mark Brown <broonie@kernel.org> | 2025-08-12 12:06:23 +0100 |
---|---|---|
committer | Mark Brown <broonie@kernel.org> | 2025-08-12 12:06:23 +0100 |
commit | eb9bb4c5b521853d29b79197f412c5c533a6483c (patch) | |
tree | c6a6780fce7fa4ce5639b0dc38646f91a80f04bf | |
parent | eccd3d9753d48cc3e873eeda5b0e271454aa08ac (diff) | |
parent | 9a200cbdb54349909a42b45379e792e4b39dd223 (diff) |
rust: regulator: relax a few constraints on
Merge series from Daniel Almeida <daniel.almeida@collabora.com>:
This series implement two related changes to address a bit of an oversight
on my end on the initial patch for the Regulator abstraction. Note that
this is not a fix, as it just relaxes the constraints on the previous code
as it is safe to do so.
Patch 1 removes some needless &mut self for functions that already provide
their own locking on the C side.
Patch 2 implements Send and Sync. In particular, there is no reason for
Regulator<T> not to be Send, and as discussed above, it is naturally Sync.
-rw-r--r-- | rust/kernel/regulator.rs | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/rust/kernel/regulator.rs b/rust/kernel/regulator.rs index 65f3a125348f..704147e18bfc 100644 --- a/rust/kernel/regulator.rs +++ b/rust/kernel/regulator.rs @@ -203,20 +203,20 @@ pub struct Error<State: RegulatorState> { /// // A fictictious probe function that obtains a regulator and sets it up. /// fn probe(dev: &Device) -> Result<PrivateData> { /// // Obtain a reference to a (fictitious) regulator. -/// let mut regulator = Regulator::<Dynamic>::get(dev, c_str!("vcc"))?; +/// let regulator = Regulator::<Dynamic>::get(dev, c_str!("vcc"))?; /// /// Ok(PrivateData { regulator }) /// } /// /// // A fictictious function that indicates that the device is going to be used. -/// fn open(dev: &Device, data: &mut PrivateData) -> Result { +/// fn open(dev: &Device, data: &PrivateData) -> Result { /// // Increase the `enabled` reference count. /// data.regulator.enable()?; /// /// Ok(()) /// } /// -/// fn close(dev: &Device, data: &mut PrivateData) -> Result { +/// fn close(dev: &Device, data: &PrivateData) -> Result { /// // Decrease the `enabled` reference count. /// data.regulator.disable()?; /// @@ -289,12 +289,12 @@ impl<T: RegulatorState> Regulator<T> { }) } - fn enable_internal(&mut self) -> Result { + fn enable_internal(&self) -> Result { // SAFETY: Safe as per the type invariants of `Regulator`. to_result(unsafe { bindings::regulator_enable(self.inner.as_ptr()) }) } - fn disable_internal(&mut self) -> Result { + fn disable_internal(&self) -> Result { // SAFETY: Safe as per the type invariants of `Regulator`. to_result(unsafe { bindings::regulator_disable(self.inner.as_ptr()) }) } @@ -310,7 +310,7 @@ impl Regulator<Disabled> { pub fn try_into_enabled(self) -> Result<Regulator<Enabled>, Error<Disabled>> { // We will be transferring the ownership of our `regulator_get()` count to // `Regulator<Enabled>`. - let mut regulator = ManuallyDrop::new(self); + let regulator = ManuallyDrop::new(self); regulator .enable_internal() @@ -339,7 +339,7 @@ impl Regulator<Enabled> { pub fn try_into_disabled(self) -> Result<Regulator<Disabled>, Error<Enabled>> { // We will be transferring the ownership of our `regulator_get()` count // to `Regulator<Disabled>`. - let mut regulator = ManuallyDrop::new(self); + let regulator = ManuallyDrop::new(self); regulator .disable_internal() @@ -366,12 +366,12 @@ impl Regulator<Dynamic> { } /// Increases the `enabled` reference count. - pub fn enable(&mut self) -> Result { + pub fn enable(&self) -> Result { self.enable_internal() } /// Decreases the `enabled` reference count. - pub fn disable(&mut self) -> Result { + pub fn disable(&self) -> Result { self.disable_internal() } } @@ -398,6 +398,14 @@ impl<T: RegulatorState> Drop for Regulator<T> { } } +// SAFETY: It is safe to send a `Regulator<T>` across threads. In particular, a +// Regulator<T> can be dropped from any thread. +unsafe impl<T: RegulatorState> Send for Regulator<T> {} + +// SAFETY: It is safe to send a &Regulator<T> across threads because the C side +// handles its own locking. +unsafe impl<T: RegulatorState> Sync for Regulator<T> {} + /// A voltage. /// /// This type represents a voltage value in microvolts. |