From b0581a172971de371ee3b131ab5638853d4e687b Mon Sep 17 00:00:00 2001 From: yiyiyi920 <568398422@qq.com> Date: Thu, 16 May 2024 21:51:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20cm=5Fspin=5Ftimed=5Flock?= =?UTF-8?q?=5Fby=5Fsid=20=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cm_concurrency/cm_spinlock.h | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/cm_concurrency/cm_spinlock.h b/src/cm_concurrency/cm_spinlock.h index 6045291..99250bd 100644 --- a/src/cm_concurrency/cm_spinlock.h +++ b/src/cm_concurrency/cm_spinlock.h @@ -332,6 +332,48 @@ static inline void cm_spin_lock_by_sid(uint32 sid, spinlock_t *lock, spin_statis } } +static inline bool32 cm_spin_timed_lock_by_sid(uint32 sid, spinlock_t *lock, spin_statis_t *stat, uint32 timeout_ticks) +{ + uint32 spin_times = 0, wait_ticks = 0; + uint32 sleep_times = 0; + + if (SECUREC_UNLIKELY(lock == NULL)) { + return CM_TRUE; + } + + for (;;) { +#if defined(__arm__) || defined(__aarch64__) + while (__atomic_load_n(lock, __ATOMIC_SEQ_CST) != 0) { +#else + while (*lock != 0) { +#endif + if (SECUREC_UNLIKELY(wait_ticks >= timeout_ticks)) { + return CM_FALSE; + } + SPIN_STAT_INC(stat, spins); + spin_times++; + if (SECUREC_UNLIKELY(spin_times == GS_SPIN_COUNT)) { + cm_spin_sleep_and_stat(stat); + spin_times = 0; + wait_ticks++; + } + } + + if (SECUREC_LIKELY(cm_spin_set(lock, sid) == 0)) { + break; + } + + SPIN_STAT_INC(stat, fails); + sleep_times++; +#ifndef WIN32 + for (uint32 i = 0; i < sleep_times; i++) { + fas_cpu_pause(); + } +#endif + } + return CM_TRUE; +} + #ifdef __cplusplus } #endif -- Gitee