diff --git a/adapter/common/kernel/liteos_a/softbus_adapter_thread.c b/adapter/common/kernel/liteos_a/softbus_adapter_thread.c index 2d5ebed8995713326642a3b2bc81f66976274505..2fa65d20ff4d7a2e103dc9bed72d61e12de5dd95 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 38102a1528392d81849368e3ed9db003eaf8c58b..ef8fc440d5a70cf640d786816dd7a5d381dc7c54 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 1b74ceb96c496c5c4e7e3836406b25d3f51cbbf2..f5bb70941a5c64d003c45d8a234182d753850dfb 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 8650ad03e04dd047d5d73491b2584a52e7174bda..6f9e12930d67136cd42a5615da5d052adb4d140b 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 6ec91c579a9da7a7f7532a9d96b1ff10f29b1301..e2ea670cf841607cdbbdac6504673703a578c2bf 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 1490e2d21fc6ce8f1912b498fbdbae5211412c52..108338be595701d7879825deea4e15b686584c47 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,38 @@ void PendingDeinit(int type) SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "PendigPackManagerDeinit init ok"); } +static inline bool TimeBefore(const SoftBusSysTime *inputTime) +{ + SoftBusSysTime now; + SoftBusGetTime(&now); + 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) { if (type < PENDING_TYPE_PROXY || type >= PENDING_TYPE_BUTT) { @@ -69,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->finded = false; - ListAdd(&pendingList->list, &item->node); pendingList->cnt++; SoftBusMutexUnlock(&pendingList->lock); @@ -96,24 +137,23 @@ 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); SoftBusMutexLock(&pendingList->lock); ListDelete(&item->node); - SoftBusMutexDestroy(&item->lock); - SoftBusCondDestroy(&item->cond); - SoftBusFree(item); pendingList->cnt--; SoftBusMutexUnlock(&pendingList->lock); - + ReleasePendingItem(item); return ret; } @@ -134,7 +174,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 +199,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 8b443435ffd3adaccba2ba3173410848781ceb94..4278fcb94c68a950c1b2a1fc8688ce6c6a361745 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)