15 #ifndef __PARSEBUFFER_H__
16 #define __PARSEBUFFER_H__
165 inline virtual const char*
get_name()
const {
return "ParseBuffer"; }
218 ex_in_use_mutex =
SG_MALLOC(pthread_mutex_t, ring_size);
219 ex_in_use_cond =
SG_MALLOC(pthread_cond_t, ring_size);
220 read_lock =
new pthread_mutex_t;
221 write_lock =
new pthread_mutex_t;
223 SG_SINFO(
"Initialized with ring size: %d.\n", ring_size);
228 for (int32_t i=0; i<ring_size; i++)
231 ex_ring[i].fv.vector =
new T();
232 ex_ring[i].fv.vlen = 1;
233 ex_ring[i].label = FLT_MAX;
235 pthread_cond_init(&ex_in_use_cond[i], NULL);
236 pthread_mutex_init(&ex_in_use_mutex[i], NULL);
238 pthread_mutex_init(read_lock, NULL);
239 pthread_mutex_init(write_lock, NULL);
241 free_vectors_on_destruct =
true;
246 for (int32_t i=0; i<ring_size; i++)
248 if (ex_ring[i].fv.vector != NULL && free_vectors_on_destruct)
249 delete ex_ring[i].fv.vector;
250 pthread_mutex_destroy(&ex_in_use_mutex[i]);
251 pthread_cond_destroy(&ex_in_use_cond[i]);
265 ex_ring[ex_write_index].label = ex->
label;
266 ex_ring[ex_write_index].fv.vector = ex->
fv.vector;
267 ex_ring[ex_write_index].fv.vlen = ex->
fv.vlen;
277 if (ex_read_index >= 0)
278 return &ex_ring[ex_read_index];
286 pthread_mutex_lock(read_lock);
289 int32_t current_index = ex_read_index;
292 pthread_mutex_lock(&ex_in_use_mutex[current_index]);
295 ex = return_example_to_read();
299 pthread_mutex_unlock(&ex_in_use_mutex[current_index]);
301 pthread_mutex_unlock(read_lock);
308 pthread_mutex_lock(write_lock);
310 int32_t current_index = ex_write_index;
312 pthread_mutex_lock(&ex_in_use_mutex[current_index]);
315 pthread_cond_wait(&ex_in_use_cond[ex_write_index], &ex_in_use_mutex[ex_write_index]);
318 ret = write_example(ex);
320 pthread_mutex_unlock(&ex_in_use_mutex[current_index]);
321 pthread_mutex_unlock(write_lock);
329 pthread_mutex_lock(read_lock);
330 pthread_mutex_lock(&ex_in_use_mutex[ex_read_index]);
331 ex_used[ex_read_index] =
E_USED;
333 if (free_after_release)
335 SG_DEBUG(
"Freeing object in ring at index %d and address: %p.\n",
336 ex_read_index, ex_ring[ex_read_index].fv.vector);
338 delete ex_ring[ex_read_index].fv.vector;
339 ex_ring[ex_read_index].fv.vector=NULL;
342 pthread_cond_signal(&ex_in_use_cond[ex_read_index]);
343 pthread_mutex_unlock(&ex_in_use_mutex[ex_read_index]);
346 pthread_mutex_unlock(read_lock);
350 #endif // __PARSEBUFFER_H__
bool get_free_vectors_on_destruct()
pthread_cond_t * ex_in_use_cond
Condition variable triggered when example is being/not being used.
Example< T > * get_free_example()
int32_t write_example(Example< T > *ex)
int32_t ex_read_index
Position of next example to be read.
virtual void inc_read_index()
Example< T > * get_unused_example()
SGVector< T > fv
Feature vector of type T.
pthread_mutex_t * ex_in_use_mutex
Lock on state of example - used or unused.
virtual const char * get_name() const
int32_t ring_size
Size of ring as number of examples.
void set_free_vectors_on_destruct(bool destroy)
Class SGObject is the base class of all shogun objects.
int32_t copy_example(Example< T > *ex)
CParseBuffer(int32_t size=1024)
E_IS_EXAMPLE_USED * ex_used
Enum used for representing used/unused/empty state of example.
pthread_mutex_t * write_lock
Lock for writing new examples.
pthread_mutex_t * read_lock
Lock for reading examples from the ring.
bool free_vectors_on_destruct
Whether examples on the ring will be freed on destruction.
Class CParseBuffer implements a ring of examples of a defined size. The ring stores objects of the Ex...
Example< T > * return_example_to_read()
Example< T > * ex_ring
Ring of examples.
void finalize_example(bool free_after_release)
#define SG_MALLOC(type, len)
Class Example is the container type for the vector+label combination.
virtual void inc_write_index()
#define SG_CALLOC(type, len)
int32_t ex_write_index
Write position for next example.