/usr/include/mapnik/pool.hpp is in libmapnik2-dev 2.0.0+ds1-3build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | /*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2006 Artem Pavlenko
*
* 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
*
*****************************************************************************/
//$Id: pool.hpp 39 2005-04-10 20:39:53Z pavlenko $
#ifndef POOL_HPP
#define POOL_HPP
// mapnik
#include <mapnik/utils.hpp>
// boost
#include <boost/shared_ptr.hpp>
#include <boost/utility.hpp>
#ifdef MAPNIK_THREADSAFE
#include <boost/thread/mutex.hpp>
#endif
// stl
#include <iostream>
#include <map>
#include <deque>
#include <ctime>
namespace mapnik
{
template <typename T, typename PoolT>
class PoolGuard
{
private:
const T& obj_;
PoolT& pool_;
public:
explicit PoolGuard(const T& ptr,PoolT& pool)
: obj_(ptr),
pool_(pool) {}
~PoolGuard()
{
pool_->returnObject(obj_);
}
private:
PoolGuard();
PoolGuard(const PoolGuard&);
PoolGuard& operator=(const PoolGuard&);
};
template <typename T,template <typename> class Creator>
class Pool : private boost::noncopyable
{
typedef boost::shared_ptr<T> HolderType;
typedef std::deque<HolderType> ContType;
Creator<T> creator_;
const unsigned initialSize_;
const unsigned maxSize_;
ContType usedPool_;
ContType unusedPool_;
#ifdef MAPNIK_THREADSAFE
mutable boost::mutex mutex_;
#endif
public:
Pool(const Creator<T>& creator,unsigned initialSize=1, unsigned maxSize=10)
:creator_(creator),
initialSize_(initialSize),
maxSize_(maxSize)
{
for (unsigned i=0; i < initialSize_; ++i)
{
HolderType conn(creator_());
if (conn->isOK())
unusedPool_.push_back(conn);
}
}
HolderType borrowObject()
{
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_);
#endif
typename ContType::iterator itr=unusedPool_.begin();
while ( itr!=unusedPool_.end())
{
#ifdef MAPNIK_DEBUG
std::clog<<"borrow "<<(*itr).get()<<"\n";
#endif
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";
#endif
itr=unusedPool_.erase(itr);
}
}
if (usedPool_.size() < maxSize_)
{
HolderType conn(creator_());
if (conn->isOK())
{
usedPool_.push_back(conn);
#ifdef MAPNIK_DEBUG
std::clog << "create << " << conn.get() << "\n";
#endif
return conn;
}
}
return HolderType();
}
void returnObject(HolderType obj)
{
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_);
#endif
typename ContType::iterator itr=usedPool_.begin();
while (itr != usedPool_.end())
{
if (obj.get()==(*itr).get())
{
#ifdef MAPNIK_DEBUG
std::clog<<"return "<<(*itr).get()<<"\n";
#endif
unusedPool_.push_back(*itr);
usedPool_.erase(itr);
return;
}
++itr;
}
}
std::pair<unsigned,unsigned> size() const
{
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_);
#endif
std::pair<unsigned,unsigned> size(unusedPool_.size(),usedPool_.size());
return size;
}
};
}
#endif //POOL_HPP
|