3737
3838using namespace SCIRun ;
3939using namespace SCIRun ::Dataflow::Networks;
40+ using namespace SCIRun ::Core::Thread;
4041
4142boost::shared_ptr<NetworkEditorPythonInterface> NetworkEditorPythonAPI::impl_;
4243ExecutableLookup* NetworkEditorPythonAPI::lookup_ = nullptr ;
4344std::map<std::string, boost::shared_ptr<PyModule>> NetworkEditorPythonAPI::modules_;
45+ Mutex NetworkEditorPythonAPI::pythonLock_ (" Python" );
4446
4547
4648template < class T >
@@ -64,6 +66,7 @@ void NetworkEditorPythonAPI::setImpl(boost::shared_ptr<NetworkEditorPythonInterf
6466 if (!impl_)
6567 {
6668 impl_ = impl;
69+ impl_->setLock (&pythonLock_);
6770
6871 boost::python::to_python_converter< std::vector< boost::shared_ptr<PyModule> >,
6972 StdVectorToListConverter< boost::shared_ptr<PyModule> >, true >();
@@ -79,6 +82,7 @@ void NetworkEditorPythonAPI::setExecutionContext(ExecutableLookup* lookup)
7982
8083boost::shared_ptr<PyModule> NetworkEditorPythonAPI::addModule (const std::string& name)
8184{
85+ Guard g (pythonLock_.get ());
8286 if (impl_)
8387 {
8488 auto m = impl_->addModule (name);
@@ -94,6 +98,7 @@ boost::shared_ptr<PyModule> NetworkEditorPythonAPI::addModule(const std::string&
9498
9599std::string NetworkEditorPythonAPI::removeModule (const std::string& id)
96100{
101+ Guard g (pythonLock_.get ());
97102 if (impl_)
98103 {
99104 modules_.erase (id);
@@ -107,15 +112,20 @@ std::string NetworkEditorPythonAPI::removeModule(const std::string& id)
107112
108113std::vector<boost::shared_ptr<PyModule>> NetworkEditorPythonAPI::modules ()
109114{
115+ Guard g (pythonLock_.get ());
110116 std::vector<boost::shared_ptr<PyModule>> moduleList;
111117 boost::copy (modules_ | boost::adaptors::map_values, std::back_inserter (moduleList));
112118 return moduleList;
113119}
114120
115121std::string NetworkEditorPythonAPI::executeAll ()
116122{
123+
117124 if (impl_)
125+ {
126+ pythonLock_.lock ();
118127 return impl_->executeAll (lookup_);
128+ }
119129 else
120130 {
121131 return " Null implementation or execution context: NetworkEditorPythonAPI::executeAll()" ;
@@ -124,6 +134,7 @@ std::string NetworkEditorPythonAPI::executeAll()
124134
125135std::string NetworkEditorPythonAPI::connect (const std::string& moduleIdFrom, int fromIndex, const std::string& moduleIdTo, int toIndex)
126136{
137+ Guard g (pythonLock_.get ());
127138 if (impl_)
128139 return impl_->connect (moduleIdFrom, fromIndex, moduleIdTo, toIndex);
129140 else
@@ -134,6 +145,7 @@ std::string NetworkEditorPythonAPI::connect(const std::string& moduleIdFrom, int
134145
135146std::string NetworkEditorPythonAPI::disconnect (const std::string& moduleIdFrom, int fromIndex, const std::string& moduleIdTo, int toIndex)
136147{
148+ Guard g (pythonLock_.get ());
137149 if (impl_)
138150 return impl_->disconnect (moduleIdFrom, fromIndex, moduleIdTo, toIndex);
139151 else
@@ -144,6 +156,7 @@ std::string NetworkEditorPythonAPI::disconnect(const std::string& moduleIdFrom,
144156
145157std::string NetworkEditorPythonAPI::saveNetwork (const std::string& filename)
146158{
159+ Guard g (pythonLock_.get ());
147160 if (impl_)
148161 return impl_->saveNetwork (filename);
149162 else
@@ -154,6 +167,7 @@ std::string NetworkEditorPythonAPI::saveNetwork(const std::string& filename)
154167
155168std::string NetworkEditorPythonAPI::loadNetwork (const std::string& filename)
156169{
170+ Guard g (pythonLock_.get ());
157171 if (impl_)
158172 return impl_->loadNetwork (filename);
159173 else
@@ -164,6 +178,7 @@ std::string NetworkEditorPythonAPI::loadNetwork(const std::string& filename)
164178
165179std::string NetworkEditorPythonAPI::quit (bool force)
166180{
181+ Guard g (pythonLock_.get ());
167182 if (impl_)
168183 return impl_->quit (force);
169184 else
@@ -174,6 +189,7 @@ std::string NetworkEditorPythonAPI::quit(bool force)
174189
175190boost::python::object NetworkEditorPythonAPI::scirun_get_module_state (const std::string& moduleId, const std::string& stateVariable)
176191{
192+ Guard g (pythonLock_.get ());
177193 auto modIter = modules_.find (moduleId);
178194 if (modIter != modules_.end ())
179195 return modIter->second ->getattr (stateVariable);
@@ -182,6 +198,7 @@ boost::python::object NetworkEditorPythonAPI::scirun_get_module_state(const std:
182198
183199std::string NetworkEditorPythonAPI::scirun_set_module_state (const std::string& moduleId, const std::string& stateVariable, const boost::python::object& value)
184200{
201+ Guard g (pythonLock_.get ());
185202 auto modIter = modules_.find (moduleId);
186203 if (modIter != modules_.end ())
187204 {
@@ -193,6 +210,7 @@ std::string NetworkEditorPythonAPI::scirun_set_module_state(const std::string& m
193210
194211std::string NetworkEditorPythonAPI::scirun_dump_module_state (const std::string& moduleId)
195212{
213+ Guard g (pythonLock_.get ());
196214 auto modIter = modules_.find (moduleId);
197215 if (modIter != modules_.end ())
198216 {
@@ -204,6 +222,7 @@ std::string NetworkEditorPythonAPI::scirun_dump_module_state(const std::string&
204222// / @todo: bizarre reason for this return type and casting. but it works.
205223boost::shared_ptr<PyPort> SCIRun::operator >>(const PyPort& from, const PyPort& to)
206224{
225+ Guard g (NetworkEditorPythonAPI::getLock ().get ());
207226 from.connect (to);
208227 auto ptr = const_cast <PyPort&>(to).shared_from_this ();
209228 return boost::ref (ptr);
@@ -216,17 +235,12 @@ std::string SimplePythonAPI::scirun_add_module(const std::string& name)
216235
217236std::string SimplePythonAPI::scirun_quit ()
218237{
219- return NetworkEditorPythonAPI::quit (true );
238+ return NetworkEditorPythonAPI::quit (false );
220239}
221240
222- std::string SimplePythonAPI::scirun_connect_modules ( const std::string& modIdFrom, int fromIndex, const std::string& modIdTo, int toIndex )
241+ std::string SimplePythonAPI::scirun_force_quit ( )
223242{
224- return NetworkEditorPythonAPI::connect (modIdFrom, fromIndex, modIdTo, toIndex);
225- }
226-
227- std::string SimplePythonAPI::scirun_disconnect_modules (const std::string& modIdFrom, int fromIndex, const std::string& modIdTo, int toIndex)
228- {
229- return NetworkEditorPythonAPI::disconnect (modIdFrom, fromIndex, modIdTo, toIndex);
243+ return NetworkEditorPythonAPI::quit (true );
230244}
231245
232246boost::python::object SimplePythonAPI::scirun_get_module_state (const std::string& moduleId, const std::string& stateVariable)
0 commit comments