From 0e819eb42b4d680a99ae7b04702bfc9510495aee Mon Sep 17 00:00:00 2001 From: deva Date: Wed, 6 Jan 2010 07:47:58 +0000 Subject: New artefact connection class (to later wrap libartefact). New environment class to hold all global resources. Made ConnectionPool a template class. Split journal code up into two files (class from commit code). --- server/src/connectionpool.h | 143 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 127 insertions(+), 16 deletions(-) (limited to 'server/src/connectionpool.h') diff --git a/server/src/connectionpool.h b/server/src/connectionpool.h index 548d76a..77fc33b 100644 --- a/server/src/connectionpool.h +++ b/server/src/connectionpool.h @@ -33,38 +33,149 @@ #include "mutex.h" #include "semaphore.h" -class Database; - +template class ConnectionPool { public: - ConnectionPool(); - - void addDatabase(Database *db); - void removeDatabase(Database *db); + void add(T t); + void remove(T t); - bool testFree(Database *db); - int numFreeDatabases(); + bool testFree(T t); + int numFree(); - Database *borrowDatabase(); - void returnDatabase(Database *db); + T borrow(); + void giveBack(T t); private: + bool contains(std::list &list, T t); + Semaphore semaphore; Mutex mutex; - std::list activedbs; - std::list passivedbs; + std::list active; + std::list passive; }; +template class AutoBorrower { public: - AutoBorrower(ConnectionPool &pool); + AutoBorrower(ConnectionPool &pool); ~AutoBorrower(); - Database *db(); + T get(); private: - ConnectionPool &pool; - Database *_db; + ConnectionPool &pool; + T t; }; + +// +// Implementation is below +// + +template +void ConnectionPool::add(T t) +{ + mutex.lock(); + passive.push_back(t); + semaphore.post(); + mutex.unlock(); +} + +template +bool ConnectionPool::contains(std::list &list, T t) +{ + typename std::list::iterator i = list.begin(); + while(i != list.end()) { + if(*i == t) return true; + i++; + } + + return false; +} + +template +void ConnectionPool::remove(T t) +{ + mutex.lock(); + if(contains(passive, t)) { + semaphore.post(); + passive.remove(t); + } + mutex.unlock(); +} + +template +bool ConnectionPool::testFree(T t) +{ + bool testfree = false; + + mutex.lock(); + testfree = contains(passive, t); + mutex.unlock(); + + return testfree; +} + +template +int ConnectionPool::numFree() +{ + int num; + mutex.lock(); + num = passive.size(); + mutex.unlock(); + return num; +} + +template +T ConnectionPool::borrow() +{ + T t = NULL; + + semaphore.wait(); + + mutex.lock(); + + t = passive.front(); + passive.remove(t); + active.push_back(t); + + mutex.unlock(); + + return t; +} + +template +void ConnectionPool::giveBack(T t) +{ + mutex.lock(); + + if(contains(active, t)) { + active.remove(t); + passive.push_back(t); + semaphore.post(); + } + + mutex.unlock(); +} + + +template +AutoBorrower::AutoBorrower(ConnectionPool &p) + : pool(p) +{ + t = pool.borrow(); +} + +template +AutoBorrower::~AutoBorrower() +{ + pool.giveBack(t); +} + +template +T AutoBorrower::get() +{ + return t; +} + #endif/*__PRACRO_CONNECTIONPOOL_H__*/ -- cgit v1.2.3