summaryrefslogtreecommitdiff
path: root/server/src/connectionpool.h
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/connectionpool.h')
-rw-r--r--server/src/connectionpool.h79
1 files changed, 62 insertions, 17 deletions
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<T> clear(bool force = true);
+
private:
bool contains(std::list<T> &list, T t);
@@ -75,10 +77,11 @@ private:
template <class T>
void ConnectionPool<T>::add(T t)
{
- mutex.lock();
+ MutexAutolock lock(mutex);
+
passive.push_back(t);
semaphore.post();
- mutex.unlock();
+
}
template <class T>
@@ -96,12 +99,13 @@ bool ConnectionPool<T>::contains(std::list<T> &list, T t)
template <class T>
void ConnectionPool<T>::remove(T t)
{
- mutex.lock();
+ MutexAutolock lock(mutex);
+
if(contains(passive, t)) {
semaphore.post();
passive.remove(t);
}
- mutex.unlock();
+
}
template <class T>
@@ -109,9 +113,8 @@ bool ConnectionPool<T>::testFree(T t)
{
bool testfree = false;
- mutex.lock();
+ MutexAutolock lock(mutex);
testfree = contains(passive, t);
- mutex.unlock();
return testfree;
}
@@ -120,9 +123,10 @@ template <class T>
int ConnectionPool<T>::numFree()
{
int num;
- mutex.lock();
+
+ MutexAutolock lock(mutex);
num = passive.size();
- mutex.unlock();
+
return num;
}
@@ -133,13 +137,13 @@ T ConnectionPool<T>::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<T>::borrow()
template <class T>
void ConnectionPool<T>::giveBack(T t)
{
- mutex.lock();
+ MutexAutolock lock(mutex);
if(contains(active, t)) {
active.remove(t);
passive.push_back(t);
semaphore.post();
}
-
- mutex.unlock();
}
+template <class T>
+std::list<T> ConnectionPool<T>::clear(bool force)
+{
+ typename std::list<T> 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<T>::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 <class T>
AutoBorrower<T>::AutoBorrower(ConnectionPool<T> &p)