diff --git a/include/ConcurrentQueue.hpp b/include/ConcurrentQueue.hpp index 437cbc7..9190a38 100644 --- a/include/ConcurrentQueue.hpp +++ b/include/ConcurrentQueue.hpp @@ -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(); } diff --git a/include/MysqlConnectionPool.hpp b/include/MysqlConnectionPool.hpp new file mode 100644 index 0000000..0577311 --- /dev/null +++ b/include/MysqlConnectionPool.hpp @@ -0,0 +1,27 @@ +#ifndef MYSQL_CONNECTION_POOL_HPP +#define MYSQL_CONNECTION_POOL_HPP + +#include "ObjectPool.hpp" +#include "MysqlClient.hpp" + + +class MysqlConnectionPool : public ObjectPool +{ +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 diff --git a/include/ObjectPool.hpp b/include/ObjectPool.hpp new file mode 100644 index 0000000..0085bf4 --- /dev/null +++ b/include/ObjectPool.hpp @@ -0,0 +1,29 @@ +#ifndef OBJECT_POOL_HPP +#define OBJECT_POOL_HPP + +#include "ConcurrentQueue.hpp" + +template +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 m_pool; + +}; + +#endif // OBJECT_POOL_HPP diff --git a/src/MysqlConnectionPool.cpp b/src/MysqlConnectionPool.cpp new file mode 100644 index 0000000..0e1e098 --- /dev/null +++ b/src/MysqlConnectionPool.cpp @@ -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; +} \ No newline at end of file diff --git a/src/ObjectPool.cpp b/src/ObjectPool.cpp new file mode 100644 index 0000000..f108379 --- /dev/null +++ b/src/ObjectPool.cpp @@ -0,0 +1,63 @@ +#include "ObjectPool.hpp" + +#include "Logger.hpp" + + +template +ObjectPool::ObjectPool() + : m_pool() +{ + TRACE; +} + + +template +ObjectPool::~ObjectPool() +{ + TRACE; +} + + +template +void ObjectPool::add(const T object) +{ + TRACE; + m_pool.push(object); +} + + +template +void ObjectPool::remove(const T object) +{ + TRACE; + +// m_pool.tryPop(object); +} + + +template +void ObjectPool::clear() +{ + TRACE; + +// while ( !m_pool.empty() ) +// m_pool. +} + + +template +T ObjectPool::get() +{ + TRACE; + + return m_pool.waitAndPop(); +} + + +template +void ObjectPool::release(const T object) +{ + TRACE; + + m_pool.push(object); +}