ALSA: hrtimer: Use guard() for locking

We can simplify the code gracefully with new guard() macro and co for
automatic cleanup of locks.

Only the code refactoring, and no functional changes.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
Link: https://lore.kernel.org/r/20240227085306.9764-5-tiwai@suse.de
This commit is contained in:
Takashi Iwai 2024-02-27 09:52:46 +01:00
parent beb45974dd
commit b04892691d

View File

@ -35,12 +35,12 @@ static enum hrtimer_restart snd_hrtimer_callback(struct hrtimer *hrt)
unsigned long ticks; unsigned long ticks;
enum hrtimer_restart ret = HRTIMER_NORESTART; enum hrtimer_restart ret = HRTIMER_NORESTART;
spin_lock(&t->lock); scoped_guard(spinlock, &t->lock) {
if (!t->running) if (!t->running)
goto out; /* fast path */ return HRTIMER_NORESTART; /* fast path */
stime->in_callback = true; stime->in_callback = true;
ticks = t->sticks; ticks = t->sticks;
spin_unlock(&t->lock); }
/* calculate the drift */ /* calculate the drift */
delta = ktime_sub(hrt->base->get_time(), hrtimer_get_expires(hrt)); delta = ktime_sub(hrt->base->get_time(), hrtimer_get_expires(hrt));
@ -49,15 +49,13 @@ static enum hrtimer_restart snd_hrtimer_callback(struct hrtimer *hrt)
snd_timer_interrupt(stime->timer, ticks); snd_timer_interrupt(stime->timer, ticks);
spin_lock(&t->lock); guard(spinlock)(&t->lock);
if (t->running) { if (t->running) {
hrtimer_add_expires_ns(hrt, t->sticks * resolution); hrtimer_add_expires_ns(hrt, t->sticks * resolution);
ret = HRTIMER_RESTART; ret = HRTIMER_RESTART;
} }
stime->in_callback = false; stime->in_callback = false;
out:
spin_unlock(&t->lock);
return ret; return ret;
} }
@ -80,10 +78,10 @@ static int snd_hrtimer_close(struct snd_timer *t)
struct snd_hrtimer *stime = t->private_data; struct snd_hrtimer *stime = t->private_data;
if (stime) { if (stime) {
spin_lock_irq(&t->lock); scoped_guard(spinlock_irq, &t->lock) {
t->running = 0; /* just to be sure */ t->running = 0; /* just to be sure */
stime->in_callback = 1; /* skip start/stop */ stime->in_callback = 1; /* skip start/stop */
spin_unlock_irq(&t->lock); }
hrtimer_cancel(&stime->hrt); hrtimer_cancel(&stime->hrt);
kfree(stime); kfree(stime);