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>
12 #include "../exceptions.h"
50 while (
free_.size() > 0) {
57 if (
used_.size() > 0) {
58 syslog(LOG_USER | LOG_WARNING,
"%ld used elements from a pool not released on destruction!", (
long)
used_.size());
69 std::string(
"No resources available"));
72 boost::mutex::scoped_lock lock(
mutex_);
77 if (
free_.size() > 0) {
91 used_.insert(std::pair<E, unsigned>(e, 1));
100 boost::mutex::scoped_lock lock(
mutex_);
103 typename std::map<E, unsigned>::const_iterator i =
used_.find(e);
104 if (i ==
used_.end())
106 std::string(
"The resource has not been locked previously!"));
120 boost::mutex::scoped_lock lock(
mutex_);
122 unsigned remaining = --
used_[e];
145 typename std::map<E, unsigned>::const_iterator i =
used_.find(e);
146 if (i ==
used_.end())
156 boost::mutex::scoped_lock lock(
mutex_);
202 #endif // DMLITE_CPP_UTILS_POOLCONTAINER_H
Convenience class that releases a resource on destruction.
Definition: poolcontainer.h:180
virtual bool isValid(E)=0
Check it is still valid.
Implements a pool of whichever resource.
Definition: poolcontainer.h:37
void resize(int ns)
Definition: poolcontainer.h:153
PoolContainer(PoolElementFactory< E > *factory, int n)
Definition: poolcontainer.h:42
virtual void destroy(E)=0
Destroys an element.
#define DMLITE_SYSERR(e)
Definition: errno.h:32
boost::mutex mutex_
Definition: poolcontainer.h:174
unsigned refCount(E e)
Count the number of instances.
Definition: poolcontainer.h:143
std::queue< E > free_
Definition: poolcontainer.h:170
E acquire(E e)
Increases the reference count of a resource.
Definition: poolcontainer.h:98
std::map< E, unsigned > used_
Definition: poolcontainer.h:171
Base exception class.
Definition: exceptions.h:17
unsigned freeSlots_
Definition: poolcontainer.h:172
PoolContainer< E > & pool_
Definition: poolcontainer.h:197
virtual ~PoolElementFactory()
Destructor.
Definition: poolcontainer.h:22
E acquire(bool block=true)
Acquires a free resource.
Definition: poolcontainer.h:63
~PoolGrabber()
Definition: poolcontainer.h:187
virtual E create()=0
Creates an element.
E element_
Definition: poolcontainer.h:198
unsigned release(E e)
Definition: poolcontainer.h:118
int max_
Definition: poolcontainer.h:166
~PoolContainer()
Destructor.
Definition: poolcontainer.h:47
boost::condition_variable available_
Definition: poolcontainer.h:175
Definition: poolcontainer.h:19
PoolElementFactory< E > * factory_
Definition: poolcontainer.h:168
PoolGrabber(PoolContainer< E > &pool, bool block=true)
Definition: poolcontainer.h:182