Skip to content

Commit ebfc6de

Browse files
committed
ALSA: timer: Unify master/slave linking code
The code in both snd_timer_check_master() and snd_timer_check_slave() are almost identical, both check whether the master/slave link and does linkage. Factor out the common code and call it from both functions for readability. Signed-off-by: Takashi Iwai <tiwai@suse.de> Link: https://lore.kernel.org/r/20191107192008.32331-2-tiwai@suse.de Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent 66a8966 commit ebfc6de

1 file changed

Lines changed: 33 additions & 33 deletions

File tree

‎sound/core/timer.c‎

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,28 @@ static void snd_timer_request(struct snd_timer_id *tid)
163163

164164
#endif
165165

166+
/* move the slave if it belongs to the master; return 1 if match */
167+
static int check_matching_master_slave(struct snd_timer_instance *master,
168+
struct snd_timer_instance *slave)
169+
{
170+
if (slave->slave_class != master->slave_class ||
171+
slave->slave_id != master->slave_id)
172+
return 0;
173+
if (master->timer->num_instances >= master->timer->max_instances)
174+
return -EBUSY;
175+
list_move_tail(&slave->open_list, &master->slave_list_head);
176+
master->timer->num_instances++;
177+
spin_lock_irq(&slave_active_lock);
178+
spin_lock(&master->timer->lock);
179+
slave->master = master;
180+
slave->timer = master->timer;
181+
if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
182+
list_add_tail(&slave->active_list, &master->slave_active_head);
183+
spin_unlock(&master->timer->lock);
184+
spin_unlock_irq(&slave_active_lock);
185+
return 1;
186+
}
187+
166188
/*
167189
* look for a master instance matching with the slave id of the given slave.
168190
* when found, relink the open_link of the slave.
@@ -173,27 +195,18 @@ static int snd_timer_check_slave(struct snd_timer_instance *slave)
173195
{
174196
struct snd_timer *timer;
175197
struct snd_timer_instance *master;
198+
int err = 0;
176199

177200
/* FIXME: it's really dumb to look up all entries.. */
178201
list_for_each_entry(timer, &snd_timer_list, device_list) {
179202
list_for_each_entry(master, &timer->open_list_head, open_list) {
180-
if (slave->slave_class == master->slave_class &&
181-
slave->slave_id == master->slave_id) {
182-
if (master->timer->num_instances >=
183-
master->timer->max_instances)
184-
return -EBUSY;
185-
list_move_tail(&slave->open_list,
186-
&master->slave_list_head);
187-
master->timer->num_instances++;
188-
spin_lock_irq(&slave_active_lock);
189-
slave->master = master;
190-
slave->timer = master->timer;
191-
spin_unlock_irq(&slave_active_lock);
192-
return 0;
193-
}
203+
err = check_matching_master_slave(master, slave);
204+
if (err != 0) /* match found or error */
205+
goto out;
194206
}
195207
}
196-
return 0;
208+
out:
209+
return err < 0 ? err : 0;
197210
}
198211

199212
/*
@@ -205,28 +218,15 @@ static int snd_timer_check_slave(struct snd_timer_instance *slave)
205218
static int snd_timer_check_master(struct snd_timer_instance *master)
206219
{
207220
struct snd_timer_instance *slave, *tmp;
221+
int err = 0;
208222

209223
/* check all pending slaves */
210224
list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
211-
if (slave->slave_class == master->slave_class &&
212-
slave->slave_id == master->slave_id) {
213-
if (master->timer->num_instances >=
214-
master->timer->max_instances)
215-
return -EBUSY;
216-
list_move_tail(&slave->open_list, &master->slave_list_head);
217-
master->timer->num_instances++;
218-
spin_lock_irq(&slave_active_lock);
219-
spin_lock(&master->timer->lock);
220-
slave->master = master;
221-
slave->timer = master->timer;
222-
if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
223-
list_add_tail(&slave->active_list,
224-
&master->slave_active_head);
225-
spin_unlock(&master->timer->lock);
226-
spin_unlock_irq(&slave_active_lock);
227-
}
225+
err = check_matching_master_slave(master, slave);
226+
if (err < 0)
227+
break;
228228
}
229-
return 0;
229+
return err < 0 ? err : 0;
230230
}
231231

232232
static int snd_timer_close_locked(struct snd_timer_instance *timeri,

0 commit comments

Comments
 (0)