include/dmlite/cpp/utils/poolcontainer.h

Go to the documentation of this file.
00001 /// @file    include/dmlite/cpp/utils/poolcontainer.h
00002 /// @brief   Pooling
00003 /// @author  Alejandro Álvarez Ayllón <aalvarez@cern.ch>
00004 #ifndef DMLITE_CPP_UTILS_POOLCONTAINER_H
00005 #define DMLITE_CPP_UTILS_POOLCONTAINER_H
00006 
00007 #include <boost/thread/mutex.hpp>
00008 #include <boost/thread/condition.hpp>
00009 #include <boost/date_time/posix_time/posix_time.hpp>
00010 #include <map>
00011 #include <syslog.h>
00012 #include <queue>
00013 #include "../exceptions.h"
00014 
00015 namespace dmlite {
00016 
00017   /// Classes implementing this interface creates the actual element
00018   /// since the pool is agnosstic
00019   template <class E>
00020   class PoolElementFactory {
00021    public:
00022     /// Destructor
00023     virtual ~PoolElementFactory() {};
00024 
00025     /// Creates an element
00026     virtual E create() = 0;
00027 
00028     /// Destroys an element
00029     virtual void destroy(E) = 0;
00030 
00031     /// Check it is still valid
00032     virtual bool isValid(E) = 0;
00033   };
00034 
00035 
00036   /// Implements a pool of whichever resource
00037   template <class E>
00038   class PoolContainer {
00039    public:
00040     /// Constructor
00041     /// @param factory The factory to use when spawning a new resource.
00042     /// @param n       The number of resources to keep in the pool. Up to 2*n slots can be created without penalty (but only n will be pooled)
00043     PoolContainer(PoolElementFactory<E>* factory, int n): max_(n), factory_(factory), freeSlots_(2*n)
00044     {
00045     }
00046 
00047     /// Destructor
00048     ~PoolContainer()
00049     {
00050       // Free 'free'
00051       while (free_.size() > 0) {
00052         E e = free_.front();
00053         free_.pop_front();
00054         factory_->destroy(e);
00055       }
00056       // Freeing used is dangerous, as we might block if the client code
00057       // forgot about something. Assume the memory leak :(
00058       if (used_.size() > 0) {
00059         syslog(LOG_USER | LOG_WARNING, "%ld used elements from a pool not released on destruction!", (long)used_.size());
00060       }
00061     }
00062 
00063     /// Acquires a free resource.
00064     E  acquire(bool block = true)
00065     {
00066       E e;
00067       // Wait for one free
00068       if (!block && (freeSlots_ == 0)) {
00069         throw DmException(DMLITE_SYSERR(EBUSY),
00070                           std::string("No resources available"));
00071       }
00072 
00073 
00074       boost::system_time const timeout = boost::get_system_time() + boost::posix_time::seconds(60);
00075       boost::mutex::scoped_lock lock(mutex_);
00076       while (freeSlots_ < 1) {
00077         if (boost::get_system_time() >= timeout) {
00078            syslog(LOG_USER | LOG_WARNING, "Timeout...%d seconds", 60);
00079            break;
00080         }
00081         available_.timed_wait(lock, timeout);
00082       }
00083 
00084       // If there is any in the queue, give one from there
00085       if (free_.size() > 0) {
00086         e = free_.front();
00087         free_.pop_front();
00088         // May have expired!
00089         if (!factory_->isValid(e)) {
00090           factory_->destroy(e);
00091           e = factory_->create();
00092         }
00093       }
00094       else {
00095         // None created, so create it now
00096         e = factory_->create();
00097       }
00098       // Keep track of used
00099       used_.insert(std::pair<E, unsigned>(e, 1));
00100 
00101       // Note that in case of timeout freeSlots_ can become negative
00102       --freeSlots_;
00103 
00104       return e;
00105     }
00106 
00107     /// Increases the reference count of a resource.
00108     E acquire(E e)
00109     {
00110       boost::mutex::scoped_lock lock(mutex_);
00111 
00112       // Make sure it is there
00113       typename std::map<E, unsigned>::const_iterator i = used_.find(e);
00114       if (i == used_.end()) {
00115         throw DmException(DMLITE_SYSERR(EINVAL), std::string("The resource has not been locked previously!"));
00116       }
00117 
00118       // Increase
00119       used_[e]++;
00120 
00121       // End
00122       return e;
00123     }
00124 
00125     /// Releases a resource
00126     /// @param e The resource to release.
00127     /// @return  The reference count after releasing.
00128     unsigned release(E e)
00129     {
00130       boost::mutex::scoped_lock lock(mutex_);
00131       // Decrease reference count
00132       unsigned remaining = --used_[e];
00133       // No one else using it (hopefully...)
00134       if (used_[e] == 0) {
00135         // Remove from used
00136         used_.erase(e);
00137         // If the free size is less than the maximum, push to free and notify
00138         if ((long)free_.size() < max_) {
00139           free_.push_back(e);
00140         }
00141         else {
00142           // If we are fine, destroy
00143           factory_->destroy(e);
00144         }
00145       }
00146       available_.notify_one();
00147       ++freeSlots_;
00148 
00149       return remaining;
00150     }
00151 
00152     /// Count the number of instances
00153     unsigned refCount(E e)
00154     {
00155       typename std::map<E, unsigned>::const_iterator i = used_.find(e);
00156       if (i == used_.end())
00157         return 0;
00158       return used_[e];
00159     }
00160 
00161     /// Change the pool size
00162     /// @param ns The new size.
00163     void resize(int ns)
00164     {
00165       // The resizing will be done as we get requests
00166       boost::mutex::scoped_lock lock(mutex_);
00167       max_ = ns;
00168 
00169 
00170       freeSlots_ = 2*max_ - used_.size();
00171       // Increment the semaphore size if needed
00172       // Take into account the used
00173       if (freeSlots_ > 0)
00174         available_.notify_all();
00175     }
00176 
00177    private:
00178     // The max count of pooled instances
00179     int max_;
00180 
00181     PoolElementFactory<E> *factory_;
00182 
00183     std::deque<E>         free_;
00184     std::map<E, unsigned> used_;
00185     unsigned freeSlots_;
00186 
00187     boost::mutex              mutex_;
00188     boost::condition_variable available_;
00189   };
00190 
00191   /// Convenience class that releases a resource on destruction
00192   template <class E>
00193   class PoolGrabber {
00194    public:
00195     PoolGrabber(PoolContainer<E>& pool, bool block = true): pool_(pool)
00196     {
00197       element_ = pool_.acquire(block);
00198     }
00199 
00200     ~PoolGrabber() {
00201       pool_.release(element_);
00202     }
00203 
00204     operator E ()
00205     {
00206       return element_;
00207     }
00208 
00209    private:
00210     PoolContainer<E>& pool_;
00211     E element_;
00212   };
00213 };
00214 
00215 #endif // DMLITE_CPP_UTILS_POOLCONTAINER_H

Generated on 28 Apr 2014 for dmlite by  doxygen 1.4.7