From ce1efa969c6ee2df8b748b9f91c6db1b77c9354e Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Wed, 18 Sep 2024 18:31:47 +0200 Subject: [PATCH] Improve `z_sleep_*` error code handling --- include/zenoh-pico/system/platform-common.h | 6 +++--- src/system/arduino/esp32/system.c | 13 +++++++++---- src/system/arduino/opencr/system.c | 6 +++--- src/system/emscripten/system.c | 6 +++--- src/system/espidf/system.c | 6 +++--- src/system/flipper/system.c | 6 +++--- src/system/freertos_plus_tcp/system.c | 6 +++--- src/system/mbed/system.cpp | 6 +++--- src/system/unix/system.c | 16 +++++++++++----- src/system/windows/system.c | 6 +++--- src/system/zephyr/system.c | 6 +++--- 11 files changed, 47 insertions(+), 36 deletions(-) diff --git a/include/zenoh-pico/system/platform-common.h b/include/zenoh-pico/system/platform-common.h index 5c9f0c00b..652b673a1 100644 --- a/include/zenoh-pico/system/platform-common.h +++ b/include/zenoh-pico/system/platform-common.h @@ -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); diff --git a/src/system/arduino/esp32/system.c b/src/system/arduino/esp32/system.c index 982791983..d7f356348 100644 --- a/src/system/arduino/esp32/system.c +++ b/src/system/arduino/esp32/system.c @@ -18,7 +18,9 @@ #include #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(); } @@ -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) { diff --git a/src/system/arduino/opencr/system.c b/src/system/arduino/opencr/system.c index 2c97707f3..2a3521e35 100644 --- a/src/system/arduino/opencr/system.c +++ b/src/system/arduino/opencr/system.c @@ -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. diff --git a/src/system/emscripten/system.c b/src/system/emscripten/system.c index 1dd8fe213..1f1cfafef 100644 --- a/src/system/emscripten/system.c +++ b/src/system/emscripten/system.c @@ -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. diff --git a/src/system/espidf/system.c b/src/system/espidf/system.c index 065999795..6010c42a0 100644 --- a/src/system/espidf/system.c +++ b/src/system/espidf/system.c @@ -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) { _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. @@ -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) { diff --git a/src/system/flipper/system.c b/src/system/flipper/system.c index cbeac7de2..8fe2a7921 100644 --- a/src/system/flipper/system.c +++ b/src/system/flipper/system.c @@ -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. diff --git a/src/system/freertos_plus_tcp/system.c b/src/system/freertos_plus_tcp/system.c index a454e7204..0a7dc9115 100644 --- a/src/system/freertos_plus_tcp/system.c +++ b/src/system/freertos_plus_tcp/system.c @@ -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; } diff --git a/src/system/mbed/system.cpp b/src/system/mbed/system.cpp index 5429cfd12..211062d72 100644 --- a/src/system/mbed/system.cpp +++ b/src/system/mbed/system.cpp @@ -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; } diff --git a/src/system/unix/system.c b/src/system/unix/system.c index 380f1b948..e05205117 100644 --- a/src/system/unix/system.c +++ b/src/system/unix/system.c @@ -17,6 +17,9 @@ #include #include +#include "zenoh-pico/system/platform-common.h" +#include "zenoh-pico/utils/result.h" + #if defined(ZENOH_LINUX) #include #include @@ -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) { diff --git a/src/system/windows/system.c b/src/system/windows/system.c index 55cefb569..09c5bdff6 100644 --- a/src/system/windows/system.c +++ b/src/system/windows/system.c @@ -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)); @@ -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. diff --git a/src/system/zephyr/system.c b/src/system/zephyr/system.c index f592f725c..8ee6bc522 100644 --- a/src/system/zephyr/system.c +++ b/src/system/zephyr/system.c @@ -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. @@ -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); @@ -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));