@@ -53,7 +53,8 @@ yield();
53
53
modm::fiber::id
54
54
get_id ();
55
55
56
- // / Yields the current fiber until `bool condition()` returns true.
56
+ // / Yields the current fiber while `bool condition()` returns true.
57
+ // / @warning If `bool condition()` is true on first call, no yield is performed!
57
58
template < class Function >
58
59
requires requires { std::is_invocable_r_v<bool , Function, void >; }
59
60
void
@@ -67,6 +68,8 @@ poll(Function &&condition)
67
68
* Yields the current fiber until `bool condition()` returns true or the time
68
69
* duration has elapsed.
69
70
*
71
+ * @warning If `bool condition()` is true on first call, no yield is performed!
72
+ *
70
73
* @returns `true` if the condition was met, `false` if the time duration has
71
74
* elapsed.
72
75
*
@@ -79,6 +82,8 @@ requires requires { std::is_invocable_r_v<bool, Function, void>; }
79
82
bool
80
83
poll_for (std::chrono::duration<Rep, Period> sleep_duration, Function &&condition)
81
84
{
85
+ if (std::forward<Function>(condition)()) return true ;
86
+
82
87
// Only choose the microsecond clock if necessary
83
88
using Clock = std::conditional_t <
84
89
std::is_convertible_v<std::chrono::duration<Rep, Period>,
@@ -87,8 +92,8 @@ poll_for(std::chrono::duration<Rep, Period> sleep_duration, Function &&condition
87
92
88
93
const auto start = Clock::now ();
89
94
do {
90
- if (std::forward<Function>(condition)()) return true ;
91
95
modm::this_fiber::yield ();
96
+ if (std::forward<Function>(condition)()) return true ;
92
97
}
93
98
while ((Clock::now () - start) <= sleep_duration);
94
99
return false ;
@@ -98,6 +103,8 @@ poll_for(std::chrono::duration<Rep, Period> sleep_duration, Function &&condition
98
103
* Yields the current fiber until `bool condition()` returns true or the sleep
99
104
* time has been reached.
100
105
*
106
+ * @warning If `bool condition()` is true on first call, no yield is performed!
107
+ *
101
108
* @returns `true` if the condition was met, `false` if the sleep time has
102
109
* elapsed.
103
110
*
@@ -110,11 +117,13 @@ requires requires { std::is_invocable_r_v<bool, Function, void>; }
110
117
bool
111
118
poll_until (std::chrono::time_point<Clock, Duration> sleep_time, Function &&condition)
112
119
{
120
+ if (std::forward<Function>(condition)()) return true ;
121
+
113
122
const auto start = Clock::now ();
114
123
const auto sleep_duration = sleep_time - start;
115
124
do {
116
- if (std::forward<Function>(condition)()) return true ;
117
125
modm::this_fiber::yield ();
126
+ if (std::forward<Function>(condition)()) return true ;
118
127
}
119
128
while ((Clock::now () - start) <= sleep_duration);
120
129
return false ;
0 commit comments