Fix mutex lock issues
This commit is contained in:
@@ -16,6 +16,8 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include "ANSMOT.h"
|
||||
#include "onnxruntime_cxx_api.h"
|
||||
|
||||
@@ -729,6 +731,58 @@ namespace ANSCENTER
|
||||
void LoadClassesFromString();
|
||||
void LoadClassesFromFile();
|
||||
std::recursive_mutex _mutex;
|
||||
// Guard: true while Initialize/LoadModel is in progress on any thread.
|
||||
// Inference callers can check this to fail-fast instead of blocking.
|
||||
std::atomic<bool> _modelLoading{ false };
|
||||
|
||||
// Try to lock _mutex with a timeout. Returns a unique_lock that
|
||||
// evaluates to true on success. On timeout, logs a warning with
|
||||
// the caller name and returns an unlocked unique_lock.
|
||||
std::unique_lock<std::recursive_mutex> TryLockWithTimeout(
|
||||
const char* caller, unsigned int timeoutMs = 5000)
|
||||
{
|
||||
const auto deadline = std::chrono::steady_clock::now()
|
||||
+ std::chrono::milliseconds(timeoutMs);
|
||||
std::unique_lock<std::recursive_mutex> lk(_mutex, std::defer_lock);
|
||||
while (!lk.try_lock()) {
|
||||
if (std::chrono::steady_clock::now() >= deadline) {
|
||||
_logger.LogWarn(caller,
|
||||
"Mutex acquisition timed out after "
|
||||
+ std::to_string(timeoutMs) + " ms"
|
||||
+ (_modelLoading.load() ? " (model loading in progress)" : ""),
|
||||
__FILE__, __LINE__);
|
||||
return lk; // unlocked
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
return lk; // locked
|
||||
}
|
||||
|
||||
// Pre-inference gate: checks _modelLoading, validates state
|
||||
// (_modelLoadValid, _licenseValid, _isInitialized), and acquires
|
||||
// _mutex with a timeout. Returns true if inference may proceed.
|
||||
// On failure (loading in progress, timeout, or invalid state)
|
||||
// returns false and the caller should return {}.
|
||||
bool PreInferenceCheck(const char* caller) {
|
||||
if (_modelLoading.load()) return false;
|
||||
auto lk = TryLockWithTimeout(caller);
|
||||
if (!lk.owns_lock()) return false;
|
||||
if (!_licenseValid || !_isInitialized)
|
||||
return false;
|
||||
return true; // lock released here — caller proceeds unlocked
|
||||
}
|
||||
|
||||
// RAII helper: sets _modelLoading=true on construction, false on destruction.
|
||||
// Use in Initialize/LoadModel/LoadModelFromFolder to guarantee the flag
|
||||
// is always cleared, even on exceptions or early returns.
|
||||
struct ModelLoadingGuard {
|
||||
std::atomic<bool>& flag;
|
||||
explicit ModelLoadingGuard(std::atomic<bool>& f) : flag(f) { flag.store(true); }
|
||||
~ModelLoadingGuard() { flag.store(false); }
|
||||
ModelLoadingGuard(const ModelLoadingGuard&) = delete;
|
||||
ModelLoadingGuard& operator=(const ModelLoadingGuard&) = delete;
|
||||
};
|
||||
|
||||
MoveDetectsHandler _handler;
|
||||
size_t QUEUE_SIZE = 20;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user