Skip to content

Commit 5f2ddd6

Browse files
committed
Add some needed execution locking
1 parent e7d6e29 commit 5f2ddd6

File tree

9 files changed

+79
-28
lines changed

9 files changed

+79
-28
lines changed

src/Dataflow/Engine/Controller/NetworkEditorController.cc

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ NetworkEditorController::NetworkEditorController(ModuleFactoryHandle mf, ModuleS
8585
}
8686

8787
NetworkEditorController::NetworkEditorController(SCIRun::Dataflow::Networks::NetworkHandle network, ExecutionStrategyFactoryHandle executorFactory, NetworkEditorSerializationManager* nesm)
88-
: theNetwork_(network), executorFactory_(executorFactory), serializationManager_(nesm)
88+
: theNetwork_(network), executorFactory_(executorFactory), serializationManager_(nesm),
89+
signalSwitch_(true)
8990
{
9091
}
9192

@@ -330,12 +331,10 @@ boost::optional<ConnectionId> NetworkEditorController::requestConnection(const P
330331
printNetwork();
331332
return id;
332333
}
333-
else
334-
{
335-
Log::get() << NOTICE << "Invalid Connection request: input port is full, or ports are different datatype or same i/o type, or on the same module." << std::endl;
336-
invalidConnection_(desc);
337-
return boost::none;
338-
}
334+
335+
Log::get() << NOTICE << "Invalid Connection request: input port is full, or ports are different datatype or same i/o type, or on the same module." << std::endl;
336+
invalidConnection_(desc);
337+
return boost::none;
339338
}
340339

341340
void NetworkEditorController::removeConnection(const ConnectionId& id)
@@ -582,7 +581,7 @@ NetworkGlobalSettings& NetworkEditorController::getSettings()
582581

583582
void NetworkEditorController::setExecutorType(int type)
584583
{
585-
executionManager_.setExecutionStrategy(executorFactory_->create((ExecutionStrategy::Type)type));
584+
executionManager_.setExecutionStrategy(executorFactory_->create(static_cast<ExecutionStrategy::Type>(type)));
586585
}
587586

588587
const ModuleDescriptionMap& NetworkEditorController::getAllAvailableModuleDescriptions() const

src/Dataflow/Engine/Controller/NetworkEditorController.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,21 @@ namespace Engine {
104104
Networks::ModuleHandle duplicateModule(const Networks::ModuleHandle& module);
105105
void connectNewModule(const Networks::ModuleHandle& moduleToConnectTo, const Networks::PortDescriptionInterface* portToConnect, const std::string& newModuleName);
106106

107-
boost::optional<Networks::ConnectionId> requestConnection(const Networks::PortDescriptionInterface* from, const Networks::PortDescriptionInterface* to);
107+
boost::optional<Networks::ConnectionId> requestConnection(const Networks::PortDescriptionInterface* from, const Networks::PortDescriptionInterface* to) override;
108108
void removeConnection(const Networks::ConnectionId& id);
109109

110110
void executeAll(const Networks::ExecutableLookup* lookup);
111111
void executeModule(const Networks::ModuleHandle& module, const Networks::ExecutableLookup* lookup);
112112

113-
virtual Networks::NetworkFileHandle saveNetwork() const;
114-
virtual void loadNetwork(const Networks::NetworkFileHandle& xml);
113+
virtual Networks::NetworkFileHandle saveNetwork() const override;
114+
virtual void loadNetwork(const Networks::NetworkFileHandle& xml) override;
115115

116116
Networks::NetworkFileHandle serializeNetworkFragment(Networks::ModuleFilter modFilter, Networks::ConnectionFilter connFilter) const;
117117
void appendToNetwork(const Networks::NetworkFileHandle& xml);
118118
//////////////////////End: To be Pythonized///////////////////////////////
119119
//////////////////////////////////////////////////////////////////////////
120120

121-
virtual void clear();
121+
virtual void clear() override;
122122

123123
boost::signals2::connection connectModuleAdded(const ModuleAddedSignalType::slot_type& subscriber);
124124
boost::signals2::connection connectModuleRemoved(const ModuleRemovedSignalType::slot_type& subscriber);

src/Dataflow/Engine/Controller/PythonImpl.cc

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
using namespace SCIRun;
4545
using namespace SCIRun::Core::Algorithms;
4646
using namespace SCIRun::Core::Commands;
47+
using namespace SCIRun::Core::Thread;
4748
using namespace SCIRun::Dataflow::Engine;
4849
using namespace SCIRun::Dataflow::Networks;
4950

@@ -160,7 +161,7 @@ namespace
160161
}
161162
}
162163

