Skip to content

Commit 543eee6

Browse files
committed
libtimeinstate: fix potential optimized variable
Per discussion at aosp/3077312/comment/38f6fa69_f6dee70c, the compiler may optimize out the busy loop. Change to a volatile variable and initialize to 1 so the *0 calc will not be optimized away. Test: treehugger Change-Id: Ic650b5c1b5ca9dcc1ed5817973f9ab831d944c99 Signed-off-by: Neill Kapron <nkapron@google.com>
1 parent b310760 commit 543eee6

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

libs/cputimeinstate/testtimeinstate.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static constexpr uint64_t NSEC_PER_SEC = 1000000000;
4141
static constexpr uint64_t NSEC_PER_YEAR = NSEC_PER_SEC * 60 * 60 * 24 * 365;
4242

4343
// Declare busy loop variable globally to prevent removal during optimization
44-
static long sum __attribute__((used)) = 0;
44+
static volatile long sum __attribute__((used)) = 1;
4545

4646
using std::vector;
4747

@@ -579,8 +579,8 @@ uint64_t timeNanos() {
579579

580580
// Keeps CPU busy with some number crunching
581581
void useCpu() {
582-
sum = 0;
583-
for (int i = 0; i < 100000; i++) {
582+
sum = 1;
583+
for (int i = 1; i < 100000; i++) {
584584
sum *= i;
585585
}
586586
}

0 commit comments

Comments
 (0)