2006-10-04 13:22:18 +02:00
|
|
|
/*****************************************************************************
|
2012-02-02 02:53:35 +01:00
|
|
|
*
|
2006-10-04 13:22:18 +02:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2011-10-23 15:04:25 +02:00
|
|
|
* Copyright (C) 2011 Artem Pavlenko
|
2006-10-04 13:22:18 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2011-10-23 16:09:47 +02:00
|
|
|
#ifndef MAPNIK_POOL_HPP
|
|
|
|
#define MAPNIK_POOL_HPP
|
2006-10-04 13:22:18 +02:00
|
|
|
|
2007-10-08 20:10:31 +02:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/utils.hpp>
|
2011-10-23 16:09:47 +02:00
|
|
|
|
2007-10-08 20:10:31 +02:00
|
|
|
// boost
|
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
#include <boost/utility.hpp>
|
2008-02-04 17:12:13 +01:00
|
|
|
#ifdef MAPNIK_THREADSAFE
|
|
|
|
#include <boost/thread/mutex.hpp>
|
|
|
|
#endif
|
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
// stl
|
|
|
|
#include <iostream>
|
|
|
|
#include <map>
|
|
|
|
#include <deque>
|
|
|
|
#include <ctime>
|
|
|
|
|
|
|
|
namespace mapnik
|
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
template <typename T, typename PoolT>
|
|
|
|
class PoolGuard
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
const T& obj_;
|
2012-02-02 02:53:35 +01:00
|
|
|
PoolT& pool_;
|
2010-06-02 13:03:30 +02:00
|
|
|
public:
|
|
|
|
explicit PoolGuard(const T& ptr,PoolT& pool)
|
|
|
|
: obj_(ptr),
|
|
|
|
pool_(pool) {}
|
2006-10-04 13:22:18 +02:00
|
|
|
|
2012-02-02 02:53:35 +01:00
|
|
|
~PoolGuard()
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
pool_->returnObject(obj_);
|
|
|
|
}
|
2006-10-04 13:22:18 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
private:
|
|
|
|
PoolGuard();
|
|
|
|
PoolGuard(const PoolGuard&);
|
|
|
|
PoolGuard& operator=(const PoolGuard&);
|
|
|
|
};
|
2006-10-04 13:22:18 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
template <typename T,template <typename> class Creator>
|
|
|
|
class Pool : private boost::noncopyable
|
|
|
|
{
|
|
|
|
typedef boost::shared_ptr<T> HolderType;
|
2012-02-02 02:53:35 +01:00
|
|
|
typedef std::deque<HolderType> ContType;
|
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
Creator<T> creator_;
|
2012-02-02 02:53:35 +01:00
|
|
|
const unsigned initialSize_;
|
2010-06-02 13:03:30 +02:00
|
|
|
const unsigned maxSize_;
|
|
|
|
ContType usedPool_;
|
|
|
|
ContType unusedPool_;
|
2008-02-04 17:12:13 +01:00
|
|
|
#ifdef MAPNIK_THREADSAFE
|
2010-06-02 13:03:30 +02:00
|
|
|
mutable boost::mutex mutex_;
|
2008-02-04 17:12:13 +01:00
|
|
|
#endif
|
2010-06-02 13:03:30 +02:00
|
|
|
public:
|
2006-10-04 13:22:18 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
Pool(const Creator<T>& creator,unsigned initialSize=1, unsigned maxSize=10)
|
|
|
|
:creator_(creator),
|
|
|
|
initialSize_(initialSize),
|
|
|
|
maxSize_(maxSize)
|
|
|
|
{
|
2012-02-02 02:53:35 +01:00
|
|
|
for (unsigned i=0; i < initialSize_; ++i)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
HolderType conn(creator_());
|
|
|
|
if (conn->isOK())
|
|
|
|
unusedPool_.push_back(conn);
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:22:18 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
HolderType borrowObject()
|
2012-02-02 02:53:35 +01:00
|
|
|
{
|
|
|
|
#ifdef MAPNIK_THREADSAFE
|
2010-06-02 13:03:30 +02:00
|
|
|
mutex::scoped_lock lock(mutex_);
|
2008-02-04 17:12:13 +01:00
|
|
|
#endif
|
2010-06-02 13:03:30 +02:00
|
|
|
typename ContType::iterator itr=unusedPool_.begin();
|
2011-09-01 21:21:03 +02:00
|
|
|
while ( itr!=unusedPool_.end())
|
2012-02-02 02:53:35 +01:00
|
|
|
{
|
2006-11-19 18:13:33 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-06-02 13:03:30 +02:00
|
|
|
std::clog<<"borrow "<<(*itr).get()<<"\n";
|
2006-11-19 18:13:33 +01:00
|
|
|
#endif
|
2010-07-02 13:42:35 +02:00
|
|
|
if ((*itr)->isOK())
|
|
|
|
{
|
|
|
|
usedPool_.push_back(*itr);
|
|
|
|
unusedPool_.erase(itr);
|
|
|
|
return usedPool_[usedPool_.size()-1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
std::clog<<"bad connection (erase)" << (*itr).get()<<"\n";
|
2012-02-02 02:53:35 +01:00
|
|
|
#endif
|
2011-09-01 21:21:03 +02:00
|
|
|
itr=unusedPool_.erase(itr);
|
2010-07-02 13:42:35 +02:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2011-09-01 21:20:26 +02:00
|
|
|
if (usedPool_.size() < maxSize_)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
HolderType conn(creator_());
|
|
|
|
if (conn->isOK())
|
2006-11-27 22:13:15 +01:00
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
usedPool_.push_back(conn);
|
2006-11-27 22:13:15 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-06-02 13:03:30 +02:00
|
|
|
std::clog << "create << " << conn.get() << "\n";
|
2006-11-27 22:13:15 +01:00
|
|
|
#endif
|
2010-06-02 13:03:30 +02:00
|
|
|
return conn;
|
2006-11-27 22:13:15 +01:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
|
|
|
return HolderType();
|
2012-02-02 02:53:35 +01:00
|
|
|
}
|
2006-10-04 13:22:18 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
void returnObject(HolderType obj)
|
|
|
|
{
|
2008-02-04 17:12:13 +01:00
|
|
|
#ifdef MAPNIK_THREADSAFE
|
2010-06-02 13:03:30 +02:00
|
|
|
mutex::scoped_lock lock(mutex_);
|
2008-02-04 17:12:13 +01:00
|
|
|
#endif
|
2010-06-02 13:03:30 +02:00
|
|
|
typename ContType::iterator itr=usedPool_.begin();
|
|
|
|
while (itr != usedPool_.end())
|
|
|
|
{
|
2012-02-02 02:53:35 +01:00
|
|
|
if (obj.get()==(*itr).get())
|
2006-10-04 13:22:18 +02:00
|
|
|
{
|
2006-11-19 18:13:33 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-06-02 13:03:30 +02:00
|
|
|
std::clog<<"return "<<(*itr).get()<<"\n";
|
2006-11-19 18:13:33 +01:00
|
|
|
#endif
|
2010-06-02 13:03:30 +02:00
|
|
|
unusedPool_.push_back(*itr);
|
|
|
|
usedPool_.erase(itr);
|
|
|
|
return;
|
2006-10-04 13:22:18 +02:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
std::pair<unsigned,unsigned> size() const
|
|
|
|
{
|
2008-02-04 17:12:13 +01:00
|
|
|
#ifdef MAPNIK_THREADSAFE
|
2010-06-02 13:03:30 +02:00
|
|
|
mutex::scoped_lock lock(mutex_);
|
2008-02-04 17:12:13 +01:00
|
|
|
#endif
|
2010-06-02 13:03:30 +02:00
|
|
|
std::pair<unsigned,unsigned> size(unusedPool_.size(),usedPool_.size());
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
};
|
2011-10-23 16:09:47 +02:00
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
}
|
2011-10-23 16:09:47 +02:00
|
|
|
|
|
|
|
#endif // MAPNIK_POOL_HPP
|