once_lite.h 925 B

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