seq_prioq.c 11 KB

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