From 26dd394e310ed408b1926f80218fb141d7c1c066 Mon Sep 17 00:00:00 2001 From: duxbbo Date: Wed, 8 Jun 2022 07:28:44 +0000 Subject: [PATCH 1/2] fix send message fack timeout; fix SoftBusCondWait bad input Signed-off-by: duxbbo --- .../kernel/liteos_a/softbus_adapter_thread.c | 3 +- core/common/message_handler/message_handler.c | 2 +- core/connection/br/src/br_connection_queue.c | 2 +- core/connection/br/src/br_pending_packet.c | 2 +- .../common/include/trans_pending_pkt.h | 9 ----- .../common/src/trans_pending_pkt.c | 36 +++++++++++++++---- .../proxy/src/client_trans_pending.c | 2 +- 7 files changed, 36 insertions(+), 20 deletions(-) diff --git a/adapter/common/kernel/liteos_a/softbus_adapter_thread.c b/adapter/common/kernel/liteos_a/softbus_adapter_thread.c index 2d5ebed899..2fa65d20ff 100644 --- a/adapter/common/kernel/liteos_a/softbus_adapter_thread.c +++ b/adapter/common/kernel/liteos_a/softbus_adapter_thread.c @@ -449,6 +449,7 @@ int32_t SoftBusCondBroadcast(SoftBusCond *cond) int32_t SoftBusCondWait(SoftBusCond *cond, SoftBusMutex *mutex, SoftBusSysTime *time) { +#define USECTONSEC 1000 if ((cond == NULL) || ((void *)(*cond) == NULL)) { HILOG_ERROR(SOFTBUS_HILOG_ID, "cond is null"); return SOFTBUS_INVALID_PARAM; @@ -469,7 +470,7 @@ int32_t SoftBusCondWait(SoftBusCond *cond, SoftBusMutex *mutex, SoftBusSysTime * } else { struct timespec tv; tv.tv_sec = time->sec; - tv.tv_nsec = time->usec; + tv.tv_nsec = time->usec * USECTONSEC; ret = pthread_cond_timedwait((pthread_cond_t *)*cond, (pthread_mutex_t *)*mutex, &tv); if (ret != 0 && ret != ETIMEDOUT) { HILOG_ERROR(SOFTBUS_HILOG_ID, "SoftBusCondTimedWait failed, ret[%{public}d]", ret); diff --git a/core/common/message_handler/message_handler.c b/core/common/message_handler/message_handler.c index 38102a1528..ef8fc440d5 100644 --- a/core/common/message_handler/message_handler.c +++ b/core/common/message_handler/message_handler.c @@ -149,7 +149,7 @@ static void *LoopTask(void *arg) } else { SoftBusSysTime tv; tv.sec = time / TIME_THOUSANDS_MULTIPLIER / TIME_THOUSANDS_MULTIPLIER; - tv.usec = time % (TIME_THOUSANDS_MULTIPLIER * TIME_THOUSANDS_MULTIPLIER) * TIME_THOUSANDS_MULTIPLIER; + tv.usec = time % (TIME_THOUSANDS_MULTIPLIER * TIME_THOUSANDS_MULTIPLIER); SoftBusCondWait(&context->cond, &context->lock, &tv); } diff --git a/core/connection/br/src/br_connection_queue.c b/core/connection/br/src/br_connection_queue.c index 1b74ceb96c..f5bb70941a 100644 --- a/core/connection/br/src/br_connection_queue.c +++ b/core/connection/br/src/br_connection_queue.c @@ -119,7 +119,7 @@ static int32_t BrSoftBusCondWait(SoftBusCond *cond, SoftBusMutex *mutex, uint32_ int64_t time = now.sec * USECTONSEC * USECTONSEC + now.usec + timeMillis * USECTONSEC; SoftBusSysTime tv; tv.sec = time / USECTONSEC / USECTONSEC; - tv.usec = time % (USECTONSEC * USECTONSEC) * USECTONSEC; + tv.usec = time % (USECTONSEC * USECTONSEC); return SoftBusCondWait(cond, mutex, &tv); } diff --git a/core/connection/br/src/br_pending_packet.c b/core/connection/br/src/br_pending_packet.c index 8650ad03e0..6f9e12930d 100644 --- a/core/connection/br/src/br_pending_packet.c +++ b/core/connection/br/src/br_pending_packet.c @@ -144,7 +144,7 @@ int32_t GetBrPendingPacket(uint32_t id, uint64_t seq, uint32_t waitMillis, void (void)SoftBusGetTime(&now); int64_t time = now.sec * USECTONSEC * USECTONSEC + now.usec + waitMillis * USECTONSEC; outtime.sec = time / USECTONSEC / USECTONSEC; - outtime.usec = time % (USECTONSEC * USECTONSEC) * USECTONSEC; + outtime.usec = time % (USECTONSEC * USECTONSEC); (void)SoftBusCondWait(&pending->cond, &pending->lock, &outtime); if (pending->finded) { *data = pending->data; diff --git a/core/transmission/common/include/trans_pending_pkt.h b/core/transmission/common/include/trans_pending_pkt.h index 6ec91c579a..e2ea670cf8 100644 --- a/core/transmission/common/include/trans_pending_pkt.h +++ b/core/transmission/common/include/trans_pending_pkt.h @@ -33,15 +33,6 @@ enum { PENDING_TYPE_BUTT, }; -typedef struct { - ListNode node; - SoftBusCond cond; - SoftBusMutex lock; - int32_t channelId; - int32_t seq; - bool finded; -} PendingPktInfo; - int32_t PendingInit(int type); void PendingDeinit(int type); int32_t ProcPendingPacket(int32_t channelId, int32_t seqNum, int type); diff --git a/core/transmission/common/src/trans_pending_pkt.c b/core/transmission/common/src/trans_pending_pkt.c index 1490e2d21f..37b66a40c9 100644 --- a/core/transmission/common/src/trans_pending_pkt.c +++ b/core/transmission/common/src/trans_pending_pkt.c @@ -25,7 +25,21 @@ #include "softbus_utils.h" #define TIME_OUT 2 -#define USECTONSEC 1000 + +typedef struct { + ListNode node; + SoftBusCond cond; + SoftBusMutex lock; + int32_t channelId; + int32_t seq; + uint8_t status; +} PendingPktInfo; + +enum PackageStatus { + PACKAGE_STATUS_PENDING = 0, + PACKAGE_STATUS_FINISHED, + PACKAGE_STATUS_CANCELED +}; static SoftBusList *g_pendingList[PENDING_TYPE_BUTT] = {NULL, NULL}; @@ -55,6 +69,13 @@ void PendingDeinit(int type) SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "PendigPackManagerDeinit init ok"); } +static inline bool TimeBefore(SoftBusSysTime *inputTime) +{ + SoftBusSysTime now; + SoftBusGetTime(&now); + return (now.sec < inputTime.sec || (now.sec == inputTime.sec && now.usec < inputTime.usec); +} + int32_t ProcPendingPacket(int32_t channelId, int32_t seqNum, int type) { if (type < PENDING_TYPE_PROXY || type >= PENDING_TYPE_BUTT) { @@ -86,7 +107,7 @@ int32_t ProcPendingPacket(int32_t channelId, int32_t seqNum, int type) SoftBusCondInit(&item->cond); item->channelId = channelId; item->seq = seqNum; - item->finded = false; + item->status = PACKAGE_STATUS_PENDING; ListAdd(&pendingList->list, &item->node); pendingList->cnt++; @@ -96,12 +117,14 @@ int32_t ProcPendingPacket(int32_t channelId, int32_t seqNum, int type) SoftBusSysTime now; SoftBusGetTime(&now); outtime.sec = now.sec + TIME_OUT; - outtime.usec = now.usec * USECTONSEC; + outtime.usec = now.usec; SoftBusMutexLock(&item->lock); - SoftBusCondWait(&item->cond, &item->lock, &outtime); + while (item->status == PACKAGE_STATUS_PENDING && TimeBefore(&outtime)) { + SoftBusCondWait(&item->cond, &item->lock, &outtime); + } int32_t ret = SOFTBUS_OK; - if (item->finded != true) { + if (item->status != PACKAGE_STATUS_FINISHED) { ret = SOFTBUS_TIMOUT; } SoftBusMutexUnlock(&item->lock); @@ -134,7 +157,7 @@ int32_t SetPendingPacket(int32_t channelId, int32_t seqNum, int type) SoftBusMutexLock(&pendingList->lock); LIST_FOR_EACH_ENTRY(item, &pendingList->list, PendingPktInfo, node) { if (item->seq == seqNum && item->channelId == channelId) { - item->finded = true; + item->status = PACKAGE_STATUS_FINISHED; SoftBusCondSignal(&item->cond); SoftBusMutexUnlock(&pendingList->lock); return SOFTBUS_OK; @@ -159,6 +182,7 @@ int32_t DelPendingPacket(int32_t channelId, int type) SoftBusMutexLock(&pendingList->lock); LIST_FOR_EACH_ENTRY(item, &pendingList->list, PendingPktInfo, node) { if (item->channelId == channelId) { + item->status = PACKAGE_STATUS_CANCELED; SoftBusCondSignal(&item->cond); SoftBusMutexUnlock(&pendingList->lock); return SOFTBUS_OK; diff --git a/sdk/transmission/trans_channel/proxy/src/client_trans_pending.c b/sdk/transmission/trans_channel/proxy/src/client_trans_pending.c index 8b443435ff..4278fcb94c 100644 --- a/sdk/transmission/trans_channel/proxy/src/client_trans_pending.c +++ b/sdk/transmission/trans_channel/proxy/src/client_trans_pending.c @@ -146,7 +146,7 @@ static void ComputeWaitPendTime(uint32_t waitMillis, SoftBusSysTime *outtime) (void)SoftBusGetTime(&now); int64_t time = now.sec * USECTONSEC * USECTONSEC + now.usec + waitMillis * USECTONSEC; outtime->sec = time / USECTONSEC / USECTONSEC; - outtime->usec = time % (USECTONSEC * USECTONSEC) * USECTONSEC; + outtime->usec = time % (USECTONSEC * USECTONSEC); } static int32_t TransPendWaitTime(const PendingPacket *pending, TransPendData *data, uint32_t waitMillis) -- Gitee From 7dd1aa150dbe4e13716497f706167ca978c9118c Mon Sep 17 00:00:00 2001 From: duxbbo Date: Wed, 8 Jun 2022 07:28:44 +0000 Subject: [PATCH 2/2] fix send message fack timeout; fix SoftBusCondWait bad input Signed-off-by: duxbbo Change-Id: Ic2a536f42047ef7a56e7f384361f620ef8d33442 --- .../common/src/trans_pending_pkt.c | 47 +++++++++++++------ 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/core/transmission/common/src/trans_pending_pkt.c b/core/transmission/common/src/trans_pending_pkt.c index 37b66a40c9..108338be59 100644 --- a/core/transmission/common/src/trans_pending_pkt.c +++ b/core/transmission/common/src/trans_pending_pkt.c @@ -69,11 +69,36 @@ void PendingDeinit(int type) SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "PendigPackManagerDeinit init ok"); } -static inline bool TimeBefore(SoftBusSysTime *inputTime) +static inline bool TimeBefore(const SoftBusSysTime *inputTime) { SoftBusSysTime now; SoftBusGetTime(&now); - return (now.sec < inputTime.sec || (now.sec == inputTime.sec && now.usec < inputTime.usec); + return (now.sec < inputTime->sec || (now.sec == inputTime->sec && now.usec < inputTime->usec)); +} + +static PendingPktInfo *CreatePendingItem(int32_t channelId, int32_t seqNum) +{ + PendingPktInfo *item = (PendingPktInfo *)SoftBusCalloc(sizeof(PendingPktInfo)); + if (item == NULL) { + return NULL; + } + + SoftBusMutexInit(&item->lock, NULL); + SoftBusCondInit(&item->cond); + item->channelId = channelId; + item->seq = seqNum; + item->status = PACKAGE_STATUS_PENDING; + return item; +} + +static void ReleasePendingItem(PendingPktInfo *item) +{ + if (item == NULL) { + return; + } + (void)SoftBusMutexDestroy(&item->lock); + (void)SoftBusCondDestroy(&item->cond); + SoftBusFree(item); } int32_t ProcPendingPacket(int32_t channelId, int32_t seqNum, int type) @@ -90,25 +115,20 @@ int32_t ProcPendingPacket(int32_t channelId, int32_t seqNum, int type) } SoftBusMutexLock(&pendingList->lock); - LIST_FOR_EACH_ENTRY(item, &pendingList->list, PendingPktInfo, node) { + LIST_FOR_EACH_ENTRY(item, &pendingList->list, PendingPktInfo, node) + { if (item->seq == seqNum && item->channelId == channelId) { SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "PendingPacket already Created"); SoftBusMutexUnlock(&pendingList->lock); return SOFTBUS_ERR; } } - item = (PendingPktInfo *)SoftBusCalloc(sizeof(PendingPktInfo)); + + item = CreatePendingItem(channelId, seqNum); if (item == NULL) { SoftBusMutexUnlock(&pendingList->lock); return SOFTBUS_MALLOC_ERR; } - - SoftBusMutexInit(&item->lock, NULL); - SoftBusCondInit(&item->cond); - item->channelId = channelId; - item->seq = seqNum; - item->status = PACKAGE_STATUS_PENDING; - ListAdd(&pendingList->list, &item->node); pendingList->cnt++; SoftBusMutexUnlock(&pendingList->lock); @@ -131,12 +151,9 @@ int32_t ProcPendingPacket(int32_t channelId, int32_t seqNum, int type) SoftBusMutexLock(&pendingList->lock); ListDelete(&item->node); - SoftBusMutexDestroy(&item->lock); - SoftBusCondDestroy(&item->cond); - SoftBusFree(item); pendingList->cnt--; SoftBusMutexUnlock(&pendingList->lock); - + ReleasePendingItem(item); return ret; } -- Gitee