You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
2.1 KiB
135 lines
2.1 KiB
14 years ago
|
#ifndef CONCURRENTQUEUE_HPP
|
||
|
#define CONCURRENTQUEUE_HPP
|
||
|
|
||
13 years ago
|
#include <deque>
|
||
13 years ago
|
#include <algorithm>
|
||
13 years ago
|
#include <type_traits>
|
||
14 years ago
|
|
||
|
#include "Mutex.hpp"
|
||
|
#include "ConditionVariable.hpp"
|
||
|
#include "ScopedLock.hpp"
|
||
|
#include "Common.hpp"
|
||
|
|
||
|
|
||
|
|
||
|
class CancelledException {};
|
||
|
|
||
|
|
||
|
template <typename T>
|
||
13 years ago
|
class ConcurrentDeque
|
||
13 years ago
|
{
|
||
|
public:
|
||
|
|
||
13 years ago
|
ConcurrentDeque()
|
||
13 years ago
|
: m_queue()
|
||
|
, m_cancelled(false)
|
||
|
, m_mutex()
|
||
|
, m_condVar(m_mutex)
|
||
|
{
|
||
|
TRACE;
|
||
|
}
|
||
|
|
||
13 years ago
|
~ConcurrentDeque()
|
||
13 years ago
|
{
|
||
|
TRACE;
|
||
13 years ago
|
freeDeque();
|
||
13 years ago
|
}
|
||
|
|
||
|
|
||
|
void push(const T value)
|
||
|
{
|
||
|
TRACE;
|
||
|
ScopedLock sl(m_mutex);
|
||
|
if (m_cancelled) throw CancelledException();
|
||
13 years ago
|
m_queue.push_back( value );
|
||
13 years ago
|
m_condVar.signal();
|
||
|
}
|
||
|
|
||
|
|
||
|
bool tryPop(T &popped_value)
|
||
|
{
|
||
|
TRACE;
|
||
|
ScopedLock sl(m_mutex);
|
||
|
if (m_cancelled) throw CancelledException();
|
||
|
if ( m_queue.empty() ) return false;
|
||
|
|
||
|
popped_value = m_queue.front();
|
||
13 years ago
|
m_queue.pop_front();
|
||
13 years ago
|
return true;
|
||
|
}
|
||
|
|
||
|
|
||
|
T waitAndPop()
|
||
|
{
|
||
|
TRACE;
|
||
|
ScopedLock sl(m_mutex);
|
||
|
|
||
|
while ( m_queue.empty() and not m_cancelled) {
|
||
|
m_condVar.wait();
|
||
14 years ago
|
}
|
||
13 years ago
|
if (m_cancelled) throw CancelledException();
|
||
14 years ago
|
|
||
13 years ago
|
T retVal = m_queue.front(); // cctor
|
||
13 years ago
|
m_queue.pop_front();
|
||
13 years ago
|
return retVal;
|
||
|
}
|
||
14 years ago
|
|
||
|
|
||
13 years ago
|
bool empty() const
|
||
|
{
|
||
|
TRACE;
|
||
|
ScopedLock sl(m_mutex);
|
||
|
if (m_cancelled) throw CancelledException();
|
||
|
return m_queue.empty();
|
||
|
}
|
||
14 years ago
|
|
||
13 years ago
|
void cancel()
|
||
13 years ago
|
{
|
||
|
TRACE;
|
||
|
ScopedLock sl(m_mutex);
|
||
|
m_cancelled = true;
|
||
|
m_condVar.broadcast();
|
||
13 years ago
|
freeDeque();
|
||
|
}
|
||
|
|
||
|
template<class U = T>
|
||
|
typename std::enable_if< std::is_pointer<U>::value >::type
|
||
|
freeDeque()
|
||
|
{
|
||
|
TRACE;
|
||
|
typename std::deque<T>::iterator it;
|
||
|
for ( it = m_queue.begin(); it != m_queue.end(); ++it )
|
||
|
delete *it;
|
||
13 years ago
|
|
||
|
m_queue.clear();
|
||
|
}
|
||
|
|
||
13 years ago
|
template<class U = T>
|
||
|
typename std::enable_if< !(std::is_pointer<U>::value) >::type
|
||
|
freeDeque()
|
||
13 years ago
|
{
|
||
|
TRACE;
|
||
|
m_queue.clear();
|
||
|
}
|
||
|
|
||
|
bool cancelled()
|
||
|
{
|
||
|
TRACE;
|
||
|
return m_cancelled;
|
||
13 years ago
|
}
|
||
14 years ago
|
|
||
13 years ago
|
private:
|
||
14 years ago
|
|
||
13 years ago
|
|
||
13 years ago
|
ConcurrentDeque& operator=( const ConcurrentDeque& );
|
||
|
ConcurrentDeque( const ConcurrentDeque& );
|
||
14 years ago
|
|
||
13 years ago
|
std::deque<T> m_queue;
|
||
13 years ago
|
bool m_cancelled;
|
||
|
mutable Mutex m_mutex;
|
||
|
ConditionVariable m_condVar;
|
||
14 years ago
|
|
||
|
};
|
||
|
|
||
|
#endif // CONCURRENTQUEUE_HPP
|