Linux Sound subsystem development
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: linux-sound@vger.kernel.org
Subject: [PATCH v3 20/24] ALSA: seq: prioq: Use guard() for locking
Date: Tue, 27 Feb 2024 09:53:02 +0100	[thread overview]
Message-ID: <20240227085306.9764-21-tiwai@suse.de> (raw)
In-Reply-To: <20240227085306.9764-1-tiwai@suse.de>

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>
---
 sound/core/seq/seq_prioq.c | 59 +++++++++++++++++---------------------
 1 file changed, 26 insertions(+), 33 deletions(-)

diff --git a/sound/core/seq/seq_prioq.c b/sound/core/seq/seq_prioq.c
index e062df7610e1..e649485a8772 100644
--- a/sound/core/seq/seq_prioq.c
+++ b/sound/core/seq/seq_prioq.c
@@ -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 *cur, *prev;
-	unsigned long flags;
 	int count;
 	int prior;
 
@@ -142,7 +141,7 @@ int snd_seq_prioq_cell_in(struct snd_seq_prioq * f,
 	/* check flags */
 	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 
 	   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,
 			f->tail = cell;
 			cell->next = NULL;
 			f->cells++;
-			spin_unlock_irqrestore(&f->lock, flags);
 			return 0;
 		}
 	}
