File: queue.c

package info (click to toggle)
unicap 0.9.12-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 3,636 kB
  • ctags: 3,520
  • sloc: ansic: 23,011; sh: 10,712; xml: 1,408; makefile: 298
file content (220 lines) | stat: -rw-r--r-- 4,405 bytes parent folder | download | duplicates (6)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
    unicap
    Copyright (C) 2004  Arne Caspari

    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
*/
#include <unistd.h>
#include <string.h>
#ifdef DEBUG
#include <stdio.h>
#endif
#include "queue.h"
#include <semaphore.h>
#include <debug.h>

void _dump_queue( struct _unicap_queue *queue )
{
	int i = 0;
	printf( "## dump queue\n" );
	while( queue->next )
	{
		printf( "entry: %d, addr: %p\n", i++, queue );
		printf( "entry->next: %p\n", queue->next );
		printf( "data: %p\n", queue->data );
		printf( "sema: %p\n", queue->psema );
		queue = queue->next;
	}
	printf( "entry: %d, addr: %p\n", i, queue );
	printf( "entry->next: %p\n", queue->next );
	printf( "data: %p\n", queue->data );
	printf( "sema: %p\n\n", queue->psema );
}

		


void _insert_back_queue( struct _unicap_queue *queue, struct _unicap_queue *entry )
{
	struct _unicap_queue *tmp_queue;
	if( !entry )
	{
		return;
	}

	if( sem_wait( queue->psema ) )
	{
		TRACE( "FATAL: sem_wait failed\n" );
		return;
	}
	
	tmp_queue = queue;

	// run to the end of the queue
	while( tmp_queue->next )
	{
		tmp_queue = tmp_queue->next;
	}
	tmp_queue->next = entry;
	entry->psema = queue->psema;
	
	entry->next = 0;
	if( sem_post( entry->psema ) )
	{
		TRACE( "FATAL: sem_post failed\n" );
		return;
	}
}

void _insert_front_queue( struct _unicap_queue *queue, struct _unicap_queue *entry )
{
	if( !entry )
	{
		return;
	}
	
	if( sem_wait( queue->psema ) )
	{
		TRACE( "FATAL: sem_wait failed\n" );
		return;
	}
	
	entry->next = queue->next;
	entry->psema = queue->psema;
	queue->next = entry;

	if( sem_post( queue->psema ) )
	{
		TRACE( "FATAL: sem_post failed\n" );
	}
}

struct _unicap_queue *_get_front_queue( struct _unicap_queue *queue )
{
	struct _unicap_queue *entry;

	if( sem_wait( queue->psema ) )
	{
		TRACE( "sem_wait failed!\n" );
		return 0;
	}

	if( !queue->next )
	{
		entry = 0;
		sem_post( queue->psema );
		goto out;
	}
	
	entry = queue->next;

	if( queue->next )
	{
		queue->next = queue->next->next;
	}
	
	entry->psema = queue->psema;
	
	entry->next = 0;

	if( sem_post( entry->psema ) )
	{
		TRACE( "FATAL: sem_post failed\n" );
	}
	
out:

	return entry;
}

void _move_to_queue( struct _unicap_queue *from_queue, struct _unicap_queue *to_queue )
{
	struct _unicap_queue *tmp_to_queue;
	struct _unicap_queue *tmp_from_queue;
	struct _unicap_queue *entry;

	if( sem_wait( from_queue->psema ) )
	{
		TRACE( "FATAL: sem_wait failed\n" );
		return;
	}
	if( sem_wait( to_queue->psema ) ) 
	{
		TRACE( "FATAL: sem_wait failed\n" );
		return;
	}

	if( !from_queue->next )
	{
		entry = 0;
		goto out;
	}
	
	tmp_from_queue = from_queue;
	///////////////////////////////////////////////////////////////
	entry = from_queue->next;
	if( tmp_from_queue->next )
	{
		tmp_from_queue->next = tmp_from_queue->next->next;
	}
	entry->next = 0;
	
	////////////////////////////////////////////////////////////// 
	tmp_to_queue = to_queue;
	// run to the end of the queue
	while( tmp_to_queue->next )
	{
		tmp_to_queue = tmp_to_queue->next;
	}
	tmp_to_queue->next = entry;
	entry->psema = to_queue->psema;
out:
	if( sem_post( from_queue->psema ) )
	{
		TRACE( "FATAL: sem_post failed\n" );
	}

	if( sem_post( to_queue->psema ) )
	{
		TRACE( "FATAL: sem_post failed\n" );
	}
}


void _init_queue( struct _unicap_queue *queue )
{
	memset( queue, 0, sizeof( struct _unicap_queue ) );
	if( sem_init( &queue->sema, 0, 1 ) )
	{
		TRACE( "FATAL: sem_init failed\n" );
	}
	queue->psema = &queue->sema;
}

int  _queue_get_size( struct _unicap_queue *queue )
{
	int entries = 0;
	struct _unicap_queue *entry = queue;

/* 	TRACE( "entries: %d\n", entries ); */
	while( entry->next )
	{
/* 		TRACE( "entries: %d\n", entries ); */
		entries++;
		entry = entry->next;
	}

	return entries;
}