4 #ifndef DMLITE_CPP_UTILS_POOLCONTAINER_H 5 #define DMLITE_CPP_UTILS_POOLCONTAINER_H 7 #include <boost/thread/mutex.hpp> 8 #include <boost/thread/condition.hpp> 9 #include <boost/date_time/posix_time/posix_time.hpp> 13 #include "../exceptions.h" 50 boost::mutex::scoped_lock lock(mutex_);
52 while (free_.size() > 0) {
59 if (used_.size() > 0) {
60 syslog(LOG_USER | LOG_WARNING,
"%ld used elements from a pool not released on destruction!", (
long)used_.size());
71 boost::mutex::scoped_lock lock(mutex_);
74 if (!block && (freeSlots_ == 0)) {
76 std::string(
"No resources available"));
79 boost::system_time
const timeout = boost::get_system_time() + boost::posix_time::seconds(60);
81 while (freeSlots_ < 1) {
82 if (boost::get_system_time() >= timeout) {
83 syslog(LOG_USER | LOG_WARNING,
"Timeout...%d seconds in '%s'", 60, __PRETTY_FUNCTION__);
86 available_.timed_wait(lock, timeout);
90 if (free_.size() > 0) {
95 if (!factory_->isValid(e)) {
107 e = factory_->create();
110 boost::mutex::scoped_lock lock(mutex_);
112 used_.insert(std::pair<E, unsigned>(e, 1));
123 boost::mutex::scoped_lock lock(mutex_);
126 typename std::map<E, unsigned>::const_iterator i = used_.find(e);
127 if (i == used_.end()) {
143 boost::mutex::scoped_lock lock(mutex_);
145 unsigned remaining = --used_[e];
151 if ((
long)free_.size() < max_) {
156 factory_->destroy(e);
159 available_.notify_one();
168 typename std::map<E, unsigned>::const_iterator i = used_.find(e);
169 if (i == used_.end())
179 boost::mutex::scoped_lock lock(mutex_);
183 freeSlots_ = 2*max_ - used_.size();
187 available_.notify_all();
210 element_ = pool_.acquire(block);
214 pool_.release(element_);
228 #endif // DMLITE_CPP_UTILS_POOLCONTAINER_H Convenience class that releases a resource on destruction.
Definition: poolcontainer.h:206
virtual bool isValid(E)=0
Check it is still valid.
Implements a pool of whichever resource.
Definition: poolcontainer.h:38
void resize(int ns)
Definition: poolcontainer.h:176
PoolContainer(PoolElementFactory< E > *factory, int n)
Definition: poolcontainer.h:43
virtual void destroy(E)=0
Destroys an element.
#define DMLITE_SYSERR(e)
Definition: errno.h:32
boost::mutex mutex_
Definition: poolcontainer.h:200
unsigned refCount(E e)
Count the number of instances.
Definition: poolcontainer.h:166
E acquire(E e)
Increases the reference count of a resource.
Definition: poolcontainer.h:121
std::map< E, unsigned > used_
Definition: poolcontainer.h:197
Base exception class.
Definition: exceptions.h:17
PoolContainer< E > & pool_
Definition: poolcontainer.h:223
virtual ~PoolElementFactory()
Destructor.
Definition: poolcontainer.h:23
E acquire(bool block=true)
Acquires a free resource.
Definition: poolcontainer.h:65
std::deque< E > free_
Definition: poolcontainer.h:196
~PoolGrabber()
Definition: poolcontainer.h:213
virtual E create()=0
Creates an element.
E element_
Definition: poolcontainer.h:224
unsigned release(E e)
Definition: poolcontainer.h:141
int max_
Definition: poolcontainer.h:192
~PoolContainer()
Destructor.
Definition: poolcontainer.h:48
int freeSlots_
Definition: poolcontainer.h:198
boost::condition_variable available_
Definition: poolcontainer.h:201
Namespace for the dmlite C++ API.
Definition: authn.h:15
Definition: poolcontainer.h:20
PoolElementFactory< E > * factory_
Definition: poolcontainer.h:194
PoolGrabber(PoolContainer< E > &pool, bool block=true)
Definition: poolcontainer.h:208