Fix race condition in BatchLoader

This commit is contained in:
Ryan Huffman 2016-10-16 10:26:57 -07:00
parent 2e64e46a7e
commit 9ef6aee3d1
2 changed files with 13 additions and 10 deletions

View file

@ -48,6 +48,11 @@ void BatchLoader::start() {
if (!self) { if (!self) {
return; return;
} }
// Because the ScriptCache may call this callback from differents threads,
// we need to make sure this is thread-safe.
std::lock_guard<std::mutex> lock(_dataLock);
if (isURL && success) { if (isURL && success) {
_data.insert(url, contents); _data.insert(url, contents);
qCDebug(scriptengine) << "Loaded: " << url; qCDebug(scriptengine) << "Loaded: " << url;
@ -55,16 +60,11 @@ void BatchLoader::start() {
_data.insert(url, QString()); _data.insert(url, QString());
qCDebug(scriptengine) << "Could not load" << url; qCDebug(scriptengine) << "Could not load" << url;
} }
checkFinished();
}, false);
}
checkFinished();
}
void BatchLoader::checkFinished() {
if (!_finished && _urls.size() == _data.size()) { if (!_finished && _urls.size() == _data.size()) {
_finished = true; _finished = true;
emit finished(_data); emit finished(_data);
} }
}, false);
}
} }

View file

@ -19,6 +19,8 @@
#include <QString> #include <QString>
#include <QUrl> #include <QUrl>
#include <mutex>
class BatchLoader : public QObject { class BatchLoader : public QObject {
Q_OBJECT Q_OBJECT
public: public:
@ -37,6 +39,7 @@ private:
bool _finished; bool _finished;
QSet<QUrl> _urls; QSet<QUrl> _urls;
QMap<QUrl, QString> _data; QMap<QUrl, QString> _data;
std::mutex _dataLock;
}; };
#endif // hifi_BatchLoader_h #endif // hifi_BatchLoader_h