diff --git a/adapter/uhdf2/hdi/src/hdi_smq_syncer.cpp b/adapter/uhdf2/hdi/src/hdi_smq_syncer.cpp index 1c9a156bcf6c74a217b0b654ac36bfddd1041718..4717577d160496dd06b2bbbb075afdbb063ea293 100644 --- a/adapter/uhdf2/hdi/src/hdi_smq_syncer.cpp +++ b/adapter/uhdf2/hdi/src/hdi_smq_syncer.cpp @@ -98,6 +98,15 @@ int SharedMemQueueSyncer::Wake(uint32_t bitset) return HDF_SUCCESS; } +int SharedMemQueueSyncer::CheckSyncStatus(uint32_t bitset) +{ + uint32_t syncWordOld = std::atomic_load(syncAddr_); + if (syncWordOld & bitset) { + return HDF_SUCCESS; + } + return HDF_FAILURE; +} + void SharedMemQueueSyncer::TimeoutToRealtime(int64_t timeout, struct timespec &realtime) { constexpr int64_t nano = SEC_TO_NANOSEC; diff --git a/interfaces/inner_api/hdi/base/hdi_smq.h b/interfaces/inner_api/hdi/base/hdi_smq.h index 88789769478f09d8201831f0aae8a291f6fe331c..b0207428e4b01621fa35339f62a09f2d5d2f83d6 100644 --- a/interfaces/inner_api/hdi/base/hdi_smq.h +++ b/interfaces/inner_api/hdi/base/hdi_smq.h @@ -196,6 +196,16 @@ public: */ int ReadNonBlocking(T *data, size_t count); + /** + * @brief Reset all the status in SharedMemQueue. + * + * all the data in SMQ will be discard. + * + * @param. + * @return Returns 0 if the operation is successful; returns a non-zero value otherwise. + */ + int Reset(); + /** * @brief Obtains the number of elements that can be written to the SMQ. * @@ -477,6 +487,29 @@ int SharedMemQueue::Write(const T *data, size_t count, int64_t waitTimeNanoSe return ret; } +template +int SharedMemQueue::Reset() +{ + // do not invoke this function in multi-thread in same side + if (meta_->GetType() != SmqType::SYNCED_SMQ) { + HDF_LOGW("unsynecd smq reset status"); + readOffset_->exchange(writeOffset_->load(std::memory_order_acquire)); + return HDF_SUCCESS; + } + int ret = syncer_->CheckSyncStatus(SharedMemQueueSyncer::SYNC_WORD_READ); + if (ret == HDF_SUCCESS) { + HDF_LOGI("Currently reset by read side, reset and wake write side."); + readOffset_->exchange(writeOffset_->load(std::memory_order_acquire)); + syncer_->Wake(SharedMemQueueSyncer::SYNC_WORD_WRITE); + return HDF_SUCCESS; + } else { + HDF_LOGI("Currently reset by write side."); + readOffset_->exchange(writeOffset_->load(std::memory_order_acquire)); + } + + return HDF_SUCCESS; +} + template int SharedMemQueue::Read(T *data, size_t count, int64_t waitTimeNanoSec) { diff --git a/interfaces/inner_api/hdi/base/hdi_smq_syncer.h b/interfaces/inner_api/hdi/base/hdi_smq_syncer.h index 00d83e6fc29092209af6f1812e83475c3634be40..246d10164b6b3a213b548ced977f5dcc9fe562a5 100644 --- a/interfaces/inner_api/hdi/base/hdi_smq_syncer.h +++ b/interfaces/inner_api/hdi/base/hdi_smq_syncer.h @@ -79,6 +79,14 @@ public: */ int Wake(uint32_t bitset); + /** + * @brief Check the sync word. + * + * @param bitset Indicates the synchronization type. + * @return Returns 0 if the waiter is woken up. + */ + int CheckSyncStatus(uint32_t bitset); + private: /** * @brief Waits until a certain condition becomes true.