diff --git a/libraries/script-engine/src/BatchLoader.cpp b/libraries/script-engine/src/BatchLoader.cpp index 15b3b6c853..692c0ecd7a 100644 --- a/libraries/script-engine/src/BatchLoader.cpp +++ b/libraries/script-engine/src/BatchLoader.cpp @@ -48,6 +48,11 @@ void BatchLoader::start() { if (!self) { return; } + + // Because the ScriptCache may call this callback from differents threads, + // we need to make sure this is thread-safe. + std::lock_guard lock(_dataLock); + if (isURL && success) { _data.insert(url, contents); qCDebug(scriptengine) << "Loaded: " << url; @@ -55,16 +60,11 @@ void BatchLoader::start() { _data.insert(url, QString()); qCDebug(scriptengine) << "Could not load" << url; } - checkFinished(); + + if (!_finished && _urls.size() == _data.size()) { + _finished = true; + emit finished(_data); + } }, false); } - - checkFinished(); -} - -void BatchLoader::checkFinished() { - if (!_finished && _urls.size() == _data.size()) { - _finished = true; - emit finished(_data); - } } diff --git a/libraries/script-engine/src/BatchLoader.h b/libraries/script-engine/src/BatchLoader.h index cda040d219..40b43d23b6 100644 --- a/libraries/script-engine/src/BatchLoader.h +++ b/libraries/script-engine/src/BatchLoader.h @@ -19,6 +19,8 @@ #include #include +#include + class BatchLoader : public QObject { Q_OBJECT public: @@ -37,6 +39,7 @@ private: bool _finished; QSet _urls; QMap _data; + std::mutex _dataLock; }; #endif // hifi_BatchLoader_h