From 12ae5a83dac4056de7d9c3d597ec2de8439156ad Mon Sep 17 00:00:00 2001 From: EnderIce2 Date: Wed, 28 May 2025 02:05:16 +0000 Subject: [PATCH] feat(kernel): :art: add whileto macro for conditional looping with timeout --- Kernel/include/types.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Kernel/include/types.h b/Kernel/include/types.h index 232727fd..c157ca0e 100644 --- a/Kernel/include/types.h +++ b/Kernel/include/types.h @@ -245,7 +245,14 @@ public: T operator->() { return ptr; } T operator*() { return *ptr; } }; -#endif // __cplusplus +#endif + +/** Usage: int timed_out = 0; whileto(cond, n, timed_out) { ... } + Loops while (cond) is true, up to n times, then breaks. + Sets timed_out to 1 if the loop exited due to timeout, 0 otherwise. */ +#define whileto(cond, n, timed_out) \ + for (int __whileto_count = 0, __whileto_break = 1; __whileto_break && (cond) && __whileto_count < (n); ++__whileto_count, __whileto_break = 1) \ + for (; __whileto_break; __whileto_break = 0, timed_out = !((cond) && __whileto_count < (n))) #define INT8_MAX __INT8_MAX__ #define INT8_MIN (-INT8_MAX - 1)