xref: /linux/include/linux/once_lite.h (revision 0bb666d5f5a2339a5692afb312c2161df9620503)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_ONCE_LITE_H
3 #define _LINUX_ONCE_LITE_H
4 
5 #include <linux/types.h>
6 
7 /* Call a function once. Similar to DO_ONCE(), but does not use jump label
8  * patching via static keys.
9  */
10 #define DO_ONCE_LITE(func, ...)						\
11 	DO_ONCE_LITE_IF(true, func, ##__VA_ARGS__)
12 
13 #define __ONCE_LITE()							\
14 	({								\
15 		static bool __section(".data..once") __already_done;	\
16 		bool __ret_once = false;				\
17 									\
18 		if (unlikely(!__already_done)) {			\
19 			__already_done = true;				\
20 			__ret_once = true;				\
21 		}							\
22 		unlikely(__ret_once);					\
23 	})
24 
25 #define __ONCE_LITE_IF(condition)					\
26 	(unlikely(condition) && __ONCE_LITE())
27 
28 #define DO_ONCE_LITE_IF(condition, func, ...)				\
29 	({								\
30 		bool __ret_do_once = !!(condition);			\
31 									\
32 		if (__ONCE_LITE_IF(__ret_do_once))			\
33 			func(__VA_ARGS__);				\
34 									\
35 		unlikely(__ret_do_once);				\
36 	})
37 
38 #endif /* _LINUX_ONCE_LITE_H */
39