Skip to content

Commit 6ab4886

Browse files
dcpleungioannisg
authored andcommitted
timing: fix timing_stop() ref counting
When there are more timing_stop() calls then timing_start(), the reference counter will go negative, resulting in the next timing_start() call not starting the timer. Without timer running, getting cycles elasped would not work. So fix the ref counting so it won't dip below zero. Fixes #30397 Signed-off-by: Daniel Leung <[email protected]>
1 parent c6253fb commit 6ab4886

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

subsys/timing/timing.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,22 @@ void timing_start(void)
4646

4747
void timing_stop(void)
4848
{
49-
if (atomic_dec(&started_ref) > 1) {
49+
atomic_t old_value, new_value;
50+
51+
/* Make sure this does decrement past zero. */
52+
do {
53+
old_value = atomic_get(&started_ref);
54+
if (old_value <= 0) {
55+
break;
56+
}
57+
58+
new_value = old_value - 1;
59+
} while (atomic_cas(&started_ref, old_value, new_value) == 0);
60+
61+
/*
62+
* new_value may be uninitialized, so use old_value here.
63+
*/
64+
if (old_value > 1) {
5065
return;
5166
}
5267

0 commit comments

Comments
 (0)