Skip to content

Commit c31d646

Browse files
Nicolas Pitrecarlescufi
Nicolas Pitre
authored andcommitted
kernel: timeout: optimize z_timeout_expires()
This currently calls timeout_rem() then adds back the result of elapsed() to cancel out the subtraction of another elapsed() call within timeout_rem(). Better not to make any calls to elapsed() in that case, especially given that elapsed() may incur hardware access overhead through sys_clock_elapsed(). Let's move the elapsed() subtraction to the only user of timeout_rem() that needs it and remove its invocation from the z_timeout_expires() path entirely. Signed-off-by: Nicolas Pitre <[email protected]>
1 parent 7072e75 commit c31d646

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

kernel/timeout.c

+8-7
Original file line numberDiff line numberDiff line change
@@ -160,26 +160,24 @@ static k_ticks_t timeout_rem(const struct _timeout *timeout)
160160
{
161161
k_ticks_t ticks = 0;
162162

163-
if (z_is_inactive_timeout(timeout)) {
164-
return 0;
165-
}
166-
167163
for (struct _timeout *t = first(); t != NULL; t = next(t)) {
168164
ticks += t->dticks;
169165
if (timeout == t) {
170166
break;
171167
}
172168
}
173169

174-
return ticks - elapsed();
170+
return ticks;
175171
}
176172

177173
k_ticks_t z_timeout_remaining(const struct _timeout *timeout)
178174
{
179175
k_ticks_t ticks = 0;
180176

181177
K_SPINLOCK(&timeout_lock) {
182-
ticks = timeout_rem(timeout);
178+
if (!z_is_inactive_timeout(timeout)) {
179+
ticks = timeout_rem(timeout) - elapsed();
180+
}
183181
}
184182

185183
return ticks;
@@ -190,7 +188,10 @@ k_ticks_t z_timeout_expires(const struct _timeout *timeout)
190188
k_ticks_t ticks = 0;
191189

192190
K_SPINLOCK(&timeout_lock) {
193-
ticks = curr_tick + timeout_rem(timeout) + elapsed();
191+
ticks = curr_tick;
192+
if (!z_is_inactive_timeout(timeout)) {
193+
ticks += timeout_rem(timeout);
194+
}
194195
}
195196

196197
return ticks;

0 commit comments

Comments
 (0)