/*############################################################################## HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ############################################################################## */ #ifndef __RESPOOL_HPP #define __RESPOOL_HPP #include "jwrapper.hpp" #include "jexcept.hpp" #include namespace esp { template interface IResourceFactory: implements IInterface { virtual T* createResource() = 0; }; // T should implement IInterface template class ResourcePool : public CInterface, implements IInterface { public: IMPLEMENT_IINTERFACE; ResourcePool() { } ResourcePool(unsigned size,IResourceFactory* fac): resources(size), factory(fac) { } void init(unsigned size,IResourceFactory* fac) { CriticalBlock b(crit); resources.clear(); resources.resize(size); factory.set(fac); } Linked get(long timeout=0) { const long interval=1000; for(;;) { { CriticalBlock b(crit); typename std::vector >::iterator it; for(it=resources.begin();it!=resources.end();it++) { if(it->get() == NULL) { Owned e = factory->createResource(); if(e) { it->set(e.get()); return e; } } else if(!it->get()->IsShared()) { return *it; } } } long slp=timeout!=INFINITE && timeout >::iterator it; for(it=resources.begin();it!=resources.end();it++) { if(it->get() && it->get() == t) { it->clear(); break; } } } } void clearAll() { CriticalBlock b(crit); typename std::vector >::iterator it; for(it=resources.begin();it!=resources.end();it++) { if(it->get()) { it->clear(); } } } protected: CriticalSection crit; Semaphore sem; std::vector > resources; Linked > factory; }; } #endif