Commit 1affe7bb authored by Takashi Iwai's avatar Takashi Iwai

ALSA: seq: prioq: 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-21-tiwai@suse.de
parent a04f2c39
...@@ -132,7 +132,6 @@ int snd_seq_prioq_cell_in(struct snd_seq_prioq * f, ...@@ -132,7 +132,6 @@ int snd_seq_prioq_cell_in(struct snd_seq_prioq * f,
struct snd_seq_event_cell * cell) struct snd_seq_event_cell * cell)
{ {
struct snd_seq_event_cell *cur, *prev; struct snd_seq_event_cell *cur, *prev;
unsigned long flags;
int count; int count;
int prior; int prior;
...@@ -142,7 +141,7 @@ int snd_seq_prioq_cell_in(struct snd_seq_prioq * f, ...@@ -142,7 +141,7 @@ int snd_seq_prioq_cell_in(struct snd_seq_prioq * f,
/* check flags */ /* check flags */
prior = (cell->event.flags & SNDRV_SEQ_PRIORITY_MASK); prior = (cell->event.flags & SNDRV_SEQ_PRIORITY_MASK);
spin_lock_irqsave(&f->lock, flags); guard(spinlock_irqsave)(&f->lock);
/* check if this element needs to inserted at the end (ie. ordered /* check if this element needs to inserted at the end (ie. ordered
data is inserted) This will be very likeley if a sequencer data is inserted) This will be very likeley if a sequencer
...@@ -154,7 +153,6 @@ int snd_seq_prioq_cell_in(struct snd_seq_prioq * f, ...@@ -154,7 +153,6 @@ int snd_seq_prioq_cell_in(struct snd_seq_prioq * f,
f->tail = cell; f->tail = cell;
cell->next = NULL; cell->next = NULL;
f->cells++; f->cells++;
spin_unlock_irqrestore(&f->lock, flags);
return 0; return 0;
} }
} }
...@@ -179,7 +177,6 @@ int snd_seq_prioq_cell_in(struct snd_seq_prioq * f, ...@@ -179,7 +177,6 @@ int snd_seq_prioq_cell_in(struct snd_seq_prioq * f,
prev = cur; prev = cur;
cur = cur->next; cur = cur->next;
if (! --count) { if (! --count) {
spin_unlock_irqrestore(&f->lock, flags);
pr_err("ALSA: seq: cannot find a pointer.. infinite loop?\n"); pr_err("ALSA: seq: cannot find a pointer.. infinite loop?\n");
return -EINVAL; return -EINVAL;
} }
...@@ -195,7 +192,6 @@ int snd_seq_prioq_cell_in(struct snd_seq_prioq * f, ...@@ -195,7 +192,6 @@ int snd_seq_prioq_cell_in(struct snd_seq_prioq * f,
if (cur == NULL) /* reached end of the list */ if (cur == NULL) /* reached end of the list */
f->tail = cell; f->tail = cell;
f->cells++; f->cells++;
spin_unlock_irqrestore(&f->lock, flags);
return 0; return 0;
} }
...@@ -213,14 +209,13 @@ struct snd_seq_event_cell *snd_seq_prioq_cell_out(struct snd_seq_prioq *f, ...@@ -213,14 +209,13 @@ struct snd_seq_event_cell *snd_seq_prioq_cell_out(struct snd_seq_prioq *f,
void *current_time) void *current_time)
{ {
struct snd_seq_event_cell *cell; struct snd_seq_event_cell *cell;
unsigned long flags;
if (f == NULL) { if (f == NULL) {
pr_debug("ALSA: seq: snd_seq_prioq_cell_in() called with NULL prioq\n"); pr_debug("ALSA: seq: snd_seq_prioq_cell_in() called with NULL prioq\n");
return NULL; return NULL;
} }
spin_lock_irqsave(&f->lock, flags);
guard(spinlock_irqsave)(&f->lock);
cell = f->head; cell = f->head;
if (cell && current_time && !event_is_ready(&cell->event, current_time)) if (cell && current_time && !event_is_ready(&cell->event, current_time))
cell = NULL; cell = NULL;
...@@ -235,7 +230,6 @@ struct snd_seq_event_cell *snd_seq_prioq_cell_out(struct snd_seq_prioq *f, ...@@ -235,7 +230,6 @@ struct snd_seq_event_cell *snd_seq_prioq_cell_out(struct snd_seq_prioq *f,
f->cells--; f->cells--;
} }
spin_unlock_irqrestore(&f->lock, flags);
return cell; return cell;
} }
...@@ -256,37 +250,36 @@ static void prioq_remove_cells(struct snd_seq_prioq *f, ...@@ -256,37 +250,36 @@ static void prioq_remove_cells(struct snd_seq_prioq *f,
void *arg) void *arg)
{ {
register struct snd_seq_event_cell *cell, *next; register struct snd_seq_event_cell *cell, *next;
unsigned long flags;
struct snd_seq_event_cell *prev = NULL; struct snd_seq_event_cell *prev = NULL;
struct snd_seq_event_cell *freefirst = NULL, *freeprev = NULL, *freenext; struct snd_seq_event_cell *freefirst = NULL, *freeprev = NULL, *freenext;
/* collect all removed cells */ /* collect all removed cells */
spin_lock_irqsave(&f->lock, flags); scoped_guard(spinlock_irqsave, &f->lock) {
for (cell = f->head; cell; cell = next) { for (cell = f->head; cell; cell = next) {
next = cell->next; next = cell->next;
if (!match(cell, arg)) { if (!match(cell, arg)) {
prev = cell; prev = cell;
continue; continue;
} }
/* remove cell from prioq */ /* remove cell from prioq */
if (cell == f->head) if (cell == f->head)
f->head = cell->next; f->head = cell->next;
else else
prev->next = cell->next; prev->next = cell->next;
if (cell == f->tail) if (cell == f->tail)
f->tail = cell->next; f->tail = cell->next;
f->cells--; f->cells--;
/* add cell to free list */ /* add cell to free list */
cell->next = NULL; cell->next = NULL;
if (freefirst == NULL) if (freefirst == NULL)
freefirst = cell; freefirst = cell;
else else
freeprev->next = cell; freeprev->next = cell;
freeprev = cell; freeprev = cell;
}
} }
spin_unlock_irqrestore(&f->lock, flags);
/* remove selected cells */ /* remove selected cells */
while (freefirst) { while (freefirst) {
......
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