From 2b603d981912422e5fa1ec6237fb7c83b5508d09 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Thu, 12 May 2022 12:43:27 +0200 Subject: [PATCH] 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 --- kerncompat.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/kerncompat.h b/kerncompat.h index 5089e102..64c2c0c3 100644 --- a/kerncompat.h +++ b/kerncompat.h @@ -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