Linux 6.13-rc4
[linux.git] / fs / bcachefs / seqmutex.h
blobc4b3d8d3f4149cdb41c217404598e83f69cfafc5
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_SEQMUTEX_H
3 #define _BCACHEFS_SEQMUTEX_H
5 #include <linux/mutex.h>
7 struct seqmutex {
8 struct mutex lock;
9 u32 seq;
12 #define seqmutex_init(_lock) mutex_init(&(_lock)->lock)
14 static inline bool seqmutex_trylock(struct seqmutex *lock)
16 return mutex_trylock(&lock->lock);
19 static inline void seqmutex_lock(struct seqmutex *lock)
21 mutex_lock(&lock->lock);
22 lock->seq++;
25 static inline u32 seqmutex_unlock(struct seqmutex *lock)
27 u32 seq = lock->seq;
28 mutex_unlock(&lock->lock);
29 return seq;
32 static inline bool seqmutex_relock(struct seqmutex *lock, u32 seq)
34 if (lock->seq != seq || !mutex_trylock(&lock->lock))
35 return false;
37 if (lock->seq != seq) {
38 mutex_unlock(&lock->lock);
39 return false;
42 return true;
45 #endif /* _BCACHEFS_SEQMUTEX_H */