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

@@ -56,6 +56,7 @@ namespace ANSCENTER
}
bool ANSYOLOV10RTOD::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;
@@ -154,6 +155,7 @@ namespace ANSCENTER
}
bool ANSYOLOV10RTOD::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);
if (!result) return false;
@@ -260,6 +262,7 @@ namespace ANSCENTER
bool ANSYOLOV10RTOD::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;
@@ -362,27 +365,7 @@ namespace ANSCENTER
std::vector<Object> ANSYOLOV10RTOD::RunInference(const cv::Mat& inputImgBGR, const std::string& camera_id) {
// Validate under brief lock
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
if (!_modelLoadValid) {
this->_logger.LogFatal("ANSYOLOV10RTOD::RunInference",
"Cannot load the TensorRT model. Please check if it is exist", __FILE__, __LINE__);
return {};
}
if (!_licenseValid) {
this->_logger.LogFatal("ANSYOLOV10RTOD::RunInference",
"Runtime license is not valid or expired. Please contact ANSCENTER", __FILE__, __LINE__);
return {};
}
if (!_isInitialized) {
this->_logger.LogFatal("ANSYOLOV10RTOD::RunInference",
"Initialisation is not valid or expired. Please contact ANSCENTER", __FILE__, __LINE__);
return {};
}
if (inputImgBGR.empty() || inputImgBGR.cols < 10 || inputImgBGR.rows < 10) {
return {};
}
}
if (!PreInferenceCheck("ANSYOLOV10RTOD::RunInference")) return {};
try {
return DetectObjects(inputImgBGR, camera_id);
}
@@ -1000,25 +983,7 @@ namespace ANSCENTER
std::vector<std::vector<Object>> ANSYOLOV10RTOD::RunInferencesBatch(
const std::vector<cv::Mat>& inputs, const std::string& camera_id)
{
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
if (!_modelLoadValid) {
_logger.LogError("ANSYOLOV10RTOD::RunInferencesBatch",
"Model not loaded", __FILE__, __LINE__);
return {};
}
if (!_licenseValid) {
_logger.LogError("ANSYOLOV10RTOD::RunInferencesBatch",
"Invalid license", __FILE__, __LINE__);
return {};
}
if (!_isInitialized) {
_logger.LogError("ANSYOLOV10RTOD::RunInferencesBatch",
"Engine not initialized", __FILE__, __LINE__);
return {};
}
if (inputs.empty()) return {};
}
if (!PreInferenceCheck("ANSYOLOV10RTOD::RunInferencesBatch")) return {};
try {
return DetectObjectsBatch(inputs, camera_id);
}