ALSA: pcm: Use for_each_pcm_substream() macro
authorTakashi Iwai <tiwai@suse.de>
Sat, 6 Feb 2021 20:36:56 +0000 (21:36 +0100)
committerTakashi Iwai <tiwai@suse.de>
Mon, 8 Feb 2021 15:01:01 +0000 (16:01 +0100)
There are a few places doing the same loop iterating all PCM
substreams belonging to the PCM object.  Introduce a local helper
macro, for_each_pcm_substream(), to simplify the code.

Link: https://lore.kernel.org/r/20210206203656.15959-5-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
sound/core/pcm.c
sound/core/pcm_local.h
sound/core/pcm_memory.c
sound/core/pcm_native.c

index 50eb29f..b163164 100644 (file)
@@ -1095,25 +1095,22 @@ static int snd_pcm_dev_disconnect(struct snd_device *device)
        mutex_lock(&pcm->open_mutex);
        wake_up(&pcm->open_wait);
        list_del_init(&pcm->list);
-       for (cidx = 0; cidx < 2; cidx++) {
-               for (substream = pcm->streams[cidx].substream; substream; substream = substream->next) {
-                       snd_pcm_stream_lock_irq(substream);
-                       if (substream->runtime) {
-                               if (snd_pcm_running(substream))
-                                       snd_pcm_stop(substream,
-                                                    SNDRV_PCM_STATE_DISCONNECTED);
-                               /* to be sure, set the state unconditionally */
-                               substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
-                               wake_up(&substream->runtime->sleep);
-                               wake_up(&substream->runtime->tsleep);
-                       }
-                       snd_pcm_stream_unlock_irq(substream);
+
+       for_each_pcm_substream(pcm, cidx, substream) {
+               snd_pcm_stream_lock_irq(substream);
+               if (substream->runtime) {
+                       if (snd_pcm_running(substream))
+                               snd_pcm_stop(substream, SNDRV_PCM_STATE_DISCONNECTED);
+                       /* to be sure, set the state unconditionally */
+                       substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
+                       wake_up(&substream->runtime->sleep);
+                       wake_up(&substream->runtime->tsleep);
                }
+               snd_pcm_stream_unlock_irq(substream);
        }
 
-       for (cidx = 0; cidx < 2; cidx++)
-               for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
-                       snd_pcm_sync_stop(substream, false);
+       for_each_pcm_substream(pcm, cidx, substream)
+               snd_pcm_sync_stop(substream, false);
 
        pcm_call_notify(pcm, n_disconnect);
        for (cidx = 0; cidx < 2; cidx++) {
index b3e8be5..e3b3558 100644 (file)
@@ -72,4 +72,10 @@ struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream,
 
 #define PCM_RUNTIME_CHECK(sub) snd_BUG_ON(!(sub) || !(sub)->runtime)
 
+/* loop over all PCM substreams */
+#define for_each_pcm_substream(pcm, str, subs) \
+       for ((str) = 0; (str) < 2; (str)++) \
+               for ((subs) = (pcm)->streams[str].substream; (subs); \
+                    (subs) = (subs)->next)
+
 #endif /* __SOUND_CORE_PCM_LOCAL_H */
index ee6e9c5..289dd1f 100644 (file)
@@ -111,9 +111,8 @@ void snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm)
        struct snd_pcm_substream *substream;
        int stream;
 
-       for (stream = 0; stream < 2; stream++)
-               for (substream = pcm->streams[stream].substream; substream; substream = substream->next)
-                       snd_pcm_lib_preallocate_free(substream);
+       for_each_pcm_substream(pcm, stream, substream)
+               snd_pcm_lib_preallocate_free(substream);
 }
 EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
 
@@ -246,11 +245,8 @@ static void preallocate_pages_for_all(struct snd_pcm *pcm, int type,
        struct snd_pcm_substream *substream;
        int stream;
 
-       for (stream = 0; stream < 2; stream++)
-               for (substream = pcm->streams[stream].substream; substream;
-                    substream = substream->next)
-                       preallocate_pages(substream, type, data, size, max,
-                                         managed);
+       for_each_pcm_substream(pcm, stream, substream)
+               preallocate_pages(substream, type, data, size, max, managed);
 }
 
 /**
index 0ae2475..17a85f4 100644 (file)
@@ -1674,30 +1674,25 @@ int snd_pcm_suspend_all(struct snd_pcm *pcm)
        if (! pcm)
                return 0;
 
-       for (stream = 0; stream < 2; stream++) {
-               for (substream = pcm->streams[stream].substream;
-                    substream; substream = substream->next) {
-                       /* FIXME: the open/close code should lock this as well */
-                       if (substream->runtime == NULL)
-                               continue;
+       for_each_pcm_substream(pcm, stream, substream) {
+               /* FIXME: the open/close code should lock this as well */
+               if (!substream->runtime)
+                       continue;
 
-                       /*
-                        * Skip BE dai link PCM's that are internal and may
-                        * not have their substream ops set.
-                        */
-                       if (!substream->ops)
-                               continue;
+               /*
+                * Skip BE dai link PCM's that are internal and may
+                * not have their substream ops set.
+                */
+               if (!substream->ops)
+                       continue;
 
-                       err = snd_pcm_suspend(substream);
-                       if (err < 0 && err != -EBUSY)
-                               return err;
-               }
+               err = snd_pcm_suspend(substream);
+               if (err < 0 && err != -EBUSY)
+                       return err;
        }
 
-       for (stream = 0; stream < 2; stream++)
-               for (substream = pcm->streams[stream].substream;
-                    substream; substream = substream->next)
-                       snd_pcm_sync_stop(substream, false);
+       for_each_pcm_substream(pcm, stream, substream)
+               snd_pcm_sync_stop(substream, false);
 
        return 0;
 }