163-
virtual std::string id() const
164+
virtual std::string id() const override
164165
{
165166
if (module_)
166167
return module_->get_id();
@@ -328,7 +329,26 @@ namespace SCIRun {
328329
}
329330
}
330331

331-
PythonImpl::PythonImpl(NetworkEditorController& nec, GlobalCommandFactoryHandle cmdFactory) : impl_(new PythonImplImpl), nec_(nec), cmdFactory_(cmdFactory) {}
332+
PythonImpl::PythonImpl(NetworkEditorController& nec, GlobalCommandFactoryHandle cmdFactory) : impl_(new PythonImplImpl), nec_(nec), cmdFactory_(cmdFactory), executionMutex_(nullptr)
333+
{
334+
nec_.connectNetworkExecutionFinished([this](int) { executionFromPythonFinish(0); });
335+
}
336+
337+
void PythonImpl::setLock(Mutex* mutex)
338+
{
339+
executionMutex_ = mutex;
340+
}
341+
342+
void PythonImpl::executionFromPythonStart()
343+
{
344+
//std::cout << "Python impl exec start" << std::endl;
345+
}
346+
347+
void PythonImpl::executionFromPythonFinish(int)
348+
{
349+
if (executionMutex_)
350+
executionMutex_->unlock();
351+
}
332352

333353
boost::shared_ptr<PyModule> PythonImpl::addModule(const std::string& name)
334354
{
@@ -408,8 +428,11 @@ std::string PythonImpl::loadNetwork(const std::string& filename)
408428

409429
std::string PythonImpl::quit(bool force)
410430
{
411-
cmdFactory_->create(GlobalCommands::SetupQuitAfterExecute)->execute();
412-
return "PythonImpl::quit";
431+
if (force)
432+
cmdFactory_->create(GlobalCommands::QuitCommand)->execute();
433+
else
434+
cmdFactory_->create(GlobalCommands::SetupQuitAfterExecute)->execute();
435+
return "Quit after execute enabled.";
413436
}
414437

415438
#endif

src/Dataflow/Engine/Controller/PythonImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,14 @@ namespace Engine {
5757
virtual std::string saveNetwork(const std::string& filename) override;
5858
virtual std::string loadNetwork(const std::string& filename) override;
5959
virtual std::string quit(bool force) override;
60+
virtual void setLock(Core::Thread::Mutex* mutex) override;
6061
private:
62+
void executionFromPythonStart();
63+
void executionFromPythonFinish(int);
6164
boost::shared_ptr<PythonImplImpl> impl_;
6265
NetworkEditorController& nec_;
6366
Core::Commands::GlobalCommandFactoryHandle cmdFactory_;
67+
Core::Thread::Mutex* executionMutex_;
6468
};
6569

6670
}}}

src/Dataflow/Engine/Python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ SCIRUN_ADD_LIBRARY(SCIRunPythonAPI
4343
)
4444

4545
TARGET_LINK_LIBRARIES(SCIRunPythonAPI
46+
Core_Thread
4647
${SCI_PYTHON_LIBRARY}
4748
${SCI_BOOST_LIBRARY}
4849
)

src/Dataflow/Engine/Python/NetworkEditorPythonAPI.cc

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@
3737

3838
using namespace SCIRun;
3939
using namespace SCIRun::Dataflow::Networks;
40+
using namespace SCIRun::Core::Thread;
4041

4142
boost::shared_ptr<NetworkEditorPythonInterface> NetworkEditorPythonAPI::impl_;
4243
ExecutableLookup* NetworkEditorPythonAPI::lookup_ = nullptr;
4344
std::map<std::string, boost::shared_ptr<PyModule>> NetworkEditorPythonAPI::modules_;
45+
Mutex NetworkEditorPythonAPI::pythonLock_("Python");
4446

4547

4648
template< 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

8083
boost::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

9599
std::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

108113
std::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

115121
std::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

125135
std::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

135146
std::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

145157
std::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

155168
std::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

165179
std::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

175190
boost::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

183199
std::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

