From f92dd279a1e26dad7507d5d6944567c23834d440 Mon Sep 17 00:00:00 2001 From: deva Date: Thu, 27 May 2010 09:45:12 +0000 Subject: A lot of session handling. A lot of new unit tests. Add of a more structured commit/discard handling. Fix of some wierd line break bugs in journalwriter --- server/src/connectionpool.h | 79 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 62 insertions(+), 17 deletions(-) (limited to 'server/src/connectionpool.h') diff --git a/server/src/connectionpool.h b/server/src/connectionpool.h index 77fc33b..33473e5 100644 --- a/server/src/connectionpool.h +++ b/server/src/connectionpool.h @@ -45,6 +45,8 @@ public: T borrow(); void giveBack(T t); + std::list clear(bool force = true); + private: bool contains(std::list &list, T t); @@ -75,10 +77,11 @@ private: template void ConnectionPool::add(T t) { - mutex.lock(); + MutexAutolock lock(mutex); + passive.push_back(t); semaphore.post(); - mutex.unlock(); + } template @@ -96,12 +99,13 @@ bool ConnectionPool::contains(std::list &list, T t) template void ConnectionPool::remove(T t) { - mutex.lock(); + MutexAutolock lock(mutex); + if(contains(passive, t)) { semaphore.post(); passive.remove(t); } - mutex.unlock(); + } template @@ -109,9 +113,8 @@ bool ConnectionPool::testFree(T t) { bool testfree = false; - mutex.lock(); + MutexAutolock lock(mutex); testfree = contains(passive, t); - mutex.unlock(); return testfree; } @@ -120,9 +123,10 @@ template int ConnectionPool::numFree() { int num; - mutex.lock(); + + MutexAutolock lock(mutex); num = passive.size(); - mutex.unlock(); + return num; } @@ -133,13 +137,13 @@ T ConnectionPool::borrow() semaphore.wait(); - mutex.lock(); + { + MutexAutolock lock(mutex); - t = passive.front(); - passive.remove(t); - active.push_back(t); - - mutex.unlock(); + t = passive.front(); + passive.remove(t); + active.push_back(t); + } return t; } @@ -147,17 +151,58 @@ T ConnectionPool::borrow() template void ConnectionPool::giveBack(T t) { - mutex.lock(); + MutexAutolock lock(mutex); if(contains(active, t)) { active.remove(t); passive.push_back(t); semaphore.post(); } - - mutex.unlock(); } +template +std::list ConnectionPool::clear(bool force) +{ + typename std::list lst; + + if(force == false) { + size_t num = 0; + { + MutexAutolock lock(mutex); + num = active.size() + passive.size(); + } + + while(num) { + borrow(); + num--; + } + } + + { + MutexAutolock lock(mutex); + + typename std::list::iterator i; + + i = active.begin(); + while(i != active.end()) { + lst.push_back(*i); + // i = active.erase(i); + semaphore.post(); + i++; + } + active.clear(); + + i = passive.begin(); + while(i != passive.end()) { + lst.push_back(*i); + // i = passive.erase(i); + i++; + } + passive.clear(); + } + + return lst; +} template AutoBorrower::AutoBorrower(ConnectionPool &p) -- cgit v1.2.3