blob: 0023fee4bbbcb4f38c4d3f6f9181e57261c4188a [file] [log] [blame]
Peter Zijlstraf405df52016-11-14 18:06:19 +01001#ifndef _LINUX_REFCOUNT_H
2#define _LINUX_REFCOUNT_H
3
Peter Zijlstraf405df52016-11-14 18:06:19 +01004#include <linux/atomic.h>
Peter Zijlstraf405df52016-11-14 18:06:19 +01005#include <linux/mutex.h>
6#include <linux/spinlock.h>
Elena Reshetova318b1de2017-02-23 15:09:34 +02007#include <linux/kernel.h>
Peter Zijlstraf405df52016-11-14 18:06:19 +01008
Peter Zijlstraf405df52016-11-14 18:06:19 +01009typedef struct refcount_struct {
10 atomic_t refs;
11} refcount_t;
12
13#define REFCOUNT_INIT(n) { .refs = ATOMIC_INIT(n), }
14
15static inline void refcount_set(refcount_t *r, unsigned int n)
16{
17 atomic_set(&r->refs, n);
18}
19
20static inline unsigned int refcount_read(const refcount_t *r)
21{
22 return atomic_read(&r->refs);
23}
24
Peter Zijlstra29dee3c2017-02-10 16:27:52 +010025extern __must_check bool refcount_add_not_zero(unsigned int i, refcount_t *r);
26extern void refcount_add(unsigned int i, refcount_t *r);
Peter Zijlstraf405df52016-11-14 18:06:19 +010027
Peter Zijlstra29dee3c2017-02-10 16:27:52 +010028extern __must_check bool refcount_inc_not_zero(refcount_t *r);
29extern void refcount_inc(refcount_t *r);
Peter Zijlstraf405df52016-11-14 18:06:19 +010030
Peter Zijlstra29dee3c2017-02-10 16:27:52 +010031extern __must_check bool refcount_sub_and_test(unsigned int i, refcount_t *r);
32extern void refcount_sub(unsigned int i, refcount_t *r);
Peter Zijlstraf405df52016-11-14 18:06:19 +010033
Peter Zijlstra29dee3c2017-02-10 16:27:52 +010034extern __must_check bool refcount_dec_and_test(refcount_t *r);
35extern void refcount_dec(refcount_t *r);
Peter Zijlstraf405df52016-11-14 18:06:19 +010036
Peter Zijlstra29dee3c2017-02-10 16:27:52 +010037extern __must_check bool refcount_dec_if_one(refcount_t *r);
38extern __must_check bool refcount_dec_not_one(refcount_t *r);
39extern __must_check bool refcount_dec_and_mutex_lock(refcount_t *r, struct mutex *lock);
40extern __must_check bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock);
Peter Zijlstraf405df52016-11-14 18:06:19 +010041
42#endif /* _LINUX_REFCOUNT_H */