Skip to content

Commit

Permalink
Improve z_sleep_* error code handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Sep 18, 2024
1 parent cc3e576 commit 0496cf0
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 36 deletions.
6 changes: 3 additions & 3 deletions include/zenoh-pico/system/platform-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ z_result_t z_condvar_signal(z_loaned_condvar_t *cv);
z_result_t z_condvar_wait(z_loaned_condvar_t *cv, z_loaned_mutex_t *m);

/*------------------ Sleep ------------------*/
int z_sleep_us(size_t time);
int z_sleep_ms(size_t time);
int z_sleep_s(size_t time);
z_result_t z_sleep_us(size_t time);
z_result_t z_sleep_ms(size_t time);
z_result_t z_sleep_s(size_t time);

/*------------------ Clock ------------------*/
z_clock_t z_clock_now(void);
Expand Down
13 changes: 9 additions & 4 deletions src/system/arduino/esp32/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
#include <sys/time.h>

#include "zenoh-pico/config.h"
#include "zenoh-pico/system/platform-common.h"
#include "zenoh-pico/system/platform.h"
#include "zenoh-pico/utils/result.h"

/*------------------ Random ------------------*/
uint8_t z_random_u8(void) { return z_random_u32(); }
Expand Down Expand Up @@ -118,22 +120,25 @@ z_result_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { _Z_CHECK_SYS_ERR(p
#endif // Z_FEATURE_MULTI_THREAD == 1

/*------------------ Sleep ------------------*/
int z_sleep_us(size_t time) { return usleep(time); }
z_result_t z_sleep_us(size_t time) { _Z_CHECK_SYS_ERR(usleep(time)); }

int z_sleep_ms(size_t time) {
z_result_t z_sleep_ms(size_t time) {
z_time_t start = z_time_now();

// Most sleep APIs promise to sleep at least whatever you asked them to.
// This may compound, so this approach may make sleeps longer than expected.
// This extra check tries to minimize the amount of extra time it might sleep.
while (z_time_elapsed_ms(&start) < time) {
z_sleep_us(1000);
z_result_t ret = z_sleep_us(1000);
if (ret < 0) {
return ret;
}
}

return 0;
}

int z_sleep_s(size_t time) { return sleep(time); }
z_result_t z_sleep_s(size_t time) { _Z_CHECK_SYS_ERR(sleep(time)); }

/*------------------ Instant ------------------*/
z_clock_t z_clock_now(void) {
Expand Down
6 changes: 3 additions & 3 deletions src/system/arduino/opencr/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,17 @@ z_result_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return -1; }
#endif // Z_FEATURE_MULTI_THREAD == 1

/*------------------ Sleep ------------------*/
int z_sleep_us(size_t time) {
z_result_t z_sleep_us(size_t time) {
delay_us(time);
return 0;
}

int z_sleep_ms(size_t time) {
z_result_t z_sleep_ms(size_t time) {
delay_ms(time);
return 0;
}

int z_sleep_s(size_t time) {
z_result_t z_sleep_s(size_t time) {
z_time_t start = z_time_now();

// Most sleep APIs promise to sleep at least whatever you asked them to.
Expand Down
6 changes: 3 additions & 3 deletions src/system/emscripten/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,17 @@ z_result_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { _Z_CHECK_SYS_ERR(p
#endif // Z_FEATURE_MULTI_THREAD == 1

/*------------------ Sleep ------------------*/
int z_sleep_us(size_t time) {
z_result_t z_sleep_us(size_t time) {
emscripten_sleep((time / 1000) + (time % 1000 == 0 ? 0 : 1));
return 0;
}

int z_sleep_ms(size_t time) {
z_result_t z_sleep_ms(size_t time) {
emscripten_sleep(time);
return 0;
}

int z_sleep_s(size_t time) {
z_result_t z_sleep_s(size_t time) {
z_time_t start = z_time_now();

// Most sleep APIs promise to sleep at least whatever you asked them to.
Expand Down
6 changes: 3 additions & 3 deletions src/system/espidf/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ z_result_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { _Z_CHECK_SYS_ERR(p
#endif // Z_FEATURE_MULTI_THREAD == 1

/*------------------ Sleep ------------------*/
int z_sleep_us(size_t time) { return usleep(time); }
z_result_t z_sleep_us(size_t time){return _Z_CHECK_SYS_ERR(usleep(time))}

int z_sleep_ms(size_t time) {
z_result_t z_sleep_ms(size_t time) {
z_time_t start = z_time_now();

// Most sleep APIs promise to sleep at least whatever you asked them to.
Expand All @@ -164,7 +164,7 @@ int z_sleep_ms(size_t time) {
return 0;
}

int z_sleep_s(size_t time) { return sleep(time); }
z_result_t z_sleep_s(size_t time) { _Z_CHECK_SYS_ERR(sleep(time)); }

/*------------------ Instant ------------------*/
z_clock_t z_clock_now(void) {
Expand Down
6 changes: 3 additions & 3 deletions src/system/flipper/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,17 @@ z_result_t _z_condvar_signal_all(_z_condvar_t* cv) { return -1; }
z_result_t _z_condvar_wait(_z_condvar_t* cv, _z_mutex_t* m) { return -1; }

/*------------------ Sleep ------------------*/
int z_sleep_us(size_t time) {
z_result_t z_sleep_us(size_t time) {
furi_delay_us(time);
return 0;
}

int z_sleep_ms(size_t time) {
z_result_t z_sleep_ms(size_t time) {
furi_delay_ms(time);
return 0;
}

int z_sleep_s(size_t time) {
z_result_t z_sleep_s(size_t time) {
z_time_t start = z_time_now();

// Most sleep APIs promise to sleep at least whatever you asked them to.
Expand Down
6 changes: 3 additions & 3 deletions src/system/freertos_plus_tcp/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,17 @@ z_result_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return -1; }
#endif // Z_MULTI_THREAD == 1

/*------------------ Sleep ------------------*/
int z_sleep_us(size_t time) {
z_result_t z_sleep_us(size_t time) {
vTaskDelay(pdMS_TO_TICKS(time / 1000));
return 0;
}

int z_sleep_ms(size_t time) {
z_result_t z_sleep_ms(size_t time) {
vTaskDelay(pdMS_TO_TICKS(time));
return 0;
}

int z_sleep_s(size_t time) {
z_result_t z_sleep_s(size_t time) {
vTaskDelay(pdMS_TO_TICKS(time * 1000));
return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions src/system/mbed/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,17 @@ z_result_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) {
#endif // Z_FEATURE_MULTI_THREAD == 1

/*------------------ Sleep ------------------*/
int z_sleep_us(size_t time) {
z_result_t z_sleep_us(size_t time) {
ThisThread::sleep_for(chrono::milliseconds(((time / 1000) + (time % 1000 == 0 ? 0 : 1))));
return 0;
}

int z_sleep_ms(size_t time) {
z_result_t z_sleep_ms(size_t time) {
ThisThread::sleep_for(chrono::milliseconds(time));
return 0;
}

int z_sleep_s(size_t time) {
z_result_t z_sleep_s(size_t time) {
ThisThread::sleep_for(chrono::seconds(time));
return 0;
}
Expand Down
16 changes: 11 additions & 5 deletions src/system/unix/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include <stdlib.h>
#include <time.h>

#include "zenoh-pico/system/platform-common.h"
#include "zenoh-pico/utils/result.h"

#if defined(ZENOH_LINUX)
#include <sys/random.h>
#include <sys/time.h>
Expand Down Expand Up @@ -139,22 +142,25 @@ z_result_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { _Z_CHECK_SYS_ERR(p
#endif // Z_FEATURE_MULTI_THREAD == 1

/*------------------ Sleep ------------------*/
int z_sleep_us(size_t time) { return usleep((unsigned int)time); }
z_result_t z_sleep_us(size_t time) { _Z_CHECK_SYS_ERR(usleep((unsigned int)time)); }

int z_sleep_ms(size_t time) {
z_result_t z_sleep_ms(size_t time) {
z_time_t start = z_time_now();

// Most sleep APIs promise to sleep at least whatever you asked them to.
// This may compound, so this approach may make sleeps longer than expected.
// This extra check tries to minimize the amount of extra time it might sleep.
while (z_time_elapsed_ms(&start) < time) {
z_sleep_us(1000);
z_result_t ret = z_sleep_us(1000);
if (ret != _Z_RES_OK) {
return ret;
}
}

return 0;
return _Z_RES_OK;
}

int z_sleep_s(size_t time) { return (int)sleep((unsigned int)time); }
z_result_t z_sleep_s(size_t time) { _Z_CHECK_SYS_ERR(sleep((unsigned int)time)); }

/*------------------ Instant ------------------*/
z_clock_t z_clock_now(void) {
Expand Down
6 changes: 3 additions & 3 deletions src/system/windows/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ z_result_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) {
#endif // Z_FEATURE_MULTI_THREAD == 1

/*------------------ Sleep ------------------*/
int z_sleep_us(size_t time) { return z_sleep_ms((time / 1000) + (time % 1000 == 0 ? 0 : 1)); }
z_result_t z_sleep_us(size_t time) { return z_sleep_ms((time / 1000) + (time % 1000 == 0 ? 0 : 1)); }

int z_sleep_ms(size_t time) {
z_result_t z_sleep_ms(size_t time) {
// Guarantees that size_t is split into DWORD segments for Sleep
uint8_t ratio = sizeof(size_t) / sizeof(DWORD);
DWORD ratio_time = (DWORD)((time / ratio) + (time % ratio == 0 ? 0 : 1));
Expand All @@ -167,7 +167,7 @@ int z_sleep_ms(size_t time) {
return 0;
}

int z_sleep_s(size_t time) {
z_result_t z_sleep_s(size_t time) {
z_time_t start = z_time_now();

// Most sleep APIs promise to sleep at least whatever you asked them to.
Expand Down
6 changes: 3 additions & 3 deletions src/system/zephyr/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ z_result_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { _Z_CHECK_SYS_ERR(p
#endif // Z_FEATURE_MULTI_THREAD == 1

/*------------------ Sleep ------------------*/
int z_sleep_us(size_t time) {
z_result_t z_sleep_us(size_t time) {
int32_t rem = time;
while (rem > 0) {
rem = k_usleep(rem); // This function is unlikely to work as expected without kernel tuning.
Expand All @@ -136,7 +136,7 @@ int z_sleep_us(size_t time) {
return 0;
}

int z_sleep_ms(size_t time) {
z_result_t z_sleep_ms(size_t time) {
int32_t rem = time;
while (rem > 0) {
rem = k_msleep(rem);
Expand All @@ -145,7 +145,7 @@ int z_sleep_ms(size_t time) {
return 0;
}

int z_sleep_s(size_t time) {
z_result_t z_sleep_s(size_t time) {
int32_t rem = time;
while (rem > 0) {
rem = k_sleep(K_SECONDS(rem));
Expand Down

0 comments on commit 0496cf0

Please sign in to comment.