00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef RESOURCEPOOL_RESOURCEPOOL_HH
00025 #define RESOURCEPOOL_RESOURCEPOOL_HH
00026
00027 #ifndef INCLUDED_GC_PTR_HH
00028 #include "mm/gc_ptr.hh"
00029 #endif
00030
00031 #ifndef MM_REFWRAP_HH
00032 #include "mm/RefWrap.hh"
00033 #endif
00034
00035 #ifndef RESOURCEPOOL_RESOURCEPOOLOPTIONS_HH
00036 #include "ResourcePoolOptions.hh"
00037 #endif
00038
00039 #ifndef RESOURCEPOOL_EXECUTE_HH
00040 #include "Execute.hh"
00041 #endif
00042
00043 #ifndef INCLUDED_THREAD_LOCK_HH
00044 #include "Thread/Lock.hh"
00045 #endif
00046
00047 #ifndef INCLUDED_THREAD_SYNCHRONIZE_HH
00048 #include "Thread/Synchronize.hh"
00049 #endif
00050
00051 #ifndef INCLUDED_CONDVAR_H
00052 #include "Thread/CondVar.hh"
00053 #endif
00054
00055 #ifndef RESOURCEPOOL_CREATEEXCEPTION_HH
00056 #include "CreateException.hh"
00057 #endif
00058
00059 #include "ResourcePoolType.hh"
00060 #include "ResourcePoolStats.hh"
00061
00062
00063
00064 #include <deque>
00065 #include <set>
00066 #include <map>
00067
00068
00075 namespace fatalmind {
00076
00077 template<class T> class Command;
00078
00079
00080
00081
00095 template<class Type>
00096 class ResourcePool: public _RPExecute<Type> {
00097 public:
00104 ResourcePool(const typename Type::factory_t&
00105 , const ResourcePoolOptions& = ResourcePoolOptions::defaultOptions
00106 );
00107
00111 virtual ~ResourcePool();
00112
00113
00114 virtual typename Type::resource_t& get();
00115 virtual bool free(const typename Type::resource_t&);
00116 virtual bool fail(const typename Type::resource_t&);
00117
00121 virtual void downsize();
00122
00123 ResourcePoolStats getStats() const;
00124
00128 typedef Type PoolType;
00129
00133 typedef typename Type::factory_t Factory;
00134
00140 inline const Factory& getFactory() const {
00141 return _factory;
00142 };
00143
00144
00145 protected:
00146
00147 typedef typename Type::ThreadingModell TM;
00148 typedef typename TM::FastCondVar condvar_t;
00149 typedef Synchronize<condvar_t> sync_t;
00150
00151 typedef gc_ptr<typename Type::resource_t, TM> resource_ptr;
00152 typedef std::deque<resource_ptr> freePool_t;
00153
00154 typedef RefWrap<typename Type::resource_t> plainresource_ptr;
00155 typedef std::map<plainresource_ptr, resource_ptr> usedPool_t;
00156
00157 private:
00158
00159 const typename Type::factory_t _factory;
00160 const ResourcePoolOptions _options;
00161
00162 freePool_t _freePool;
00163 usedPool_t _usedPool;
00164
00165 mutable condvar_t _condvar;
00166
00167 bool _stopped;
00168
00169 ResourcePoolStats _stats;
00170
00171 public:
00187 typedef typename Type::command_t Command;
00188
00189
00190
00191
00192 protected:
00193
00194 bool sleepit(int tryno) const;
00195 typename Type::resource_t* moveToUsed();
00196
00197
00198 virtual const ResourcePoolOptions& getOptions() {
00199 return _options;
00200 };
00201
00202
00203 inline void _downsize();
00204 inline void _inc_pool();
00205
00206 void _waitForFree(const CreateException&, const SingleThreadedModel<>::Type);
00207 void _waitForFree(const CreateException&, const MultiThreadedModel<>::Type);
00208 friend
00209 class fatalmind::Command<Type>;
00210
00211
00212 private:
00213
00214 ResourcePool(const ResourcePool<Type>&);
00215 ResourcePool<Type>& operator=(const ResourcePool<Type>&);
00216 };
00217
00218
00219 }
00220 #endif