Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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.

Mark Brown eb9bb4c5 eccd3d97

+17 -9
+17 -9
rust/kernel/regulator.rs
··· 203 203 /// // A fictictious probe function that obtains a regulator and sets it up. 204 204 /// fn probe(dev: &Device) -> Result<PrivateData> { 205 205 /// // 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"))?; 207 207 /// 208 208 /// Ok(PrivateData { regulator }) 209 209 /// } 210 210 /// 211 211 /// // 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 { 213 213 /// // Increase the `enabled` reference count. 214 214 /// data.regulator.enable()?; 215 215 /// 216 216 /// Ok(()) 217 217 /// } 218 218 /// 219 - /// fn close(dev: &Device, data: &mut PrivateData) -> Result { 219 + /// fn close(dev: &Device, data: &PrivateData) -> Result { 220 220 /// // Decrease the `enabled` reference count. 221 221 /// data.regulator.disable()?; 222 222 /// ··· 289 289 }) 290 290 } 291 291 292 - fn enable_internal(&mut self) -> Result { 292 + fn enable_internal(&self) -> Result { 293 293 // SAFETY: Safe as per the type invariants of `Regulator`. 294 294 to_result(unsafe { bindings::regulator_enable(self.inner.as_ptr()) }) 295 295 } 296 296 297 - fn disable_internal(&mut self) -> Result { 297 + fn disable_internal(&self) -> Result { 298 298 // SAFETY: Safe as per the type invariants of `Regulator`. 299 299 to_result(unsafe { bindings::regulator_disable(self.inner.as_ptr()) }) 300 300 } ··· 310 310 pub fn try_into_enabled(self) -> Result<Regulator<Enabled>, Error<Disabled>> { 311 311 // We will be transferring the ownership of our `regulator_get()` count to 312 312 // `Regulator<Enabled>`. 313 - let mut regulator = ManuallyDrop::new(self); 313 + let regulator = ManuallyDrop::new(self); 314 314 315 315 regulator 316 316 .enable_internal() ··· 339 339 pub fn try_into_disabled(self) -> Result<Regulator<Disabled>, Error<Enabled>> { 340 340 // We will be transferring the ownership of our `regulator_get()` count 341 341 // to `Regulator<Disabled>`. 342 - let mut regulator = ManuallyDrop::new(self); 342 + let regulator = ManuallyDrop::new(self); 343 343 344 344 regulator 345 345 .disable_internal() ··· 366 366 } 367 367 368 368 /// Increases the `enabled` reference count. 369 - pub fn enable(&mut self) -> Result { 369 + pub fn enable(&self) -> Result { 370 370 self.enable_internal() 371 371 } 372 372 373 373 /// Decreases the `enabled` reference count. 374 - pub fn disable(&mut self) -> Result { 374 + pub fn disable(&self) -> Result { 375 375 self.disable_internal() 376 376 } 377 377 } ··· 397 397 unsafe { bindings::regulator_put(self.inner.as_ptr()) }; 398 398 } 399 399 } 400 + 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> {} 400 408 401 409 /// A voltage. 402 410 ///