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"
51 while (
free_.size() > 0) {
58 if (
used_.size() > 0) {
59 syslog(LOG_USER | LOG_WARNING,
"%ld used elements from a pool not released on destruction!", (
long)
used_.size());
70 std::string(
"No resources available"));
74 boost::system_time
const timeout = boost::get_system_time() + boost::posix_time::seconds(60);
75 boost::mutex::scoped_lock lock(
mutex_);
77 if (boost::get_system_time() >= timeout) {
78 syslog(LOG_USER | LOG_WARNING,
"Timeout...%d seconds", 60);
85 if (
free_.size() > 0) {
99 used_.insert(std::pair<E, unsigned>(e, 1));
110 boost::mutex::scoped_lock lock(
mutex_);
113 typename std::map<E, unsigned>::const_iterator i =
used_.find(e);
114 if (i ==
used_.end()) {
130 boost::mutex::scoped_lock lock(
mutex_);
132 unsigned remaining = --
used_[e];
155 typename std::map<E, unsigned>::const_iterator i =
used_.find(e);
156 if (i ==
used_.end())
166 boost::mutex::scoped_lock lock(
mutex_);
215 #endif // DMLITE_CPP_UTILS_POOLCONTAINER_H
Convenience class that releases a resource on destruction.
Definition: poolcontainer.h:193
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:163
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:187
unsigned refCount(E e)
Count the number of instances.
Definition: poolcontainer.h:153
E acquire(E e)
Increases the reference count of a resource.
Definition: poolcontainer.h:108
std::map< E, unsigned > used_
Definition: poolcontainer.h:184
Base exception class.
Definition: exceptions.h:17
unsigned freeSlots_
Definition: poolcontainer.h:185
PoolContainer< E > & pool_
Definition: poolcontainer.h:210
virtual ~PoolElementFactory()
Destructor.
Definition: poolcontainer.h:23
E acquire(bool block=true)
Acquires a free resource.
Definition: poolcontainer.h:64
std::deque< E > free_
Definition: poolcontainer.h:183
~PoolGrabber()
Definition: poolcontainer.h:200
virtual E create()=0
Creates an element.
E element_
Definition: poolcontainer.h:211
unsigned release(E e)
Definition: poolcontainer.h:128
int max_
Definition: poolcontainer.h:179
~PoolContainer()
Destructor.
Definition: poolcontainer.h:48
boost::condition_variable available_
Definition: poolcontainer.h:188
Definition: poolcontainer.h:20
PoolElementFactory< E > * factory_
Definition: poolcontainer.h:181
PoolGrabber(PoolContainer< E > &pool, bool block=true)
Definition: poolcontainer.h:195