2 * ALSA sequencer Priority Queue
3 * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/time.h>
23 #include <linux/slab.h>
24 #include <sound/core.h>
25 #include "seq_timer.h"
26 #include "seq_prioq.h"
29 /* Implementation is a simple linked list for now...
31 This priority queue orders the events on timestamp. For events with an
32 equeal timestamp the queue behaves as a FIFO.
56 /* create new prioq (constructor) */
57 struct snd_seq_prioq *snd_seq_prioq_new(void)
59 struct snd_seq_prioq *f;
61 f = kzalloc(sizeof(*f), GFP_KERNEL);
63 snd_printd("oops: malloc failed for snd_seq_prioq_new()\n");
67 spin_lock_init(&f->lock);
75 /* delete prioq (destructor) */
76 void snd_seq_prioq_delete(struct snd_seq_prioq **fifo)
78 struct snd_seq_prioq *f = *fifo;
82 snd_printd("oops: snd_seq_prioq_delete() called with NULL prioq\n");
86 /* release resources...*/
87 /*....................*/
92 snd_seq_cell_free(snd_seq_prioq_cell_out(f));
101 /* compare timestamp between events */
102 /* return 1 if a >= b; 0 */
103 static inline int compare_timestamp(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 return (snd_seq_compare_tick_time(&a->time.tick, &b->time.tick));
110 /* compare real time */
111 return (snd_seq_compare_real_time(&a->time.time, &b->time.time));
115 /* compare timestamp between events */
116 /* return negative if a < b;
120 static inline int compare_timestamp_rel(struct snd_seq_event *a,
121 struct snd_seq_event *b)
123 if ((a->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK) {
125 if (a->time.tick > b->time.tick)
127 else if (a->time.tick == b->time.tick)
132 /* compare real time */
133 if (a->time.time.tv_sec > b->time.time.tv_sec)
135 else if (a->time.time.tv_sec == b->time.time.tv_sec) {
136 if (a->time.time.tv_nsec > b->time.time.tv_nsec)
138 else if (a->time.time.tv_nsec == b->time.time.tv_nsec)
147 /* enqueue cell to prioq */
148 int snd_seq_prioq_cell_in(struct snd_seq_prioq * f,
149 struct snd_seq_event_cell * cell)
151 struct snd_seq_event_cell *cur, *prev;
156 snd_assert(f, return -EINVAL);
157 snd_assert(cell, return -EINVAL);
160 prior = (cell->event.flags & SNDRV_SEQ_PRIORITY_MASK);
162 spin_lock_irqsave(&f->lock, flags);
164 /* check if this element needs to inserted at the end (ie. ordered
165 data is inserted) This will be very likeley if a sequencer
166 application or midi file player is feeding us (sequential) data */
167 if (f->tail && !prior) {
168 if (compare_timestamp(&cell->event, &f->tail->event)) {
169 /* add new cell to tail of the fifo */
170 f->tail->next = cell;
174 spin_unlock_irqrestore(&f->lock, flags);
178 /* traverse list of elements to find the place where the new cell is
179 to be inserted... Note that this is a order n process ! */
181 prev = NULL; /* previous cell */
182 cur = f->head; /* cursor */
184 count = 10000; /* FIXME: enough big, isn't it? */
185 while (cur != NULL) {
186 /* compare timestamps */
187 int rel = compare_timestamp_rel(&cell->event, &cur->event);
189 /* new cell has earlier schedule time, */
191 else if (rel == 0 && prior)
192 /* equal schedule time and prior to others */
194 /* new cell has equal or larger schedule time, */
195 /* move cursor to next cell */
199 spin_unlock_irqrestore(&f->lock, flags);
200 snd_printk(KERN_ERR "cannot find a pointer.. infinite loop?\n");
205 /* insert it before cursor */
210 if (f->head == cur) /* this is the first cell, set head to it */
212 if (cur == NULL) /* reached end of the list */
215 spin_unlock_irqrestore(&f->lock, flags);
219 /* dequeue cell from prioq */
220 struct snd_seq_event_cell *snd_seq_prioq_cell_out(struct snd_seq_prioq *f)
222 struct snd_seq_event_cell *cell;
226 snd_printd("oops: snd_seq_prioq_cell_in() called with NULL prioq\n");
229 spin_lock_irqsave(&f->lock, flags);
233 f->head = cell->next;
235 /* reset tail if this was the last element */
243 spin_unlock_irqrestore(&f->lock, flags);
247 /* return number of events available in prioq */
248 int snd_seq_prioq_avail(struct snd_seq_prioq * f)
251 snd_printd("oops: snd_seq_prioq_cell_in() called with NULL prioq\n");
258 /* peek at cell at the head of the prioq */
259 struct snd_seq_event_cell *snd_seq_prioq_cell_peek(struct snd_seq_prioq * f)
262 snd_printd("oops: snd_seq_prioq_cell_in() called with NULL prioq\n");
269 static inline int prioq_match(struct snd_seq_event_cell *cell,
270 int client, int timestamp)
272 if (cell->event.source.client == client ||
273 cell->event.dest.client == client)
277 switch (cell->event.flags & SNDRV_SEQ_TIME_STAMP_MASK) {
278 case SNDRV_SEQ_TIME_STAMP_TICK:
279 if (cell->event.time.tick)
282 case SNDRV_SEQ_TIME_STAMP_REAL:
283 if (cell->event.time.time.tv_sec ||
284 cell->event.time.time.tv_nsec)
291 /* remove cells for left client */
292 void snd_seq_prioq_leave(struct snd_seq_prioq * f, int client, int timestamp)
294 register struct snd_seq_event_cell *cell, *next;
296 struct snd_seq_event_cell *prev = NULL;
297 struct snd_seq_event_cell *freefirst = NULL, *freeprev = NULL, *freenext;
299 /* collect all removed cells */
300 spin_lock_irqsave(&f->lock, flags);
304 if (prioq_match(cell, client, timestamp)) {
305 /* remove cell from prioq */
306 if (cell == f->head) {
307 f->head = cell->next;
309 prev->next = cell->next;
312 f->tail = cell->next;
314 /* add cell to free list */
316 if (freefirst == NULL) {
319 freeprev->next = cell;
324 printk("type = %i, source = %i, dest = %i, client = %i\n",
326 cell->event.source.client,
327 cell->event.dest.client,
334 spin_unlock_irqrestore(&f->lock, flags);
336 /* remove selected cells */
338 freenext = freefirst->next;
339 snd_seq_cell_free(freefirst);
340 freefirst = freenext;
344 static int prioq_remove_match(struct snd_seq_remove_events *info,
345 struct snd_seq_event *ev)
349 if (info->remove_mode & SNDRV_SEQ_REMOVE_DEST) {
350 if (ev->dest.client != info->dest.client ||
351 ev->dest.port != info->dest.port)
354 if (info->remove_mode & SNDRV_SEQ_REMOVE_DEST_CHANNEL) {
355 if (! snd_seq_ev_is_channel_type(ev))
357 /* data.note.channel and data.control.channel are identical */
358 if (ev->data.note.channel != info->channel)
361 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_AFTER) {
362 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_TICK)
363 res = snd_seq_compare_tick_time(&ev->time.tick, &info->time.tick);
365 res = snd_seq_compare_real_time(&ev->time.time, &info->time.time);
369 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_BEFORE) {
370 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_TICK)
371 res = snd_seq_compare_tick_time(&ev->time.tick, &info->time.tick);
373 res = snd_seq_compare_real_time(&ev->time.time, &info->time.time);
377 if (info->remove_mode & SNDRV_SEQ_REMOVE_EVENT_TYPE) {
378 if (ev->type != info->type)
381 if (info->remove_mode & SNDRV_SEQ_REMOVE_IGNORE_OFF) {
382 /* Do not remove off events */
384 case SNDRV_SEQ_EVENT_NOTEOFF:
385 /* case SNDRV_SEQ_EVENT_SAMPLE_STOP: */
391 if (info->remove_mode & SNDRV_SEQ_REMOVE_TAG_MATCH) {
392 if (info->tag != ev->tag)
399 /* remove cells matching remove criteria */
400 void snd_seq_prioq_remove_events(struct snd_seq_prioq * f, int client,
401 struct snd_seq_remove_events *info)
403 struct snd_seq_event_cell *cell, *next;
405 struct snd_seq_event_cell *prev = NULL;
406 struct snd_seq_event_cell *freefirst = NULL, *freeprev = NULL, *freenext;
408 /* collect all removed cells */
409 spin_lock_irqsave(&f->lock, flags);
414 if (cell->event.source.client == client &&
415 prioq_remove_match(info, &cell->event)) {
417 /* remove cell from prioq */
418 if (cell == f->head) {
419 f->head = cell->next;
421 prev->next = cell->next;
425 f->tail = cell->next;
428 /* add cell to free list */
430 if (freefirst == NULL) {
433 freeprev->next = cell;
442 spin_unlock_irqrestore(&f->lock, flags);
444 /* remove selected cells */
446 freenext = freefirst->next;
447 snd_seq_cell_free(freefirst);
448 freefirst = freenext;