allow multi/single threaded variants

This commit is contained in:
Artem Pavlenko 2008-02-04 16:12:13 +00:00
parent fa82f6fc50
commit b44762e592
8 changed files with 98 additions and 42 deletions

View file

@ -29,8 +29,12 @@
#include <mapnik/utils.hpp>
// boost
#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/utility.hpp>
#ifdef MAPNIK_THREADSAFE
#include <boost/thread/mutex.hpp>
#endif
// stl
#include <iostream>
#include <map>
@ -72,7 +76,9 @@ namespace mapnik
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)
@ -89,8 +95,10 @@ namespace mapnik
}
HolderType borrowObject()
{
{
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_);
#endif
typename ContType::iterator itr=unusedPool_.begin();
if (itr!=unusedPool_.end())
{
@ -118,7 +126,9 @@ namespace mapnik
void returnObject(HolderType obj)
{
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_);
#endif
typename ContType::iterator itr=usedPool_.begin();
while (itr != usedPool_.end())
{
@ -137,7 +147,9 @@ namespace mapnik
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;
}

View file

@ -27,8 +27,12 @@
// mapnik
#include <mapnik/envelope.hpp>
// boost
#ifdef MAPNIK_THREADSAFE
#include <boost/thread/mutex.hpp>
#endif
#include <boost/utility.hpp>
// stl
#include <string>
@ -67,7 +71,9 @@ namespace mapnik {
private:
std::string params_;
void * proj_;
#ifdef MAPNIK_THREADSAFE
static boost::mutex mutex_;
#endif
};
}

View file

@ -24,8 +24,11 @@
#ifndef UTILS_HPP
#define UTILS_HPP
// boost
#ifdef MAPNIK_THREADSAFE
#include <boost/thread/mutex.hpp>
#endif
// stl
#include <stdexcept>
#include <cstdlib>
@ -38,8 +41,10 @@
namespace mapnik
{
#ifdef MAPNIK_THREADSAFE
using boost::mutex;
#endif
template <typename T>
class CreateUsingNew
{
@ -121,35 +126,42 @@ namespace mapnik
#endif
}
protected:
static mutex mutex_;
singleton() {}
public:
protected:
#ifdef MAPNIK_THREADSAFE
static mutex mutex_;
#endif
singleton() {}
public:
static T* instance()
{
if (!pInstance_)
{
mutex::scoped_lock lock(mutex_);
if (!pInstance_)
{
if (destroyed_)
{
onDeadReference();
}
else
{
if (!pInstance_)
{
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_);
#endif
if (!pInstance_)
{
if (destroyed_)
{
onDeadReference();
}
else
{
pInstance_=CreatePolicy<T>::create();
// register destruction
std::atexit(&DestroySingleton);
}
}
}
return pInstance_;
}
}
}
return pInstance_;
}
};
#ifdef MAPNIK_THREADSAFE
template <typename T,
template <typename T> class CreatePolicy> mutex singleton<T,CreatePolicy>::mutex_;
#endif
template <typename T,
template <typename T> class CreatePolicy> T* singleton<T,CreatePolicy>::pInstance_=0;
template <typename T,

View file

