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 <sound/driver.h>
23 #include <linux/time.h>
24 #include <linux/slab.h>
25 #include <sound/core.h>
26 #include "seq_timer.h"
27 #include "seq_prioq.h"
30 /* Implementation is a simple linked list for now...
32 This priority queue orders the events on timestamp. For events with an
33 equeal timestamp the queue behaves as a FIFO.
57 /* create new prioq (constructor) */
58 struct snd_seq_prioq
*snd_seq_prioq_new(void)
60 struct snd_seq_prioq
*f
;
62 f
= kzalloc(sizeof(*f
), GFP_KERNEL
);
64 snd_printd("oops: malloc failed for snd_seq_prioq_new()\n");
68 spin_lock_init(&f
->lock
);
76 /* delete prioq (destructor) */
77 void snd_seq_prioq_delete(struct snd_seq_prioq
**fifo
)
79 struct snd_seq_prioq
*f
= *fifo
;
83 snd_printd("oops: snd_seq_prioq_delete() called with NULL prioq\n");
87 /* release resources...*/
88 /*....................*/
93 snd_seq_cell_free(snd_seq_prioq_cell_out(f
));
102 /* compare timestamp between events */
103 /* return 1 if a >= b; 0 */
104 static inline int compare_timestamp(struct snd_seq_event
*a
,
105 struct snd_seq_event
*b
)
107 if ((a
->flags
& SNDRV_SEQ_TIME_STAMP_MASK
) == SNDRV_SEQ_TIME_STAMP_TICK
) {
109 return (snd_seq_compare_tick_time(&a
->time
.tick
, &b
->time
.tick
));
111 /* compare real time */
112 return (snd_seq_compare_real_time(&a
->time
.time
, &b
->time
.time
));
116 /* compare timestamp between events */
117 /* return negative if a < b;
121 static inline int compare_timestamp_rel(struct snd_seq_event
*a
,
122 struct snd_seq_event
*b
)
124 if ((a
->flags
& SNDRV_SEQ_TIME_STAMP_MASK
) == SNDRV_SEQ_TIME_STAMP_TICK
) {
126 if (a
->time
.tick
> b
->time
.tick
)
128 else if (a
->time
.tick
== b
->time
.tick
)
133 /* compare real time */
134 if (a
->time
.time
.tv_sec
> b
->time
.time
.tv_sec
)
136 else if (a
->time
.time
.tv_sec
== b
->time
.time
.tv_sec
) {
137 if (a
->time
.time
.tv_nsec
> b
->time
.time
.tv_nsec
)
139 else if (a
->time
.time
.tv_nsec
== b
->time
.time
.tv_nsec
)
148 /* enqueue cell to prioq */
149 int snd_seq_prioq_cell_in(struct snd_seq_prioq
* f
,
150 struct snd_seq_event_cell
* cell
)
152 struct snd_seq_event_cell
*cur
, *prev
;
157 snd_assert(f
, return -EINVAL
);
158 snd_assert(cell
, return -EINVAL
);
161 prior
= (cell
->event
.flags
& SNDRV_SEQ_PRIORITY_MASK
);
163 spin_lock_irqsave(&f
->lock
, flags
);
165 /* check if this element needs to inserted at the end (ie. ordered
166 data is inserted) This will be very likeley if a sequencer
167 application or midi file player is feeding us (sequential) data */
168 if (f
->tail
&& !prior
) {
169 if (compare_timestamp(&cell
->event
, &f
->tail
->event
)) {
170 /* add new cell to tail of the fifo */
171 f
->tail
->next
= cell
;
175 spin_unlock_irqrestore(&f
->lock
, flags
);
179 /* traverse list of elements to find the place where the new cell is
180 to be inserted... Note that this is a order n process ! */
182 prev
= NULL
; /* previous cell */
183 cur
= f
->head
; /* cursor */
185 count
= 10000; /* FIXME: enough big, isn't it? */
186 while (cur
!= NULL
) {
187 /* compare timestamps */
188 int rel
= compare_timestamp_rel(&cell
->event
, &cur
->event
);
190 /* new cell has earlier schedule time, */
192 else if (rel
== 0 && prior
)
193 /* equal schedule time and prior to others */
195 /* new cell has equal or larger schedule time, */
196 /* move cursor to next cell */
200 spin_unlock_irqrestore(&f
->lock
, flags
);
201 snd_printk(KERN_ERR
"cannot find a pointer.. infinite loop?\n");
206 /* insert it before cursor */
211 if (f
->head
== cur
) /* this is the first cell, set head to it */
213 if (cur
== NULL
) /* reached end of the list */
216 spin_unlock_irqrestore(&f
->lock
, flags
);
220 /* dequeue cell from prioq */
221 struct snd_seq_event_cell
*snd_seq_prioq_cell_out(struct snd_seq_prioq
*f
)
223 struct snd_seq_event_cell
*cell
;
227 snd_printd("oops: snd_seq_prioq_cell_in() called with NULL prioq\n");
230 spin_lock_irqsave(&f
->lock
, flags
);
234 f
->head
= cell
->next
;
236 /* reset tail if this was the last element */
244 spin_unlock_irqrestore(&f
->lock
, flags
);
248 /* return number of events available in prioq */
249 int snd_seq_prioq_avail(struct snd_seq_prioq
* f
)
252 snd_printd("oops: snd_seq_prioq_cell_in() called with NULL prioq\n");
259 /* peek at cell at the head of the prioq */
260 struct snd_seq_event_cell
*snd_seq_prioq_cell_peek(struct snd_seq_prioq
* f
)
263 snd_printd("oops: snd_seq_prioq_cell_in() called with NULL prioq\n");
270 static inline int prioq_match(struct snd_seq_event_cell
*cell
,
271 int client
, int timestamp
)
273 if (cell
->event
.source
.client
== client
||
274 cell
->event
.dest
.client
== client
)
278 switch (cell
->event
.flags
& SNDRV_SEQ_TIME_STAMP_MASK
) {
279 case SNDRV_SEQ_TIME_STAMP_TICK
:
280 if (cell
->event
.time
.tick
)
283 case SNDRV_SEQ_TIME_STAMP_REAL
:
284 if (cell
->event
.time
.time
.tv_sec
||
285 cell
->event
.time
.time
.tv_nsec
)
292 /* remove cells for left client */
293 void snd_seq_prioq_leave(struct snd_seq_prioq
* f
, int client
, int timestamp
)
295 register struct snd_seq_event_cell
*cell
, *next
;
297 struct snd_seq_event_cell
*prev
= NULL
;
298 struct snd_seq_event_cell
*freefirst
= NULL
, *freeprev
= NULL
, *freenext
;
300 /* collect all removed cells */
301 spin_lock_irqsave(&f
->lock
, flags
);
305 if (prioq_match(cell
, client
, timestamp
)) {
306 /* remove cell from prioq */
307 if (cell
== f
->head
) {
308 f
->head
= cell
->next
;
310 prev
->next
= cell
->next
;
313 f
->tail
= cell
->next
;
315 /* add cell to free list */
317 if (freefirst
== NULL
) {
320 freeprev
->next
= cell
;
325 printk("type = %i, source = %i, dest = %i, client = %i\n",
327 cell
->event
.source
.client
,
328 cell
->event
.dest
.client
,
335 spin_unlock_irqrestore(&f
->lock
, flags
);
337 /* remove selected cells */
339 freenext
= freefirst
->next
;
340 snd_seq_cell_free(freefirst
);
341 freefirst
= freenext
;
345 static int prioq_remove_match(struct snd_seq_remove_events
*info
,
346 struct snd_seq_event
*ev
)
350 if (info
->remove_mode
& SNDRV_SEQ_REMOVE_DEST
) {
351 if (ev
->dest
.client
!= info
->dest
.client
||
352 ev
->dest
.port
!= info
->dest
.port
)
355 if (info
->remove_mode
& SNDRV_SEQ_REMOVE_DEST_CHANNEL
) {
356 if (! snd_seq_ev_is_channel_type(ev
))
358 /* data.note.channel and data.control.channel are identical */
359 if (ev
->data
.note
.channel
!= info
->channel
)
362 if (info
->remove_mode
& SNDRV_SEQ_REMOVE_TIME_AFTER
) {
363 if (info
->remove_mode
& SNDRV_SEQ_REMOVE_TIME_TICK
)
364 res
= snd_seq_compare_tick_time(&ev
->time
.tick
, &info
->time
.tick
);
366 res
= snd_seq_compare_real_time(&ev
->time
.time
, &info
->time
.time
);
370 if (info
->remove_mode
& SNDRV_SEQ_REMOVE_TIME_BEFORE
) {
371 if (info
->remove_mode
& SNDRV_SEQ_REMOVE_TIME_TICK
)
372 res
= snd_seq_compare_tick_time(&ev
->time
.tick
, &info
->time
.tick
);
374 res
= snd_seq_compare_real_time(&ev
->time
.time
, &info
->time
.time
);
378 if (info
->remove_mode
& SNDRV_SEQ_REMOVE_EVENT_TYPE
) {
379 if (ev
->type
!= info
->type
)
382 if (info
->remove_mode
& SNDRV_SEQ_REMOVE_IGNORE_OFF
) {
383 /* Do not remove off events */
385 case SNDRV_SEQ_EVENT_NOTEOFF
:
386 /* case SNDRV_SEQ_EVENT_SAMPLE_STOP: */
392 if (info
->remove_mode
& SNDRV_SEQ_REMOVE_TAG_MATCH
) {
393 if (info
->tag
!= ev
->tag
)
400 /* remove cells matching remove criteria */
401 void snd_seq_prioq_remove_events(struct snd_seq_prioq
* f
, int client
,
402 struct snd_seq_remove_events
*info
)
404 struct snd_seq_event_cell
*cell
, *next
;
406 struct snd_seq_event_cell
*prev
= NULL
;
407 struct snd_seq_event_cell
*freefirst
= NULL
, *freeprev
= NULL
, *freenext
;
409 /* collect all removed cells */
410 spin_lock_irqsave(&f
->lock
, flags
);
415 if (cell
->event
.source
.client
== client
&&
416 prioq_remove_match(info
, &cell
->event
)) {
418 /* remove cell from prioq */
419 if (cell
== f
->head
) {
420 f
->head
= cell
->next
;
422 prev
->next
= cell
->next
;
426 f
->tail
= cell
->next
;
429 /* add cell to free list */
431 if (freefirst
== NULL
) {
434 freeprev
->next
= cell
;
443 spin_unlock_irqrestore(&f
->lock
, flags
);
445 /* remove selected cells */
447 freenext
= freefirst
->next
;
448 snd_seq_cell_free(freefirst
);
449 freefirst
= freenext
;