From 682bbe397215a99c4b9ad2faa75082b4abd93f6f Mon Sep 17 00:00:00 2001 From: 2657262686@qq.com <2657262686@qq.com> Date: Thu, 3 Nov 2022 11:27:08 +0800 Subject: [PATCH] 修正任务自然退出死锁的问题 --- FFNvDecoder/FFNvDecoder.cpp | 10 +++++++--- FFNvDecoder/FFNvDecoder.h | 2 ++ FFNvDecoder/FFNvDecoderManager.cpp | 17 +++++++++++++++++ FFNvDecoder/FFNvDecoderManager.h | 9 +++++++++ tsl_aiplatform/ai_platform/MultiSourceProcess.cpp | 44 ++++++++++++++++++++++++++++++++++++++++---- tsl_aiplatform/ai_platform/MultiSourceProcess.h | 2 +- 6 files changed, 76 insertions(+), 8 deletions(-) diff --git a/FFNvDecoder/FFNvDecoder.cpp b/FFNvDecoder/FFNvDecoder.cpp index 990a98e..2fa20b6 100644 --- a/FFNvDecoder/FFNvDecoder.cpp +++ b/FFNvDecoder/FFNvDecoder.cpp @@ -257,8 +257,6 @@ void FFNvDecoder::decode_thread() m_bRunning = false; - decode_finished_cbk(m_userPtr); - // long end_time = get_cur_time(); // cout << "解码用时:" << end_time - start_time << endl; @@ -268,6 +266,8 @@ void FFNvDecoder::decode_thread() pthread_join(m_post_decode_thread,0); } + decode_finished_cbk(m_userPtr); + decode_finished(); cout << "decode thread exited." << endl; @@ -291,7 +291,7 @@ void FFNvDecoder::decode_finished() void FFNvDecoder::post_decode_thread() { - while (m_bRunning) + while (m_bRunning || mFrameQueue.length() > 0) { AVFrame * gpuFrame = mFrameQueue.getHead(); if (gpuFrame == nullptr) @@ -369,4 +369,8 @@ void FFNvDecoder::resume() void FFNvDecoder::setDecKeyframe(bool bKeyframe) { m_dec_keyframe = bKeyframe; +} + +int FFNvDecoder::getCachedQueueLength(){ + return mFrameQueue.length(); } \ No newline at end of file diff --git a/FFNvDecoder/FFNvDecoder.h b/FFNvDecoder/FFNvDecoder.h index d306c61..d3f3edc 100644 --- a/FFNvDecoder/FFNvDecoder.h +++ b/FFNvDecoder/FFNvDecoder.h @@ -62,6 +62,8 @@ public: bool isSurport(FFDecConfig& cfg); + int getCachedQueueLength(); + public: AVPixelFormat getHwPixFmt(); diff --git a/FFNvDecoder/FFNvDecoderManager.cpp b/FFNvDecoder/FFNvDecoderManager.cpp index 9a6581b..9997da3 100644 --- a/FFNvDecoder/FFNvDecoderManager.cpp +++ b/FFNvDecoder/FFNvDecoderManager.cpp @@ -339,4 +339,21 @@ vector FFNvDecoderManager::getAllDecodeName(){ decode_names.push_back(it->first); } return decode_names; +} + +int FFNvDecoderManager::getCachedQueueLength(const string name){ + if (name.empty()){ + cout << "name 为空!"<< endl; + return -1; + } + + std::lock_guard l(m_mutex); + + auto dec = decoderMap.find(name); + if (dec != decoderMap.end()){ + return dec->second->getCachedQueueLength(); + } + + cout << "没有找到name为" << name << "的解码器!" << endl; + return -1; } \ No newline at end of file diff --git a/FFNvDecoder/FFNvDecoderManager.h b/FFNvDecoder/FFNvDecoderManager.h index afe91ed..628385a 100644 --- a/FFNvDecoder/FFNvDecoderManager.h +++ b/FFNvDecoder/FFNvDecoderManager.h @@ -213,6 +213,15 @@ public: **************************************************/ vector getAllDecodeName(); + /************************************************** + * 接口:getCachedQueueLength + * 功能:获取解码缓冲队列当前长度 + * 参数:const string name 解码器名称 + * 返回:int 解码缓冲队列当前长度 + * 备注: + **************************************************/ + int getCachedQueueLength(const string name); + private: FFNvDecoderManager(){} diff --git a/tsl_aiplatform/ai_platform/MultiSourceProcess.cpp b/tsl_aiplatform/ai_platform/MultiSourceProcess.cpp index 9eca760..f0f64f0 100644 --- a/tsl_aiplatform/ai_platform/MultiSourceProcess.cpp +++ b/tsl_aiplatform/ai_platform/MultiSourceProcess.cpp @@ -50,6 +50,7 @@ void decode_finished_cbk(const void * userPtr){ CMultiSourceProcess::CMultiSourceProcess(){ m_bfinish = false; + m_bProcessEnd = false; } CMultiSourceProcess::~CMultiSourceProcess(){ @@ -393,11 +394,43 @@ void CMultiSourceProcess::post_decode_thread(decode_cbk_userdata* userPtr, AVFra // 解码线程退出的最后回调的实际任务函数 void CMultiSourceProcess::decode_finished_thread(task_param t_param){ - // 任务结束,关闭跟踪 - if (!FinishTaskTracker(VPT_Handle_, t_param.task_id)) - LOG_ERROR("Finish VPT Tracker failed, task_id: {}", t_param.task_id); - finish_task(t_param.task_id,false); + FFNvDecoderManager* pDecManager = FFNvDecoderManager::getInstance(); + + while(true){ + + if(m_bfinish){ + break; + } + + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + + std::lock_guard l(m_QueueMtx); + + // 确保 m_RgbDataList 中没有任务数据 + for (auto iter = m_RgbDataList.begin(); iter!=m_RgbDataList.end(); ++ iter){ + GpuRgbMemory* gpuMem = *iter; + if(gpuMem->getId() == t_param.task_id){ + continue; + } + } + + // 确保 对应解码器中没有缓存的数据 + if (pDecManager->getCachedQueueLength(t_param.task_id) > 0){ + continue; + } + + // 确保对应任务没有正在执行算法的数据 + if (m_bProcessEnd){ + // 任务结束,关闭跟踪 + if (!FinishTaskTracker(VPT_Handle_, t_param.task_id)) + LOG_ERROR("Finish VPT Tracker failed, task_id: {}", t_param.task_id); + + finish_task(t_param.task_id,false); + + break; + } + } } bool CMultiSourceProcess::task_has_vpt_algor(const std::string &task_id){ @@ -446,6 +479,7 @@ void CMultiSourceProcess::algorthim_process_thread(){ int sum = 0; while(true){ + m_bProcessEnd = true; /* step1. 授权check */ if (licence_status_ <= -3) { LOG_FATAL("authority failed!"); @@ -458,6 +492,7 @@ void CMultiSourceProcess::algorthim_process_thread(){ /* step5. 凑齐的解码数据 拼batch */ m_QueueMtx.lock(); + m_bProcessEnd = false; vector vec_gpuMem; for (auto iter = m_RgbDataList.begin(); iter!=m_RgbDataList.end(); ++ iter){ @@ -529,6 +564,7 @@ void CMultiSourceProcess::algorthim_process_thread(){ } #endif + m_bProcessEnd = true; cout << "algorthim_process_thread end. " << endl; } diff --git a/tsl_aiplatform/ai_platform/MultiSourceProcess.h b/tsl_aiplatform/ai_platform/MultiSourceProcess.h index 9055c27..365fbc6 100644 --- a/tsl_aiplatform/ai_platform/MultiSourceProcess.h +++ b/tsl_aiplatform/ai_platform/MultiSourceProcess.h @@ -192,6 +192,7 @@ private: list m_RgbDataList; std::mutex m_QueueMtx; bool m_bfinish; + bool m_bProcessEnd; pthread_t m_authority_check_thread; @@ -222,7 +223,6 @@ private: face_det_ai_engine m_face_det_ai_engine; // 人脸检测 #endif -private: base_reprocessing_unit *m_save_snapshot_reprocessing{nullptr}; }; -- libgit2 0.21.4