Re: [PATCH v2 2/5] iio: chemical: sgp40: Replace manual locking with guard()

From: Andy Shevchenko

Date: Tue Nov 03 2026 - 05:12:17 EST


On Mon, Nov 02, 2026 at 11:12:55AM +0200, Jaakko Koivisto wrote:
> Replace manual mutex_lock() and mutex_unlock() with RAII-style
> guard(mutex)().
>
> Extend the lock in sgp40_measure_resistance_raw() to cover the
> I2C send and receive cycle, preventing I/O-errors if the device
> is currently running a command, or clobbering the output of a previous
> command if a new command is issued during the sleep-period between
> i2c_master_send() and i2c_master_recv().

> /* we calculate as a multiple of 16384 (2^14) */
> - mutex_lock(&data->lock);
> + guard(mutex)(&data->lock);
> x = ((int)resistance_raw - data->res_calibbias) * 106;
> - mutex_unlock(&data->lock);

We don't wont heavy code to be in the critical section. Here it's quite obvious
that the call should be scoped_guard().

Side note, in case of use guard()() make sure it's delimited by blank lines, so
it's visible at a glance when reading a code.

--
With Best Regards,
Andy Shevchenko