194211
std::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.
205223
boost::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

217236
std::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

232246
boost::python::object SimplePythonAPI::scirun_get_module_state(const std::string& moduleId, const std::string& stateVariable)

src/Dataflow/Engine/Python/NetworkEditorPythonAPI.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <vector>
3535
#include <boost/python.hpp>
3636
#include <Dataflow/Network/NetworkFwd.h>
37+
#include <Core/Thread/Mutex.h>
3738
#include <Dataflow/Engine/Python/share.h>
3839

3940
namespace SCIRun {
@@ -61,22 +62,23 @@ namespace SCIRun {
6162
static void setImpl(boost::shared_ptr<NetworkEditorPythonInterface> impl);
6263
/// @todo: smelly!
6364
static void setExecutionContext(Dataflow::Networks::ExecutableLookup* lookup);
65+
static Core::Thread::Mutex& getLock() { return pythonLock_; }
6466
private:
6567
NetworkEditorPythonAPI();
6668
static boost::shared_ptr<NetworkEditorPythonInterface> impl_;
6769
static Dataflow::Networks::ExecutableLookup* lookup_;
6870
static std::map<std::string, boost::shared_ptr<PyModule>> modules_;
71+
static Core::Thread::Mutex pythonLock_;
6972
};
7073

7174
class SCISHARE SimplePythonAPI
7275
{
7376
public:
7477
static std::string scirun_add_module(const std::string& name);
75-
static std::string scirun_connect_modules(const std::string& modIdFrom, int fromIndex, const std::string& modIdTo, int toIndex);
76-
static std::string scirun_disconnect_modules(const std::string& modIdFrom, int fromIndex, const std::string& modIdTo, int toIndex);
7778
static boost::python::object scirun_get_module_state(const std::string& moduleId, const std::string& stateVariable);
7879
static std::string scirun_set_module_state(const std::string& moduleId, const std::string& stateVariable, const boost::python::object& value);
7980
static std::string scirun_quit();
81+
static std::string scirun_force_quit();
8082
private:
8183
SimplePythonAPI();
8284
};

src/Dataflow/Engine/Python/NetworkEditorPythonInterface.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838

3939
namespace SCIRun
4040
{
41+
namespace Core
42+
{
43+
namespace Thread
44+
{
45+
class Mutex;
46+
}}
4147

4248
class SCISHARE PyModule
4349
{
@@ -110,6 +116,7 @@ namespace SCIRun
110116
virtual std::string saveNetwork(const std::string& filename) = 0;
111117
virtual std::string loadNetwork(const std::string& filename) = 0;
112118
virtual std::string quit(bool force) = 0;
119+
virtual void setLock(Core::Thread::Mutex* mutex) = 0;
113120
};
114121

115122
}

src/Dataflow/Engine/Python/SCIRunPythonModule.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ BOOST_PYTHON_MODULE(SCIRunPythonAPI)
7979
boost::python::def("scirun_remove_module", &NetworkEditorPythonAPI::removeModule);
8080
boost::python::def("scirun_execute_all", &NetworkEditorPythonAPI::executeAll);
8181

82-
boost::python::def("scirun_connect_modules", &SimplePythonAPI::scirun_connect_modules);
83-
boost::python::def("scirun_disconnect_modules", &SimplePythonAPI::scirun_disconnect_modules);
82+
boost::python::def("scirun_connect_modules", &NetworkEditorPythonAPI::connect);
83+
boost::python::def("scirun_disconnect_modules", &NetworkEditorPythonAPI::disconnect);
8484

8585
boost::python::def("scirun_get_module_state", &SimplePythonAPI::scirun_get_module_state);
8686
boost::python::def("scirun_set_module_state", &SimplePythonAPI::scirun_set_module_state);
@@ -89,6 +89,7 @@ BOOST_PYTHON_MODULE(SCIRunPythonAPI)
8989
boost::python::def("scirun_save_network", &NetworkEditorPythonAPI::saveNetwork);
9090
boost::python::def("scirun_load_network", &NetworkEditorPythonAPI::loadNetwork);
9191
boost::python::def("scirun_quit", &SimplePythonAPI::scirun_quit);
92+
boost::python::def("scirun_force_quit", &SimplePythonAPI::scirun_force_quit);
9293
}
9394

9495
#endif

0 commit comments

Comments
 (0)