Salome HOME
38e58830364c57614bd913a61002385776a0b5d9
[modules/yacs.git] / src / runtime / SalomeHPContainerTools.cxx
1 // Copyright (C) 2006-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "SalomeHPContainerTools.hxx"
21 #include "SalomeHPContainer.hxx"
22 #include "AutoLocker.hxx"
23 #include "Exception.hxx"
24
25 #include <algorithm>
26 #include <limits>
27
28 using namespace YACS::ENGINE;
29
30 void SalomeHPContainerVectOfHelper::resize(std::size_t sz)
31 {
32   std::size_t oldSize(_launchModeType.size());
33   if(sz==oldSize)
34     return;
35   checkNoCurrentWork();
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();
41 }
42
43 std::size_t SalomeHPContainerVectOfHelper::getNumberOfFreePlace() const
44 {
45   return std::count(_whichOccupied.begin(),_whichOccupied.end(),false);
46 }
47
48 std::size_t SalomeHPContainerVectOfHelper::getNumberOfFreePlaceAmong(const std::vector<std::size_t>& idsOfKernelContainers) const
49 {
50   std::size_t ret(0);
51   for(std::vector<std::size_t>::const_iterator it=idsOfKernelContainers.begin();it!=idsOfKernelContainers.end();it++)
52     if(!_whichOccupied[*it])
53       ret++;
54   return ret;
55 }
56
57 void SalomeHPContainerVectOfHelper::allocateFor(const std::vector<const Task *>& nodes)
58 {
59   for(std::vector<const Task *>::const_iterator it=nodes.begin();it!=nodes.end();it++)
60     {
61       if(!(*it))
62         continue;
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;
70     }
71 }
72
73 void SalomeHPContainerVectOfHelper::allocateForAmong(const std::vector<std::size_t>& idsOfKernelContainers, const std::vector<const Task *>& nodes)
74 {
75   for(std::vector<const Task *>::const_iterator it=nodes.begin();it!=nodes.end();it++)
76     {
77       if(!(*it))
78         continue;
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])
84           {
85             it2=*it;
86             break;
87           }
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;
91     }
92 }
93
94 void SalomeHPContainerVectOfHelper::release(const Task *node)
95 {
96   if(!node)
97     return ;
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);
103 }
104
105 std::size_t SalomeHPContainerVectOfHelper::locateTask(const Task *node) const
106 {
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);
111   checkPosInVec(ret);
112   return ret;
113 }
114
115 const SalomeContainerMonoHelper *SalomeHPContainerVectOfHelper::getHelperOfTaskThreadSafe(const Task *node) const
116 {
117   YACS::BASES::AutoLocker<SalomeHPContainerVectOfHelper> alck(const_cast<SalomeHPContainerVectOfHelper *>(this));
118   return _launchModeType[locateTask(node)];
119 }
120
121 const SalomeContainerMonoHelper *SalomeHPContainerVectOfHelper::getHelperOfTask(const Task *node) const
122 {
123   return _launchModeType[locateTask(node)];
124 }
125
126 SalomeContainerMonoHelper *SalomeHPContainerVectOfHelper::getHelperOfTaskThreadSafe(const Task *node)
127 {
128   YACS::BASES::AutoLocker<SalomeHPContainerVectOfHelper> alck(this);
129   return _launchModeType[locateTask(node)];
130 }
131
132 SalomeContainerMonoHelper *SalomeHPContainerVectOfHelper::getHelperOfTask(const Task *node)
133 {
134   return _launchModeType[locateTask(node)];
135 }
136
137 void SalomeHPContainerVectOfHelper::checkNoCurrentWork() const
138 {
139   for(std::map<const Task *,std::size_t >::const_iterator it=_currentlyWorking.begin();it!=_currentlyWorking.end();it++)
140     if((*it).first)
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 !");
145 }
146
147 void SalomeHPContainerVectOfHelper::checkPosInVec(std::size_t pos) const
148 {
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 ?");
151 }
152
153 void SalomeHPContainerVectOfHelper::shutdown()
154 {
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())
158         (*it)->shutdown();
159 }
160
161 std::vector<std::string> SalomeHPContainerVectOfHelper::getKernelContainerNames() const
162 {
163   std::vector<std::string> ret;
164   {
165     YACS::BASES::AutoLocker<SalomeHPContainerVectOfHelper> alck(const_cast<SalomeHPContainerVectOfHelper *>(this));
166     std::size_t sz(_launchModeType.size());
167     ret.resize(sz);
168     for(std::size_t i=0;i<sz;i++)
169       {
170         ret[i]=_launchModeType[i]->getKernelContainerName();
171       }
172   }
173   return ret;
174 }
175
176 void SalomeHPContainerVectOfHelper::lock()
177 {
178   _mutex.lock();
179 }
180
181 void SalomeHPContainerVectOfHelper::unLock()
182 {
183   _mutex.unLock();
184 }