seq_prioq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * ALSA sequencer Priority Queue
  3. * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
  4. *
  5. *
  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.
  10. *
  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.
  15. *
  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
  19. *
  20. */
  21. #include <sound/driver.h>
  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"
  27. /* Implementation is a simple linked list for now...
  28. This priority queue orders the events on timestamp. For events with an
  29. equeal timestamp the queue behaves as a FIFO.
  30. *
  31. * +-------+
  32. * Head --> | first |
  33. * +-------+
  34. * |next
  35. * +-----v-+
  36. * | |
  37. * +-------+
  38. * |
  39. * +-----v-+
  40. * | |
  41. * +-------+
  42. * |
  43. * +-----v-+
  44. * Tail --> | last |
  45. * +-------+
  46. *
  47. */
  48. /* create new prioq (constructor) */
  49. prioq_t *snd_seq_prioq_new(void)
  50. {
  51. prioq_t *f;
  52. f = kcalloc(1, sizeof(*f), GFP_KERNEL);
  53. if (f == NULL) {
  54. snd_printd("oops: malloc failed for snd_seq_prioq_new()\n");
  55. return NULL;
  56. }
  57. spin_lock_init(&f->lock);
  58. f->head = NULL;
  59. f->tail = NULL;
  60. f->cells = 0;
  61. return f;
  62. }
  63. /* delete prioq (destructor) */
  64. void snd_seq_prioq_delete(prioq_t **fifo)
  65. {
  66. prioq_t *f = *fifo;
  67. *fifo = NULL;
  68. if (f == NULL) {
  69. snd_printd("oops: snd_seq_prioq_delete() called with NULL prioq\n");
  70. return;
  71. }
  72. /* release resources...*/
  73. /*....................*/
  74. if (f->cells > 0) {
  75. /* drain prioQ */
  76. while (f->cells > 0)
  77. snd_seq_cell_free(snd_seq_prioq_cell_out(f));
  78. }
  79. kfree(f);
  80. }
  81. /* compare timestamp between events */
  82. /* return 1 if a >= b; 0 */
  83. static inline int compare_timestamp(snd_seq_event_t * a, snd_seq_event_t * b)
  84. {
  85. if ((a->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK) {
  86. /* compare ticks */
  87. return (snd_seq_compare_tick_time(&a->time.tick, &b->time.tick));
  88. } else {
  89. /* compare real time */
  90. return (snd_seq_compare_real_time(&a->time.time, &b->time.time));
  91. }
  92. }
  93. /* compare timestamp between events */
  94. /* return negative if a < b;
  95. * zero if a = b;
  96. * positive if a > b;
  97. */
  98. static inline int compare_timestamp_rel(snd_seq_event_t *a, snd_seq_event_t *b)
  99. {
  100. if ((a->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK) {
  101. /* compare ticks */
  102. if (a->time.tick > b->time.tick)
  103. return 1;
  104. else if (a->time.tick == b->time.tick)
  105. return 0;
  106. else
  107. return -1;
  108. } else {
  109. /* compare real time */
  110. if (a->time.time.tv_sec > b->time.time.tv_sec)
  111. return 1;
  112. else if (a->time.time.tv_sec == b->time.time.tv_sec) {
  113. if (a->time.time.tv_nsec > b->time.time.tv_nsec)
  114. return 1;
  115. else if (a->time.time.tv_nsec == b->time.time.tv_nsec)
  116. return 0;
  117. else
  118. return -1;
  119. } else
  120. return -1;
  121. }
  122. }
  123. /* enqueue cell to prioq */
  124. int snd_seq_prioq_cell_in(prioq_t * f, snd_seq_event_cell_t * cell)
  125. {
  126. snd_seq_event_cell_t *cur, *prev;
  127. unsigned long flags;
  128. int count;
  129. int prior;
  130. snd_assert(f, return -EINVAL);
  131. snd_assert(cell, return -EINVAL);
  132. /* check flags */
  133. prior = (cell->event.flags & SNDRV_SEQ_PRIORITY_MASK);
  134. spin_lock_irqsave(&f->lock, flags);
  135. /* check if this element needs to inserted at the end (ie. ordered
  136. data is inserted) This will be very likeley if a sequencer
  137. application or midi file player is feeding us (sequential) data */
  138. if (f->tail && !prior) {
  139. if (compare_timestamp(&cell->event, &f->tail->event)) {
  140. /* add new cell to tail of the fifo */
  141. f->tail->next = cell;
  142. f->tail = cell;
  143. cell->next = NULL;
  144. f->cells++;
  145. spin_unlock_irqrestore(&f->lock, flags);
  146. return 0;
  147. }
  148. }
  149. /* traverse list of elements to find the place where the new cell is
  150. to be inserted... Note that this is a order n process ! */
  151. prev = NULL; /* previous cell */
  152. cur = f->head; /* cursor */
  153. count = 10000; /* FIXME: enough big, isn't it? */
  154. while (cur != NULL) {
  155. /* compare timestamps */
  156. int rel = compare_timestamp_rel(&cell->event, &cur->event);
  157. if (rel < 0)
  158. /* new cell has earlier schedule time, */
  159. break;
  160. else if (rel == 0 && prior)
  161. /* equal schedule time and prior to others */
  162. break;
  163. /* new cell has equal or larger schedule time, */
  164. /* move cursor to next cell */
  165. prev = cur;
  166. cur = cur->next;
  167. if (! --count) {
  168. spin_unlock_irqrestore(&f->lock, flags);
  169. snd_printk(KERN_ERR "cannot find a pointer.. infinite loop?\n");
  170. return -EINVAL;
  171. }
  172. }
  173. /* insert it before cursor */
  174. if (prev != NULL)
  175. prev->next = cell;
  176. cell->next = cur;
  177. if (f->head == cur) /* this is the first cell, set head to it */
  178. f->head = cell;
  179. if (cur == NULL) /* reached end of the list */
  180. f->tail = cell;
  181. f->cells++;
  182. spin_unlock_irqrestore(&f->lock, flags);
  183. return 0;
  184. }
  185. /* dequeue cell from prioq */
  186. snd_seq_event_cell_t *snd_seq_prioq_cell_out(prioq_t * f)
  187. {
  188. snd_seq_event_cell_t *cell;
  189. unsigned long flags;
  190. if (f == NULL) {
  191. snd_printd("oops: snd_seq_prioq_cell_in() called with NULL prioq\n");
  192. return NULL;
  193. }
  194. spin_lock_irqsave(&f->lock, flags);
  195. cell = f->head;
  196. if (cell) {
  197. f->head = cell->next;
  198. /* reset tail if this was the last element */
  199. if (f->tail == cell)
  200. f->tail = NULL;
  201. cell->next = NULL;
  202. f->cells--;
  203. }
  204. spin_unlock_irqrestore(&f->lock, flags);
  205. return cell;
  206. }
  207. /* return number of events available in prioq */
  208. int snd_seq_prioq_avail(prioq_t * f)
  209. {
  210. if (f == NULL) {
  211. snd_printd("oops: snd_seq_prioq_cell_in() called with NULL prioq\n");
  212. return 0;
  213. }
  214. return f->cells;
  215. }
  216. /* peek at cell at the head of the prioq */
  217. snd_seq_event_cell_t *snd_seq_prioq_cell_peek(prioq_t * f)
  218. {
  219. if (f == NULL) {
  220. snd_printd("oops: snd_seq_prioq_cell_in() called with NULL prioq\n");
  221. return NULL;
  222. }
  223. return f->head;
  224. }
  225. static inline int prioq_match(snd_seq_event_cell_t *cell, int client, int timestamp)
  226. {
  227. if (cell->event.source.client == client ||
  228. cell->event.dest.client == client)
  229. return 1;
  230. if (!timestamp)
  231. return 0;
  232. switch (cell->event.flags & SNDRV_SEQ_TIME_STAMP_MASK) {
  233. case SNDRV_SEQ_TIME_STAMP_TICK:
  234. if (cell->event.time.tick)
  235. return 1;
  236. break;
  237. case SNDRV_SEQ_TIME_STAMP_REAL:
  238. if (cell->event.time.time.tv_sec ||
  239. cell->event.time.time.tv_nsec)
  240. return 1;
  241. break;
  242. }
  243. return 0;
  244. }
  245. /* remove cells for left client */
  246. void snd_seq_prioq_leave(prioq_t * f, int client, int timestamp)
  247. {
  248. register snd_seq_event_cell_t *cell, *next;
  249. unsigned long flags;
  250. snd_seq_event_cell_t *prev = NULL;
  251. snd_seq_event_cell_t *freefirst = NULL, *freeprev = NULL, *freenext;
  252. /* collect all removed cells */
  253. spin_lock_irqsave(&f->lock, flags);
  254. cell = f->head;
  255. while (cell) {
  256. next = cell->next;
  257. if (prioq_match(cell, client, timestamp)) {
  258. /* remove cell from prioq */
  259. if (cell == f->head) {
  260. f->head = cell->next;
  261. } else {
  262. prev->next = cell->next;
  263. }
  264. if (cell == f->tail)
  265. f->tail = cell->next;
  266. f->cells--;
  267. /* add cell to free list */
  268. cell->next = NULL;
  269. if (freefirst == NULL) {
  270. freefirst = cell;
  271. } else {
  272. freeprev->next = cell;
  273. }
  274. freeprev = cell;
  275. } else {
  276. #if 0
  277. printk("type = %i, source = %i, dest = %i, client = %i\n",
  278. cell->event.type,
  279. cell->event.source.client,
  280. cell->event.dest.client,
  281. client);
  282. #endif
  283. prev = cell;
  284. }
  285. cell = next;
  286. }
  287. spin_unlock_irqrestore(&f->lock, flags);
  288. /* remove selected cells */
  289. while (freefirst) {
  290. freenext = freefirst->next;
  291. snd_seq_cell_free(freefirst);
  292. freefirst = freenext;
  293. }
  294. }
  295. static int prioq_remove_match(snd_seq_remove_events_t *info,
  296. snd_seq_event_t *ev)
  297. {
  298. int res;
  299. if (info->remove_mode & SNDRV_SEQ_REMOVE_DEST) {
  300. if (ev->dest.client != info->dest.client ||
  301. ev->dest.port != info->dest.port)
  302. return 0;
  303. }
  304. if (info->remove_mode & SNDRV_SEQ_REMOVE_DEST_CHANNEL) {
  305. if (! snd_seq_ev_is_channel_type(ev))
  306. return 0;
  307. /* data.note.channel and data.control.channel are identical */
  308. if (ev->data.note.channel != info->channel)
  309. return 0;
  310. }
  311. if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_AFTER) {
  312. if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_TICK)
  313. res = snd_seq_compare_tick_time(&ev->time.tick, &info->time.tick);
  314. else
  315. res = snd_seq_compare_real_time(&ev->time.time, &info->time.time);
  316. if (!res)
  317. return 0;
  318. }
  319. if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_BEFORE) {
  320. if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_TICK)
  321. res = snd_seq_compare_tick_time(&ev->time.tick, &info->time.tick);
  322. else
  323. res = snd_seq_compare_real_time(&ev->time.time, &info->time.time);
  324. if (res)
  325. return 0;
  326. }
  327. if (info->remove_mode & SNDRV_SEQ_REMOVE_EVENT_TYPE) {
  328. if (ev->type != info->type)
  329. return 0;
  330. }
  331. if (info->remove_mode & SNDRV_SEQ_REMOVE_IGNORE_OFF) {
  332. /* Do not remove off events */
  333. switch (ev->type) {
  334. case SNDRV_SEQ_EVENT_NOTEOFF:
  335. /* case SNDRV_SEQ_EVENT_SAMPLE_STOP: */
  336. return 0;
  337. default:
  338. break;
  339. }
  340. }
  341. if (info->remove_mode & SNDRV_SEQ_REMOVE_TAG_MATCH) {
  342. if (info->tag != ev->tag)
  343. return 0;
  344. }
  345. return 1;
  346. }
  347. /* remove cells matching remove criteria */
  348. void snd_seq_prioq_remove_events(prioq_t * f, int client,
  349. snd_seq_remove_events_t *info)
  350. {
  351. register snd_seq_event_cell_t *cell, *next;
  352. unsigned long flags;
  353. snd_seq_event_cell_t *prev = NULL;
  354. snd_seq_event_cell_t *freefirst = NULL, *freeprev = NULL, *freenext;
  355. /* collect all removed cells */
  356. spin_lock_irqsave(&f->lock, flags);
  357. cell = f->head;
  358. while (cell) {
  359. next = cell->next;
  360. if (cell->event.source.client == client &&
  361. prioq_remove_match(info, &cell->event)) {
  362. /* remove cell from prioq */
  363. if (cell == f->head) {
  364. f->head = cell->next;
  365. } else {
  366. prev->next = cell->next;
  367. }
  368. if (cell == f->tail)
  369. f->tail = cell->next;
  370. f->cells--;
  371. /* add cell to free list */
  372. cell->next = NULL;
  373. if (freefirst == NULL) {
  374. freefirst = cell;
  375. } else {
  376. freeprev->next = cell;
  377. }
  378. freeprev = cell;
  379. } else {
  380. prev = cell;
  381. }
  382. cell = next;
  383. }
  384. spin_unlock_irqrestore(&f->lock, flags);
  385. /* remove selected cells */
  386. while (freefirst) {
  387. freenext = freefirst->next;
  388. snd_seq_cell_free(freefirst);
  389. freefirst = freenext;
  390. }
  391. }