24#ifndef LIBTHREADAR_RATELIER_SCATTER_HPP
25#define LIBTHREADAR_RATELIER_SCATTER_HPP
81 void scatter(std::unique_ptr<T> & one,
signed int flag = 0);
91 std::unique_ptr<T>
worker_get_one(
unsigned int & slot,
signed int & flag);
98 static const unsigned int cond_empty = 0;
99 static const unsigned int cond_full = 1;
103 std::unique_ptr<T> obj;
108 slot(
signed int val) { empty =
true; flag = val; };
109 slot(
const slot & ref) { obj.reset(); empty = ref.empty; index = ref.index; flag = ref.flag; };
112 unsigned int next_index;
113 unsigned int lowest_index;
114 std::vector<slot> table;
115 std::map<unsigned int, unsigned int> corres;
116 std::deque<unsigned int> empty_slot;
121 table(size, slot(flag)),
127 for(
unsigned int i = 0; i < size; ++i)
128 empty_slot.push_back(i);
133 unsigned int tableindex;
138 while(empty_slot.empty())
139 verrou.wait(cond_full);
141 tableindex = empty_slot.back();
145 if(tableindex >= table.size())
147 if( ! table[tableindex].empty)
152 table[tableindex].empty =
false;
153 table[tableindex].obj = std::move(one);
154 table[tableindex].index = next_index;
155 table[tableindex].flag = flag;
157 corres[next_index] = tableindex;
160 empty_slot.pop_back();
161 if(verrou.get_waiting_thread_count(cond_empty) > 0)
162 verrou.signal(cond_empty);
167 verrou.broadcast(cond_empty);
168 verrou.broadcast(cond_full);
176 std::unique_ptr<T> ret;
181 std::map<unsigned int, unsigned int>::iterator it = corres.begin();
187 if(it != corres.end())
189 if(it->first < lowest_index)
196 if(it->second >= table.size())
198 if(table[it->second].empty)
200 if( ! table[it->second].obj)
205 ret = std::move(table[it->second].obj);
206 slot = table[it->second].index;
207 flag = table[it->second].flag;
208 table[it->second].empty =
true;
210 if(lowest_index != slot)
216 empty_slot.push_back(it->second);
219 if(verrou.get_waiting_thread_count(cond_full) > 0)
220 verrou.signal(cond_full);
227 verrou.wait(cond_empty);
236 verrou.broadcast(cond_empty);
237 verrou.broadcast(cond_full);
247 unsigned int size = table.size();
253 for(
unsigned int i = 0; i < size; ++i)
255 table[i].obj.reset();
256 table[i].empty =
true;
257 empty_slot.push_back(i);
261 verrou.broadcast(cond_empty);
262 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 ...