/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set et sw=2 ts=2: */ /*************************************************************************** * connectionpool.h * * Wed Dec 16 12:20:44 CET 2009 * Copyright 2009 Bent Bisballe Nyeng * deva@aasimon.org ****************************************************************************/ /* * This file is part of Pracro. * * Pracro is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Pracro is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Pracro; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ #ifndef __PRACRO_CONNECTIONPOOL_H__ #define __PRACRO_CONNECTIONPOOL_H__ #include #include "mutex.h" #include "semaphore.h" template class ConnectionPool { public: void add(T t); void remove(T t); bool testFree(T t); int numFree(); T borrow(); void giveBack(T t); private: bool contains(std::list &list, T t); Semaphore semaphore; Mutex mutex; std::list active; std::list passive; }; template class AutoBorrower { public: AutoBorrower(ConnectionPool &pool); ~AutoBorrower(); T get(); private: 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__*/