Skip to content

Commit 79baa40

Browse files
committed
rqspinlock: Introduce res_spin_trylock
A trylock variant for rqspinlock was missing owing to lack of users in the tree thus far, add one now as it would be needed in subsequent patches. Mark as __must_check and __always_inline. This essentially copies queued_spin_trylock, but doesn't depend on it as rqspinlock compiles down to a TAS when CONFIG_QUEUED_SPINLOCKS=n. Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
1 parent 4617b30 commit 79baa40

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

include/asm-generic/rqspinlock.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ static __always_inline int res_spin_lock(rqspinlock_t *lock)
175175
{
176176
int val = 0;
177177

178-
if (likely(atomic_try_cmpxchg_acquire(&lock->val, &val, _Q_LOCKED_VAL))) {
178+
if (likely(atomic_try_cmpxchg_acquire(&lock->val, &val, 1))) {
179179
grab_held_lock_entry(lock);
180180
return 0;
181181
}
@@ -217,12 +217,38 @@ static __always_inline void res_spin_unlock(rqspinlock_t *lock)
217217
this_cpu_dec(rqspinlock_held_locks.cnt);
218218
}
219219

220+
static __must_check __always_inline int res_spin_trylock(rqspinlock_t *lock)
221+
{
222+
int val = atomic_read(&lock->val);
223+
int ret;
224+
225+
if (unlikely(val))
226+
return 0;
227+
228+
ret = likely(atomic_try_cmpxchg_acquire(&lock->val, &val, _Q_LOCKED_VAL));
229+
if (ret)
230+
grab_held_lock_entry(lock);
231+
return ret;
232+
}
233+
220234
#ifdef CONFIG_QUEUED_SPINLOCKS
221235
#define raw_res_spin_lock_init(lock) ({ *(lock) = (rqspinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; })
222236
#else
223237
#define raw_res_spin_lock_init(lock) ({ *(lock) = (rqspinlock_t){0}; })
224238
#endif
225239

240+
#define raw_res_spin_trylock(lock) res_spin_trylock(lock)
241+
242+
#define raw_res_spin_trylock_irqsave(lock, flags) \
243+
({ \
244+
int __ret; \
245+
local_irq_save(flags); \
246+
__ret = raw_res_spin_trylock(lock); \
247+
if (!__ret) \
248+
local_irq_restore(flags); \
249+
__ret; \
250+
})
251+
226252
#define raw_res_spin_lock(lock) \
227253
({ \
228254
int __ret; \

0 commit comments

Comments
 (0)