Re: [PATCH v3] gpio: sim: simplify code with cleanup helpers

From: Andy Shevchenko
Date: Tue Aug 15 2023 - 06:31:38 EST


On Sat, Aug 12, 2023 at 08:36:35PM +0200, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxx>
>
> Use macros defined in linux/cleanup.h to automate resource lifetime
> control in gpio-sim.

...

> static void gpio_sim_set(struct gpio_chip *gc, unsigned int offset, int value)
> {
> struct gpio_sim_chip *chip = gpiochip_get_data(gc);
>
> - mutex_lock(&chip->lock);
> - __assign_bit(offset, chip->value_map, value);
> - mutex_unlock(&chip->lock);
> + scoped_guard(mutex, &chip->lock)
> + __assign_bit(offset, chip->value_map, value);

But this can also be guarded.

guard(mutex)(&chip->lock);

__assign_bit(offset, chip->value_map, value);

> }

...

> {
> struct gpio_sim_chip *chip = gpiochip_get_data(gc);
>
> - mutex_lock(&chip->lock);
> - bitmap_replace(chip->value_map, chip->value_map, bits, mask, gc->ngpio);
> - mutex_unlock(&chip->lock);
> + scoped_guard(mutex, &chip->lock)
> + bitmap_replace(chip->value_map, chip->value_map, bits, mask,
> + gc->ngpio);

Ditto.

guard(mutex)(&chip->lock);

bitmap_replace(chip->value_map, chip->value_map, bits, mask, gc->ngpio);

(exactly 80 for the sectants of 80 characters :).

> }

...

> {
> struct gpio_sim_chip *chip = gpiochip_get_data(gc);
>
> - mutex_lock(&chip->lock);
> - __assign_bit(offset, chip->value_map, !!test_bit(offset, chip->pull_map));
> - mutex_unlock(&chip->lock);
> + scoped_guard(mutex, &chip->lock)
> + __assign_bit(offset, chip->value_map,
> + !!test_bit(offset, chip->pull_map));

Ditto.

guard(mutex)(&chip->lock);

__assign_bit(offset, chip->value_map, test_bit(offset, chip->pull_map));

(in this form fanatics of 80 can sleep well :-)

Note that !! is redundant as test_bit() family of functions were fixed to
return boolean.

> }

--
With Best Regards,
Andy Shevchenko