From fdb6c5b7a1b8f14d0a51785b85f57ee080ccea6e Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Tue, 15 Nov 2011 15:06:45 +0100 Subject: [PATCH] Specialization of ConqurrentDeque::freeDeque and ObjectPool::release with Substitution failure is not an error (SFINAE) --- build/CMakeLists.txt | 2 +- ...oncurrentQueue.hpp => ConcurrentDeque.hpp} | 39 +++++++++++-------- include/MysqlConnectionPool.hpp | 1 - include/ObjectPool.hpp | 23 ++++++----- include/ThreadPool.hpp | 4 +- test/test_ObjectPool.hpp | 14 ++++--- 6 files changed, 45 insertions(+), 38 deletions(-) rename include/{ConcurrentQueue.hpp => ConcurrentDeque.hpp} (74%) diff --git a/build/CMakeLists.txt b/build/CMakeLists.txt index ac6ba0b..f501db5 100644 --- a/build/CMakeLists.txt +++ b/build/CMakeLists.txt @@ -4,7 +4,7 @@ project (CPP_UTILS_LIB) set(CMAKE_CXX_COMPILER "/usr/lib/colorgcc/bin/g++") 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} ) include_directories (../include) diff --git a/include/ConcurrentQueue.hpp b/include/ConcurrentDeque.hpp similarity index 74% rename from include/ConcurrentQueue.hpp rename to include/ConcurrentDeque.hpp index 57e309e..a5908f7 100644 --- a/include/ConcurrentQueue.hpp +++ b/include/ConcurrentDeque.hpp @@ -3,6 +3,7 @@ #include #include +#include #include "Mutex.hpp" #include "ConditionVariable.hpp" @@ -15,11 +16,11 @@ class CancelledException {}; template -class ConcurrentQueue +class ConcurrentDeque { public: - ConcurrentQueue() + ConcurrentDeque() : m_queue() , m_cancelled(false) , m_mutex() @@ -28,9 +29,10 @@ public: TRACE; } - ~ConcurrentQueue() + ~ConcurrentDeque() { TRACE; + freeDeque(); } @@ -81,29 +83,32 @@ public: return m_queue.empty(); } - - void cancel(T a) + void cancel() { TRACE; ScopedLock sl(m_mutex); m_cancelled = true; m_condVar.broadcast(); + freeDeque(); + } + + template + typename std::enable_if< std::is_pointer::value >::type + freeDeque() + { + TRACE; + typename std::deque::iterator it; + for ( it = m_queue.begin(); it != m_queue.end(); ++it ) + delete *it; m_queue.clear(); } - void cancel(T *a) + template + typename std::enable_if< !(std::is_pointer::value) >::type + freeDeque() { TRACE; - ScopedLock sl(m_mutex); - m_cancelled = true; - m_condVar.broadcast(); - - typename std::deque::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(); } @@ -116,8 +121,8 @@ public: private: - ConcurrentQueue& operator=( const ConcurrentQueue& ); - ConcurrentQueue( const ConcurrentQueue& ); + ConcurrentDeque& operator=( const ConcurrentDeque& ); + ConcurrentDeque( const ConcurrentDeque& ); std::deque m_queue; bool m_cancelled; diff --git a/include/MysqlConnectionPool.hpp b/include/MysqlConnectionPool.hpp index 9b3f069..668b6ac 100644 --- a/include/MysqlConnectionPool.hpp +++ b/include/MysqlConnectionPool.hpp @@ -17,7 +17,6 @@ public: void create(); - /// @note Shall this be a specialized ObjectPool::clear? void clear(); private: diff --git a/include/ObjectPool.hpp b/include/ObjectPool.hpp index 8fdb63d..1fb092a 100644 --- a/include/ObjectPool.hpp +++ b/include/ObjectPool.hpp @@ -1,7 +1,7 @@ #ifndef OBJECT_POOL_HPP #define OBJECT_POOL_HPP -#include "ConcurrentQueue.hpp" +#include "ConcurrentDeque.hpp" #include "Logger.hpp" template @@ -36,11 +36,14 @@ public: return tmp; } - void release(T object) + template + typename std::enable_if< std::is_pointer::value >::type + release(T object) { TRACE; if ( m_pool.cancelled() ) { m_numberOfUsedObjects--; + delete object; return; } @@ -48,12 +51,13 @@ public: m_numberOfUsedObjects--; } - void release(T* object) + template + typename std::enable_if< !(std::is_pointer::value) >::type + release(T object) { TRACE; if ( m_pool.cancelled() ) { m_numberOfUsedObjects--; - delete object; return; } @@ -61,21 +65,16 @@ public: m_numberOfUsedObjects--; } - void clear(T a) + void clear() { TRACE; - m_pool.cancel(a); + m_pool.cancel(); } - void clear(T* a) - { - TRACE; - m_pool.cancel(a); - } private: - ConcurrentQueue m_pool; + ConcurrentDeque m_pool; int m_numberOfUsedObjects; }; diff --git a/include/ThreadPool.hpp b/include/ThreadPool.hpp index f88ac26..300b117 100644 --- a/include/ThreadPool.hpp +++ b/include/ThreadPool.hpp @@ -3,7 +3,7 @@ #include -#include "ConcurrentQueue.hpp" +#include "ConcurrentDeque.hpp" #include "Task.hpp" #include "Thread.hpp" #include "Mutex.hpp" @@ -32,7 +32,7 @@ class ThreadPool ThreadPool& operator=( const ThreadPool& ); std::vector m_threads; - ConcurrentQueue m_tasks; + ConcurrentDeque m_tasks; }; diff --git a/test/test_ObjectPool.hpp b/test/test_ObjectPool.hpp index 64a8b05..b0bb51a 100644 --- a/test/test_ObjectPool.hpp +++ b/test/test_ObjectPool.hpp @@ -34,11 +34,15 @@ public: op.add(a); op.add(b); - TS_ASSERT_EQUALS( op.acquire(), a ); - TS_ASSERT_EQUALS( op.acquire(), b ); + int *tmp_a = op.acquire(); + int *tmp_b = op.acquire(); + + TS_ASSERT_EQUALS( *tmp_a, *a ); + TS_ASSERT_EQUALS( *tmp_b, *b ); - delete a; - delete b; + // release will delete them + op.release(tmp_a); + op.release(tmp_b); } @@ -100,7 +104,7 @@ public: t1.join(); t2.join(); - delete a; + // no need to delete "a", dtor of the ConqurrentDeque takes care of it }