blob: a9a4f4e1eff5b681b779519a490461801dd35820 [file] [log] [blame]
Linus Torvalds2f4f12e2013-09-02 11:58:20 -07001#include <linux/export.h>
2#include <linux/lockref.h>
3
4/**
5 * lockref_get - Increments reference count unconditionally
6 * @lockcnt: pointer to lockref structure
7 *
8 * This operation is only valid if you already hold a reference
9 * to the object, so you know the count cannot be zero.
10 */
11void lockref_get(struct lockref *lockref)
12{
13 spin_lock(&lockref->lock);
14 lockref->count++;
15 spin_unlock(&lockref->lock);
16}
17EXPORT_SYMBOL(lockref_get);
18
19/**
20 * lockref_get_not_zero - Increments count unless the count is 0
21 * @lockcnt: pointer to lockref structure
22 * Return: 1 if count updated successfully or 0 if count was zero
23 */
24int lockref_get_not_zero(struct lockref *lockref)
25{
26 int retval = 0;
27
28 spin_lock(&lockref->lock);
29 if (lockref->count) {
30 lockref->count++;
31 retval = 1;
32 }
33 spin_unlock(&lockref->lock);
34 return retval;
35}
36EXPORT_SYMBOL(lockref_get_not_zero);
37
38/**
39 * lockref_get_or_lock - Increments count unless the count is 0
40 * @lockcnt: pointer to lockref structure
41 * Return: 1 if count updated successfully or 0 if count was zero
42 * and we got the lock instead.
43 */
44int lockref_get_or_lock(struct lockref *lockref)
45{
46 spin_lock(&lockref->lock);
47 if (!lockref->count)
48 return 0;
49 lockref->count++;
50 spin_unlock(&lockref->lock);
51 return 1;
52}
53EXPORT_SYMBOL(lockref_get_or_lock);
54
55/**
56 * lockref_put_or_lock - decrements count unless count <= 1 before decrement
57 * @lockcnt: pointer to lockref structure
58 * Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
59 */
60int lockref_put_or_lock(struct lockref *lockref)
61{
62 spin_lock(&lockref->lock);
63 if (lockref->count <= 1)
64 return 0;
65 lockref->count--;
66 spin_unlock(&lockref->lock);
67 return 1;
68}
69EXPORT_SYMBOL(lockref_put_or_lock);