cyclic.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * A general-purpose cyclic execution infrastructure, to allow "small"
  4. * (run-time wise) functions to be executed at a specified frequency.
  5. * Things like LED blinking or watchdog triggering are examples for such
  6. * tasks.
  7. *
  8. * Copyright (C) 2022 Stefan Roese <sr@denx.de>
  9. */
  10. #ifndef __cyclic_h
  11. #define __cyclic_h
  12. #include <linux/list.h>
  13. #include <asm/types.h>
  14. /**
  15. * struct cyclic_info - Information about cyclic execution function
  16. *
  17. * @func: Function to call periodically
  18. * @ctx: Context pointer to get passed to this function
  19. * @name: Name of the cyclic function, e.g. shown in the commands
  20. * @delay_ns: Delay is ns after which this function shall get executed
  21. * @start_time_us: Start time in us, when this function started its execution
  22. * @cpu_time_us: Total CPU time of this function
  23. * @run_cnt: Counter of executions occurances
  24. * @next_call: Next time in us, when the function shall be executed again
  25. * @list: List node
  26. * @already_warned: Flag that we've warned about exceeding CPU time usage
  27. */
  28. struct cyclic_info {
  29. void (*func)(void *ctx);
  30. void *ctx;
  31. char *name;
  32. uint64_t delay_us;
  33. uint64_t start_time_us;
  34. uint64_t cpu_time_us;
  35. uint64_t run_cnt;
  36. uint64_t next_call;
  37. struct hlist_node list;
  38. bool already_warned;
  39. };
  40. /** Function type for cyclic functions */
  41. typedef void (*cyclic_func_t)(void *ctx);
  42. #if defined(CONFIG_CYCLIC)
  43. /**
  44. * cyclic_register - Register a new cyclic function
  45. *
  46. * @func: Function to call periodically
  47. * @delay_us: Delay is us after which this function shall get executed
  48. * @name: Cyclic function name/id
  49. * @ctx: Context to pass to the function
  50. * @return: pointer to cyclic_struct if OK, NULL on error
  51. */
  52. struct cyclic_info *cyclic_register(cyclic_func_t func, uint64_t delay_us,
  53. const char *name, void *ctx);
  54. /**
  55. * cyclic_unregister - Unregister a cyclic function
  56. *
  57. * @cyclic: Pointer to cyclic_struct of the function that shall be removed
  58. * @return: 0 if OK, -ve on error
  59. */
  60. int cyclic_unregister(struct cyclic_info *cyclic);
  61. /**
  62. * cyclic_unregister_all() - Clean up cyclic functions
  63. *
  64. * This removes all cyclic functions
  65. */
  66. int cyclic_unregister_all(void);
  67. /**
  68. * cyclic_get_list() - Get cyclic list pointer
  69. *
  70. * Return the cyclic list pointer
  71. *
  72. * @return: pointer to cyclic_list
  73. */
  74. struct hlist_head *cyclic_get_list(void);
  75. /**
  76. * cyclic_run() - Interate over all registered cyclic functions
  77. *
  78. * Interate over all registered cyclic functions and if the it's function
  79. * needs to be executed, then call into these registered functions.
  80. */
  81. void cyclic_run(void);
  82. /**
  83. * schedule() - Schedule all potentially waiting tasks
  84. *
  85. * Basically a wrapper for cyclic_run(), pontentially enhanced by some
  86. * other parts, that need to get handled periodically.
  87. */
  88. void schedule(void);
  89. #else
  90. static inline struct cyclic_info *cyclic_register(cyclic_func_t func,
  91. uint64_t delay_us,
  92. const char *name,
  93. void *ctx)
  94. {
  95. return NULL;
  96. }
  97. static inline int cyclic_unregister(struct cyclic_info *cyclic)
  98. {
  99. return 0;
  100. }
  101. static inline void cyclic_run(void)
  102. {
  103. }
  104. static inline void schedule(void)
  105. {
  106. }
  107. static inline int cyclic_unregister_all(void)
  108. {
  109. return 0;
  110. }
  111. #endif
  112. #endif