File: seq_fifo.c

package info (click to toggle)
alsadriver 0.2.0-pre8-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 2,808 kB
  • ctags: 6,550
  • sloc: ansic: 43,490; sh: 916; makefile: 759; perl: 54
file content (164 lines) | stat: -rw-r--r-- 3,498 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 *   ALSA sequencer FIFO
 *   Copyright (c) 1998 by Frank van de Pol <F.K.W.van.de.Pol@inter.nl.net>
 *
 *
 *   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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include "driver.h"
#include "seq_fifo.h"


/* FIFO */

/* create new fifo */
fifo_t *snd_seq_fifo_new(void)
{
	fifo_t *f;
	unsigned long flags;

	f = snd_malloc(sizeof(fifo_t));
	if (f == NULL) {
		snd_printk("malloc failed for snd_seq_fifo_new() \n");
		return NULL;
	}

	/* clear unused variables */
	memset(f, 0, sizeof(fifo_t));
	
	snd_spin_prepare(f, lock);
	snd_sleep_prepare(f, input);
	f->flags = 0;

	snd_spin_lock(f, lock, &flags);
	f->head = NULL;
	f->tail = NULL;
	f->cells = 0;
	snd_spin_unlock(f, lock, &flags);
	
	return f;
}

void snd_seq_fifo_delete(fifo_t **fifo)
{
	fifo_t *f = *fifo;
	*fifo = NULL;

	if (f == NULL) {
		snd_printk("oops: snd_seq_fifo_delete() called with NULL fifo\n");
		return;
	}

	/* release resources...*/
	/*....................*/
	
	if (f->cells > 0) {
		/* should drain the fifo */
		snd_printk("warning: releasing non-empty fifo\n");
	}
	
	snd_free(f,sizeof(fifo_t));
}


/* enqueue cell to fifo */
void snd_seq_fifo_cell_in(fifo_t * f, snd_seq_event_cell_t * cell)
{
	unsigned long flags;

	if (f == NULL) {
		snd_printk("oops: snd_seq_fifo_cell_in() called with NULL fifo\n");
		return;
	}
	if (cell == NULL) {
		snd_printk("oops: snd_seq_fifo_cell_in() called with NULL cell\n");
		return;
	}
	snd_spin_lock(f, lock, &flags);

	/* add new cell to tail of the fifo */
	if (f->tail != NULL)
		f->tail->ptr_l = cell;
	f->tail = cell;
	cell->ptr_l = NULL;

	/* register if this is the first element */
	if (f->head == NULL)
		f->head = cell;

	f->cells++;
	snd_spin_unlock(f, lock, &flags);

	/* wakeup clients */
	if (f->flags & SND_WK_SLEEP)
		snd_wakeup(f, input);
}


/* dequeue cell from fifo */
snd_seq_event_cell_t *snd_seq_fifo_cell_out(fifo_t * f)
{
	snd_seq_event_cell_t *cell;
	unsigned long flags;

	if (f == NULL) {
		snd_printk("oops: snd_seq_fifo_cell_out() called with NULL fifo\n");
		return NULL;
	}
	snd_spin_lock(f, lock, &flags);

	if (f->head != NULL) {
		cell = f->head;
		f->head = cell->ptr_l;

		/* reset tail if this was the last element */
		if (f->tail == cell)
			f->tail = NULL;

		cell->ptr_l = NULL;
		f->cells--;
	} else {
		/* empty queue */
		snd_spin_unlock(f, lock, &flags);
		return NULL;
	}

	snd_spin_unlock(f, lock, &flags);
	return cell;
}


/* return number of events available in fifo */
int snd_seq_fifo_avail(fifo_t * f)
{
	if (f == NULL) {
		snd_printk("oops: snd_seq_fifo_avail() called with NULL fifo\n");
		return 0;
	}
	return f->cells;
}


/* peek at cell at the head of the fifo */
snd_seq_event_cell_t *snd_seq_fifo_cell_peek(fifo_t * f)
{
	if (f == NULL) {
		snd_printk("oops: snd_seq_fifo_avail() called with NULL fifo\n");
		return 0;
	}
	return f->head;
}