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)) { \
28 #define CMPXCHG_LOOP(CODE, SUCCESS) do { } while (0)
33 * lockref_get - Increments reference count unconditionally
34 * @lockref: pointer to lockref structure
36 * This operation is only valid if you already hold a reference
37 * to the object, so you know the count cannot be zero.
39 void lockref_get(struct lockref *lockref)
47 spin_lock(&lockref->lock);
49 spin_unlock(&lockref->lock);
51 EXPORT_SYMBOL(lockref_get);
54 * lockref_get_not_zero - Increments count unless the count is 0
55 * @lockref: pointer to lockref structure
56 * Return: 1 if count updated successfully or 0 if count was zero
58 int lockref_get_not_zero(struct lockref *lockref)
70 spin_lock(&lockref->lock);
76 spin_unlock(&lockref->lock);
79 EXPORT_SYMBOL(lockref_get_not_zero);
82 * lockref_get_or_lock - Increments count unless the count is 0
83 * @lockref: pointer to lockref structure
84 * Return: 1 if count updated successfully or 0 if count was zero
85 * and we got the lock instead.
87 int lockref_get_or_lock(struct lockref *lockref)
97 spin_lock(&lockref->lock);
101 spin_unlock(&lockref->lock);
104 EXPORT_SYMBOL(lockref_get_or_lock);
107 * lockref_put_or_lock - decrements count unless count <= 1 before decrement
108 * @lockref: pointer to lockref structure
109 * Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
111 int lockref_put_or_lock(struct lockref *lockref)
121 spin_lock(&lockref->lock);
122 if (lockref->count <= 1)
125 spin_unlock(&lockref->lock);
128 EXPORT_SYMBOL(lockref_put_or_lock);
131 * lockref_mark_dead - mark lockref dead
132 * @lockref: pointer to lockref structure
134 void lockref_mark_dead(struct lockref *lockref)
136 assert_spin_locked(&lockref->lock);
137 lockref->count = -128;
141 * lockref_get_not_dead - Increments count unless the ref is dead
142 * @lockref: pointer to lockref structure
143 * Return: 1 if count updated successfully or 0 if lockref was dead
145 int lockref_get_not_dead(struct lockref *lockref)
151 if ((int)old.count < 0)
157 spin_lock(&lockref->lock);
159 if ((int) lockref->count >= 0) {
163 spin_unlock(&lockref->lock);
166 EXPORT_SYMBOL(lockref_get_not_dead);