channel.h
Go to the documentation of this file.
/********************* */
#include "cvc4_public.h"
#ifndef __CVC4__CHANNEL_H
#define __CVC4__CHANNEL_H
#include <boost/circular_buffer.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>
#include <boost/thread/thread.hpp>
#include <boost/call_traits.hpp>
#include <boost/progress.hpp>
#include <boost/bind.hpp>
namespace CVC4 {
template <typename T>
class CVC4_PUBLIC SharedChannel {
private:
int d_maxsize; // just call it size?
public:
SharedChannel() {}
SharedChannel(int maxsize) : d_maxsize(maxsize) {}
virtual ~SharedChannel() {}
/* Tries to add element and returns true if successful */
virtual bool push(const T&) = 0;
/* Removes an element from the channel */
virtual T pop() = 0;
/* */
virtual bool empty() = 0;
/* */
virtual bool full() = 0;
};/* class SharedChannel<T> */
/*
This code is from
http://live.boost.org/doc/libs/1_46_1/libs/circular_buffer/doc/circular_buffer.html#boundedbuffer
and is covered by the Boost Software License, version 1.0.
*/
template <typename T>
class CVC4_PUBLIC SynchronizedSharedChannel : public SharedChannel<T> {
public:
typedef boost::circular_buffer<T> container_type;
typedef typename container_type::size_type size_type;
typedef typename container_type::value_type value_type;
typedef typename boost::call_traits<value_type>::param_type param_type;
explicit SynchronizedSharedChannel(size_type capacity) : m_unread(0), m_container(capacity) {}
bool push(param_type item){
// param_type represents the "best" way to pass a parameter of type value_type to a method
boost::mutex::scoped_lock lock(m_mutex);
m_not_full.wait(lock, boost::bind(&SynchronizedSharedChannel<value_type>::is_not_full, this));
m_container.push_front(item);
++m_unread;
lock.unlock();
m_not_empty.notify_one();
return true;
}//function definitions need to be moved to cpp
value_type pop(){
value_type ret;
boost::mutex::scoped_lock lock(m_mutex);
m_not_empty.wait(lock, boost::bind(&SynchronizedSharedChannel<value_type>::is_not_empty, this));
ret = m_container[--m_unread];
lock.unlock();
m_not_full.notify_one();
return ret;
}
bool empty() { return not is_not_empty(); }
bool full() { return not is_not_full(); }
private:
SynchronizedSharedChannel(const SynchronizedSharedChannel&); // Disabled copy constructor
SynchronizedSharedChannel& operator = (const SynchronizedSharedChannel&); // Disabled assign operator
bool is_not_empty() const { return m_unread > 0; }
bool is_not_full() const { return m_unread < m_container.capacity(); }
size_type m_unread;
container_type m_container;
boost::mutex m_mutex;
boost::condition m_not_empty;
boost::condition m_not_full;
};/* class SynchronizedSharedChannel<T> */
}/* CVC4 namespace */
#endif /* __CVC4__CHANNEL_H */
CVC4::SharedChannel
Definition channel.h:34
CVC4::SharedChannel::SharedChannel
SharedChannel()
Definition channel.h:38
CVC4::SharedChannel::SharedChannel
SharedChannel(int maxsize)
Definition channel.h:39
CVC4::SharedChannel::push
virtual bool push(const T &)=0
CVC4::SharedChannel::~SharedChannel
virtual ~SharedChannel()
Definition channel.h:40
CVC4::SharedChannel::empty
virtual bool empty()=0
CVC4::SharedChannel::pop
virtual T pop()=0
CVC4::SharedChannel::full
virtual bool full()=0
CVC4::SynchronizedSharedChannel
Definition channel.h:63
CVC4::SynchronizedSharedChannel::push
bool push(param_type item)
Definition channel.h:72
CVC4::SynchronizedSharedChannel::container_type
boost::circular_buffer< T > container_type
Definition channel.h:65
CVC4::SynchronizedSharedChannel::full
bool full()
Definition channel.h:96
CVC4::SynchronizedSharedChannel::SynchronizedSharedChannel
SynchronizedSharedChannel(size_type capacity)
Definition channel.h:70
CVC4::SynchronizedSharedChannel::size_type
container_type::size_type size_type
Definition channel.h:66
CVC4::SynchronizedSharedChannel::value_type
container_type::value_type value_type
Definition channel.h:67
CVC4::SynchronizedSharedChannel::pop
value_type pop()
Definition channel.h:84
CVC4::SynchronizedSharedChannel::empty
bool empty()
Definition channel.h:95
CVC4::SynchronizedSharedChannel::param_type
boost::call_traits< value_type >::param_type param_type
Definition channel.h:68
cvc4_public.h
Macros that should be defined everywhere during the building of the libraries and driver binary,...
CVC4_PUBLIC
#define CVC4_PUBLIC
Definition cvc4_public.h:30
CVC4
Definition expr.h:106