@ -30,14 +30,17 @@
#include <mapnik/utils.hpp>
#include "connection.hpp"
#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/optional.hpp>
#ifdef MAPNIK_THREADSAFE
#include <boost/thread/mutex.hpp>
using boost::mutex;
#endif
using mapnik::Pool;
using mapnik::singleton;
using mapnik::CreateStatic;
using std::string;
using boost::mutex;
template <typename T>
class ConnectionCreator
@ -99,7 +102,9 @@ public:
bool registerPool(const ConnectionCreator<Connection>& creator,unsigned initialSize,unsigned maxSize)
{
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_);
#endif
if (pools_.find(creator.id())==pools_.end())
{
return pools_.insert(std::make_pair(creator.id(),
@ -112,7 +117,9 @@ public:
boost::shared_ptr<PoolType> getPool(std::string const& key)
{
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_);
#endif
ContType::const_iterator itr=pools_.find(key);
if (itr!=pools_.end())
{
@ -124,7 +131,9 @@ public:
HolderType get(std::string const& key)
{
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_);
#endif
ContType::const_iterator itr=pools_.find(key);
if (itr!=pools_.end())
{

View file

@ -27,7 +27,6 @@
#include <mapnik/config_error.hpp>
// boost
#include <boost/thread/mutex.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/algorithm/string.hpp>
@ -116,8 +115,10 @@ namespace mapnik
void datasource_cache::register_datasources(const std::string& str)
{
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mapnik::singleton<mapnik::datasource_cache,
mapnik::CreateStatic>::mutex_);
#endif
filesystem::path path(str);
filesystem::directory_iterator end_itr;
if (exists(path) && is_directory(path))

View file

@ -41,7 +41,9 @@ namespace mapnik
bool freetype_engine::register_font(std::string const& file_name)
{
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_);
#endif
FT_Library library;
FT_Error error = FT_Init_FreeType(&library);
if (error)
@ -76,9 +78,6 @@ namespace mapnik
face_ptr freetype_engine::create_face(std::string const& family_name)
{
//mutex::scoped_lock lock(mapnik::singleton<freetype_engine,
// mapnik::CreateStatic>::mutex_);
std::map<std::string,std::string>::iterator itr;
itr = name2file_.find(family_name);
if (itr != name2file_.end())
@ -93,8 +92,8 @@ namespace mapnik
}
return face_ptr();
}
//FT_Library freetype_engine::library_;
#ifdef MAPNIK_THREADSAFE
boost::mutex freetype_engine::mutex_;
#endif
std::map<std::string,std::string> freetype_engine::name2file_;
}

View file

@ -35,16 +35,18 @@ namespace mapnik {
: source_(source),
dest_(dest)
{
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(projection::mutex_);
#endif
is_source_latlong_ = pj_is_latlong(source_.proj_);
is_dest_latlong_ = pj_is_latlong(dest_.proj_);
}
bool proj_transform::forward (double & x, double & y , double & z) const
{
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(projection::mutex_);
#endif
if (is_source_latlong_)
{
x *= DEG_TO_RAD;
@ -68,8 +70,10 @@ namespace mapnik {
bool proj_transform::backward (double & x, double & y , double & z) const
{
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(projection::mutex_);
#endif
if (is_dest_latlong_)
{
x *= DEG_TO_RAD;

View file

@ -29,8 +29,11 @@
#include <proj_api.h>
namespace mapnik {
boost::mutex projection::mutex_;
#ifdef MAPNIK_THREADSAFE
boost::mutex projection::mutex_;
#endif
projection::projection(std::string params)
: params_(params)
{
@ -57,7 +60,9 @@ namespace mapnik {
bool projection::is_geographic() const
{
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_);
#endif
return pj_is_latlong(proj_);
}
@ -68,7 +73,9 @@ namespace mapnik {
void projection::forward(double & x, double &y ) const
{
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_);
#endif
projUV p;
p.u = x * DEG_TO_RAD;
p.v = y * DEG_TO_RAD;
@ -79,7 +86,9 @@ namespace mapnik {
void projection::inverse(double & x,double & y) const
{
mutex::scoped_lock lock(mutex_);
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_);
#endif
projUV p;
p.u = x;
p.v = y;
@ -90,15 +99,19 @@ namespace mapnik {
projection::~projection()
{
mutex::scoped_lock lock(mutex_);
if (proj_) pj_free(proj_);
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_);
#endif
if (proj_) pj_free(proj_);
}
void projection::init()
{
mutex::scoped_lock lock(mutex_);
proj_=pj_init_plus(params_.c_str());
if (!proj_) throw proj_init_error(params_);
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_);
#endif
proj_=pj_init_plus(params_.c_str());
if (!proj_) throw proj_init_error(params_);
}
void projection::swap (projection& rhs)