Skip to content

Commit eb9bb4c

Browse files
committed
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.
2 parents eccd3d9 + 9a200cb commit eb9bb4c

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

rust/kernel/regulator.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,20 +203,20 @@ pub struct Error<State: RegulatorState> {
203203
/// // A fictictious probe function that obtains a regulator and sets it up.
204204
/// fn probe(dev: &Device) -> Result<PrivateData> {
205205
/// // Obtain a reference to a (fictitious) regulator.
206-
/// let mut regulator = Regulator::<Dynamic>::get(dev, c_str!("vcc"))?;
206+
/// let regulator = Regulator::<Dynamic>::get(dev, c_str!("vcc"))?;
207207
///
208208
/// Ok(PrivateData { regulator })
209209
/// }
210210
///
211211
/// // A fictictious function that indicates that the device is going to be used.
212-
/// fn open(dev: &Device, data: &mut PrivateData) -> Result {
212+
/// fn open(dev: &Device, data: &PrivateData) -> Result {
213213
/// // Increase the `enabled` reference count.
214214
/// data.regulator.enable()?;
215215
///
216216
/// Ok(())
217217
/// }
218218
///
219-
/// fn close(dev: &Device, data: &mut PrivateData) -> Result {
219+
/// fn close(dev: &Device, data: &PrivateData) -> Result {
220220
/// // Decrease the `enabled` reference count.
221221
/// data.regulator.disable()?;
222222
///
@@ -289,12 +289,12 @@ impl<T: RegulatorState> Regulator<T> {
289289
})
290290
}
291291

292-
fn enable_internal(&mut self) -> Result {
292+
fn enable_internal(&self) -> Result {
293293
// SAFETY: Safe as per the type invariants of `Regulator`.
294294
to_result(unsafe { bindings::regulator_enable(self.inner.as_ptr()) })
295295
}
296296

297-
fn disable_internal(&mut self) -> Result {
297+
fn disable_internal(&self) -> Result {
298298
// SAFETY: Safe as per the type invariants of `Regulator`.
299299
to_result(unsafe { bindings::regulator_disable(self.inner.as_ptr()) })
300300
}
@@ -310,7 +310,7 @@ impl Regulator<Disabled> {
310310
pub fn try_into_enabled(self) -> Result<Regulator<Enabled>, Error<Disabled>> {
311311
// We will be transferring the ownership of our `regulator_get()` count to
312312
// `Regulator<Enabled>`.
313-
let mut regulator = ManuallyDrop::new(self);
313+
let regulator = ManuallyDrop::new(self);
314314

315315
regulator
316316
.enable_internal()
@@ -339,7 +339,7 @@ impl Regulator<Enabled> {
339339
pub fn try_into_disabled(self) -> Result<Regulator<Disabled>, Error<Enabled>> {
340340
// We will be transferring the ownership of our `regulator_get()` count
341341
// to `Regulator<Disabled>`.
342-
let mut regulator = ManuallyDrop::new(self);
342+
let regulator = ManuallyDrop::new(self);
343343

344344
regulator
345345
.disable_internal()
@@ -366,12 +366,12 @@ impl Regulator<Dynamic> {
366366
}
367367

368368
/// Increases the `enabled` reference count.
369-
pub fn enable(&mut self) -> Result {
369+
pub fn enable(&self) -> Result {
370370
self.enable_internal()
371371
}
372372

373373
/// Decreases the `enabled` reference count.
374-
pub fn disable(&mut self) -> Result {
374+
pub fn disable(&self) -> Result {
375375
self.disable_internal()
376376
}
377377
}
@@ -398,6 +398,14 @@ impl<T: RegulatorState> Drop for Regulator<T> {
398398
}
399399
}
400400

401+
// SAFETY: It is safe to send a `Regulator<T>` across threads. In particular, a
402+
// Regulator<T> can be dropped from any thread.
403+
unsafe impl<T: RegulatorState> Send for Regulator<T> {}
404+
405+
// SAFETY: It is safe to send a &Regulator<T> across threads because the C side
406+
// handles its own locking.
407+
unsafe impl<T: RegulatorState> Sync for Regulator<T> {}
408+
401409
/// A voltage.
402410
///
403411
/// This type represents a voltage value in microvolts.

0 commit comments

Comments
 (0)