1 // Copyright (C) 2006-2016 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SalomeHPContainerTools.hxx"
21 #include "SalomeHPContainer.hxx"
22 #include "AutoLocker.hxx"
23 #include "Exception.hxx"
27 using namespace YACS::ENGINE;
29 void SalomeHPContainerVectOfHelper::resize(std::size_t sz)
31 std::size_t oldSize(_launchModeType.size());
35 _whichOccupied.resize(sz); std::fill(_whichOccupied.begin(),_whichOccupied.end(),false);
36 _launchModeType.resize(sz);
37 for(std::size_t i=oldSize;i<sz;i++)
38 _launchModeType[i]=new SalomeContainerMonoHelper;
39 _currentlyWorking.clear();
42 std::size_t SalomeHPContainerVectOfHelper::getNumberOfFreePlace() const
44 return std::count(_whichOccupied.begin(),_whichOccupied.end(),false);
47 std::size_t SalomeHPContainerVectOfHelper::getNumberOfFreePlaceAmong(const std::vector<std::size_t>& idsOfKernelContainers) const
50 for(std::vector<std::size_t>::const_iterator it=idsOfKernelContainers.begin();it!=idsOfKernelContainers.end();it++)
51 if(!_whichOccupied[*it])
56 void SalomeHPContainerVectOfHelper::allocateFor(const std::vector<const Task *>& nodes)
58 for(std::vector<const Task *>::const_iterator it=nodes.begin();it!=nodes.end();it++)
62 if(_currentlyWorking.find(*it)!=_currentlyWorking.end())
63 throw Exception("Searching to allocate for a ServiceNode instance already declared as allocated !");
64 std::vector<bool>::iterator it2(std::find(_whichOccupied.begin(),_whichOccupied.end(),false));
65 if(it2==_whichOccupied.end())
66 throw Exception("All ressources are already occupied ! You are expected to wait for released resources !");
67 std::size_t pos(std::distance(_whichOccupied.begin(),it2));
68 _currentlyWorking[*it]=pos; _whichOccupied[pos]=true;
72 void SalomeHPContainerVectOfHelper::allocateForAmong(const std::vector<std::size_t>& idsOfKernelContainers, const std::vector<const Task *>& nodes)
74 for(std::vector<const Task *>::const_iterator it=nodes.begin();it!=nodes.end();it++)
78 if(_currentlyWorking.find(*it)!=_currentlyWorking.end())
79 throw Exception("Searching 2 to allocate for a ServiceNode instance already declared as allocated !");
80 std::size_t it2(std::numeric_limits<std::size_t>::max());
81 for(std::vector<std::size_t>::const_iterator it=idsOfKernelContainers.begin();it!=idsOfKernelContainers.end();it++)
82 if(!_whichOccupied[*it])
87 if(it2==std::numeric_limits<std::size_t>::max())
88 throw Exception("All 2 ressources are already occupied ! You are expected to wait for released resources !");
89 _currentlyWorking[*it]=it2; _whichOccupied[it2]=true;
93 void SalomeHPContainerVectOfHelper::release(const Task *node)
97 std::map< const Task *,std::size_t >::iterator it(_currentlyWorking.find(node));
98 if(it==_currentlyWorking.end())
99 throw Exception("Request to release a resource not declared as working !");
100 _whichOccupied[(*it).second]=false;
101 _currentlyWorking.erase(it);
104 std::size_t SalomeHPContainerVectOfHelper::locateTask(const Task *node) const
106 std::map< const Task *,std::size_t >::const_iterator it(_currentlyWorking.find(node));
107 if(it==_currentlyWorking.end())
108 throw Exception("current Node to be located is not marked as launched !");
109 std::size_t ret((*it).second);
114 const SalomeContainerMonoHelper *SalomeHPContainerVectOfHelper::getHelperOfTaskThreadSafe(const Task *node) const
116 YACS::BASES::AutoLocker<SalomeHPContainerVectOfHelper> alck(const_cast<SalomeHPContainerVectOfHelper *>(this));
117 return _launchModeType[locateTask(node)];
120 const SalomeContainerMonoHelper *SalomeHPContainerVectOfHelper::getHelperOfTask(const Task *node) const
122 return _launchModeType[locateTask(node)];
125 SalomeContainerMonoHelper *SalomeHPContainerVectOfHelper::getHelperOfTaskThreadSafe(const Task *node)
127 YACS::BASES::AutoLocker<SalomeHPContainerVectOfHelper> alck(this);
128 return _launchModeType[locateTask(node)];
131 SalomeContainerMonoHelper *SalomeHPContainerVectOfHelper::getHelperOfTask(const Task *node)
133 return _launchModeType[locateTask(node)];
136 void SalomeHPContainerVectOfHelper::checkNoCurrentWork() const
138 for(std::map<const Task *,std::size_t >::const_iterator it=_currentlyWorking.begin();it!=_currentlyWorking.end();it++)
140 throw Exception("Something wrong a node is still declared to be using the ressource !");
141 for(std::vector< YACS::BASES::AutoRefCnt<SalomeContainerMonoHelper> >::const_iterator it=_launchModeType.begin();it!=_launchModeType.end();it++)
142 if((*it)->isAlreadyStarted(0))
143 throw Exception("Some of the containers have be started ! Please shutdown them before !");
146 void SalomeHPContainerVectOfHelper::checkPosInVec(std::size_t pos) const
148 if(pos<0 || pos>=_launchModeType.size())
149 throw Exception("The task has been found, but its id is not in the correct range ! resize of of container size during run ?");
152 void SalomeHPContainerVectOfHelper::shutdown()
154 for(std::vector< BASES::AutoRefCnt<YACS::ENGINE::SalomeContainerMonoHelper> >::iterator it=_launchModeType.begin();it!=_launchModeType.end();it++)
155 if((*it).isNotNull())
156 if(!(*it)->isKernelContNull())
160 std::vector<std::string> SalomeHPContainerVectOfHelper::getKernelContainerNames() const
162 std::vector<std::string> ret;
164 YACS::BASES::AutoLocker<SalomeHPContainerVectOfHelper> alck(const_cast<SalomeHPContainerVectOfHelper *>(this));
165 std::size_t sz(_launchModeType.size());
167 for(std::size_t i=0;i<sz;i++)
169 ret[i]=_launchModeType[i]->getKernelContainerName();
175 void SalomeHPContainerVectOfHelper::lock()
180 void SalomeHPContainerVectOfHelper::unLock()