1 #ifndef _BCACHEFS_SIX_H
2 #define _BCACHEFS_SIX_H
4 #include <linux/lockdep.h>
5 #include <linux/osq_lock.h>
6 #include <linux/sched.h>
7 #include <linux/types.h>
14 * read, intent, write (i.e. shared/intent/exclusive, hence the name)
16 * read and write work as with normal read/write locks - a lock can have
17 * multiple readers, but write excludes reads and other write locks.
19 * Intent does not block read, but it does block other intent locks. The idea is
20 * by taking an intent lock, you can then later upgrade to a write lock without
21 * dropping your read lock and without deadlocking - because no other thread has
22 * the intent lock and thus no other thread could be trying to take the write
26 union six_lock_state {
36 /* for waitlist_bitnr() */
41 unsigned read_lock:26;
42 unsigned intent_lock:3;
45 * seq works much like in seqlocks: it's incremented every time
46 * we lock and unlock for write.
48 * If it's odd write lock is held, even unlocked.
50 * Thus readers can unlock, and then lock again later iff it
51 * hasn't been modified in the meantime.
57 #define SIX_LOCK_MAX_RECURSE ((1 << 3) - 1)
66 union six_lock_state state;
67 struct task_struct *owner;
68 struct optimistic_spin_queue osq;
70 raw_spinlock_t wait_lock;
71 struct list_head wait_list[3];
72 #ifdef CONFIG_DEBUG_LOCK_ALLOC
73 struct lockdep_map dep_map;
77 static __always_inline void __six_lock_init(struct six_lock *lock,
79 struct lock_class_key *key)
81 atomic64_set(&lock->state.counter, 0);
82 raw_spin_lock_init(&lock->wait_lock);
83 INIT_LIST_HEAD(&lock->wait_list[SIX_LOCK_read]);
84 INIT_LIST_HEAD(&lock->wait_list[SIX_LOCK_intent]);
85 INIT_LIST_HEAD(&lock->wait_list[SIX_LOCK_write]);
86 #ifdef CONFIG_DEBUG_LOCK_ALLOC
87 debug_check_no_locks_freed((void *) lock, sizeof(*lock));
88 lockdep_init_map(&lock->dep_map, name, key, 0);
92 #define six_lock_init(lock) \
94 static struct lock_class_key __key; \
96 __six_lock_init((lock), #lock, &__key); \
99 bool six_trylock_type(struct six_lock *, enum six_lock_type);
100 bool six_relock_type(struct six_lock *, enum six_lock_type, unsigned);
101 void six_lock_type(struct six_lock *, enum six_lock_type);
102 void six_unlock_type(struct six_lock *, enum six_lock_type);
103 bool six_trylock_convert(struct six_lock *, enum six_lock_type,
105 void six_lock_increment(struct six_lock *, enum six_lock_type);
106 void six_lock_downgrade(struct six_lock *);
108 #define __SIX_VAL(field, _v) (((union six_lock_state) { .field = _v }).v)
110 #define __SIX_LOCK(type) \
111 static __always_inline bool six_trylock_##type(struct six_lock *lock) \
113 return six_trylock_type(lock, SIX_LOCK_##type); \
116 static __always_inline bool six_relock_##type(struct six_lock *lock, u32 seq)\
118 return six_relock_type(lock, SIX_LOCK_##type, seq); \
121 static __always_inline void six_lock_##type(struct six_lock *lock) \
123 six_lock_type(lock, SIX_LOCK_##type); \
126 static __always_inline void six_unlock_##type(struct six_lock *lock) \
128 six_unlock_type(lock, SIX_LOCK_##type); \
135 #endif /* _BCACHEFS_SIX_H */