Lost in the template specialization - type as pointer deduction jungle

master
Denes Matetelki 13 years ago
parent 0f69afd01d
commit 8aecc59916

@ -1,7 +1,7 @@
#ifndef CONCURRENTQUEUE_HPP #ifndef CONCURRENTQUEUE_HPP
#define CONCURRENTQUEUE_HPP #define CONCURRENTQUEUE_HPP
#include <queue> #include <deque>
#include <algorithm> #include <algorithm>
#include "Mutex.hpp" #include "Mutex.hpp"
@ -39,7 +39,7 @@ public:
TRACE; TRACE;
ScopedLock sl(m_mutex); ScopedLock sl(m_mutex);
if (m_cancelled) throw CancelledException(); if (m_cancelled) throw CancelledException();
m_queue.push( value ); m_queue.push_back( value );
m_condVar.signal(); m_condVar.signal();
} }
@ -52,7 +52,7 @@ public:
if ( m_queue.empty() ) return false; if ( m_queue.empty() ) return false;
popped_value = m_queue.front(); popped_value = m_queue.front();
m_queue.pop(); m_queue.pop_front();
return true; return true;
} }
@ -68,7 +68,7 @@ public:
if (m_cancelled) throw CancelledException(); if (m_cancelled) throw CancelledException();
T retVal = m_queue.front(); // cctor T retVal = m_queue.front(); // cctor
m_queue.pop(); m_queue.pop_front();
return retVal; return retVal;
} }
@ -82,20 +82,44 @@ public:
} }
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();
m_queue.clear();
}
void cancel(T *a)
{
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();
}
bool cancelled()
{
TRACE;
return m_cancelled;
} }
private: private:
ConcurrentQueue& operator=( const ConcurrentQueue& ); ConcurrentQueue& operator=( const ConcurrentQueue& );
ConcurrentQueue( const ConcurrentQueue& ); ConcurrentQueue( const ConcurrentQueue& );
std::queue<T> m_queue; std::deque<T> m_queue;
bool m_cancelled; bool m_cancelled;
mutable Mutex m_mutex; mutable Mutex m_mutex;
ConditionVariable m_condVar; ConditionVariable m_condVar;

@ -9,7 +9,9 @@ class ObjectPool
{ {
public: public:
ObjectPool() : m_pool() ObjectPool()
: m_pool()
, m_numberOfUsedObjects(0)
{ {
TRACE; TRACE;
} }
@ -19,29 +21,62 @@ public:
TRACE; TRACE;
} }
void add(const T object) // throws CancelledException
{
TRACE;
m_pool.push(object);
}
T acquire()
T acquire() // throws CancelledException
{ {
TRACE; TRACE;
return m_pool.waitAndPop(); T tmp = m_pool.waitAndPop();
m_numberOfUsedObjects++;
return tmp;
} }
void release(const T object) void release(T object)
{ {
TRACE; TRACE;
if ( m_pool.cancelled() ) {
m_numberOfUsedObjects--;
return;
}
m_pool.push(object); m_pool.push(object);
m_numberOfUsedObjects--;
} }
bool empty() const void release(T* object)
{ {
TRACE; TRACE;
return m_pool.empty(); if ( m_pool.cancelled() ) {
m_numberOfUsedObjects--;
delete object;
return;
}
m_pool.push(object);
m_numberOfUsedObjects--;
} }
void clear(T a)
{
TRACE;
m_pool.cancel(a);
}
void clear(T* a)
{
TRACE;
m_pool.cancel(a);
}
private: private:
ConcurrentQueue<T> m_pool; ConcurrentQueue<T> m_pool;
int m_numberOfUsedObjects;
}; };
#endif // OBJECT_POOL_HPP #endif // OBJECT_POOL_HPP

@ -17,17 +17,15 @@ public:
void sendSignal( const int nSignal ) const; void sendSignal( const int nSignal ) const;
bool isRunning() const; bool isRunning() const;
private:
virtual void* run() = 0;
static void* threadStarter( void* pData );
protected: protected:
bool m_isRunning; bool m_isRunning;
private: private:
virtual void* run() = 0;
static void* threadStarter( void* pData );
pthread_t m_threadHandler; pthread_t m_threadHandler;
}; };

