1 #include <linux/export.h>
2 #include <linux/lockref.h>
4 #ifdef CONFIG_CMPXCHG_LOCKREF
7 * Note that the "cmpxchg()" reloads the "old" value for the
10 #define CMPXCHG_LOOP(CODE, SUCCESS) do { \
12 BUILD_BUG_ON(sizeof(old) != 8); \
13 old.lock_count = ACCESS_ONCE(lockref->lock_count); \
14 while (likely(arch_spin_value_unlocked(old.lock.rlock.raw_lock))) { \
15 struct lockref new = old, prev = old; \
17 old.lock_count = cmpxchg(&lockref->lock_count, \
18 old.lock_count, new.lock_count); \
19 if (likely(old.lock_count == prev.lock_count)) { \
27 #define CMPXCHG_LOOP(CODE, SUCCESS) do { } while (0)
32 * lockref_get - Increments reference count unconditionally
33 * @lockcnt: pointer to lockref structure
35 * This operation is only valid if you already hold a reference
36 * to the object, so you know the count cannot be zero.
38 void lockref_get(struct lockref *lockref)
46 spin_lock(&lockref->lock);
48 spin_unlock(&lockref->lock);
50 EXPORT_SYMBOL(lockref_get);
53 * lockref_get_not_zero - Increments count unless the count is 0
54 * @lockcnt: pointer to lockref structure
55 * Return: 1 if count updated successfully or 0 if count was zero
57 int lockref_get_not_zero(struct lockref *lockref)
69 spin_lock(&lockref->lock);
75 spin_unlock(&lockref->lock);
78 EXPORT_SYMBOL(lockref_get_not_zero);
81 * lockref_get_or_lock - Increments count unless the count is 0
82 * @lockcnt: pointer to lockref structure
83 * Return: 1 if count updated successfully or 0 if count was zero
84 * and we got the lock instead.
86 int lockref_get_or_lock(struct lockref *lockref)
96 spin_lock(&lockref->lock);
100 spin_unlock(&lockref->lock);
103 EXPORT_SYMBOL(lockref_get_or_lock);
106 * lockref_put_or_lock - decrements count unless count <= 1 before decrement
107 * @lockcnt: pointer to lockref structure
108 * Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
110 int lockref_put_or_lock(struct lockref *lockref)
120 spin_lock(&lockref->lock);
121 if (lockref->count <= 1)
124 spin_unlock(&lockref->lock);
127 EXPORT_SYMBOL(lockref_put_or_lock);