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"
28 using namespace YACS::ENGINE;
30 void SalomeHPContainerVectOfHelper::resize(std::size_t sz)
32 std::size_t oldSize(_launchModeType.size());
36 _whichOccupied.resize(sz); std::fill(_whichOccupied.begin(),_whichOccupied.end(),false);
37 _launchModeType.resize(sz);
38 for(std::size_t i=oldSize;i<sz;i++)
39 _launchModeType[i]=new SalomeContainerMonoHelper;
40 _currentlyWorking.clear();
43 std::size_t SalomeHPContainerVectOfHelper::getNumberOfFreePlace() const
45 return std::count(_whichOccupied.begin(),_whichOccupied.end(),false);
48 std::size_t SalomeHPContainerVectOfHelper::getNumberOfFreePlaceAmong(const std::vector<std::size_t>& idsOfKernelContainers) const
51 for(std::vector<std::size_t>::const_iterator it=idsOfKernelContainers.begin();it!=idsOfKernelContainers.end();it++)
52 if(!_whichOccupied[*it])
57 void SalomeHPContainerVectOfHelper::allocateFor(const std::vector<const Task *>& nodes)
59 for(std::vector<const Task *>::const_iterator it=nodes.begin();it!=nodes.end();it++)
63 if(_currentlyWorking.find(*it)!=_currentlyWorking.end())
64 throw Exception("Searching to allocate for a ServiceNode instance already declared as allocated !");
65 std::vector<bool>::iterator it2(std::find(_whichOccupied.begin(),_whichOccupied.end(),false));
66 if(it2==_whichOccupied.end())
67 throw Exception("All ressources are already occupied ! You are expected to wait for released resources !");
68 std::size_t pos(std::distance(_whichOccupied.begin(),it2));
69 _currentlyWorking[*it]=pos; _whichOccupied[pos]=true;
73 void SalomeHPContainerVectOfHelper::allocateForAmong(const std::vector<std::size_t>& idsOfKernelContainers, const std::vector<const Task *>& nodes)
75 for(std::vector<const Task *>::const_iterator it=nodes.begin();it!=nodes.end();it++)
79 if(_currentlyWorking.find(*it)!=_currentlyWorking.end())
80 throw Exception("Searching 2 to allocate for a ServiceNode instance already declared as allocated !");
81 std::size_t it2(std::numeric_limits<std::size_t>::max());
82 for(std::vector<std::size_t>::const_iterator it=idsOfKernelContainers.begin();it!=idsOfKernelContainers.end();it++)
83 if(!_whichOccupied[*it])
88 if(it2==std::numeric_limits<std::size_t>::max())
89 throw Exception("All 2 ressources are already occupied ! You are expected to wait for released resources !");
90 _currentlyWorking[*it]=it2; _whichOccupied[it2]=true;
94 void SalomeHPContainerVectOfHelper::release(const Task *node)
98 std::map< const Task *,std::size_t >::iterator it(_currentlyWorking.find(node));
99 if(it==_currentlyWorking.end())
100 throw Exception("Request to release a resource not declared as working !");
101 _whichOccupied[(*it).second]=false;
102 _currentlyWorking.erase(it);
105 std::size_t SalomeHPContainerVectOfHelper::locateTask(const Task *node) const
107 std::map< const Task *,std::size_t >::const_iterator it(_currentlyWorking.find(node));
108 if(it==_currentlyWorking.end())
109 throw Exception("current Node to be located is not marked as launched !");
110 std::size_t ret((*it).second);
115 const SalomeContainerMonoHelper *SalomeHPContainerVectOfHelper::getHelperOfTaskThreadSafe(const Task *node) const
117 YACS::BASES::AutoLocker<SalomeHPContainerVectOfHelper> alck(const_cast<SalomeHPContainerVectOfHelper *>(this));
118 return _launchModeType[locateTask(node)];
121 const SalomeContainerMonoHelper *SalomeHPContainerVectOfHelper::getHelperOfTask(const Task *node) const
123 return _launchModeType[locateTask(node)];
126 SalomeContainerMonoHelper *SalomeHPContainerVectOfHelper::getHelperOfTaskThreadSafe(const Task *node)
128 YACS::BASES::AutoLocker<SalomeHPContainerVectOfHelper> alck(this);
129 return _launchModeType[locateTask(node)];
132 SalomeContainerMonoHelper *SalomeHPContainerVectOfHelper::getHelperOfTask(const Task *node)
134 return _launchModeType[locateTask(node)];
137 void SalomeHPContainerVectOfHelper::checkNoCurrentWork() const
139 for(std::map<const Task *,std::size_t >::const_iterator it=_currentlyWorking.begin();it!=_currentlyWorking.end();it++)
141 throw Exception("Something wrong a node is still declared to be using the ressource !");
142 for(std::vector< YACS::BASES::AutoRefCnt<SalomeContainerMonoHelper> >::const_iterator it=_launchModeType.begin();it!=_launchModeType.end();it++)
143 if((*it)->isAlreadyStarted(0))
144 throw Exception("Some of the containers have be started ! Please shutdown them before !");
147 void SalomeHPContainerVectOfHelper::checkPosInVec(std::size_t pos) const
149 if(pos<0 || pos>=_launchModeType.size())
150 throw Exception("The task has been found, but its id is not in the correct range ! resize of of container size during run ?");
153 void SalomeHPContainerVectOfHelper::shutdown()
155 for(std::vector< BASES::AutoRefCnt<YACS::ENGINE::SalomeContainerMonoHelper> >::iterator it=_launchModeType.begin();it!=_launchModeType.end();it++)
156 if((*it).isNotNull())
157 if(!(*it)->isKernelContNull())
161 std::vector<std::string> SalomeHPContainerVectOfHelper::getKernelContainerNames() const
163 std::vector<std::string> ret;
165 YACS::BASES::AutoLocker<SalomeHPContainerVectOfHelper> alck(const_cast<SalomeHPContainerVectOfHelper *>(this));
166 std::size_t sz(_launchModeType.size());
168 for(std::size_t i=0;i<sz;i++)
170 ret[i]=_launchModeType[i]->getKernelContainerName();
176 void SalomeHPContainerVectOfHelper::lock()
181 void SalomeHPContainerVectOfHelper::unLock()