Specialization of ConqurrentDeque::freeDeque and ObjectPool::release with Substitution failure is not an error (SFINAE)

master
Denes Matetelki 13 years ago
parent 8aecc59916
commit fdb6c5b7a1

@ -4,7 +4,7 @@ project (CPP_UTILS_LIB)
set(CMAKE_CXX_COMPILER "/usr/lib/colorgcc/bin/g++") set(CMAKE_CXX_COMPILER "/usr/lib/colorgcc/bin/g++")
set (CXX_FLAGS "-Wall -Wextra -pedantic -Weffc++ -Wshadow " set (CXX_FLAGS "-Wall -Wextra -pedantic -Weffc++ -Wshadow "
"-ggdb -fprofile-arcs -ftest-coverage") "-ggdb -fprofile-arcs -ftest-coverage -std=c++0x")
add_definitions( ${CXX_FLAGS} ) add_definitions( ${CXX_FLAGS} )
include_directories (../include) include_directories (../include)

@ -3,6 +3,7 @@
#include <deque> #include <deque>
#include <algorithm> #include <algorithm>
#include <type_traits>
#include "Mutex.hpp" #include "Mutex.hpp"
#include "ConditionVariable.hpp" #include "ConditionVariable.hpp"
@ -15,11 +16,11 @@ class CancelledException {};
template <typename T> template <typename T>
class ConcurrentQueue class ConcurrentDeque
{ {
public: public:
ConcurrentQueue() ConcurrentDeque()
: m_queue() : m_queue()
, m_cancelled(false) , m_cancelled(false)
, m_mutex() , m_mutex()
@ -28,9 +29,10 @@ public:
TRACE; TRACE;
} }
~ConcurrentQueue() ~ConcurrentDeque()
{ {
TRACE; TRACE;
freeDeque();
} }
@ -81,29 +83,32 @@ public:
return m_queue.empty(); return m_queue.empty();
} }
void cancel()
void cancel(T a)
{ {
TRACE; TRACE;
ScopedLock sl(m_mutex); ScopedLock sl(m_mutex);
m_cancelled = true; m_cancelled = true;
m_condVar.broadcast(); m_condVar.broadcast();
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;
m_queue.clear(); m_queue.clear();
} }
void cancel(T *a) template<class U = T>
typename std::enable_if< !(std::is_pointer<U>::value) >::type
freeDeque()
{ {
TRACE; TRACE;
ScopedLock sl(m_mutex);
m_cancelled = true;
m_condVar.broadcast();
typename std::deque<T*>::iterator it;
for ( it = m_queue.begin(); it != m_queue.end(); ++it ) {
LOG( Logger::INFO, std::string("Deleting: ").append(*it).c_str() );
delete *it;
}
m_queue.clear(); m_queue.clear();
} }
@ -116,8 +121,8 @@ public:
private: private:
ConcurrentQueue& operator=( const ConcurrentQueue& ); ConcurrentDeque& operator=( const ConcurrentDeque& );
ConcurrentQueue( const ConcurrentQueue& ); ConcurrentDeque( const ConcurrentDeque& );
std::deque<T> m_queue; std::deque<T> m_queue;
bool m_cancelled; bool m_cancelled;

@ -17,7 +17,6 @@ public:
void create(); void create();
/// @note Shall this be a specialized ObjectPool::clear?
void clear(); void clear();
private: private:

@ -1,7 +1,7 @@
#ifndef OBJECT_POOL_HPP #ifndef OBJECT_POOL_HPP
#define OBJECT_POOL_HPP #define OBJECT_POOL_HPP
#include "ConcurrentQueue.hpp" #include "ConcurrentDeque.hpp"
#include "Logger.hpp" #include "Logger.hpp"
template <typename T> template <typename T>
@ -36,11 +36,14 @@ public:
return tmp; return tmp;
} }
void release(T object) template<class U = T>
typename std::enable_if< std::is_pointer<U>::value >::type
release(T object)
{ {
TRACE; TRACE;
if ( m_pool.cancelled() ) { if ( m_pool.cancelled() ) {
m_numberOfUsedObjects--; m_numberOfUsedObjects--;
delete object;
return; return;
} }
@ -48,12 +51,13 @@ public:
m_numberOfUsedObjects--; m_numberOfUsedObjects--;
} }
void release(T* object) template<class U = T>
typename std::enable_if< !(std::is_pointer<U>::value) >::type
release(T object)
{ {
TRACE; TRACE;
if ( m_pool.cancelled() ) { if ( m_pool.cancelled() ) {
m_numberOfUsedObjects--; m_numberOfUsedObjects--;
delete object;
return; return;
} }
@ -61,21 +65,16 @@ public:
m_numberOfUsedObjects--; m_numberOfUsedObjects--;
} }
void clear(T a) void clear()
{ {
TRACE; TRACE;
m_pool.cancel(a); m_pool.cancel();
} }
void clear(T* a)
{
TRACE;
m_pool.cancel(a);
}
private: private:
ConcurrentQueue<T> m_pool; ConcurrentDeque<T> m_pool;
int m_numberOfUsedObjects; int m_numberOfUsedObjects;
}; };

@ -3,7 +3,7 @@
#include <vector> #include <vector>
#include "ConcurrentQueue.hpp" #include "ConcurrentDeque.hpp"
#include "Task.hpp" #include "Task.hpp"
#include "Thread.hpp" #include "Thread.hpp"
#include "Mutex.hpp" #include "Mutex.hpp"
@ -32,7 +32,7 @@ class ThreadPool
ThreadPool& operator=( const ThreadPool& ); ThreadPool& operator=( const ThreadPool& );
std::vector<Thread*> m_threads; std::vector<Thread*> m_threads;
ConcurrentQueue<Task*> m_tasks; ConcurrentDeque<Task*> m_tasks;
}; };

@ -34,11 +34,15 @@ public:
op.add(a); op.add(a);
op.add(b); op.add(b);
TS_ASSERT_EQUALS( op.acquire(), a ); int *tmp_a = op.acquire();
TS_ASSERT_EQUALS( op.acquire(), b ); int *tmp_b = op.acquire();
TS_ASSERT_EQUALS( *tmp_a, *a );
TS_ASSERT_EQUALS( *tmp_b, *b );
delete a; // release will delete them
delete b; op.release(tmp_a);
op.release(tmp_b);
} }
@ -100,7 +104,7 @@ public:
t1.join(); t1.join();
t2.join(); t2.join();
delete a; // no need to delete "a", dtor of the ConqurrentDeque takes care of it
} }

Loading…
Cancel
Save