24#ifndef LIBTHREADAR_RATELIER_GATHER_HPP
25#define LIBTHREADAR_RATELIER_GATHER_HPP
115 void worker_push_one(
unsigned int slot, std::unique_ptr<T> & one,
signed int flag = 0);
122 void gather(std::deque<std::unique_ptr<T> > & ones, std::deque<signed int> & flag);
129 static const unsigned int cond_pending_data = 0;
130 static const unsigned int cond_full = 1;
134 std::unique_ptr<T> obj;
139 slot(
signed int val) { empty =
true; flag = val; };
140 slot(
const slot & ref) { obj.reset(); empty = ref.empty; index = ref.index; flag = ref.flag; };
143 unsigned int next_index;
144 std::vector<slot> table;
145 std::map<unsigned int, unsigned int> corres;
146 std::deque<unsigned int> empty_slot;
151 table(size, slot(flag)),
156 for(
unsigned int i = 0; i < size; ++i)
157 empty_slot.push_back(i);
166 while(empty_slot.empty()
167 || ((empty_slot.size() == 1 && slot != next_index)
168 && corres.begin() != corres.end() && (corres.begin())->first != next_index))
169 verrou.wait(cond_full);
171 std::map<unsigned int, unsigned int>::iterator it = corres.find(slot);
174 if(it != corres.end())
175 throw exception_range(
"the ratelier_gather index to fill is already used");
177 index = empty_slot.back();
181 if(index >= table.size())
183 if( ! table[index].empty)
188 corres[slot] = index;
189 table[index].obj = std::move(one);
190 table[index].empty =
false;
191 table[index].index = slot;
192 table[index].flag = flag;
194 empty_slot.pop_back();
196 if(verrou.get_waiting_thread_count(cond_pending_data) > 0)
197 if(corres.find(next_index) != corres.end())
198 verrou.signal(cond_pending_data);
203 verrou.broadcast(cond_pending_data);
204 verrou.broadcast(cond_full);
218 std::map<unsigned int, unsigned int>::iterator it;
219 std::map<unsigned int, unsigned int>::iterator tmp;
225 while(it != corres.end())
227 if(it->first > next_index)
230 if(it->first == next_index)
235 if(it->second >= table.size())
237 if(table[it->second].index != next_index)
239 if(table[it->second].empty)
241 if( ! table[it->second].obj)
246 ones.push_back(std::move(table[it->second].obj));
247 flag.push_back(table[it->second].flag);
249 table[it->second].empty =
true;
250 empty_slot.push_back(it->second);
261 verrou.wait(cond_pending_data);
265 if(verrou.get_waiting_thread_count(cond_full) > 0)
266 verrou.broadcast(cond_full);
271 verrou.broadcast(cond_pending_data);
272 verrou.broadcast(cond_full);
277 if(ones.size() != flag.size())
283 unsigned int size = table.size();
288 for(
unsigned int i = 0; i < size; ++i)
290 table[i].obj.reset();
291 table[i].empty =
true;
292 empty_slot.push_back(i);
296 verrou.broadcast(cond_pending_data);
297 verrou.broadcast(cond_full);
Wrapper around the Posix pthread_cond_t object and its associated mutex.
Exception used to report out or range value or argument.
the class ratelier_gather has a fixed length range of slots of arbitrary defined object type
void worker_push_one(unsigned int slot, std::unique_ptr< T > &one, signed int flag=0)
provides to a worker thread a mean to given data with its associated index to a gathering thread
void gather(std::deque< std::unique_ptr< T > > &ones, std::deque< signed int > &flag)
obtain the lowest continuous filled slots of the ratelier_gather and free them
void reset()
reset the object in its prestine state
#define THREADAR_BUG
Macro used to throw an exception_bug when execution reach that statement.
defines the mutex C++ class
This is the only namespace used in libthreadar and all symbols provided by libthreadar are member of ...