1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ALSA sequencer Priority Queue
7 #include <linux/time.h>
8 #include <linux/slab.h>
9 #include <sound/core.h>
10 #include "seq_timer.h"
11 #include "seq_prioq.h"
14 /* Implementation is a simple linked list for now...
16 This priority queue orders the events on timestamp. For events with an
17 equeal timestamp the queue behaves as a FIFO.
41 /* create new prioq (constructor) */
42 struct snd_seq_prioq *snd_seq_prioq_new(void)
44 struct snd_seq_prioq *f;
46 f = kzalloc(sizeof(*f), GFP_KERNEL);
50 spin_lock_init(&f->lock);
58 /* delete prioq (destructor) */
59 void snd_seq_prioq_delete(struct snd_seq_prioq **fifo)
61 struct snd_seq_prioq *f = *fifo;
65 pr_debug("ALSA: seq: snd_seq_prioq_delete() called with NULL prioq\n");
69 /* release resources...*/
70 /*....................*/
75 snd_seq_cell_free(snd_seq_prioq_cell_out(f, NULL));
84 /* compare timestamp between events */
85 /* return 1 if a >= b; 0 */
86 static inline int compare_timestamp(struct snd_seq_event *a,
87 struct snd_seq_event *b)
89 if ((a->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK) {
91 return (snd_seq_compare_tick_time(&a->time.tick, &b->time.tick));
93 /* compare real time */
94 return (snd_seq_compare_real_time(&a->time.time, &b->time.time));
98 /* compare timestamp between events */
99 /* return negative if a < b;
103 static inline int compare_timestamp_rel(struct snd_seq_event *a,
104 struct snd_seq_event *b)
106 if ((a->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK) {
108 if (a->time.tick > b->time.tick)
110 else if (a->time.tick == b->time.tick)
115 /* compare real time */
116 if (a->time.time.tv_sec > b->time.time.tv_sec)
118 else if (a->time.time.tv_sec == b->time.time.tv_sec) {
119 if (a->time.time.tv_nsec > b->time.time.tv_nsec)
121 else if (a->time.time.tv_nsec == b->time.time.tv_nsec)
130 /* enqueue cell to prioq */
131 int snd_seq_prioq_cell_in(struct snd_seq_prioq * f,
132 struct snd_seq_event_cell * cell)
134 struct snd_seq_event_cell *cur, *prev;
139 if (snd_BUG_ON(!f || !cell))
143 prior = (cell->event.flags & SNDRV_SEQ_PRIORITY_MASK);
145 spin_lock_irqsave(&f->lock, flags);
147 /* check if this element needs to inserted at the end (ie. ordered
148 data is inserted) This will be very likeley if a sequencer
149 application or midi file player is feeding us (sequential) data */
150 if (f->tail && !prior) {
151 if (compare_timestamp(&cell->event, &f->tail->event)) {
152 /* add new cell to tail of the fifo */
153 f->tail->next = cell;
157 spin_unlock_irqrestore(&f->lock, flags);
161 /* traverse list of elements to find the place where the new cell is
162 to be inserted... Note that this is a order n process ! */
164 prev = NULL; /* previous cell */
165 cur = f->head; /* cursor */
167 count = 10000; /* FIXME: enough big, isn't it? */
168 while (cur != NULL) {
169 /* compare timestamps */
170 int rel = compare_timestamp_rel(&cell->event, &cur->event);
172 /* new cell has earlier schedule time, */
174 else if (rel == 0 && prior)
175 /* equal schedule time and prior to others */
177 /* new cell has equal or larger schedule time, */
178 /* move cursor to next cell */
182 spin_unlock_irqrestore(&f->lock, flags);
183 pr_err("ALSA: seq: cannot find a pointer.. infinite loop?\n");
188 /* insert it before cursor */
193 if (f->head == cur) /* this is the first cell, set head to it */
195 if (cur == NULL) /* reached end of the list */
198 spin_unlock_irqrestore(&f->lock, flags);
202 /* return 1 if the current time >= event timestamp */
203 static int event_is_ready(struct snd_seq_event *ev, void *current_time)
205 if ((ev->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK)
206 return snd_seq_compare_tick_time(current_time, &ev->time.tick);
208 return snd_seq_compare_real_time(current_time, &ev->time.time);
211 /* dequeue cell from prioq */
212 struct snd_seq_event_cell *snd_seq_prioq_cell_out(struct snd_seq_prioq *f,
215 struct snd_seq_event_cell *cell;
219 pr_debug("ALSA: seq: snd_seq_prioq_cell_in() called with NULL prioq\n");
222 spin_lock_irqsave(&f->lock, flags);
225 if (cell && current_time && !event_is_ready(&cell->event, current_time))
228 f->head = cell->next;
230 /* reset tail if this was the last element */
238 spin_unlock_irqrestore(&f->lock, flags);
242 /* return number of events available in prioq */
243 int snd_seq_prioq_avail(struct snd_seq_prioq * f)
246 pr_debug("ALSA: seq: snd_seq_prioq_cell_in() called with NULL prioq\n");
252 static inline int prioq_match(struct snd_seq_event_cell *cell,
253 int client, int timestamp)
255 if (cell->event.source.client == client ||
256 cell->event.dest.client == client)
260 switch (cell->event.flags & SNDRV_SEQ_TIME_STAMP_MASK) {
261 case SNDRV_SEQ_TIME_STAMP_TICK:
262 if (cell->event.time.tick)
265 case SNDRV_SEQ_TIME_STAMP_REAL:
266 if (cell->event.time.time.tv_sec ||
267 cell->event.time.time.tv_nsec)
274 /* remove cells for left client */
275 void snd_seq_prioq_leave(struct snd_seq_prioq * f, int client, int timestamp)
277 register struct snd_seq_event_cell *cell, *next;
279 struct snd_seq_event_cell *prev = NULL;
280 struct snd_seq_event_cell *freefirst = NULL, *freeprev = NULL, *freenext;
282 /* collect all removed cells */
283 spin_lock_irqsave(&f->lock, flags);
287 if (prioq_match(cell, client, timestamp)) {
288 /* remove cell from prioq */
289 if (cell == f->head) {
290 f->head = cell->next;
292 prev->next = cell->next;
295 f->tail = cell->next;
297 /* add cell to free list */
299 if (freefirst == NULL) {
302 freeprev->next = cell;
307 pr_debug("ALSA: seq: type = %i, source = %i, dest = %i, "
310 cell->event.source.client,
311 cell->event.dest.client,
318 spin_unlock_irqrestore(&f->lock, flags);
320 /* remove selected cells */
322 freenext = freefirst->next;
323 snd_seq_cell_free(freefirst);
324 freefirst = freenext;
328 static int prioq_remove_match(struct snd_seq_remove_events *info,
329 struct snd_seq_event *ev)
333 if (info->remove_mode & SNDRV_SEQ_REMOVE_DEST) {
334 if (ev->dest.client != info->dest.client ||
335 ev->dest.port != info->dest.port)
338 if (info->remove_mode & SNDRV_SEQ_REMOVE_DEST_CHANNEL) {
339 if (! snd_seq_ev_is_channel_type(ev))
341 /* data.note.channel and data.control.channel are identical */
342 if (ev->data.note.channel != info->channel)
345 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_AFTER) {
346 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_TICK)
347 res = snd_seq_compare_tick_time(&ev->time.tick, &info->time.tick);
349 res = snd_seq_compare_real_time(&ev->time.time, &info->time.time);
353 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_BEFORE) {
354 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_TICK)
355 res = snd_seq_compare_tick_time(&ev->time.tick, &info->time.tick);
357 res = snd_seq_compare_real_time(&ev->time.time, &info->time.time);
361 if (info->remove_mode & SNDRV_SEQ_REMOVE_EVENT_TYPE) {
362 if (ev->type != info->type)
365 if (info->remove_mode & SNDRV_SEQ_REMOVE_IGNORE_OFF) {
366 /* Do not remove off events */
368 case SNDRV_SEQ_EVENT_NOTEOFF:
369 /* case SNDRV_SEQ_EVENT_SAMPLE_STOP: */
375 if (info->remove_mode & SNDRV_SEQ_REMOVE_TAG_MATCH) {
376 if (info->tag != ev->tag)
383 /* remove cells matching remove criteria */
384 void snd_seq_prioq_remove_events(struct snd_seq_prioq * f, int client,
385 struct snd_seq_remove_events *info)
387 struct snd_seq_event_cell *cell, *next;
389 struct snd_seq_event_cell *prev = NULL;
390 struct snd_seq_event_cell *freefirst = NULL, *freeprev = NULL, *freenext;
392 /* collect all removed cells */
393 spin_lock_irqsave(&f->lock, flags);
398 if (cell->event.source.client == client &&
399 prioq_remove_match(info, &cell->event)) {
401 /* remove cell from prioq */
402 if (cell == f->head) {
403 f->head = cell->next;
405 prev->next = cell->next;
409 f->tail = cell->next;
412 /* add cell to free list */
414 if (freefirst == NULL) {
417 freeprev->next = cell;
426 spin_unlock_irqrestore(&f->lock, flags);
428 /* remove selected cells */
430 freenext = freefirst->next;
431 snd_seq_cell_free(freefirst);
432 freefirst = freenext;