@ -27,12 +27,5 @@ void MysqlConnectionPool::create()
MysqlClient *client = new MysqlClient ( m_host, m_user, m_passwd, m_db ); MysqlClient *client = new MysqlClient ( m_host, m_user, m_passwd, m_db );
client->connect(); client->connect();
release(client); add(client);
}
void MysqlConnectionPool::clear()
{
TRACE;
while ( !empty() )
delete acquire();
} }

@ -61,7 +61,8 @@ void ThreadPool::stop()
(*it)->stop(); (*it)->stop();
} }
m_tasks.cancel(); /// @todo solve this!
// m_tasks.cancel( );
} }

@ -18,20 +18,21 @@ if(CXXTEST_FOUND)
generated_main.cpp generated_main.cpp
Fixture.hpp Fixture.hpp
test_ArgParse.hpp # test_ArgParse.hpp
test_Common.hpp # test_Common.hpp
test_ConditionalVariable.hpp # test_ConditionalVariable.hpp
test_Multiton.hpp # test_Multiton.hpp
test_Mutex.hpp # test_Mutex.hpp
test_ScopedLock.hpp test_ObjectPool.hpp
test_Semaphore.hpp # test_ScopedLock.hpp
test_Singleton_DCLP.hpp # test_Semaphore.hpp
test_Singleton_call_once.hpp # test_Singleton_DCLP.hpp
# test_Singleton.hpp Cannot test private member, Ficture.hpp loads it # test_Singleton_call_once.hpp
test_Singleton_meyers.hpp # # test_Singleton.hpp Cannot test private member, Ficture.hpp loads it
test_Thread.hpp # test_Singleton_meyers.hpp
test_ThreadPool.hpp # test_Thread.hpp
# test_Timer.hpp Takes too much time&buggy # test_ThreadPool.hpp
# # test_Timer.hpp Takes too much time&buggy
) )
target_link_libraries(testCppUtils CppUtils gcov) target_link_libraries(testCppUtils CppUtils gcov)
endif() endif()

@ -0,0 +1,130 @@
#include <cxxtest/TestSuite.h>
#include "Common.hpp"
#include "Fixture.hpp"
#include "ObjectPool.hpp"
#include "Thread.hpp"
class TestObjectPool : public CxxTest::TestSuite
{
public:
void testBasic( void )
{
TEST_HEADER;
ObjectPool<int> op;
int a(1);
op.add(a);
TS_ASSERT_EQUALS( op.acquire(), a );
}
void testPointers( void )
{
TEST_HEADER;
ObjectPool<int*> op;
int *a = new int(1);
int *b = new int(2);
op.add(a);
op.add(b);
TS_ASSERT_EQUALS( op.acquire(), a );
TS_ASSERT_EQUALS( op.acquire(), b );
delete a;
delete b;
}
private:
class ObjecPoolUserThread : public Thread
{
public:
ObjecPoolUserThread( ObjectPool<int*> &objectPool
)
: m_objectPool(objectPool)
{
TRACE;
}
private:
void* run()
{
TRACE;
int *a;
try {
a = m_objectPool.acquire();
LOG( Logger::DEBUG, std::string("Acquired int: ").
append(TToStr(*a)).c_str() );
} catch ( CancelledException ex ) {
LOG( Logger::DEBUG, "Cancelled while acquiring" );
}
sleep(1);
try {
m_objectPool.release(a);
} catch ( CancelledException ex ) {
LOG( Logger::DEBUG, "Cancelled while releasing" );
}
return 0;
}
ObjectPool<int*> &m_objectPool;
};
public:
void testCompetingThreads( void )
{
TEST_HEADER;
ObjectPool<int*> op;
ObjecPoolUserThread t1(op);
ObjecPoolUserThread t2(op);
int *a = new int(27);
op.add(a);
t1.start();
t2.start();
t1.join();
t2.join();
delete a;
}
public:
void testCleanUp( void )
{
TEST_HEADER;
ObjectPool<int*> cop;
int *a = new int(12);
int *b = new int(13);
int *c = new int(14);
cop.add(a);
cop.add(b);
cop.add(c);
// ObjecPoolUserThread t1(cop);
// ObjecPoolUserThread t2(cop);
// t1.start();
// t2.start();
cop.clear();
}
};

@ -106,7 +106,6 @@ public:
t1->join(); t1->join();
t2->join(); t2->join();
sleep(1);
delete t1; delete t1;
delete t2; delete t2;
} }

Loading…
Cancel
Save