Skip to content

Commit

Permalink
fix: use CLOCK_MONOTONIC for time measurements
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Oct 26, 2023
1 parent 1c26410 commit 8c6b9bb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/system/arduino/esp32/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,29 +135,29 @@ int z_sleep_s(size_t time) { return sleep(time); }
/*------------------ Instant ------------------*/
z_clock_t z_clock_now(void) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);
return now;
}

unsigned long z_clock_elapsed_us(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = (1000000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000);
return elapsed;
}

unsigned long z_clock_elapsed_ms(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = (1000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000000);
return elapsed;
}

unsigned long z_clock_elapsed_s(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = now.tv_sec - instant->tv_sec;
return elapsed;
Expand Down
8 changes: 4 additions & 4 deletions src/system/espidf/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,29 +136,29 @@ int z_sleep_s(size_t time) { return sleep(time); }
/*------------------ Instant ------------------*/
z_clock_t z_clock_now(void) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);
return now;
}

unsigned long z_clock_elapsed_us(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = (1000000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000);
return elapsed;
}

unsigned long z_clock_elapsed_ms(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = (1000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000000);
return elapsed;
}

unsigned long z_clock_elapsed_s(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = now.tv_sec - instant->tv_sec;
return elapsed;
Expand Down
8 changes: 4 additions & 4 deletions src/system/unix/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,29 +156,29 @@ int z_sleep_s(size_t time) { return sleep(time); }
/*------------------ Instant ------------------*/
z_clock_t z_clock_now(void) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);
return now;
}

unsigned long z_clock_elapsed_us(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = (1000000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000);
return elapsed;
}

unsigned long z_clock_elapsed_ms(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = (1000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000000);
return elapsed;
}

unsigned long z_clock_elapsed_s(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = now.tv_sec - instant->tv_sec;
return elapsed;
Expand Down
8 changes: 4 additions & 4 deletions src/system/zephyr/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,29 +150,29 @@ int z_sleep_s(size_t time) {
/*------------------ Instant ------------------*/
z_clock_t z_clock_now(void) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);
return now;
}

unsigned long z_clock_elapsed_us(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = (1000000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000);
return elapsed;
}

unsigned long z_clock_elapsed_ms(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = (1000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000000);
return elapsed;
}

unsigned long z_clock_elapsed_s(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = now.tv_sec - instant->tv_sec;
return elapsed;
Expand Down

0 comments on commit 8c6b9bb

Please sign in to comment.