Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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

+26 -33
+26 -33
sound/core/seq/seq_prioq.c
··· 132 132 struct snd_seq_event_cell * cell) 133 133 { 134 134 struct snd_seq_event_cell *cur, *prev; 135 - unsigned long flags; 136 135 int count; 137 136 int prior; 138 137 ··· 141 142 /* check flags */ 142 143 prior = (cell->event.flags & SNDRV_SEQ_PRIORITY_MASK); 143 144 144 - spin_lock_irqsave(&f->lock, flags); 145 + guard(spinlock_irqsave)(&f->lock); 145 146 146 147 /* check if this element needs to inserted at the end (ie. ordered 147 148 data is inserted) This will be very likeley if a sequencer ··· 153 154 f->tail = cell; 154 155 cell->next = NULL; 155 156 f->cells++; 156 - spin_unlock_irqrestore(&f->lock, flags); 157 157 return 0; 158 158 } 159 159 } ··· 177 179 prev = cur; 178 180 cur = cur->next; 179 181 if (! --count) { 180 - spin_unlock_irqrestore(&f->lock, flags); 181 182 pr_err("ALSA: seq: cannot find a pointer.. infinite loop?\n"); 182 183 return -EINVAL; 183 184 } ··· 192 195 if (cur == NULL) /* reached end of the list */ 193 196 f->tail = cell; 194 197 f->cells++; 195 - spin_unlock_irqrestore(&f->lock, flags); 196 198 return 0; 197 199 } 198 200 ··· 209 213 void *current_time) 210 214 { 211 215 struct snd_seq_event_cell *cell; 212 - unsigned long flags; 213 216 214 217 if (f == NULL) { 215 218 pr_debug("ALSA: seq: snd_seq_prioq_cell_in() called with NULL prioq\n"); 216 219 return NULL; 217 220 } 218 - spin_lock_irqsave(&f->lock, flags); 219 221 222 + guard(spinlock_irqsave)(&f->lock); 220 223 cell = f->head; 221 224 if (cell && current_time && !event_is_ready(&cell->event, current_time)) 222 225 cell = NULL; ··· 230 235 f->cells--; 231 236 } 232 237 233 - spin_unlock_irqrestore(&f->lock, flags); 234 238 return cell; 235 239 } 236 240 ··· 250 256 void *arg) 251 257 { 252 258 register struct snd_seq_event_cell *cell, *next; 253 - unsigned long flags; 254 259 struct snd_seq_event_cell *prev = NULL; 255 260 struct snd_seq_event_cell *freefirst = NULL, *freeprev = NULL, *freenext; 256 261 257 262 /* collect all removed cells */ 258 - spin_lock_irqsave(&f->lock, flags); 259 - for (cell = f->head; cell; cell = next) { 260 - next = cell->next; 261 - if (!match(cell, arg)) { 262 - prev = cell; 263 - continue; 263 + scoped_guard(spinlock_irqsave, &f->lock) { 264 + for (cell = f->head; cell; cell = next) { 265 + next = cell->next; 266 + if (!match(cell, arg)) { 267 + prev = cell; 268 + continue; 269 + } 270 + 271 + /* remove cell from prioq */ 272 + if (cell == f->head) 273 + f->head = cell->next; 274 + else 275 + prev->next = cell->next; 276 + if (cell == f->tail) 277 + f->tail = cell->next; 278 + f->cells--; 279 + 280 + /* add cell to free list */ 281 + cell->next = NULL; 282 + if (freefirst == NULL) 283 + freefirst = cell; 284 + else 285 + freeprev->next = cell; 286 + freeprev = cell; 264 287 } 265 - 266 - /* remove cell from prioq */ 267 - if (cell == f->head) 268 - f->head = cell->next; 269 - else 270 - prev->next = cell->next; 271 - if (cell == f->tail) 272 - f->tail = cell->next; 273 - f->cells--; 274 - 275 - /* add cell to free list */ 276 - cell->next = NULL; 277 - if (freefirst == NULL) 278 - freefirst = cell; 279 - else 280 - freeprev->next = cell; 281 - freeprev = cell; 282 288 } 283 - spin_unlock_irqrestore(&f->lock, flags); 284 289 285 290 /* remove selected cells */ 286 291 while (freefirst) {