24 #ifndef LIBTHREADAR_FAST_TAMPON_H
25 #define LIBTHREADAR_FAST_TAMPON_H
126 void feed(T* ptr,
unsigned int written);
141 void fetch(T* & ptr,
unsigned int & num);
162 bool is_empty()
const {
return next_feed == next_fetch; };
168 bool is_full()
const {
unsigned int tmp = next_feed; shift_by_one(tmp);
return tmp == next_fetch; };
176 unsigned int size()
const {
return table_size; };
191 unsigned int data_size;
193 atom() { mem =
nullptr; data_size = 0; };
196 static const unsigned int cond_full = 0;
197 static const unsigned int cond_empty = 0;
201 unsigned int table_size;
202 unsigned int alloc_size;
203 unsigned int next_feed;
204 unsigned int next_fetch;
209 void shift_by_one(
unsigned int & x)
const;
216 throw exception_range(
"max_block for fast_tampon should be strictly greater than 1");
217 table_size = max_block;
218 table =
new atom[table_size];
226 for(
unsigned int i = 0 ; i < table_size ; ++i)
228 table[i].mem =
new T[alloc_size];
229 if(table[i].mem ==
nullptr)
231 table[i].data_size = 0;
237 for(
unsigned int i = 0; i < table_size ; ++i)
239 if(table[i].mem !=
nullptr)
240 delete [] table[i].mem;
259 for(
unsigned int i = 0 ; i < table_size ; ++i)
261 if(table[i].mem !=
nullptr)
262 delete [] table[i].mem;
280 modif.wait(cond_full);
300 ptr = table[next_feed].mem;
308 feed_outside =
false;
310 if(ptr != table[next_feed].mem)
311 throw exception_range(
"returned ptr is not the one given earlier for feeding");
312 table[next_feed].data_size = num;
317 shift_by_one(next_feed);
318 if(modif.get_waiting_thread_count(cond_empty) > 0)
319 modif.signal(cond_empty);
333 feed_outside =
false;
334 if(ptr != table[next_feed].mem)
335 throw exception_range(
"returned ptr is not the one given earlier for feeding");
350 modif.wait(cond_empty);
369 fetch_outside =
true;
370 ptr = table[next_fetch].mem;
371 num = table[next_fetch].data_size;
378 fetch_outside =
false;
379 if(ptr != table[next_fetch].mem)
380 throw exception_range(
"returned ptr is no the one given earlier for fetching");
385 shift_by_one(next_fetch);
386 if(modif.get_waiting_thread_count(cond_full) > 0)
387 modif.signal(cond_full);
401 fetch_outside =
false;
403 if(ptr != table[next_fetch].mem)
404 throw exception_range(
"returned ptr is not the one given earlier for fetching");
405 table[next_fetch].data_size = new_num;
414 if(modif.get_waiting_thread_count(cond_full) > 0
415 || modif.get_waiting_thread_count(cond_empty) > 0)
417 modif.broadcast(cond_full);
418 modif.broadcast(cond_empty);
419 throw exception_range(
"reseting fast_tampon while some thread were waiting on it");
424 fetch_outside =
false;
425 feed_outside =
false;
bool is_not_empty() const
to know whether the fast_tampon is not empty
#define THREADAR_BUG
Macro used to throw an exception_bug when execution reach that statement.
void feed_cancel_get_block(T *ptr)
feeder call - step 2 alternative
void reset()
reset the object fields and mutex as if the object was just created
void feed(T *ptr, unsigned int written)
feeder call - step 2
bool is_empty() const
to know whether the fast_tampon has objects (readable or skipped)
bool is_full() const
for feeder to know whether the next call to get_block_to_feed() will be blocking
defines a set of exceptions that are used by libthreadar to report error situations ...
void fetch_push_back(T *ptr, unsigned int new_num)
fetcher call - step 2 alternative
unsigned int size() const
fast_tampon & operator=(const fast_tampon &ref)=delete
no assignment operator
void fetch_recycle(T *ptr)
fetcher call - step 2
Exception used to report memory allocation failures.
void fetch(T *&ptr, unsigned int &num)
fetcher call - step 1
fast_tampon(unsigned int max_block, unsigned int block_size)
constructor
unsigned int block_size() const
void get_block_to_feed(T *&ptr, unsigned int &num)
feeder call - step 1
Class fast_tampon provides asynchronous communication between two threads.
defines the condition class
This is the only namespace used in libthreadar and all symbols provided by libthreadar are member of ...
bool is_not_full() const
to know whether the fast_tampon is not full
Exception used to report out or range value or argument.