From ba1cf23af65bc63d796a5164a7393fbcafa66226 Mon Sep 17 00:00:00 2001 From: huanggang <313414059@qq.com> Date: Thu, 17 Jul 2025 02:32:24 +0000 Subject: [PATCH 1/3] update services/common/ring_buffer/ring_buffer.h. Signed-off-by: huanggang <313414059@qq.com> --- services/common/ring_buffer/ring_buffer.h | 1 + 1 file changed, 1 insertion(+) diff --git a/services/common/ring_buffer/ring_buffer.h b/services/common/ring_buffer/ring_buffer.h index 165d8ffe..7f37fa38 100644 --- a/services/common/ring_buffer/ring_buffer.h +++ b/services/common/ring_buffer/ring_buffer.h @@ -52,6 +52,7 @@ private: std::condition_variable notFull_; std::condition_variable notEmpty_; std::mutex notifyMtx_; + std::mutex arrayMtx_; }; } // namespace Updater #endif // __RING_BUFFER_H__ \ No newline at end of file -- Gitee From aee41f35a84127a51b0795d23a729c59d906cef5 Mon Sep 17 00:00:00 2001 From: huanggang <313414059@qq.com> Date: Thu, 17 Jul 2025 02:32:56 +0000 Subject: [PATCH 2/3] update services/common/ring_buffer/ring_buffer.cpp. Signed-off-by: huanggang <313414059@qq.com> --- services/common/ring_buffer/ring_buffer.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/services/common/ring_buffer/ring_buffer.cpp b/services/common/ring_buffer/ring_buffer.cpp index c7348b99..757002c2 100644 --- a/services/common/ring_buffer/ring_buffer.cpp +++ b/services/common/ring_buffer/ring_buffer.cpp @@ -96,6 +96,7 @@ bool RingBuffer::Push(uint8_t *buf, uint32_t len) } } + uint32_t index = writeIndex_ & (num_ - 1); if (memcpy_s(bufArray_[index], singleSize_, buf, len) != EOK) { LOG(ERROR) << "memcpy error, len:" << len; -- Gitee From 2cef34d692a1594fcb5318214f03f2ad93b182bf Mon Sep 17 00:00:00 2001 From: huanggang <313414059@qq.com> Date: Thu, 17 Jul 2025 02:42:12 +0000 Subject: [PATCH 3/3] update services/common/ring_buffer/ring_buffer.cpp. Signed-off-by: huanggang <313414059@qq.com> --- services/common/ring_buffer/ring_buffer.cpp | 31 ++++++++++++--------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/services/common/ring_buffer/ring_buffer.cpp b/services/common/ring_buffer/ring_buffer.cpp index 757002c2..9ec69a03 100644 --- a/services/common/ring_buffer/ring_buffer.cpp +++ b/services/common/ring_buffer/ring_buffer.cpp @@ -96,14 +96,16 @@ bool RingBuffer::Push(uint8_t *buf, uint32_t len) } } - - uint32_t index = writeIndex_ & (num_ - 1); - if (memcpy_s(bufArray_[index], singleSize_, buf, len) != EOK) { - LOG(ERROR) << "memcpy error, len:" << len; - return false; + { + std::unique_lock arrayLock(arrayMtx_); + uint32_t index = writeIndex_ & (num_ - 1); + if (memcpy_s(bufArray_[index], singleSize_, buf, len) != EOK) { + LOG(ERROR) << "memcpy error, len:" << len; + return false; + } + lenArray_[index] = len; + writeIndex_ = (writeIndex_ + 1) & (2 * num_ - 1); // 2: logic buffer size } - lenArray_[index] = len; - writeIndex_ = (writeIndex_ + 1) & (2 * num_ - 1); // 2: logic buffer size std::unique_lock popLock(notifyMtx_); notEmpty_.notify_all(); @@ -128,13 +130,16 @@ bool RingBuffer::Pop(uint8_t *buf, uint32_t maxLen, uint32_t &len) } } - uint32_t index = readIndex_ & (num_ - 1); - if (memcpy_s(buf, maxLen, bufArray_[index], lenArray_[index]) != EOK) { - LOG(ERROR) << "memcpy error, len:" << lenArray_[index]; - return false; + { + std::unique_lock arrayLock(arrayMtx_); + uint32_t index = readIndex_ & (num_ - 1); + if (memcpy_s(buf, maxLen, bufArray_[index], lenArray_[index]) != EOK) { + LOG(ERROR) << "memcpy error, len:" << lenArray_[index]; + return false; + } + len = lenArray_[index]; + readIndex_ = (readIndex_ + 1) & (2 * num_ - 1); // 2: logic buffer size } - len = lenArray_[index]; - readIndex_ = (readIndex_ + 1) & (2 * num_ - 1); // 2: logic buffer size std::unique_lock popLock(notifyMtx_); notFull_.notify_all(); -- Gitee