Salome HOME
bb9ae661ddf5dd9a40e7ed38d673d778db9519db
[modules/yacs.git] / src / runtime / SalomeHPContainerTools.cxx
1 // Copyright (C) 2006-2016  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
27 using namespace YACS::ENGINE;
28
29 void SalomeHPContainerVectOfHelper::resize(std::size_t sz)
30 {
31   std::size_t oldSize(_launchModeType.size());
32   if(sz==oldSize)
33     return;
34   checkNoCurrentWork();
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();
40 }
41
42 std::size_t SalomeHPContainerVectOfHelper::getNumberOfFreePlace() const
43 {
44   return std::count(_whichOccupied.begin(),_whichOccupied.end(),false);
45 }
46
47 std::size_t SalomeHPContainerVectOfHelper::getNumberOfFreePlaceAmong(const std::vector<std::size_t>& idsOfKernelContainers) const
48 {
49   std::size_t ret;
50   for(std::vector<std::size_t>::const_iterator it=idsOfKernelContainers.begin();it!=idsOfKernelContainers.end();it++)
51     if(!_whichOccupied[*it])
52       ret++;
53   return ret;
54 }
55
56 void SalomeHPContainerVectOfHelper::allocateFor(const std::vector<const Task *>& nodes)
57 {
58   for(std::vector<const Task *>::const_iterator it=nodes.begin();it!=nodes.end();it++)
59     {
60       if(!(*it))
61         continue;
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;
69     }
70 }
71
72 void SalomeHPContainerVectOfHelper::allocateForAmong(const std::vector<std::size_t>& idsOfKernelContainers, const std::vector<const Task *>& nodes)
73 {
74   for(std::vector<const Task *>::const_iterator it=nodes.begin();it!=nodes.end();it++)
75     {
76       if(!(*it))
77         continue;
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])
83           {
84             it2=*it;
85             break;
86           }
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;
90     }
91 }
92
93 void SalomeHPContainerVectOfHelper::release(const Task *node)
94 {
95   if(!node)
96     return ;
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);
102 }
103
104 std::size_t SalomeHPContainerVectOfHelper::locateTask(const Task *node) const
105 {
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);
110   checkPosInVec(ret);
111   return ret;
112 }
113
114 const SalomeContainerMonoHelper *SalomeHPContainerVectOfHelper::getHelperOfTaskThreadSafe(const Task *node) const
115 {
116   YACS::BASES::AutoLocker<SalomeHPContainerVectOfHelper> alck(const_cast<SalomeHPContainerVectOfHelper *>(this));
117   return _launchModeType[locateTask(node)];
118 }
119
120 const SalomeContainerMonoHelper *SalomeHPContainerVectOfHelper::getHelperOfTask(const Task *node) const
121 {
122   return _launchModeType[locateTask(node)];
123 }
124
125 SalomeContainerMonoHelper *SalomeHPContainerVectOfHelper::getHelperOfTaskThreadSafe(const Task *node)
126 {
127   YACS::BASES::AutoLocker<SalomeHPContainerVectOfHelper> alck(this);
128   return _launchModeType[locateTask(node)];
129 }
130
131 SalomeContainerMonoHelper *SalomeHPContainerVectOfHelper::getHelperOfTask(const Task *node)
132 {
133   return _launchModeType[locateTask(node)];
134 }
135
136 void SalomeHPContainerVectOfHelper::checkNoCurrentWork() const
137 {
138   for(std::map<const Task *,std::size_t >::const_iterator it=_currentlyWorking.begin();it!=_currentlyWorking.end();it++)
139     if((*it).first)
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 !");
144 }
145
146 void SalomeHPContainerVectOfHelper::checkPosInVec(std::size_t pos) const
147 {
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 ?");
150 }
151
152 void SalomeHPContainerVectOfHelper::shutdown()
153 {
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())
157         (*it)->shutdown();
158 }
159
160 void SalomeHPContainerVectOfHelper::lock()
161 {
162   _mutex.lock();
163 }
164
165 void SalomeHPContainerVectOfHelper::unLock()
166 {
167   _mutex.unLock();
168 }