@@ -179,7 +177,6 @@ int snd_seq_prioq_cell_in(struct snd_seq_prioq * f,
 		prev = cur;
 		cur = cur->next;
 		if (! --count) {
-			spin_unlock_irqrestore(&f->lock, flags);
 			pr_err("ALSA: seq: cannot find a pointer.. infinite loop?\n");
 			return -EINVAL;
 		}
@@ -195,7 +192,6 @@ int snd_seq_prioq_cell_in(struct snd_seq_prioq * f,
 	if (cur == NULL) /* reached end of the list */
 		f->tail = cell;
 	f->cells++;
-	spin_unlock_irqrestore(&f->lock, flags);
 	return 0;
 }
 
@@ -213,14 +209,13 @@ struct snd_seq_event_cell *snd_seq_prioq_cell_out(struct snd_seq_prioq *f,
 						  void *current_time)
 {
 	struct snd_seq_event_cell *cell;
-	unsigned long flags;
 
 	if (f == NULL) {
 		pr_debug("ALSA: seq: snd_seq_prioq_cell_in() called with NULL prioq\n");
 		return NULL;
 	}
-	spin_lock_irqsave(&f->lock, flags);
 
+	guard(spinlock_irqsave)(&f->lock);
 	cell = f->head;
 	if (cell && current_time && !event_is_ready(&cell->event, current_time))
 		cell = NULL;
@@ -235,7 +230,6 @@ struct snd_seq_event_cell *snd_seq_prioq_cell_out(struct snd_seq_prioq *f,
 		f->cells--;
 	}
 
-	spin_unlock_irqrestore(&f->lock, flags);
 	return cell;
 }
 
@@ -256,37 +250,36 @@ static void prioq_remove_cells(struct snd_seq_prioq *f,
 			       void *arg)
 {
 	register struct snd_seq_event_cell *cell, *next;
-	unsigned long flags;
 	struct snd_seq_event_cell *prev = NULL;
 	struct snd_seq_event_cell *freefirst = NULL, *freeprev = NULL, *freenext;
 
 	/* collect all removed cells */
-	spin_lock_irqsave(&f->lock, flags);
-	for (cell = f->head; cell; cell = next) {
-		next = cell->next;
-		if (!match(cell, arg)) {
-			prev = cell;
-			continue;
+	scoped_guard(spinlock_irqsave, &f->lock) {
+		for (cell = f->head; cell; cell = next) {
+			next = cell->next;
+			if (!match(cell, arg)) {
+				prev = cell;
+				continue;
+			}
+
+			/* remove cell from prioq */
+			if (cell == f->head)
+				f->head = cell->next;
+			else
+				prev->next = cell->next;
+			if (cell == f->tail)
+				f->tail = cell->next;
+			f->cells--;
+
+			/* add cell to free list */
+			cell->next = NULL;
+			if (freefirst == NULL)
+				freefirst = cell;
+			else
+				freeprev->next = cell;
+			freeprev = cell;
 		}
-
-		/* remove cell from prioq */
-		if (cell == f->head)
-			f->head = cell->next;
-		else
-			prev->next = cell->next;
-		if (cell == f->tail)
-			f->tail = cell->next;
-		f->cells--;
-
-		/* add cell to free list */
-		cell->next = NULL;
-		if (freefirst == NULL)
-			freefirst = cell;
-		else
-			freeprev->next = cell;
-		freeprev = cell;
 	}
-	spin_unlock_irqrestore(&f->lock, flags);	
 
 	/* remove selected cells */
 	while (freefirst) {
-- 
2.35.3


  parent reply	other threads:[~2024-02-27  8:53 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-27  8:52 [PATCH v3 00/24] Clean up locking with guard() in ALSA core Takashi Iwai
2024-02-27  8:52 ` [PATCH v3 01/24] ALSA: ump: Use guard() for locking Takashi Iwai
2024-02-27  8:52 ` [PATCH v3 02/24] ALSA: compress_offload: " Takashi Iwai
2024-02-27  8:52 ` [PATCH v3 03/24] ALSA: timer: " Takashi Iwai
2024-02-27  8:52 ` [PATCH v3 04/24] ALSA: hrtimer: " Takashi Iwai
2024-02-27  8:52 ` [PATCH v3 05/24] ALSA: hwdep: " Takashi Iwai
2024-02-29 21:00   ` Nathan Chancellor
2024-03-01  7:27     ` Takashi Iwai
2024-03-01 17:10       ` Nathan Chancellor
2024-02-27  8:52 ` [PATCH v3 06/24] ALSA: info: " Takashi Iwai
2024-02-27  8:52 ` [PATCH v3 07/24] ALSA: mixer_oss: " Takashi Iwai
2024-02-27  8:52 ` [PATCH v3 08/24] ALSA: control: " Takashi Iwai
2024-02-27  8:52 ` [PATCH v3 09/24] ALSA: rawmidi: " Takashi Iwai
2024-02-27  8:52 ` [PATCH v3 10/24] ALSA: jack: " Takashi Iwai
2024-02-27  8:52 ` [PATCH v3 11/24] ALSA: core: " Takashi Iwai
2024-02-27  8:52 ` [PATCH v3 12/24] ALSA: seq: fifo: " Takashi Iwai
2024-02-27  8:52 ` [PATCH v3 13/24] ALSA: seq: memory: " Takashi Iwai
2024-02-27  8:52 ` [PATCH v3 14/24] ALSA: seq: ports: " Takashi Iwai
2024-02-27  8:52 ` [PATCH v3 15/24] ALSA: seq: queue: " Takashi Iwai
2024-02-27  8:52 ` [PATCH v3 16/24] ALSA: seq: timer: " Takashi Iwai
2024-02-27  8:52 ` [PATCH v3 17/24] ALSA: seq: midi: " Takashi Iwai
2024-02-27  8:53 ` [PATCH v3 18/24] ALSA: seq: ump: " Takashi Iwai
2024-02-27  8:53 ` [PATCH v3 19/24] ALSA: seq: virmidi: " Takashi Iwai
2024-02-27  8:53 ` Takashi Iwai [this message]
2024-02-27  8:53 ` [PATCH v3 21/24] ALSA: pcm: " Takashi Iwai
2024-02-27  8:53 ` [PATCH v3 22/24] ALSA: pcm: Use guard() for PCM stream locks Takashi Iwai
2024-02-27  8:53 ` [PATCH v3 23/24] ALSA: pcm: oss: Use guard() for setup Takashi Iwai
2024-02-27  8:53 ` [PATCH v3 24/24] ALSA: control_led: Use guard() for locking Takashi Iwai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240227085306.9764-21-tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=linux-sound@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox