btrfs-progs: kernel-lib: add simplified READ_ONCE and WRITE_ONCE

For easier source synchronization with kernel, add the _ONCE wrappers,
but only the simplified version as we don't do any lock-less
algorithms or use the semantics in userspace.

Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
David Sterba 2022-05-12 12:43:27 +02:00
parent c1b24d742f
commit 2b603d9819
1 changed files with 32 additions and 0 deletions

View File

@ -531,4 +531,36 @@ struct __una_u64 { __le64 x; } __attribute__((__packed__));
#define noinline
#endif
/*
* Note: simplified versions of READ_ONCE and WRITE_ONCE for source
* compatibility only, not usable for lock-less implementation like in kernel.
*
* Changed:
* - __unqual_scalar_typeof: volatile cast to typeof()
* - compiletime_assert_rwonce_type: no word size compatibility checks
*/
/*
* Use __READ_ONCE() instead of READ_ONCE() if you do not require any
* atomicity. Note that this may result in tears!
*/
#ifndef __READ_ONCE
#define __READ_ONCE(x) (*(const volatile typeof(x) *)&(x))
#endif
#define READ_ONCE(x) \
({ \
__READ_ONCE(x); \
})
#define __WRITE_ONCE(x, val) \
do { \
*(volatile typeof(x) *)&(x) = (val); \
} while (0)
#define WRITE_ONCE(x, val) \
do { \
__WRITE_ONCE(x, val); \
} while (0)
#endif