24#ifndef LIBTHREADAR_RATELIER_SCATTER_HPP
25#define LIBTHREADAR_RATELIER_SCATTER_HPP
81 void scatter(std::unique_ptr<T> & one,
signed int flag = 0);
90 std::unique_ptr<T>
worker_get_one(
unsigned int & slot,
signed int & flag);
97 static const unsigned int cond_empty = 0;
98 static const unsigned int cond_full = 1;
102 std::unique_ptr<T> obj;
107 slot(
signed int val) { empty =
true; flag = val; };
108 slot(
const slot & ref) { obj.reset(); empty = ref.empty; index = ref.index; flag = ref.flag; };
111 unsigned int next_index;
112 unsigned int lowest_index;
113 std::vector<slot> table;
114 std::map<unsigned int, unsigned int> corres;
115 std::deque<unsigned int> empty_slot;
120 table(size, slot(flag)),
126 for(
unsigned int i = 0; i < size; ++i)
127 empty_slot.push_back(i);
132 unsigned int tableindex;
137 while(empty_slot.empty())
138 verrou.wait(cond_full);
140 tableindex = empty_slot.back();
144 if(tableindex >= table.size())
146 if( ! table[tableindex].empty)
151 table[tableindex].empty =
false;
152 table[tableindex].obj = std::move(one);
153 table[tableindex].index = next_index;
154 table[tableindex].flag = flag;
156 corres[next_index] = tableindex;
159 empty_slot.pop_back();
160 if(verrou.get_waiting_thread_count(cond_empty) > 0)
161 verrou.signal(cond_empty);
166 verrou.broadcast(cond_empty);
167 verrou.broadcast(cond_full);
175 std::unique_ptr<T> ret;
180 std::map<unsigned int, unsigned int>::iterator it = corres.begin();
186 if(it != corres.end())
188 if(it->first < lowest_index)
195 if(it->second >= table.size())
197 if(table[it->second].empty)
199 if( ! table[it->second].obj)
204 ret = std::move(table[it->second].obj);
205 slot = table[it->second].index;
206 flag = table[it->second].flag;
207 table[it->second].empty =
true;
209 if(lowest_index != slot)
215 empty_slot.push_back(it->second);
218 if(verrou.get_waiting_thread_count(cond_full) > 0)
219 verrou.signal(cond_full);
226 verrou.wait(cond_empty);
235 verrou.broadcast(cond_empty);
236 verrou.broadcast(cond_full);
246 unsigned int size = table.size();
252 for(
unsigned int i = 0; i < size; ++i)
254 table[i].obj.reset();
255 table[i].empty =
true;
256 empty_slot.push_back(i);
260 verrou.broadcast(cond_empty);
261 verrou.broadcast(cond_full);
Wrapper around the Posix pthread_cond_t object and its associated mutex.
the class ratelier_scatter has a fixed length range of slots of arbitrary defined object type
std::unique_ptr< T > worker_get_one(unsigned int &slot, signed int &flag)
void scatter(std::unique_ptr< T > &one, signed int flag=0)
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 ...