Peter Zijlstra | f405df5 | 2016-11-14 18:06:19 +0100 | [diff] [blame] | 1 | #ifndef _LINUX_REFCOUNT_H |
| 2 | #define _LINUX_REFCOUNT_H |
| 3 | |
Peter Zijlstra | f405df5 | 2016-11-14 18:06:19 +0100 | [diff] [blame] | 4 | #include <linux/atomic.h> |
Peter Zijlstra | f405df5 | 2016-11-14 18:06:19 +0100 | [diff] [blame] | 5 | #include <linux/mutex.h> |
| 6 | #include <linux/spinlock.h> |
Elena Reshetova | 318b1de | 2017-02-23 15:09:34 +0200 | [diff] [blame] | 7 | #include <linux/kernel.h> |
Peter Zijlstra | f405df5 | 2016-11-14 18:06:19 +0100 | [diff] [blame] | 8 | |
Peter Zijlstra | f405df5 | 2016-11-14 18:06:19 +0100 | [diff] [blame] | 9 | typedef struct refcount_struct { |
| 10 | atomic_t refs; |
| 11 | } refcount_t; |
| 12 | |
| 13 | #define REFCOUNT_INIT(n) { .refs = ATOMIC_INIT(n), } |
| 14 | |
| 15 | static inline void refcount_set(refcount_t *r, unsigned int n) |
| 16 | { |
| 17 | atomic_set(&r->refs, n); |
| 18 | } |
| 19 | |
| 20 | static inline unsigned int refcount_read(const refcount_t *r) |
| 21 | { |
| 22 | return atomic_read(&r->refs); |
| 23 | } |
| 24 | |
Peter Zijlstra | 29dee3c | 2017-02-10 16:27:52 +0100 | [diff] [blame] | 25 | extern __must_check bool refcount_add_not_zero(unsigned int i, refcount_t *r); |
| 26 | extern void refcount_add(unsigned int i, refcount_t *r); |
Peter Zijlstra | f405df5 | 2016-11-14 18:06:19 +0100 | [diff] [blame] | 27 | |
Peter Zijlstra | 29dee3c | 2017-02-10 16:27:52 +0100 | [diff] [blame] | 28 | extern __must_check bool refcount_inc_not_zero(refcount_t *r); |
| 29 | extern void refcount_inc(refcount_t *r); |
Peter Zijlstra | f405df5 | 2016-11-14 18:06:19 +0100 | [diff] [blame] | 30 | |
Peter Zijlstra | 29dee3c | 2017-02-10 16:27:52 +0100 | [diff] [blame] | 31 | extern __must_check bool refcount_sub_and_test(unsigned int i, refcount_t *r); |
| 32 | extern void refcount_sub(unsigned int i, refcount_t *r); |
Peter Zijlstra | f405df5 | 2016-11-14 18:06:19 +0100 | [diff] [blame] | 33 | |
Peter Zijlstra | 29dee3c | 2017-02-10 16:27:52 +0100 | [diff] [blame] | 34 | extern __must_check bool refcount_dec_and_test(refcount_t *r); |
| 35 | extern void refcount_dec(refcount_t *r); |
Peter Zijlstra | f405df5 | 2016-11-14 18:06:19 +0100 | [diff] [blame] | 36 | |
Peter Zijlstra | 29dee3c | 2017-02-10 16:27:52 +0100 | [diff] [blame] | 37 | extern __must_check bool refcount_dec_if_one(refcount_t *r); |
| 38 | extern __must_check bool refcount_dec_not_one(refcount_t *r); |
| 39 | extern __must_check bool refcount_dec_and_mutex_lock(refcount_t *r, struct mutex *lock); |
| 40 | extern __must_check bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock); |
Peter Zijlstra | f405df5 | 2016-11-14 18:06:19 +0100 | [diff] [blame] | 41 | |
| 42 | #endif /* _LINUX_REFCOUNT_H */ |