-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #853 from overte-org/fix/script_deadlocks
Move helper script engines to their own threads
- Loading branch information
Showing
10 changed files
with
169 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// HelperScriptEngine.h | ||
// libraries/script-engine/src/HelperScriptEngine.h | ||
// | ||
// Created by dr Karol Suprynowicz on 2024/04/28. | ||
// Copyright 2024 Overte e.V. | ||
// | ||
// Distributed under the Apache License, Version 2.0. | ||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | ||
// | ||
|
||
#include "HelperScriptEngine.h" | ||
|
||
HelperScriptEngine::HelperScriptEngine() { | ||
std::lock_guard<std::mutex> lock(_scriptEngineLock); | ||
_scriptEngine = newScriptEngine(); | ||
_scriptEngineThread.reset(new QThread()); | ||
_scriptEngine->setThread(_scriptEngineThread.get()); | ||
_scriptEngineThread->start(); | ||
} | ||
|
||
HelperScriptEngine::~HelperScriptEngine() { | ||
std::lock_guard<std::mutex> lock(_scriptEngineLock); | ||
if (_scriptEngine) { | ||
if (_scriptEngineThread) { | ||
_scriptEngineThread->quit(); | ||
_scriptEngineThread->wait(); | ||
} | ||
_scriptEngine.reset(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// HelperScriptEngine.h | ||
// libraries/script-engine/src/HelperScriptEngine.h | ||
// | ||
// Created by dr Karol Suprynowicz on 2024/04/28. | ||
// Copyright 2024 Overte e.V. | ||
// | ||
// Distributed under the Apache License, Version 2.0. | ||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | ||
// | ||
|
||
#ifndef overte_HelperScriptEngine_h | ||
#define overte_HelperScriptEngine_h | ||
|
||
#include <mutex> | ||
#include "QThread" | ||
|
||
#include "ScriptEngine.h" | ||
|
||
/** | ||
* @brief Provides a wrapper around script engine that does not have ScriptManager | ||
* | ||
* HelperScriptEngine is used for performing smaller tasks, like for example conversions between entity | ||
* properties and JSON data. | ||
* For thread safety all accesses to helper script engine need to be done either through HelperScriptEngine::run() | ||
* or HelperScriptEngine::runWithResult(). | ||
* | ||
*/ | ||
|
||
|
||
class HelperScriptEngine { | ||
public: | ||
HelperScriptEngine(); | ||
~HelperScriptEngine(); | ||
|
||
template <typename F> | ||
inline void run(F&& f) { | ||
std::lock_guard<std::mutex> guard(_scriptEngineLock); | ||
f(); | ||
} | ||
|
||
template <typename T, typename F> | ||
inline T runWithResult(F&& f) { | ||
T result; | ||
{ | ||
std::lock_guard<std::mutex> guard(_scriptEngineLock); | ||
result = f(); | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* @brief Returns pointer to the script engine | ||
* | ||
* This function should be used only inside HelperScriptEngine::run() or HelperScriptEngine::runWithResult() | ||
*/ | ||
ScriptEngine* get() { return _scriptEngine.get(); }; | ||
ScriptEnginePointer getShared() { return _scriptEngine; }; | ||
private: | ||
std::mutex _scriptEngineLock; | ||
ScriptEnginePointer _scriptEngine { nullptr }; | ||
std::shared_ptr<QThread> _scriptEngineThread { nullptr }; | ||
}; | ||
|
||
#endif //overte_HelperScriptEngine_h |