Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __LINUX_LOCKREF_H
0003 #define __LINUX_LOCKREF_H
0004 
0005 /*
0006  * Locked reference counts.
0007  *
0008  * These are different from just plain atomic refcounts in that they
0009  * are atomic with respect to the spinlock that goes with them.  In
0010  * particular, there can be implementations that don't actually get
0011  * the spinlock for the common decrement/increment operations, but they
0012  * still have to check that the operation is done semantically as if
0013  * the spinlock had been taken (using a cmpxchg operation that covers
0014  * both the lock and the count word, or using memory transactions, for
0015  * example).
0016  */
0017 
0018 #include <linux/spinlock.h>
0019 #include <generated/bounds.h>
0020 
0021 #define USE_CMPXCHG_LOCKREF \
0022     (IS_ENABLED(CONFIG_ARCH_USE_CMPXCHG_LOCKREF) && \
0023      IS_ENABLED(CONFIG_SMP) && SPINLOCK_SIZE <= 4)
0024 
0025 struct lockref {
0026     union {
0027 #if USE_CMPXCHG_LOCKREF
0028         aligned_u64 lock_count;
0029 #endif
0030         struct {
0031             spinlock_t lock;
0032             int count;
0033         };
0034     };
0035 };
0036 
0037 extern void lockref_get(struct lockref *);
0038 extern int lockref_put_return(struct lockref *);
0039 extern int lockref_get_not_zero(struct lockref *);
0040 extern int lockref_put_not_zero(struct lockref *);
0041 extern int lockref_put_or_lock(struct lockref *);
0042 
0043 extern void lockref_mark_dead(struct lockref *);
0044 extern int lockref_get_not_dead(struct lockref *);
0045 
0046 /* Must be called under spinlock for reliable results */
0047 static inline bool __lockref_is_dead(const struct lockref *l)
0048 {
0049     return ((int)l->count < 0);
0050 }
0051 
0052 #endif /* __LINUX_LOCKREF_H */