Skip to content

Commit 18f772c

Browse files
authored
Merge pull request #382 from scratchcpp/event_loop_mutex
Fix #336: Add event loop mutex
2 parents fec84ae + 65e25e3 commit 18f772c

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)
@@ -1178,14 +1186,17 @@ BlockSectionContainer *Engine::blockSectionContainer(IBlockSection *section) con
11781186

11791187
void Engine::finalize()
11801188
{
1189+
m_eventLoopMutex.lock();
11811190
m_runningScripts.clear();
11821191
m_scriptsToRemove.clear();
11831192
m_running = false;
11841193
m_redrawRequested = false;
1194+
m_eventLoopMutex.unlock();
11851195
}
11861196

11871197
void Engine::deleteClones()
11881198
{
1199+
m_eventLoopMutex.lock();
11891200
removeExecutableClones();
11901201
m_clones.clear();
11911202

@@ -1201,6 +1212,8 @@ void Engine::deleteClones()
12011212
}
12021213
}
12031214
}
1215+
1216+
m_eventLoopMutex.unlock();
12041217
}
12051218

12061219
void Engine::removeExecutableClones()

src/engine/internal/engine.h

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

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

0 commit comments

Comments
 (0)