📄 seq_queue.c
字号:
/* * ALSA sequencer Timing queue handling * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * MAJOR CHANGES * Nov. 13, 1999 Takashi Iwai <iwai@ww.uni-erlangen.de> * - Queues are allocated dynamically via ioctl. * - When owner client is deleted, all owned queues are deleted, too. * - Owner of unlocked queue is kept unmodified even if it is * manipulated by other clients. * - Owner field in SET_QUEUE_OWNER ioctl must be identical with the * caller client. i.e. Changing owner to a third client is not * allowed. * * Aug. 30, 2000 Takashi Iwai * - Queues are managed in static array again, but with better way. * The API itself is identical. * - The queue is locked when queue_t pinter is returned via * queueptr(). This pointer *MUST* be released afterward by * queuefree(ptr). * - Addition of experimental sync support. */#include <sound/driver.h>#include <linux/init.h>#include <linux/slab.h>#include <sound/core.h>#include "seq_memory.h"#include "seq_queue.h"#include "seq_clientmgr.h"#include "seq_fifo.h"#include "seq_timer.h"#include "seq_info.h"/* list of allocated queues */static queue_t *queue_list[SNDRV_SEQ_MAX_QUEUES];static DEFINE_SPINLOCK(queue_list_lock);/* number of queues allocated */static int num_queues;int snd_seq_queue_get_cur_queues(void){ return num_queues;}/*----------------------------------------------------------------*//* assign queue id and insert to list */static int queue_list_add(queue_t *q){ int i; unsigned long flags; spin_lock_irqsave(&queue_list_lock, flags); for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) { if (! queue_list[i]) { queue_list[i] = q; q->queue = i; num_queues++; spin_unlock_irqrestore(&queue_list_lock, flags); return i; } } spin_unlock_irqrestore(&queue_list_lock, flags); return -1;}static queue_t *queue_list_remove(int id, int client){ queue_t *q; unsigned long flags; spin_lock_irqsave(&queue_list_lock, flags); q = queue_list[id]; if (q) { spin_lock(&q->owner_lock); if (q->owner == client) { /* found */ q->klocked = 1; spin_unlock(&q->owner_lock); queue_list[id] = NULL; num_queues--; spin_unlock_irqrestore(&queue_list_lock, flags); return q; } spin_unlock(&q->owner_lock); } spin_unlock_irqrestore(&queue_list_lock, flags); return NULL;}/*----------------------------------------------------------------*//* create new queue (constructor) */static queue_t *queue_new(int owner, int locked){ queue_t *q; q = kzalloc(sizeof(*q), GFP_KERNEL); if (q == NULL) { snd_printd("malloc failed for snd_seq_queue_new()\n"); return NULL; } spin_lock_init(&q->owner_lock); spin_lock_init(&q->check_lock); init_MUTEX(&q->timer_mutex); snd_use_lock_init(&q->use_lock); q->queue = -1; q->tickq = snd_seq_prioq_new(); q->timeq = snd_seq_prioq_new(); q->timer = snd_seq_timer_new(); if (q->tickq == NULL || q->timeq == NULL || q->timer == NULL) { snd_seq_prioq_delete(&q->tickq); snd_seq_prioq_delete(&q->timeq); snd_seq_timer_delete(&q->timer); kfree(q); return NULL; } q->owner = owner; q->locked = locked; q->klocked = 0; return q;}/* delete queue (destructor) */static void queue_delete(queue_t *q){ /* stop and release the timer */ snd_seq_timer_stop(q->timer); snd_seq_timer_close(q); /* wait until access free */ snd_use_lock_sync(&q->use_lock); /* release resources... */ snd_seq_prioq_delete(&q->tickq); snd_seq_prioq_delete(&q->timeq); snd_seq_timer_delete(&q->timer); kfree(q);}/*----------------------------------------------------------------*//* setup queues */int __init snd_seq_queues_init(void){ /* memset(queue_list, 0, sizeof(queue_list)); num_queues = 0; */ return 0;}/* delete all existing queues */void __exit snd_seq_queues_delete(void){ int i; /* clear list */ for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) { if (queue_list[i]) queue_delete(queue_list[i]); }}/* allocate a new queue - * return queue index value or negative value for error */int snd_seq_queue_alloc(int client, int locked, unsigned int info_flags){ queue_t *q; q = queue_new(client, locked); if (q == NULL) return -ENOMEM; q->info_flags = info_flags; if (queue_list_add(q) < 0) { queue_delete(q); return -ENOMEM; } snd_seq_queue_use(q->queue, client, 1); /* use this queue */ return q->queue;}/* delete a queue - queue must be owned by the client */int snd_seq_queue_delete(int client, int queueid){ queue_t *q; if (queueid < 0 || queueid >= SNDRV_SEQ_MAX_QUEUES) return -EINVAL; q = queue_list_remove(queueid, client); if (q == NULL) return -EINVAL; queue_delete(q); return 0;}/* return pointer to queue structure for specified id */queue_t *queueptr(int queueid){ queue_t *q; unsigned long flags; if (queueid < 0 || queueid >= SNDRV_SEQ_MAX_QUEUES) return NULL; spin_lock_irqsave(&queue_list_lock, flags); q = queue_list[queueid]; if (q) snd_use_lock_use(&q->use_lock); spin_unlock_irqrestore(&queue_list_lock, flags); return q;}/* return the (first) queue matching with the specified name */queue_t *snd_seq_queue_find_name(char *name){ int i; queue_t *q; for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) { if ((q = queueptr(i)) != NULL) { if (strncmp(q->name, name, sizeof(q->name)) == 0) return q; queuefree(q); } } return NULL;}/* -------------------------------------------------------- */void snd_seq_check_queue(queue_t *q, int atomic, int hop){ unsigned long flags; snd_seq_event_cell_t *cell; if (q == NULL) return; /* make this function non-reentrant */ spin_lock_irqsave(&q->check_lock, flags); if (q->check_blocked) { q->check_again = 1; spin_unlock_irqrestore(&q->check_lock, flags); return; /* other thread is already checking queues */ } q->check_blocked = 1; spin_unlock_irqrestore(&q->check_lock, flags); __again: /* Process tick queue... */ while ((cell = snd_seq_prioq_cell_peek(q->tickq)) != NULL) { if (snd_seq_compare_tick_time(&q->timer->tick.cur_tick, &cell->event.time.tick)) { cell = snd_seq_prioq_cell_out(q->tickq); if (cell) snd_seq_dispatch_event(cell, atomic, hop); } else { /* event remains in the queue */ break; } } /* Process time queue... */ while ((cell = snd_seq_prioq_cell_peek(q->timeq)) != NULL) { if (snd_seq_compare_real_time(&q->timer->cur_time, &cell->event.time.time)) { cell = snd_seq_prioq_cell_out(q->timeq); if (cell) snd_seq_dispatch_event(cell, atomic, hop); } else { /* event remains in the queue */ break; } } /* free lock */ spin_lock_irqsave(&q->check_lock, flags); if (q->check_again) { q->check_again = 0; spin_unlock_irqrestore(&q->check_lock, flags); goto __again; } q->check_blocked = 0; spin_unlock_irqrestore(&q->check_lock, flags);}/* enqueue a event to singe queue */int snd_seq_enqueue_event(snd_seq_event_cell_t *cell, int atomic, int hop){ int dest, err; queue_t *q; snd_assert(cell != NULL, return -EINVAL); dest = cell->event.queue; /* destination queue */ q = queueptr(dest); if (q == NULL) return -EINVAL; /* handle relative time stamps, convert them into absolute */ if ((cell->event.flags & SNDRV_SEQ_TIME_MODE_MASK) == SNDRV_SEQ_TIME_MODE_REL) { switch (cell->event.flags & SNDRV_SEQ_TIME_STAMP_MASK) { case SNDRV_SEQ_TIME_STAMP_TICK: cell->event.time.tick += q->timer->tick.cur_tick; break; case SNDRV_SEQ_TIME_STAMP_REAL: snd_seq_inc_real_time(&cell->event.time.time, &q->timer->cur_time); break; } cell->event.flags &= ~SNDRV_SEQ_TIME_MODE_MASK; cell->event.flags |= SNDRV_SEQ_TIME_MODE_ABS; } /* enqueue event in the real-time or midi queue */ switch (cell->event.flags & SNDRV_SEQ_TIME_STAMP_MASK) { case SNDRV_SEQ_TIME_STAMP_TICK: err = snd_seq_prioq_cell_in(q->tickq, cell); break; case SNDRV_SEQ_TIME_STAMP_REAL: default: err = snd_seq_prioq_cell_in(q->timeq, cell); break; } if (err < 0) { queuefree(q); /* unlock */ return err; } /* trigger dispatching */ snd_seq_check_queue(q, atomic, hop); queuefree(q); /* unlock */ return 0;}/*----------------------------------------------------------------*/static inline int check_access(queue_t *q, int client){ return (q->owner == client) || (!q->locked && !q->klocked);}/* check if the client has permission to modify queue parameters. * if it does, lock the queue */static int queue_access_lock(queue_t *q, int client){ unsigned long flags; int access_ok; spin_lock_irqsave(&q->owner_lock, flags); access_ok = check_access(q, client); if (access_ok) q->klocked = 1; spin_unlock_irqrestore(&q->owner_lock, flags); return access_ok;}/* unlock the queue */static inline void queue_access_unlock(queue_t *q){ unsigned long flags; spin_lock_irqsave(&q->owner_lock, flags); q->klocked = 0; spin_unlock_irqrestore(&q->owner_lock, flags);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -