Fix mutex lock issues

This commit is contained in:
2026-04-13 19:48:32 +10:00
parent 56a8f09adf
commit 844d7396b2
30 changed files with 445 additions and 575 deletions

View File

@@ -55,6 +55,7 @@ namespace ANSCENTER
}
bool ANSTENSORRTPOSE::LoadModel(const std::string& modelZipFilePath, const std::string& modelZipPassword) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
ModelLoadingGuard mlg(_modelLoading);
try {
bool result = ANSODBase::LoadModel(modelZipFilePath, modelZipPassword);
if (!result) return false;
@@ -144,6 +145,7 @@ namespace ANSCENTER
}
bool ANSTENSORRTPOSE::LoadModelFromFolder(std::string licenseKey, ModelConfig modelConfig, std::string modelName, std::string className, const std::string& modelFolder, std::string& labelMap) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
ModelLoadingGuard mlg(_modelLoading);
try
{
bool result = ANSODBase::LoadModelFromFolder(licenseKey, modelConfig, modelName, className, modelFolder, labelMap);
@@ -245,6 +247,7 @@ namespace ANSCENTER
}
bool ANSTENSORRTPOSE::Initialize(std::string licenseKey, ModelConfig modelConfig, const std::string& modelZipFilePath, const std::string& modelZipPassword, std::string& labelMap) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
ModelLoadingGuard mlg(_modelLoading);
try {
const bool engineAlreadyLoaded = _modelLoadValid && _isInitialized && m_trtEngine != nullptr;
_modelLoadValid = false;
@@ -347,35 +350,7 @@ namespace ANSCENTER
}
std::vector<Object> ANSTENSORRTPOSE::RunInference(const cv::Mat& inputImgBGR,const std::string& camera_id)
{
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
// Validation checks
if (!_modelLoadValid) {
_logger.LogError("ANSTENSORRTPOSE::RunInference",
"Cannot load the TensorRT model. Please check if it exists",
__FILE__, __LINE__);
return {};
}
if (!_licenseValid) {
_logger.LogError("ANSTENSORRTPOSE::RunInference",
"Runtime license is not valid or expired. Please contact ANSCENTER",
__FILE__, __LINE__);
return {};
}
if (!_isInitialized) {
_logger.LogError("ANSTENSORRTPOSE::RunInference",
"Model is not initialized",
__FILE__, __LINE__);
return {};
}
if (inputImgBGR.empty() || inputImgBGR.cols < 10 || inputImgBGR.rows < 10) {
return {};
}
}
if (!PreInferenceCheck("ANSTENSORRTPOSE::RunInference")) return {};
try {
return DetectObjects(inputImgBGR, camera_id);
}
@@ -983,25 +958,7 @@ namespace ANSCENTER
std::vector<std::vector<Object>> ANSTENSORRTPOSE::RunInferencesBatch(
const std::vector<cv::Mat>& inputs, const std::string& camera_id)
{
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
if (!_modelLoadValid) {
_logger.LogError("ANSTENSORRTPOSE::RunInferencesBatch",
"Model not loaded", __FILE__, __LINE__);
return {};
}
if (!_licenseValid) {
_logger.LogError("ANSTENSORRTPOSE::RunInferencesBatch",
"Invalid license", __FILE__, __LINE__);
return {};
}
if (!_isInitialized) {
_logger.LogError("ANSTENSORRTPOSE::RunInferencesBatch",
"Engine not initialized", __FILE__, __LINE__);
return {};
}
if (inputs.empty()) return {};
}
if (!PreInferenceCheck("ANSTENSORRTPOSE::RunInferencesBatch")) return {};
try {
return DetectObjectsBatch(inputs, camera_id);
}