iio: chemical: sps30: Replace manual locking with RAII locking
Subsystems (2)
PATCH v2 1/1 iio: chemical: sps30: Replace manual locking with RAII locking
Replace manual mutex_lock() and mutex_unlock() calls with the much newer guard(mutex)() and scoped_guard() macros to enable RAII patterns, modernize the driver, and to increase readability.
Add new wrapper sps30_do_meas() for cleaner locking, and rename existing sps30_do_meas() to __sps30_do_meas() to denote locking requirements.
---
v2:
- Switch over some scoped_guard()s to guard(mutex)() per David's
suggestion.
- Remove redundant whitespace per Andy's suggestion.
- Add new wrapper sps30_do_meas() per Andy's suggestion (see commit
message).
- Add Joshua's RB
(link: https://lore.kernel.org/linux-iio/CAKqfh0FWig8mRR-xhvnfcFeSinR6RySyPaf9Gbpb6WU+diiiUQ@mail.gmail.com/T/#t)
drivers/iio/chemical/sps30.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-)
drivers/iio/chemical/sps30.c +15 -15
@@ -5,6 +5,7 @@* Copyright (c) Tomasz Duszynski <[email protected]> */ +#include <linux/cleanup.h> #include <linux/crc8.h> #include <linux/delay.h> #include <linux/i2c.h>
@@ -65,7 +66,7 @@ static s32 sps30_float_to_int_clamped(__be32 *fp)return val *100 + ((fraction *100 ) >> shift); } -static int sps30_do_meas (struct sps30_state *state,s32 *data,int size) +static int __sps30_do_meas (struct sps30_state *state,s32 *data,int size) {int i, ret;
@@ -87,6 +88,12 @@ static int sps30_do_meas(struct sps30_state *state, s32 *data, int size)return 0 ; } +static int sps30_do_meas (struct sps30_state *state,s32 *data,int size) +{ +guard (mutex)(&state->lock); +return __sps30_do_meas (state, data, size); +} +static int sps30_do_reset (struct sps30_state *state) {int ret;
@@ -111,9 +118,7 @@ static irqreturn_t sps30_trigger_handler(int irq, void *p)aligned_s64 ts; } scan; -mutex_lock (&state->lock); ret =sps30_do_meas (state, scan.data,ARRAY_SIZE (scan.data)); -mutex_unlock (&state->lock);if (ret) goto err;
@@ -136,7 +141,6 @@ static int sps30_read_raw(struct iio_dev *indio_dev,case IIO_CHAN_INFO_PROCESSED :switch (chan->type) {case IIO_MASSCONCENTRATION : -mutex_lock (&state->lock);/* read up to the number of bytes actually needed */ switch (chan->channel2) {case IIO_MOD_PM1 :
@@ -152,7 +156,6 @@ static int sps30_read_raw(struct iio_dev *indio_dev, ret =sps30_do_meas (state, data,4 );break ; } -mutex_unlock (&state->lock);if (ret)return ret;
@@ -197,9 +200,9 @@ static ssize_t start_cleaning_store(struct device *dev,if (kstrtoint (buf,0 , &val) || val !=1 )return -EINVAL ; -mutex_lock (&state->lock); +guard (mutex)(&state->lock); + ret = state->ops->clean_fan (state); -mutex_unlock (&state->lock);if (ret)return ret;
@@ -215,9 +218,9 @@ static ssize_t cleaning_period_show(struct device *dev,__be32 val;int ret; -mutex_lock (&state->lock); +guard (mutex)(&state->lock); + ret = state->ops->read_cleaning_period (state, &val); -mutex_unlock (&state->lock);if (ret)return ret;
@@ -238,12 +241,11 @@ static ssize_t cleaning_period_store(struct device *dev, struct device_attribute (val > SPS30_AUTO_CLEANING_PERIOD_MAX))return -EINVAL ; -mutex_lock (&state->lock); +guard (mutex)(&state->lock); + ret = state->ops->write_cleaning_period (state,cpu_to_be32 (val)); -if (ret) { -mutex_unlock (&state->lock); +if (ret)return ret; - }msleep (20 );
@@ -256,8 +258,6 @@ static ssize_t cleaning_period_store(struct device *dev, struct device_attributedev_warn (dev,"period changed but reads will return the old value\n" ); -mutex_unlock (&state->lock); -return len; }
Signature · 1 line
2.54.0
Quoted 6 lines · Maxwell Doose
Replace manual
mutex_lock()andmutex_unlock()calls with the much newer guard(mutex)() andscoped_guard()macros to enable RAII patterns, modernize the driver, and to increase readability.Add new wrapper
sps30_do_meas()for cleaner locking, and rename existingsps30_do_meas()to__sps30_do_meas()to denote locking requirements.
Ah, I was under impression that this is (unlocked version) is used somewhere else. Since it's not the case, the wrapper is not needed, just use guard()() in the original code.
Signature · 2 lines
With Best Regards, Andy Shevchenko