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_);
76 std::string(
"No resources available"));
79 boost::system_time
const timeout = boost::get_system_time() + boost::posix_time::seconds(1);
82 if (boost::get_system_time() >= timeout) {
83 syslog(LOG_USER | LOG_WARNING,
"Poolcontainer timeout. Size: %d free (can be negative): %d Stall: %d seconds in '%s'",
max_,
freeSlots_, 1, __PRETTY_FUNCTION__);
91 while (
free_.size() > 0) {
113 boost::mutex::scoped_lock lock(
mutex_);
115 used_.insert(std::pair<E, unsigned>(e, 1));
126 boost::mutex::scoped_lock lock(
mutex_);
129 typename std::map<E, unsigned>::const_iterator i =
used_.find(e);
130 if (i ==
used_.end()) {
146 boost::mutex::scoped_lock lock(
mutex_);
148 unsigned remaining = --
used_[e];
171 typename std::map<E, unsigned>::const_iterator i =
used_.find(e);
172 if (i ==
used_.end())
182 boost::mutex::scoped_lock lock(
mutex_);
231 #endif // DMLITE_CPP_UTILS_POOLCONTAINER_H Convenience class that releases a resource on destruction.
Definition: poolcontainer.h:209
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:179
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:203
unsigned refCount(E e)
Count the number of instances.
Definition: poolcontainer.h:169
E acquire(E e)
Increases the reference count of a resource.
Definition: poolcontainer.h:124
std::map< E, unsigned > used_
Definition: poolcontainer.h:200
Base exception class.
Definition: exceptions.h:17
PoolContainer< E > & pool_
Definition: poolcontainer.h:226
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:199
~PoolGrabber()
Definition: poolcontainer.h:216
virtual E create()=0
Creates an element.
E element_
Definition: poolcontainer.h:227
unsigned release(E e)
Definition: poolcontainer.h:144
int max_
Definition: poolcontainer.h:195
~PoolContainer()
Destructor.
Definition: poolcontainer.h:48
int freeSlots_
Definition: poolcontainer.h:201
boost::condition_variable available_
Definition: poolcontainer.h:204
Namespace for the dmlite C++ API.
Definition: authn.h:16
Definition: poolcontainer.h:20
PoolElementFactory< E > * factory_
Definition: poolcontainer.h:197
PoolGrabber(PoolContainer< E > &pool, bool block=true)
Definition: poolcontainer.h:211