new classes: ObjectPool, MysqlConnectionPool

master
Denes Matetelki 13 years ago
parent e85a403f44
commit 9ea53b322c

@ -34,12 +34,12 @@ class ConcurrentQueue {
}
void push(const T task)
void push(const T value)
{
TRACE;
ScopedLock sl(m_mutex);
if (m_cancelled) throw CancelledException();
m_queue.push( task );
m_queue.push( value );
m_condVar.signal();
}

@ -0,0 +1,27 @@
#ifndef MYSQL_CONNECTION_POOL_HPP
#define MYSQL_CONNECTION_POOL_HPP
#include "ObjectPool.hpp"
#include "MysqlClient.hpp"
class MysqlConnectionPool : public ObjectPool<MysqlClient *>
{
public:
MysqlConnectionPool();
~MysqlConnectionPool();
MysqlClient* create( const char *host = NULL,
const char *user = NULL,
const char *passwd = NULL,
const char *db = NULL,
unsigned int port = 0,
const char *unix_socket = NULL,
unsigned long clientflag = 0 );
bool reset(const MysqlClient* client);
};
#endif // MYSQL_CONNECTION_POOL_HPP

@ -0,0 +1,29 @@
#ifndef OBJECT_POOL_HPP
#define OBJECT_POOL_HPP
#include "ConcurrentQueue.hpp"
template <typename T>
class ObjectPool
{
public:
ObjectPool();
virtual ~ObjectPool();
void add(const T object);
void remove(const T object);
void clear();
T get();
virtual void reset(const T object) = 0;
void release(const T object);
private:
ConcurrentQueue<T> m_pool;
};
#endif // OBJECT_POOL_HPP

@ -0,0 +1,47 @@
#include "MysqlConnectionPool.hpp"
#include "Logger.hpp"
MysqlConnectionPool::MysqlConnectionPool()
{
TRACE;
}
MysqlConnectionPool::~MysqlConnectionPool()
{
TRACE;
}
MysqlClient* MysqlConnectionPool::create( const char* host,
const char* user,
const char* passwd,
const char* db,
unsigned int port,
const char* unix_socket,
long unsigned int clientflag )
{
TRACE;
MysqlClient *client = new MysqlClient(host,
user,
passwd,
db,
port,
unix_socket,
clientflag);
return client;
}
bool MysqlConnectionPool::reset(const MysqlClient* client)
{
TRACE;
// The MysqlClient is stateless
return true;
}

@ -0,0 +1,63 @@
#include "ObjectPool.hpp"
#include "Logger.hpp"
template <typename T>
ObjectPool<T>::ObjectPool()
: m_pool()
{
TRACE;
}
template <typename T>
ObjectPool<T>::~ObjectPool()
{
TRACE;
}
template <typename T>
void ObjectPool<T>::add(const T object)
{
TRACE;
m_pool.push(object);
}
template <typename T>
void ObjectPool<T>::remove(const T object)
{
TRACE;
// m_pool.tryPop(object);
}
template <typename T>
void ObjectPool<T>::clear()
{
TRACE;
// while ( !m_pool.empty() )
// m_pool.
}
template <typename T>
T ObjectPool<T>::get()
{
TRACE;
return m_pool.waitAndPop();
}
template <typename T>
void ObjectPool<T>::release(const T object)
{
TRACE;
m_pool.push(object);
}
Loading…
Cancel
Save