3 * Copyright 2006 Free Software Foundation, Inc.
5 * This file is part of GNU Radio.
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
23 #ifndef _CIRCULAR_LINKED_LIST_H_
24 #define _CIRCULAR_LINKED_LIST_H_
26 #include <mld_threads.h>
29 #define __INLINE__ inline
33 #define DEBUG(X) do{X} while(0);
35 #define DEBUG(X) do{} while(0);
38 template <class T> class s_both;
40 template <class T> class s_node
42 typedef s_node<T>* s_node_ptr;
47 s_node_ptr d_prev, d_next;
52 s_node_ptr l_prev = NULL,
53 s_node_ptr l_next = NULL)
54 : d_object (l_object), d_available (TRUE), d_prev (l_prev),
55 d_next (l_next), d_both (0) {};
57 __INLINE__ s_node (s_node_ptr l_prev, s_node_ptr l_next = NULL) {
58 s_node ((T) NULL, l_prev, l_next); };
59 __INLINE__ s_node () { s_node (NULL, NULL, NULL); };
60 __INLINE__ ~s_node () {};
63 d_prev->next (d_next);
64 d_next->prev (d_prev);
65 d_prev = d_next = this;
68 void insert_before (s_node_ptr l_next) {
70 s_node_ptr l_prev = l_next->prev ();
76 d_next = d_prev = this;
79 void insert_after (s_node_ptr l_prev) {
81 s_node_ptr l_next = l_prev->next ();
87 d_prev = d_next = this;
90 __INLINE__ T object () { return (d_object); };
91 __INLINE__ void object (T l_object) { d_object = l_object; };
92 __INLINE__ bool available () { return (d_available); };
93 __INLINE__ void set_available () { d_available = TRUE; };
94 __INLINE__ void set_available (bool l_avail) { d_available = l_avail; };
95 __INLINE__ void set_not_available () { d_available = FALSE; };
96 __INLINE__ s_node_ptr next () { return (d_next); };
97 __INLINE__ s_node_ptr prev () { return (d_prev); };
98 __INLINE__ s_both<T>* both () { return (d_both); };
99 __INLINE__ void next (s_node_ptr l_next) { d_next = l_next; };
100 __INLINE__ void prev (s_node_ptr l_prev) { d_prev = l_prev; };
101 __INLINE__ void both (s_both<T>* l_both) { d_both = l_both; };
104 template <class T> class circular_linked_list {
105 typedef s_node<T>* s_node_ptr;
108 s_node_ptr d_current, d_iterate, d_available, d_inUse;
109 UInt32 d_n_nodes, d_n_used;
110 mld_mutex_ptr d_internal;
111 mld_condition_ptr d_ioBlock;
114 circular_linked_list (UInt32 n_nodes) {
116 throw std::runtime_error ("circular_linked_list(): n_nodes == 0");
121 s_node_ptr l_prev, l_next;
122 d_inUse = d_current = l_next = l_prev = NULL;
124 l_prev = new s_node<T> ();
125 l_prev->set_available ();
126 l_prev->next (l_prev);
127 l_prev->prev (l_prev);
129 l_next = new s_node<T> (l_prev, l_prev);
130 l_next->set_available ();
131 l_next->next (l_prev);
132 l_next->prev (l_prev);
133 l_prev->next (l_next);
134 l_prev->prev (l_next);
136 UInt32 n = n_nodes - 2;
138 d_current = new s_node<T> (l_prev, l_next);
139 d_current->set_available ();
140 d_current->prev (l_prev);
141 d_current->next (l_next);
142 l_prev->next (d_current);
143 l_next->prev (d_current);
149 d_available = d_current = l_prev;
150 d_ioBlock = new mld_condition ();
151 d_internal = d_ioBlock->mutex ();
154 ~circular_linked_list () {
156 s_node_ptr l_node = iterate_next ();
159 l_node = iterate_next ();
163 d_available = d_inUse = d_iterate = d_current = NULL;
164 d_n_used = d_n_nodes = 0;
167 s_node_ptr find_next_available_node () {
169 // find an available node
170 s_node_ptr l_node = d_available;
171 DEBUG (fprintf (stderr, "w "));
173 DEBUG (fprintf (stderr, "x\n"));
174 // the ioBlock condition will automatically unlock() d_internal
176 // and lock() is here
177 DEBUG (fprintf (stderr, "y\n"));
178 l_node = d_available;
180 DEBUG (fprintf (stderr, "::f_n_a_n: #u = %ld, node = %p\n",
181 num_used(), l_node));
182 // remove this one from the current available list
183 if (num_available () == 1) {
184 // last one, just set available to NULL
187 d_available = l_node->next ();
189 // add is to the inUse list
193 l_node->insert_before (d_inUse);
195 l_node->set_not_available ();
196 d_internal->unlock ();
200 void make_node_available (s_node_ptr l_node) {
203 DEBUG (fprintf (stderr, "::m_n_a: #u = %ld, node = %p\n",
204 num_used(), l_node));
205 // remove this node from the inUse list
206 if (num_used () == 1) {
207 // last one, just set inUse to NULL
210 d_inUse = l_node->next ();
212 // add this node to the available list
214 d_available = l_node;
216 l_node->insert_before (d_available);
219 DEBUG (fprintf (stderr, "s%ld ", d_n_used));
220 // signal the condition when new data arrives
221 d_ioBlock->signal ();
222 DEBUG (fprintf (stderr, "t "));
224 // unlock the mutex for thread safety
225 d_internal->unlock ();
228 __INLINE__ void iterate_start () { d_iterate = d_current; };
230 s_node_ptr iterate_next () {
232 // lock the mutex for thread safety
235 s_node_ptr l_this = NULL;
238 d_iterate = d_iterate->next ();
239 if (d_iterate == d_current)
243 // unlock the mutex for thread safety
244 d_internal->unlock ();
249 __INLINE__ T object () { return (d_current->d_object); };
250 __INLINE__ void object (T l_object) { d_current->d_object = l_object; };
251 __INLINE__ UInt32 num_nodes () { return (d_n_nodes); };
252 __INLINE__ UInt32 num_used () { return (d_n_used); };
253 __INLINE__ void num_used (UInt32 l_n_used) { d_n_used = l_n_used; };
254 __INLINE__ UInt32 num_available () { return (d_n_nodes - d_n_used); };
255 __INLINE__ void num_used_inc (void) {
256 if (d_n_used < d_n_nodes) ++d_n_used;
258 __INLINE__ void num_used_dec (void) {
259 if (d_n_used != 0) --d_n_used;
260 // signal the condition that new data has arrived
261 d_ioBlock->signal ();
263 __INLINE__ bool in_use () { return (d_n_used != 0); };
266 template <class T> class s_both
272 __INLINE__ s_both (s_node<T>* l_node, void* l_this)
273 : d_node (l_node), d_this (l_this) {};
274 __INLINE__ ~s_both () {};
275 __INLINE__ s_node<T>* node () { return (d_node); };
276 __INLINE__ void* This () { return (d_this); };
277 __INLINE__ void set (s_node<T>* l_node, void* l_this) {
278 d_node = l_node; d_this = l_this;};
281 #endif /* _CIRCULAR_LINKED_LIST_H_ */