5 #ifndef _CNE_SPINLOCK_H_
6 #define _CNE_SPINLOCK_H_
29 #include <cne_pause.h>
36 #define CNE_RTM_MAX_RETRIES (20)
37 #define CNE_RTM_MAX_RETRIES (20)
38 #define CNE_XABORT_LOCK_BUSY (0xff)
50 #define CNE_SPINLOCK_INITIALIZER \
67 #ifdef CNE_FORCE_INTRINSICS
82 !__atomic_compare_exchange_n(&sl->
locked, &exp, 1, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
83 while (__atomic_load_n(&sl->
locked, __ATOMIC_RELAXED))
100 __atomic_store_n(&sl->
locked, 0, __ATOMIC_RELEASE);
117 return __atomic_compare_exchange_n(&sl->
locked, &exp, 1, 0,
118 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
128 "xchg %[locked], %[lv]\n"
129 "test %[lv], %[lv]\n"
133 "cmpl $0, %[locked]\n"
137 : [locked]
"=m"(sl->
locked), [lv]
"=q"(lock_val)
146 asm volatile(
"xchg %[locked], %[ulv]\n"
147 : [locked]
"=m"(sl->
locked), [ulv]
"=q"(unlock_val)
148 :
"[ulv]"(unlock_val)
157 asm volatile(
"xchg %[locked], %[lockval]"
158 : [locked]
"=m"(sl->
locked), [lockval]
"=q"(lockval)
159 :
"[lockval]"(lockval)
177 return __atomic_load_n(&sl->
locked, __ATOMIC_ACQUIRE);
192 #define CNE_SPINLOCK_RECURSIVE_INITIALIZER \
194 CNE_SPINLOCK_INITIALIZER, -1, 0 \
222 if (slr->
user !=
id) {
223 cne_spinlock_lock(&slr->
sl);
237 if (--(slr->
count) == 0) {
239 cne_spinlock_unlock(&slr->
sl);
256 if (slr->
user !=
id) {
257 if (cne_spinlock_trylock(&slr->
sl) == 0)
266 cne_try_tm(
volatile int *lock)
273 retries = CNE_RTM_MAX_RETRIES;
275 while (
likely(retries--)) {
277 unsigned int status = cne_xbegin();
279 if (
likely(CNE_XBEGIN_STARTED == status)) {
281 cne_xabort(CNE_XABORT_LOCK_BUSY);
288 if ((status & CNE_XABORT_CONFLICT) ||
289 ((status & CNE_XABORT_EXPLICIT) && (CNE_XABORT_CODE(status) == CNE_XABORT_LOCK_BUSY))) {
295 int try_count = CNE_RTM_MAX_RETRIES - retries;
296 int pause_count = (
cne_rdtsc() & 0x7) | 1;
297 pause_count <<= try_count;
298 for (i = 0; i < pause_count; i++)
303 if ((status & CNE_XABORT_RETRY) == 0)
315 cne_spinlock_lock(sl);
324 return cne_spinlock_trylock(sl);
331 cne_spinlock_unlock(sl);
CNDP_API bool cne_cpu_rtm_is_supported(void)
static uint64_t cne_rdtsc(void)
static int cne_spinlock_is_locked(cne_spinlock_t *sl)
static void cne_spinlock_recursive_unlock(cne_spinlock_recursive_t *slr)
static void cne_spinlock_recursive_lock(cne_spinlock_recursive_t *slr)
static void cne_spinlock_recursive_init(cne_spinlock_recursive_t *slr)
static void cne_spinlock_init(cne_spinlock_t *sl)
static int cne_spinlock_recursive_trylock(cne_spinlock_recursive_t *slr)