Skip to content

Commit c04807b

Browse files
committed
fix #336: Add event loop mutex
1 parent 54fa05f commit c04807b

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/engine/internal/engine.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ void Engine::start()
152152

153153
deleteClones();
154154

155+
m_eventLoopMutex.lock();
155156
m_timer->reset();
156157
m_running = true;
157158

@@ -160,6 +161,8 @@ void Engine::start()
160161
for (auto block : gfBlocks)
161162
startScript(block, target);
162163
}
164+
165+
m_eventLoopMutex.unlock();
163166
}
164167

165168
void Engine::stop()
@@ -360,6 +363,7 @@ void Engine::eventLoop(bool untilProjectStops)
360363
TargetScriptMap scripts = m_runningScripts; // this must be copied (for now)
361364

362365
do {
366+
m_eventLoopMutex.lock();
363367
m_scriptsToRemove.clear();
364368

365369
// Execute new scripts from last frame
@@ -376,12 +380,14 @@ void Engine::eventLoop(bool untilProjectStops)
376380
for (const auto &pair : m_runningScripts) {
377381
if (!pair.second.empty()) {
378382
empty = false;
383+
m_eventLoopMutex.unlock();
379384
break;
380385
}
381386
}
382387

383388
if (empty) {
384389
stop = true;
390+
m_eventLoopMutex.unlock();
385391
break;
386392
}
387393
}
@@ -391,6 +397,7 @@ void Engine::eventLoop(bool untilProjectStops)
391397

392398
if (m_stopEventLoop) {
393399
stop = true;
400+
m_eventLoopMutex.unlock();
394401
break;
395402
}
396403

@@ -400,6 +407,7 @@ void Engine::eventLoop(bool untilProjectStops)
400407
elapsedTime = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - frameStart);
401408
sleepTime = m_frameDuration - elapsedTime;
402409
timeout = sleepTime <= std::chrono::milliseconds::zero();
410+
m_eventLoopMutex.unlock();
403411
} while (!((m_redrawRequested && !m_turboModeEnabled) || timeout || stop));
404412

405413
if (stop)
@@ -1173,14 +1181,17 @@ BlockSectionContainer *Engine::blockSectionContainer(IBlockSection *section) con
11731181

11741182
void Engine::finalize()
11751183
{
1184+
m_eventLoopMutex.lock();
11761185
m_runningScripts.clear();
11771186
m_scriptsToRemove.clear();
11781187
m_running = false;
11791188
m_redrawRequested = false;
1189+
m_eventLoopMutex.unlock();
11801190
}
11811191

11821192
void Engine::deleteClones()
11831193
{
1194+
m_eventLoopMutex.lock();
11841195
removeExecutableClones();
11851196
m_clones.clear();
11861197

@@ -1196,6 +1207,8 @@ void Engine::deleteClones()
11961207
}
11971208
}
11981209
}
1210+
1211+
m_eventLoopMutex.unlock();
11991212
}
12001213

12011214
void Engine::removeExecutableClones()

src/engine/internal/engine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ class Engine : public IEngine
167167
std::vector<VirtualMachine *> m_scriptsToRemove;
168168
std::unordered_map<std::shared_ptr<Block>, std::shared_ptr<Script>> m_scripts;
169169
std::vector<BlockFunc> m_functions;
170+
std::recursive_mutex m_eventLoopMutex;
170171

171172
std::unique_ptr<ITimer> m_defaultTimer;
172173
ITimer *m_timer = nullptr;

0 commit comments

Comments
 (0)