summaryrefslogtreecommitdiff
path: root/include/sound
diff options
context:
space:
mode:
authorTakashi Iwai <tiwai@suse.de>2026-06-10 17:45:31 +0200
committerTakashi Iwai <tiwai@suse.de>2026-06-11 09:34:09 +0200
commitdcca9b6064c33c84fbeb9f09814178a206321249 (patch)
tree728716586caf1d2fd552c616211638d97b0a804b /include/sound
parent03a10859061b7f6939babd3e9b5d5a891a93c836 (diff)
downloadlinux-next-dcca9b6064c33c84fbeb9f09814178a206321249.tar.gz
linux-next-dcca9b6064c33c84fbeb9f09814178a206321249.zip
ALSA: Add simple refcount helper functions
There are many open-code to manage the same pattern for refcount + wakeup sync at closing. Let's provide the common helper functions to replace the open-code. - The recount is kept in struct snd_refcount, where it's initialized by snd_refcount_init(). - The user can simply reference or unreference via snd_refcount_get() and snd_refcount_put() functions - The user can wait for the all usages gone by snd_refcount_sync() Note that here we use atomic_t instead of refcount_t since the current users allow reusing the refcount after sync again. The design of refcount_t prevents exactly this behavior, so it doesn't fit. Signed-off-by: Takashi Iwai <tiwai@suse.de> Link: https://patch.msgid.link/20260610154538.51076-2-tiwai@suse.de
Diffstat (limited to 'include/sound')
-rw-r--r--include/sound/core.h21
1 files changed, 21 insertions, 0 deletions
diff --git a/include/sound/core.h b/include/sound/core.h
index 8b2ca95d13f7..a3f10294e834 100644
--- a/include/sound/core.h
+++ b/include/sound/core.h
@@ -75,6 +75,27 @@ struct snd_device {
#define snd_device(n) list_entry(n, struct snd_device, list)
+/*
+ * A simple reference counter with a wait queue;
+ * typically used for usage counts, and you can synchronize at finishing
+ * via snd_refcount_sync(), which is woken up when the refcount reaches to
+ * zero again.
+ */
+struct snd_refcount {
+ atomic_t count;
+ wait_queue_head_t waiter;
+};
+
+void snd_refcount_init(struct snd_refcount *ref);
+
+static inline void snd_refcount_get(struct snd_refcount *ref)
+{
+ atomic_inc(&ref->count);
+}
+
+void snd_refcount_put(struct snd_refcount *ref);
+void snd_refcount_sync(struct snd_refcount *ref);
+
/* main structure for soundcard */
struct snd_card {