24#ifndef LIBTHREADAR_RATELIER_GATHER_HPP
25#define LIBTHREADAR_RATELIER_GATHER_HPP
117 void worker_push_one(
unsigned int slot, std::unique_ptr<T> & one,
signed int flag = 0);
125 void gather(std::deque<std::unique_ptr<T> > & ones, std::deque<signed int> & flag);
135 static const unsigned int cond_pending_data = 0;
136 static const unsigned int cond_full = 1;
140 std::unique_ptr<T> obj;
145 slot(
signed int val) { empty =
true; flag = val; };
146 slot(
const slot & ref) { obj.reset(); empty = ref.empty; index = ref.index; flag = ref.flag; };
149 unsigned int next_index;
150 std::vector<slot> table;
151 std::map<unsigned int, unsigned int> corres;
152 std::deque<unsigned int> empty_slot;
157 table(size, slot(flag)),
162 for(
unsigned int i = 0; i < size; ++i)
163 empty_slot.push_back(i);
172 while(empty_slot.empty()
173 || ((empty_slot.size() == 1 && slot != next_index)
174 && corres.begin() != corres.end() && (corres.begin())->first != next_index))
175 verrou.wait(cond_full);
177 std::map<unsigned int, unsigned int>::iterator it = corres.find(slot);
180 if(it != corres.end())
181 throw exception_range(
"the ratelier_gather index to fill is already used");
183 index = empty_slot.back();
187 if(index >= table.size())
189 if( ! table[index].empty)
194 corres[slot] = index;
195 table[index].obj = std::move(one);
196 table[index].empty =
false;
197 table[index].index = slot;
198 table[index].flag = flag;
200 empty_slot.pop_back();
202 if(verrou.get_waiting_thread_count(cond_pending_data) > 0)
203 if(corres.find(next_index) != corres.end())
204 verrou.signal(cond_pending_data);
209 verrou.broadcast(cond_pending_data);
210 verrou.broadcast(cond_full);
224 std::map<unsigned int, unsigned int>::iterator it;
225 std::map<unsigned int, unsigned int>::iterator tmp;
231 while(it != corres.end())
233 if(it->first > next_index)
236 if(it->first == next_index)
241 if(it->second >= table.size())
243 if(table[it->second].index != next_index)
245 if(table[it->second].empty)
247 if( ! table[it->second].obj)
252 ones.push_back(std::move(table[it->second].obj));
253 flag.push_back(table[it->second].flag);
255 table[it->second].empty =
true;
256 empty_slot.push_back(it->second);
267 verrou.wait(cond_pending_data);
271 if(verrou.get_waiting_thread_count(cond_full) > 0)
272 verrou.broadcast(cond_full);
277 verrou.broadcast(cond_pending_data);
278 verrou.broadcast(cond_full);
283 if(ones.size() != flag.size())
289 unsigned int size = table.size();
294 for(
unsigned int i = 0; i < size; ++i)
296 table[i].obj.reset();
297 table[i].empty =
true;
298 empty_slot.push_back(i);
302 verrou.broadcast(cond_pending_data);
303 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's purpose it gather works from several worker threads
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 the gathering thread
void gather(std::deque< std::unique_ptr< T > > &ones, std::deque< signed int > &flag)
obtain the lowest continuous filled slots from 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 ...