From: Takashi Iwai <tiwai@suse.de>
To: linux-sound@vger.kernel.org
Subject: [PATCH v3 15/24] ALSA: seq: queue: Use guard() for locking
Date: Tue, 27 Feb 2024 09:52:57 +0100 [thread overview]
Message-ID: <20240227085306.9764-16-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_queue.c | 78 ++++++++++++++------------------------
1 file changed, 28 insertions(+), 50 deletions(-)
diff --git a/sound/core/seq/seq_queue.c b/sound/core/seq/seq_queue.c
index bc933104c3ee..500ee6b19c71 100644
--- a/sound/core/seq/seq_queue.c
+++ b/sound/core/seq/seq_queue.c
@@ -50,43 +50,35 @@ int snd_seq_queue_get_cur_queues(void)
static int queue_list_add(struct snd_seq_queue *q)
{
int i;
- unsigned long flags;
- spin_lock_irqsave(&queue_list_lock, flags);
+ guard(spinlock_irqsave)(&queue_list_lock);
for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) {
if (! queue_list[i]) {
queue_list[i] = q;
q->queue = i;
num_queues++;
- spin_unlock_irqrestore(&queue_list_lock, flags);
return i;
}
}
- spin_unlock_irqrestore(&queue_list_lock, flags);
return -1;
}
static struct snd_seq_queue *queue_list_remove(int id, int client)
{
struct snd_seq_queue *q;
- unsigned long flags;
- spin_lock_irqsave(&queue_list_lock, flags);
+ guard(spinlock_irqsave)(&queue_list_lock);
q = queue_list[id];
if (q) {
- spin_lock(&q->owner_lock);
+ guard(spinlock)(&q->owner_lock);
if (q->owner == client) {
/* found */
q->klocked = 1;
- spin_unlock(&q->owner_lock);
queue_list[id] = NULL;
num_queues--;
- spin_unlock_irqrestore(&queue_list_lock, flags);
return q;
}
- spin_unlock(&q->owner_lock);
}
- spin_unlock_irqrestore(&queue_list_lock, flags);
return NULL;
}
@@ -203,15 +195,13 @@ int snd_seq_queue_delete(int client, int queueid)
struct snd_seq_queue *queueptr(int queueid)
{
struct snd_seq_queue *q;
- unsigned long flags;
if (queueid < 0 || queueid >= SNDRV_SEQ_MAX_QUEUES)
return NULL;
- spin_lock_irqsave(&queue_list_lock, flags);
+ guard(spinlock_irqsave)(&queue_list_lock);
q = queue_list[queueid];
if (q)
snd_use_lock_use(&q->use_lock);
- spin_unlock_irqrestore(&queue_list_lock, flags);
return q;
}
@@ -239,7 +229,6 @@ struct snd_seq_queue *snd_seq_queue_find_name(char *name)
void snd_seq_check_queue(struct snd_seq_queue *q, int atomic, int hop)
{
- unsigned long flags;
struct snd_seq_event_cell *cell;
snd_seq_tick_time_t cur_tick;
snd_seq_real_time_t cur_time;
@@ -249,14 +238,13 @@ void snd_seq_check_queue(struct snd_seq_queue *q, int atomic, int hop)
return;
/* make this function non-reentrant */
- spin_lock_irqsave(&q->check_lock, flags);
- if (q->check_blocked) {
- q->check_again = 1;
- spin_unlock_irqrestore(&q->check_lock, flags);
- return; /* other thread is already checking queues */
+ scoped_guard(spinlock_irqsave, &q->check_lock) {
+ if (q->check_blocked) {
+ q->check_again = 1;
+ return; /* other thread is already checking queues */
+ }
+ q->check_blocked = 1;
}
- q->check_blocked = 1;
- spin_unlock_irqrestore(&q->check_lock, flags);
__again:
/* Process tick queue... */
@@ -283,16 +271,14 @@ void snd_seq_check_queue(struct snd_seq_queue *q, int atomic, int hop)
out:
/* free lock */
- spin_lock_irqsave(&q->check_lock, flags);
- if (q->check_again) {
- q->check_again = 0;
- if (processed < MAX_CELL_PROCESSES_IN_QUEUE) {
- spin_unlock_irqrestore(&q->check_lock, flags);
- goto __again;
+ scoped_guard(spinlock_irqsave, &q->check_lock) {
+ if (q->check_again) {
+ q->check_again = 0;
+ if (processed < MAX_CELL_PROCESSES_IN_QUEUE)
+ goto __again;
}
+ q->check_blocked = 0;
}
- q->check_blocked = 0;
- spin_unlock_irqrestore(&q->check_lock, flags);
}
@@ -361,25 +347,20 @@ static inline int check_access(struct snd_seq_queue *q, int client)
*/
static int queue_access_lock(struct snd_seq_queue *q, int client)
{
- unsigned long flags;
int access_ok;
- spin_lock_irqsave(&q->owner_lock, flags);
+ guard(spinlock_irqsave)(&q->owner_lock);
access_ok = check_access(q, client);
if (access_ok)
q->klocked = 1;
- spin_unlock_irqrestore(&q->owner_lock, flags);
return access_ok;
}
/* unlock the queue */
static inline void queue_access_unlock(struct snd_seq_queue *q)
{
- unsigned long flags;
-
- spin_lock_irqsave(&q->owner_lock, flags);
+ guard(spinlock_irqsave)(&q->owner_lock);
q->klocked = 0;
- spin_unlock_irqrestore(&q->owner_lock, flags);
}
/* exported - only checking permission */
@@ -387,13 +368,11 @@ int snd_seq_queue_check_access(int queueid, int client)
{
struct snd_seq_queue *q = queueptr(queueid);
int access_ok;
- unsigned long flags;
if (! q)
return 0;
- spin_lock_irqsave(&q->owner_lock, flags);
- access_ok = check_access(q, client);
- spin_unlock_irqrestore(&q->owner_lock, flags);
+ scoped_guard(spinlock_irqsave, &q->owner_lock)
+ access_ok = check_access(q, client);
queuefree(q);
return access_ok;
}
@@ -406,7 +385,6 @@ int snd_seq_queue_check_access(int queueid, int client)
int snd_seq_queue_set_owner(int queueid, int client, int locked)
{
struct snd_seq_queue *q = queueptr(queueid);
- unsigned long flags;
if (q == NULL)
return -EINVAL;
@@ -416,10 +394,10 @@ int snd_seq_queue_set_owner(int queueid, int client, int locked)
return -EPERM;
}
- spin_lock_irqsave(&q->owner_lock, flags);
- q->locked = locked ? 1 : 0;
- q->owner = client;
- spin_unlock_irqrestore(&q->owner_lock, flags);
+ scoped_guard(spinlock_irqsave, &q->owner_lock) {
+ q->locked = locked ? 1 : 0;
+ q->owner = client;
+ }
queue_access_unlock(q);
queuefree(q);
@@ -750,10 +728,10 @@ void snd_seq_info_queues_read(struct snd_info_entry *entry,
else
bpm = 0;
- spin_lock_irq(&q->owner_lock);
- locked = q->locked;
- owner = q->owner;
- spin_unlock_irq(&q->owner_lock);
+ scoped_guard(spinlock_irq, &q->owner_lock) {
+ locked = q->locked;
+ owner = q->owner;
+ }
snd_iprintf(buffer, "queue %d: [%s]\n", q->queue, q->name);
snd_iprintf(buffer, "owned by client : %d\n", owner);
--
2.35.3
next prev 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 ` Takashi Iwai [this message]
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 ` [PATCH v3 20/24] ALSA: seq: prioq: " Takashi Iwai
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-16-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