seq_prioq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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)
  53. return NULL;
  54. spin_lock_init(&f->lock);
  55. f->head = NULL;
  56. f->tail = NULL;
  57. f->cells = 0;
  58. return f;
  59. }
  60. /* delete prioq (destructor) */
  61. void snd_seq_prioq_delete(struct snd_seq_prioq **fifo)
  62. {
  63. struct snd_seq_prioq *f = *fifo;
  64. *fifo = NULL;
  65. if (f == NULL) {
  66. pr_debug("ALSA: seq: snd_seq_prioq_delete() called with NULL prioq\n");
  67. return;
  68. }
  69. /* release resources...*/
  70. /*....................*/
  71. if (f->cells > 0) {
  72. /* drain prioQ */
  73. while (f->cells > 0)
  74. snd_seq_cell_free(snd_seq_prioq_cell_out(f, NULL));
  75. }
  76. kfree(f);
  77. }
  78. /* compare timestamp between events */
  79. /* return 1 if a >= b; 0 */
  80. static inline int compare_timestamp(struct snd_seq_event *a,
  81. struct snd_seq_event *b)
  82. {
  83. if ((a->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK) {
  84. /* compare ticks */
  85. return (snd_seq_compare_tick_time(&a->time.tick, &b->time.tick));
  86. } else {
  87. /* compare real time */
  88. return (snd_seq_compare_real_time(&a->time.time, &b->time.time));
  89. }
  90. }
  91. /* compare timestamp between events */
  92. /* return negative if a < b;
  93. * zero if a = b;
  94. * positive if a > b;
  95. */
  96. static inline int compare_timestamp_rel(struct snd_seq_event *a,
  97. struct snd_seq_event *b)
  98. {
  99. if ((a->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK) {
  100. /* compare ticks */
  101. if (a->time.tick > b->time.tick)
  102. return 1;
  103. else if (a->time.tick == b->time.tick)
  104. return 0;
  105. else
  106. return -1;
  107. } else {
  108. /* compare real time */
  109. if (a->time.time.tv_sec > b->time.time.tv_sec)
  110. return 1;
  111. else if (a->time.time.tv_sec == b->time.time.tv_sec) {
  112. if (a->time.time.tv_nsec > b->time.time.tv_nsec)
  113. return 1;
  114. else if (a->time.time.tv_nsec == b->time.time.tv_nsec)
  115. return 0;
  116. else
  117. return -1;
  118. } else
  119. return -1;
  120. }
  121. }
  122. /* enqueue cell to prioq */
  123. int snd_seq_prioq_cell_in(struct snd_seq_prioq * f,
  124. struct snd_seq_event_cell * cell)
  125. {
  126. struct snd_seq_event_cell *cur, *prev;
  127. unsigned long flags;
  128. int count;
  129. int prior;
  130. if (snd_BUG_ON(!f || !cell))
  131. 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. pr_err("ALSA: seq: 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. /* return 1 if the current time >= event timestamp */
  186. static int event_is_ready(struct snd_seq_event *ev, void *current_time)
  187. {
  188. if ((ev->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK)
  189. return snd_seq_compare_tick_time(current_time, &ev->time.tick);
  190. else
  191. return snd_seq_compare_real_time(current_time, &ev->time.time);
  192. }
  193. /* dequeue cell from prioq */
  194. struct snd_seq_event_cell *snd_seq_prioq_cell_out(struct snd_seq_prioq *f,
  195. void *current_time)
  196. {
  197. struct snd_seq_event_cell *cell;
  198. unsigned long flags;
  199. if (f == NULL) {
  200. pr_debug("ALSA: seq: snd_seq_prioq_cell_in() called with NULL prioq\n");
  201. return NULL;
  202. }
  203. spin_lock_irqsave(&f->lock, flags);
  204. cell = f->head;
  205. if (cell && current_time && !event_is_ready(&cell->event, current_time))
  206. cell = NULL;
  207. if (cell) {
  208. f->head = cell->next;
  209. /* reset tail if this was the last element */
  210. if (f->tail == cell)
  211. f->tail = NULL;
  212. cell->next = NULL;
  213. f->cells--;
  214. }
  215. spin_unlock_irqrestore(&f->lock, flags);
  216. return cell;
  217. }
  218. /* return number of events available in prioq */
  219. int snd_seq_prioq_avail(struct snd_seq_prioq * f)
  220. {
  221. if (f == NULL) {
  222. pr_debug("ALSA: seq: snd_seq_prioq_cell_in() called with NULL prioq\n");
  223. return 0;
  224. }
  225. return f->cells;
  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. pr_debug("ALSA: seq: 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. }