lockref: add 'lockref_get_or_lock() helper
[linux-2.6/btrfs-unstable.git] / include / linux / lockref.h
blob0ea026092d1d5b34ab51596f4d324efe33db21e8
1 #ifndef __LINUX_LOCKREF_H
2 #define __LINUX_LOCKREF_H
4 /*
5 * Locked reference counts.
7 * These are different from just plain atomic refcounts in that they
8 * are atomic with respect to the spinlock that goes with them. In
9 * particular, there can be implementations that don't actually get
10 * the spinlock for the common decrement/increment operations, but they
11 * still have to check that the operation is done semantically as if
12 * the spinlock had been taken (using a cmpxchg operation that covers
13 * both the lock and the count word, or using memory transactions, for
14 * example).
17 #include <linux/spinlock.h>
19 struct lockref {
20 spinlock_t lock;
21 unsigned int count;
24 /**
25 * lockref_get - Increments reference count unconditionally
26 * @lockcnt: pointer to lockref structure
28 * This operation is only valid if you already hold a reference
29 * to the object, so you know the count cannot be zero.
31 static inline void lockref_get(struct lockref *lockref)
33 spin_lock(&lockref->lock);
34 lockref->count++;
35 spin_unlock(&lockref->lock);
38 /**
39 * lockref_get_not_zero - Increments count unless the count is 0
40 * @lockcnt: pointer to lockref structure
41 * Return: 1 if count updated successfully or 0 if count is 0
43 static inline int lockref_get_not_zero(struct lockref *lockref)
45 int retval = 0;
47 spin_lock(&lockref->lock);
48 if (lockref->count) {
49 lockref->count++;
50 retval = 1;
52 spin_unlock(&lockref->lock);
53 return retval;
56 /**
57 * lockref_get_or_lock - Increments count unless the count is 0
58 * @lockcnt: pointer to lockref structure
59 * Return: 1 if count updated successfully or 0 if count was zero
60 * and we got the lock instead.
62 static inline int lockref_get_or_lock(struct lockref *lockref)
64 spin_lock(&lockref->lock);
65 if (!lockref->count)
66 return 0;
67 lockref->count++;
68 spin_unlock(&lockref->lock);
69 return 1;
72 /**
73 * lockref_put_or_lock - decrements count unless count <= 1 before decrement
74 * @lockcnt: pointer to lockref structure
75 * Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
77 static inline int lockref_put_or_lock(struct lockref *lockref)
79 spin_lock(&lockref->lock);
80 if (lockref->count <= 1)
81 return 0;
82 lockref->count--;
83 spin_unlock(&lockref->lock);
84 return 1;
87 #endif /* __LINUX_LOCKREF_H */