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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
/* -*- c++ -*- */
/*
* Copyright 2006 Free Software Foundation, Inc.
*
* This file is part of GNU Radio.
*
* GNU Radio 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 3, or (at your option)
* any later version.
*
* GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifndef _CIRCULAR_LINKED_LIST_H_
#define _CIRCULAR_LINKED_LIST_H_
#include <mld_threads.h>
#include <stdexcept>
#define __INLINE__ inline
template <class T> class s_both;
template <class T> class s_node
{
typedef s_node<T>* s_node_ptr;
private:
T d_object;
bool d_available;
s_node_ptr d_prev, d_next;
s_both<T>* d_both;
public:
s_node (T l_object,
s_node_ptr l_prev = NULL,
s_node_ptr l_next = NULL)
: d_object (l_object), d_available (TRUE), d_prev (l_prev),
d_next (l_next), d_both (0) {};
__INLINE__ s_node (s_node_ptr l_prev, s_node_ptr l_next = NULL) {
s_node ((T) NULL, l_prev, l_next); };
__INLINE__ s_node () { s_node (NULL, NULL, NULL); };
__INLINE__ ~s_node () {};
void remove () {
d_prev->next (d_next);
d_next->prev (d_prev);
d_prev = d_next = this;
};
void insert_before (s_node_ptr l_next) {
if (l_next) {
s_node_ptr l_prev = l_next->prev ();
d_next = l_next;
d_prev = l_prev;
l_prev->next (this);
l_next->prev (this);
} else
d_next = d_prev = this;
};
void insert_after (s_node_ptr l_prev) {
if (l_prev) {
s_node_ptr l_next = l_prev->next ();
d_prev = l_prev;
d_next = l_next;
l_next->prev (this);
l_prev->next (this);
} else
d_prev = d_next = this;
};
__INLINE__ T object () { return (d_object); };
__INLINE__ void object (T l_object) { d_object = l_object; };
__INLINE__ bool available () { return (d_available); };
__INLINE__ void set_available () { d_available = TRUE; };
__INLINE__ void set_available (bool l_avail) { d_available = l_avail; };
__INLINE__ void set_not_available () { d_available = FALSE; };
__INLINE__ s_node_ptr next () { return (d_next); };
__INLINE__ s_node_ptr prev () { return (d_prev); };
__INLINE__ s_both<T>* both () { return (d_both); };
__INLINE__ void next (s_node_ptr l_next) { d_next = l_next; };
__INLINE__ void prev (s_node_ptr l_prev) { d_prev = l_prev; };
__INLINE__ void both (s_both<T>* l_both) { d_both = l_both; };
};
template <class T> class circular_linked_list {
typedef s_node<T>* s_node_ptr;
private:
s_node_ptr d_current, d_iterate, d_available, d_inUse;
UInt32 d_n_nodes, d_n_used;
mld_mutex_ptr d_internal;
mld_condition_ptr d_ioBlock;
public:
circular_linked_list (UInt32 n_nodes) {
if (n_nodes == 0)
throw std::runtime_error ("circular_linked_list(): n_nodes == 0");
d_iterate = NULL;
d_n_nodes = n_nodes;
d_n_used = 0;
s_node_ptr l_prev, l_next;
d_inUse = d_current = l_next = l_prev = NULL;
l_prev = new s_node<T> ();
l_prev->set_available ();
l_prev->next (l_prev);
l_prev->prev (l_prev);
if (n_nodes > 1) {
l_next = new s_node<T> (l_prev, l_prev);
l_next->set_available ();
l_next->next (l_prev);
l_next->prev (l_prev);
l_prev->next (l_next);
l_prev->prev (l_next);
if (n_nodes > 2) {
UInt32 n = n_nodes - 2;
while (n-- > 0) {
d_current = new s_node<T> (l_prev, l_next);
d_current->set_available ();
d_current->prev (l_prev);
d_current->next (l_next);
l_prev->next (d_current);
l_next->prev (d_current);
l_next = d_current;
d_current = NULL;
}
}
}
d_available = d_current = l_prev;
d_internal = new mld_mutex ();
d_ioBlock = new mld_condition ();
};
~circular_linked_list () {
iterate_start ();
s_node_ptr l_node = iterate_next ();
while (l_node) {
delete l_node;
l_node = iterate_next ();
}
delete d_internal;
d_internal = NULL;
delete d_ioBlock;
d_ioBlock = NULL;
d_available = d_inUse = d_iterate = d_current = NULL;
d_n_used = d_n_nodes = 0;
};
s_node_ptr find_next_available_node () {
d_internal->lock ();
// find an available node
s_node_ptr l_node = d_available;
while (! l_node) {
d_internal->unlock ();
d_ioBlock->wait ();
d_internal->lock ();
l_node = d_available;
}
// fprintf (stderr, "::f_n_a_n: #u = %ld, node = %p\n", num_used(), l_node);
// remove this one from the current available list
if (num_available () == 1) {
// last one, just set available to NULL
d_available = NULL;
} else
d_available = l_node->next ();
l_node->remove ();
// add is to the inUse list
if (! d_inUse)
d_inUse = l_node;
else
l_node->insert_before (d_inUse);
d_n_used++;
l_node->set_not_available ();
d_internal->unlock ();
return (l_node);
};
void make_node_available (s_node_ptr l_node) {
if (!l_node) return;
d_internal->lock ();
// fprintf (stderr, "::m_n_a: #u = %ld, node = %p\n", num_used(), l_node);
// remove this node from the inUse list
if (num_used () == 1) {
// last one, just set inUse to NULL
d_inUse = NULL;
} else
d_inUse = l_node->next ();
l_node->remove ();
// add this node to the available list
if (! d_available)
d_available = l_node;
else
l_node->insert_before (d_available);
d_n_used--;
// signal the condition when new data arrives
d_ioBlock->signal ();
// unlock the mutex for thread safety
d_internal->unlock ();
};
__INLINE__ void iterate_start () { d_iterate = d_current; };
s_node_ptr iterate_next () {
#if 0
// lock the mutex for thread safety
d_internal->lock ();
#endif
s_node_ptr l_this = NULL;
if (d_iterate) {
l_this = d_iterate;
d_iterate = d_iterate->next ();
if (d_iterate == d_current)
d_iterate = NULL;
}
#if 0
// unlock the mutex for thread safety
d_internal->unlock ();
#endif
return (l_this);
};
__INLINE__ T object () { return (d_current->d_object); };
__INLINE__ void object (T l_object) { d_current->d_object = l_object; };
__INLINE__ UInt32 num_nodes () { return (d_n_nodes); };
__INLINE__ UInt32 num_used () { return (d_n_used); };
__INLINE__ void num_used (UInt32 l_n_used) { d_n_used = l_n_used; };
__INLINE__ UInt32 num_available () { return (d_n_nodes - d_n_used); };
__INLINE__ void num_used_inc (void) {
if (d_n_used < d_n_nodes) ++d_n_used;
};
__INLINE__ void num_used_dec (void) {
if (d_n_used != 0) --d_n_used;
// signal the condition that new data has arrived
d_ioBlock->signal ();
};
__INLINE__ bool in_use () { return (d_n_used != 0); };
};
template <class T> class s_both
{
private:
s_node<T>* d_node;
void* d_this;
public:
__INLINE__ s_both (s_node<T>* l_node, void* l_this)
: d_node (l_node), d_this (l_this) {};
__INLINE__ ~s_both () {};
__INLINE__ s_node<T>* node () { return (d_node); };
__INLINE__ void* This () { return (d_this); };
__INLINE__ void set (s_node<T>* l_node, void* l_this) {
d_node = l_node; d_this = l_this;};
};
#endif /* _CIRCULAR_LINKED_LIST_H_ */
|