Commit aa75a222 authored by Takashi Iwai's avatar Takashi Iwai

ALSA: seq: timer: 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: default avatarTakashi Iwai <tiwai@suse.de>
Link: https://lore.kernel.org/r/20240227085306.9764-17-tiwai@suse.de
parent 7c2e9821
...@@ -75,9 +75,7 @@ void snd_seq_timer_delete(struct snd_seq_timer **tmr) ...@@ -75,9 +75,7 @@ void snd_seq_timer_delete(struct snd_seq_timer **tmr)
void snd_seq_timer_defaults(struct snd_seq_timer * tmr) void snd_seq_timer_defaults(struct snd_seq_timer * tmr)
{ {
unsigned long flags; guard(spinlock_irqsave)(&tmr->lock);
spin_lock_irqsave(&tmr->lock, flags);
/* setup defaults */ /* setup defaults */
tmr->ppq = 96; /* 96 PPQ */ tmr->ppq = 96; /* 96 PPQ */
tmr->tempo = 500000; /* 120 BPM */ tmr->tempo = 500000; /* 120 BPM */
...@@ -93,7 +91,6 @@ void snd_seq_timer_defaults(struct snd_seq_timer * tmr) ...@@ -93,7 +91,6 @@ void snd_seq_timer_defaults(struct snd_seq_timer * tmr)
tmr->preferred_resolution = seq_default_timer_resolution; tmr->preferred_resolution = seq_default_timer_resolution;
tmr->skew = tmr->skew_base = SKEW_BASE; tmr->skew = tmr->skew_base = SKEW_BASE;
spin_unlock_irqrestore(&tmr->lock, flags);
} }
static void seq_timer_reset(struct snd_seq_timer *tmr) static void seq_timer_reset(struct snd_seq_timer *tmr)
...@@ -108,11 +105,8 @@ static void seq_timer_reset(struct snd_seq_timer *tmr) ...@@ -108,11 +105,8 @@ static void seq_timer_reset(struct snd_seq_timer *tmr)
void snd_seq_timer_reset(struct snd_seq_timer *tmr) void snd_seq_timer_reset(struct snd_seq_timer *tmr)
{ {
unsigned long flags; guard(spinlock_irqsave)(&tmr->lock);
spin_lock_irqsave(&tmr->lock, flags);
seq_timer_reset(tmr); seq_timer_reset(tmr);
spin_unlock_irqrestore(&tmr->lock, flags);
} }
...@@ -121,7 +115,6 @@ static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri, ...@@ -121,7 +115,6 @@ static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri,
unsigned long resolution, unsigned long resolution,
unsigned long ticks) unsigned long ticks)
{ {
unsigned long flags;
struct snd_seq_queue *q = timeri->callback_data; struct snd_seq_queue *q = timeri->callback_data;
struct snd_seq_timer *tmr; struct snd_seq_timer *tmr;
...@@ -130,29 +123,27 @@ static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri, ...@@ -130,29 +123,27 @@ static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri,
tmr = q->timer; tmr = q->timer;
if (tmr == NULL) if (tmr == NULL)
return; return;
spin_lock_irqsave(&tmr->lock, flags);
if (!tmr->running) {
spin_unlock_irqrestore(&tmr->lock, flags);
return;
}
resolution *= ticks; scoped_guard(spinlock_irqsave, &tmr->lock) {
if (tmr->skew != tmr->skew_base) { if (!tmr->running)
/* FIXME: assuming skew_base = 0x10000 */ return;
resolution = (resolution >> 16) * tmr->skew +
(((resolution & 0xffff) * tmr->skew) >> 16);
}
/* update timer */ resolution *= ticks;
snd_seq_inc_time_nsec(&tmr->cur_time, resolution); if (tmr->skew != tmr->skew_base) {
/* FIXME: assuming skew_base = 0x10000 */
resolution = (resolution >> 16) * tmr->skew +
(((resolution & 0xffff) * tmr->skew) >> 16);
}
/* calculate current tick */ /* update timer */
snd_seq_timer_update_tick(&tmr->tick, resolution); snd_seq_inc_time_nsec(&tmr->cur_time, resolution);
/* register actual time of this timer update */ /* calculate current tick */
ktime_get_ts64(&tmr->last_update); snd_seq_timer_update_tick(&tmr->tick, resolution);
spin_unlock_irqrestore(&tmr->lock, flags); /* register actual time of this timer update */
ktime_get_ts64(&tmr->last_update);
}
/* check queues and dispatch events */ /* check queues and dispatch events */
snd_seq_check_queue(q, 1, 0); snd_seq_check_queue(q, 1, 0);
...@@ -161,18 +152,15 @@ static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri, ...@@ -161,18 +152,15 @@ static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri,
/* set current tempo */ /* set current tempo */
int snd_seq_timer_set_tempo(struct snd_seq_timer * tmr, int tempo) int snd_seq_timer_set_tempo(struct snd_seq_timer * tmr, int tempo)
{ {
unsigned long flags;
if (snd_BUG_ON(!tmr)) if (snd_BUG_ON(!tmr))
return -EINVAL; return -EINVAL;
if (tempo <= 0) if (tempo <= 0)
return -EINVAL; return -EINVAL;
spin_lock_irqsave(&tmr->lock, flags); guard(spinlock_irqsave)(&tmr->lock);
if ((unsigned int)tempo != tmr->tempo) { if ((unsigned int)tempo != tmr->tempo) {
tmr->tempo = tempo; tmr->tempo = tempo;
snd_seq_timer_set_tick_resolution(tmr); snd_seq_timer_set_tick_resolution(tmr);
} }
spin_unlock_irqrestore(&tmr->lock, flags);
return 0; return 0;
} }
...@@ -180,17 +168,15 @@ int snd_seq_timer_set_tempo(struct snd_seq_timer * tmr, int tempo) ...@@ -180,17 +168,15 @@ int snd_seq_timer_set_tempo(struct snd_seq_timer * tmr, int tempo)
int snd_seq_timer_set_tempo_ppq(struct snd_seq_timer *tmr, int tempo, int ppq) int snd_seq_timer_set_tempo_ppq(struct snd_seq_timer *tmr, int tempo, int ppq)
{ {
int changed; int changed;
unsigned long flags;
if (snd_BUG_ON(!tmr)) if (snd_BUG_ON(!tmr))
return -EINVAL; return -EINVAL;
if (tempo <= 0 || ppq <= 0) if (tempo <= 0 || ppq <= 0)
return -EINVAL; return -EINVAL;
spin_lock_irqsave(&tmr->lock, flags); guard(spinlock_irqsave)(&tmr->lock);
if (tmr->running && (ppq != tmr->ppq)) { if (tmr->running && (ppq != tmr->ppq)) {
/* refuse to change ppq on running timers */ /* refuse to change ppq on running timers */
/* because it will upset the song position (ticks) */ /* because it will upset the song position (ticks) */
spin_unlock_irqrestore(&tmr->lock, flags);
pr_debug("ALSA: seq: cannot change ppq of a running timer\n"); pr_debug("ALSA: seq: cannot change ppq of a running timer\n");
return -EBUSY; return -EBUSY;
} }
...@@ -199,7 +185,6 @@ int snd_seq_timer_set_tempo_ppq(struct snd_seq_timer *tmr, int tempo, int ppq) ...@@ -199,7 +185,6 @@ int snd_seq_timer_set_tempo_ppq(struct snd_seq_timer *tmr, int tempo, int ppq)
tmr->ppq = ppq; tmr->ppq = ppq;
if (changed) if (changed)
snd_seq_timer_set_tick_resolution(tmr); snd_seq_timer_set_tick_resolution(tmr);
spin_unlock_irqrestore(&tmr->lock, flags);
return 0; return 0;
} }
...@@ -207,15 +192,12 @@ int snd_seq_timer_set_tempo_ppq(struct snd_seq_timer *tmr, int tempo, int ppq) ...@@ -207,15 +192,12 @@ int snd_seq_timer_set_tempo_ppq(struct snd_seq_timer *tmr, int tempo, int ppq)
int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr, int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr,
snd_seq_tick_time_t position) snd_seq_tick_time_t position)
{ {
unsigned long flags;
if (snd_BUG_ON(!tmr)) if (snd_BUG_ON(!tmr))
return -EINVAL; return -EINVAL;
spin_lock_irqsave(&tmr->lock, flags); guard(spinlock_irqsave)(&tmr->lock);
tmr->tick.cur_tick = position; tmr->tick.cur_tick = position;
tmr->tick.fraction = 0; tmr->tick.fraction = 0;
spin_unlock_irqrestore(&tmr->lock, flags);
return 0; return 0;
} }
...@@ -223,15 +205,12 @@ int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr, ...@@ -223,15 +205,12 @@ int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr,
int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr, int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr,
snd_seq_real_time_t position) snd_seq_real_time_t position)
{ {
unsigned long flags;
if (snd_BUG_ON(!tmr)) if (snd_BUG_ON(!tmr))
return -EINVAL; return -EINVAL;
snd_seq_sanity_real_time(&position); snd_seq_sanity_real_time(&position);
spin_lock_irqsave(&tmr->lock, flags); guard(spinlock_irqsave)(&tmr->lock);
tmr->cur_time = position; tmr->cur_time = position;
spin_unlock_irqrestore(&tmr->lock, flags);
return 0; return 0;
} }
...@@ -239,8 +218,6 @@ int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr, ...@@ -239,8 +218,6 @@ int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr,
int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew, int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew,
unsigned int base) unsigned int base)
{ {
unsigned long flags;
if (snd_BUG_ON(!tmr)) if (snd_BUG_ON(!tmr))
return -EINVAL; return -EINVAL;
...@@ -249,9 +226,8 @@ int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew, ...@@ -249,9 +226,8 @@ int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew,
pr_debug("ALSA: seq: invalid skew base 0x%x\n", base); pr_debug("ALSA: seq: invalid skew base 0x%x\n", base);
return -EINVAL; return -EINVAL;
} }
spin_lock_irqsave(&tmr->lock, flags); guard(spinlock_irqsave)(&tmr->lock);
tmr->skew = skew; tmr->skew = skew;
spin_unlock_irqrestore(&tmr->lock, flags);
return 0; return 0;
} }
...@@ -296,12 +272,12 @@ int snd_seq_timer_open(struct snd_seq_queue *q) ...@@ -296,12 +272,12 @@ int snd_seq_timer_open(struct snd_seq_queue *q)
snd_timer_instance_free(t); snd_timer_instance_free(t);
return err; return err;
} }
spin_lock_irq(&tmr->lock); scoped_guard(spinlock_irq, &tmr->lock) {
if (tmr->timeri) if (tmr->timeri)
err = -EBUSY; err = -EBUSY;
else else
tmr->timeri = t; tmr->timeri = t;
spin_unlock_irq(&tmr->lock); }
if (err < 0) { if (err < 0) {
snd_timer_close(t); snd_timer_close(t);
snd_timer_instance_free(t); snd_timer_instance_free(t);
...@@ -318,10 +294,10 @@ int snd_seq_timer_close(struct snd_seq_queue *q) ...@@ -318,10 +294,10 @@ int snd_seq_timer_close(struct snd_seq_queue *q)
tmr = q->timer; tmr = q->timer;
if (snd_BUG_ON(!tmr)) if (snd_BUG_ON(!tmr))
return -EINVAL; return -EINVAL;
spin_lock_irq(&tmr->lock); scoped_guard(spinlock_irq, &tmr->lock) {
t = tmr->timeri; t = tmr->timeri;
tmr->timeri = NULL; tmr->timeri = NULL;
spin_unlock_irq(&tmr->lock); }
if (t) { if (t) {
snd_timer_close(t); snd_timer_close(t);
snd_timer_instance_free(t); snd_timer_instance_free(t);
...@@ -342,13 +318,8 @@ static int seq_timer_stop(struct snd_seq_timer *tmr) ...@@ -342,13 +318,8 @@ static int seq_timer_stop(struct snd_seq_timer *tmr)
int snd_seq_timer_stop(struct snd_seq_timer *tmr) int snd_seq_timer_stop(struct snd_seq_timer *tmr)
{ {
unsigned long flags; guard(spinlock_irqsave)(&tmr->lock);
int err; return seq_timer_stop(tmr);
spin_lock_irqsave(&tmr->lock, flags);
err = seq_timer_stop(tmr);
spin_unlock_irqrestore(&tmr->lock, flags);
return err;
} }
static int initialize_timer(struct snd_seq_timer *tmr) static int initialize_timer(struct snd_seq_timer *tmr)
...@@ -398,13 +369,8 @@ static int seq_timer_start(struct snd_seq_timer *tmr) ...@@ -398,13 +369,8 @@ static int seq_timer_start(struct snd_seq_timer *tmr)
int snd_seq_timer_start(struct snd_seq_timer *tmr) int snd_seq_timer_start(struct snd_seq_timer *tmr)
{ {
unsigned long flags; guard(spinlock_irqsave)(&tmr->lock);
int err; return seq_timer_start(tmr);
spin_lock_irqsave(&tmr->lock, flags);
err = seq_timer_start(tmr);
spin_unlock_irqrestore(&tmr->lock, flags);
return err;
} }
static int seq_timer_continue(struct snd_seq_timer *tmr) static int seq_timer_continue(struct snd_seq_timer *tmr)
...@@ -426,13 +392,8 @@ static int seq_timer_continue(struct snd_seq_timer *tmr) ...@@ -426,13 +392,8 @@ static int seq_timer_continue(struct snd_seq_timer *tmr)
int snd_seq_timer_continue(struct snd_seq_timer *tmr) int snd_seq_timer_continue(struct snd_seq_timer *tmr)
{ {
unsigned long flags; guard(spinlock_irqsave)(&tmr->lock);
int err; return seq_timer_continue(tmr);
spin_lock_irqsave(&tmr->lock, flags);
err = seq_timer_continue(tmr);
spin_unlock_irqrestore(&tmr->lock, flags);
return err;
} }
/* return current 'real' time. use timeofday() to get better granularity. */ /* return current 'real' time. use timeofday() to get better granularity. */
...@@ -440,9 +401,8 @@ snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr, ...@@ -440,9 +401,8 @@ snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr,
bool adjust_ktime) bool adjust_ktime)
{ {
snd_seq_real_time_t cur_time; snd_seq_real_time_t cur_time;
unsigned long flags;
spin_lock_irqsave(&tmr->lock, flags); guard(spinlock_irqsave)(&tmr->lock);
cur_time = tmr->cur_time; cur_time = tmr->cur_time;
if (adjust_ktime && tmr->running) { if (adjust_ktime && tmr->running) {
struct timespec64 tm; struct timespec64 tm;
...@@ -453,7 +413,6 @@ snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr, ...@@ -453,7 +413,6 @@ snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr,
cur_time.tv_sec += tm.tv_sec; cur_time.tv_sec += tm.tv_sec;
snd_seq_sanity_real_time(&cur_time); snd_seq_sanity_real_time(&cur_time);
} }
spin_unlock_irqrestore(&tmr->lock, flags);
return cur_time; return cur_time;
} }
...@@ -461,13 +420,8 @@ snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr, ...@@ -461,13 +420,8 @@ snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr,
high PPQ values) */ high PPQ values) */
snd_seq_tick_time_t snd_seq_timer_get_cur_tick(struct snd_seq_timer *tmr) snd_seq_tick_time_t snd_seq_timer_get_cur_tick(struct snd_seq_timer *tmr)
{ {
snd_seq_tick_time_t cur_tick; guard(spinlock_irqsave)(&tmr->lock);
unsigned long flags; return tmr->tick.cur_tick;
spin_lock_irqsave(&tmr->lock, flags);
cur_tick = tmr->tick.cur_tick;
spin_unlock_irqrestore(&tmr->lock, flags);
return cur_tick;
} }
...@@ -486,19 +440,18 @@ void snd_seq_info_timer_read(struct snd_info_entry *entry, ...@@ -486,19 +440,18 @@ void snd_seq_info_timer_read(struct snd_info_entry *entry,
q = queueptr(idx); q = queueptr(idx);
if (q == NULL) if (q == NULL)
continue; continue;
mutex_lock(&q->timer_mutex); scoped_guard(mutex, &q->timer_mutex) {
tmr = q->timer; tmr = q->timer;
if (!tmr) if (!tmr)
goto unlock; break;
ti = tmr->timeri; ti = tmr->timeri;
if (!ti) if (!ti)
goto unlock; break;
snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name); snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name);
resolution = snd_timer_resolution(ti) * tmr->ticks; resolution = snd_timer_resolution(ti) * tmr->ticks;
snd_iprintf(buffer, " Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000); snd_iprintf(buffer, " Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000);
snd_iprintf(buffer, " Skew : %u / %u\n", tmr->skew, tmr->skew_base); snd_iprintf(buffer, " Skew : %u / %u\n", tmr->skew, tmr->skew_base);
unlock: }
mutex_unlock(&q->timer_mutex);
queuefree(q); queuefree(q);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment