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> #include <mapnik/utils.hpp>
// boost // boost
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/utility.hpp> #include <boost/utility.hpp>
#ifdef MAPNIK_THREADSAFE
#include <boost/thread/mutex.hpp>
#endif
// stl // stl
#include <iostream> #include <iostream>
#include <map> #include <map>
@ -72,7 +76,9 @@ namespace mapnik
const unsigned maxSize_; const unsigned maxSize_;
ContType usedPool_; ContType usedPool_;
ContType unusedPool_; ContType unusedPool_;
#ifdef MAPNIK_THREADSAFE
mutable boost::mutex mutex_; mutable boost::mutex mutex_;
#endif
public: public:
Pool(const Creator<T>& creator,unsigned initialSize=1, unsigned maxSize=10) Pool(const Creator<T>& creator,unsigned initialSize=1, unsigned maxSize=10)
@ -89,8 +95,10 @@ namespace mapnik
} }
HolderType borrowObject() HolderType borrowObject()
{ {
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_); mutex::scoped_lock lock(mutex_);
#endif
typename ContType::iterator itr=unusedPool_.begin(); typename ContType::iterator itr=unusedPool_.begin();
if (itr!=unusedPool_.end()) if (itr!=unusedPool_.end())
{ {
@ -118,7 +126,9 @@ namespace mapnik
void returnObject(HolderType obj) void returnObject(HolderType obj)
{ {
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_); mutex::scoped_lock lock(mutex_);
#endif
typename ContType::iterator itr=usedPool_.begin(); typename ContType::iterator itr=usedPool_.begin();
while (itr != usedPool_.end()) while (itr != usedPool_.end())
{ {
@ -137,7 +147,9 @@ namespace mapnik
std::pair<unsigned,unsigned> size() const std::pair<unsigned,unsigned> size() const
{ {
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_); mutex::scoped_lock lock(mutex_);
#endif
std::pair<unsigned,unsigned> size(unusedPool_.size(),usedPool_.size()); std::pair<unsigned,unsigned> size(unusedPool_.size(),usedPool_.size());
return size; return size;
} }

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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