Skip to content

Commit 1affe7b

Browse files
committed
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: Takashi Iwai <tiwai@suse.de> Link: https://lore.kernel.org/r/20240227085306.9764-21-tiwai@suse.de
1 parent a04f2c3 commit 1affe7b

1 file changed

Lines changed: 26 additions & 33 deletions

File tree

sound/core/seq/seq_prioq.c

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ int snd_seq_prioq_cell_in(struct snd_seq_prioq * f,
132132
struct snd_seq_event_cell * cell)
133133
{
134134
struct snd_seq_event_cell *cur, *prev;
135-
unsigned long flags;
136135
int count;
137136
int prior;
138137

@@ -142,7 +141,7 @@ int snd_seq_prioq_cell_in(struct snd_seq_prioq * f,
142141
/* check flags */
143142
prior = (cell->event.flags & SNDRV_SEQ_PRIORITY_MASK);
144143

145-
spin_lock_irqsave(&f->lock, flags);
144+
guard(spinlock_irqsave)(&f->lock);
146145

147146
/* check if this element needs to inserted at the end (ie. ordered
148147
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,
154153
f->tail = cell;
155154
cell->next = NULL;
156155
f->cells++;
157-
spin_unlock_irqrestore(&f->lock, flags);
158156
return 0;
159157
}
160158
}
@@ -179,7 +177,6 @@ int snd_seq_prioq_cell_in(struct snd_seq_prioq * f,
179177
prev = cur;
180178
cur = cur->next;
181179
if (! --count) {
182-
spin_unlock_irqrestore(&f->lock, flags);
183180
pr_err("ALSA: seq: cannot find a pointer.. infinite loop?\n");
184181
return -EINVAL;
185182
}
@@ -195,7 +192,6 @@ int snd_seq_prioq_cell_in(struct snd_seq_prioq * f,
195192
if (cur == NULL) /* reached end of the list */
196193
f->tail = cell;
197194
f->cells++;
198-
spin_unlock_irqrestore(&f->lock, flags);
199195
return 0;
200196
}
201197

@@ -213,14 +209,13 @@ struct snd_seq_event_cell *snd_seq_prioq_cell_out(struct snd_seq_prioq *f,
213209
void *current_time)
214210
{
215211
struct snd_seq_event_cell *cell;
216-
unsigned long flags;
217212

218213
if (f == NULL) {
219214
pr_debug("ALSA: seq: snd_seq_prioq_cell_in() called with NULL prioq\n");
220215
return NULL;
221216
}
222-
spin_lock_irqsave(&f->lock, flags);
223217

218+
guard(spinlock_irqsave)(&f->lock);
224219
cell = f->head;
225220
if (cell && current_time && !event_is_ready(&cell->event, current_time))
226221
cell = NULL;
@@ -235,7 +230,6 @@ struct snd_seq_event_cell *snd_seq_prioq_cell_out(struct snd_seq_prioq *f,
235230
f->cells--;
236231
}
237232

238-
spin_unlock_irqrestore(&f->lock, flags);
239233
return cell;
240234
}
241235

@@ -256,37 +250,36 @@ static void prioq_remove_cells(struct snd_seq_prioq *f,
256250
void *arg)
257251
{
258252
register struct snd_seq_event_cell *cell, *next;
259-
unsigned long flags;
260253
struct snd_seq_event_cell *prev = NULL;
261254
struct snd_seq_event_cell *freefirst = NULL, *freeprev = NULL, *freenext;
262255

263256
/* collect all removed cells */
264-
spin_lock_irqsave(&f->lock, flags);
265-
for (cell = f->head; cell; cell = next) {
266-
next = cell->next;
267-
if (!match(cell, arg)) {
268-
prev = cell;
269-
continue;
270-
}
271-
272-
/* remove cell from prioq */
273-
if (cell == f->head)
274-
f->head = cell->next;
275-
else
276-
prev->next = cell->next;
277-
if (cell == f->tail)
278-
f->tail = cell->next;
279-
f->cells--;
257+
scoped_guard(spinlock_irqsave, &f->lock) {
258+
for (cell = f->head; cell; cell = next) {
259+
next = cell->next;
260+
if (!match(cell, arg)) {
261+
prev = cell;
262+
continue;
263+
}
264+
265+
/* remove cell from prioq */
266+
if (cell == f->head)
267+
f->head = cell->next;
268+
else
269+
prev->next = cell->next;
270+
if (cell == f->tail)
271+
f->tail = cell->next;
272+
f->cells--;
280273

281-
/* add cell to free list */
282-
cell->next = NULL;
283-
if (freefirst == NULL)
284-
freefirst = cell;
285-
else
286-
freeprev->next = cell;
287-
freeprev = cell;
274+
/* add cell to free list */
275+
cell->next = NULL;
276+
if (freefirst == NULL)
277+
freefirst = cell;
278+
else
279+
freeprev->next = cell;
280+
freeprev = cell;
281+
}
288282
}
289-
spin_unlock_irqrestore(&f->lock, flags);
290283

291284
/* remove selected cells */
292285
while (freefirst) {

0 commit comments

Comments
 (0)