From 57b672aab47f04b8339fc84900def9fc0aedc5d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C5=82a=C5=BCej=20Sowa?= Date: Mon, 16 Dec 2024 22:48:43 +0000 Subject: [PATCH] Return a timeout result code instead of setting timeout variable --- include/zenoh-pico/system/common/platform.h | 9 +++------ include/zenoh-pico/utils/result.h | 1 + src/system/arduino/esp32/system.c | 10 ++-------- src/system/arduino/opencr/system.c | 4 +--- src/system/common/platform.c | 4 ++-- src/system/emscripten/system.c | 10 ++-------- src/system/espidf/system.c | 10 ++-------- src/system/flipper/system.c | 4 +--- src/system/freertos_plus_tcp/system.c | 7 ++----- src/system/mbed/system.cpp | 7 ++----- src/system/rpi_pico/system.c | 7 ++----- src/system/unix/system.c | 10 ++-------- src/system/windows/system.c | 15 ++++----------- src/system/zephyr/system.c | 8 +------- 14 files changed, 27 insertions(+), 79 deletions(-) diff --git a/include/zenoh-pico/system/common/platform.h b/include/zenoh-pico/system/common/platform.h index 9a8fa6e7f..4b3937abd 100644 --- a/include/zenoh-pico/system/common/platform.h +++ b/include/zenoh-pico/system/common/platform.h @@ -274,7 +274,7 @@ z_result_t _z_condvar_drop(_z_condvar_t *cv); z_result_t _z_condvar_signal(_z_condvar_t *cv); z_result_t _z_condvar_signal_all(_z_condvar_t *cv); z_result_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m); -z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime, bool *timeout); +z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime); /** * Initializes a condition variable. @@ -329,19 +329,16 @@ z_result_t z_condvar_wait(z_loaned_condvar_t *cv, z_loaned_mutex_t *m); * * The calling thread is blocked until the condition variable is signaled or the timeout occurs. * The associated mutex must be locked by the calling thread, and it will be automatically unlocked while waiting. - * The `timeout` bool pointer should either be NULL or point to a valid memory, in which case the function will store a - * value indicating whether a timeout occurred. If NULL is passed in for `timeout`, it will not be set. * * Parameters: * cv: Pointer to a :c:type:`z_loaned_condvar_t` on which to wait. * m: Pointer to a :c:type:`z_loaned_mutex_t` that will be unlocked during the wait. * abstime: Absolute end time. - * timeout: Whether a timeout occurred. * * Returns: - * ``0`` if the wait is successful, a negative value otherwise. + * ``0`` if the wait is successful, ``Z_ETIMEDOUT`` if a timeout occurred, other negative value otherwise. */ -z_result_t z_condvar_wait_until(z_loaned_condvar_t *cv, z_loaned_mutex_t *m, const z_clock_t *abstime, bool *timeout); +z_result_t z_condvar_wait_until(z_loaned_condvar_t *cv, z_loaned_mutex_t *m, const z_clock_t *abstime); /*------------------ Sleep ------------------*/ /** diff --git a/include/zenoh-pico/utils/result.h b/include/zenoh-pico/utils/result.h index 6b1061d21..7ac80ee1f 100644 --- a/include/zenoh-pico/utils/result.h +++ b/include/zenoh-pico/utils/result.h @@ -86,6 +86,7 @@ typedef enum { _Z_ERR_OVERFLOW = -74, _Z_ERR_SESSION_CLOSED = -73, Z_EDESERIALIZE = -72, + Z_ETIMEDOUT = -71, _Z_ERR_GENERIC = -128 } _z_res_t; diff --git a/src/system/arduino/esp32/system.c b/src/system/arduino/esp32/system.c index af58f3383..e0d4a630d 100644 --- a/src/system/arduino/esp32/system.c +++ b/src/system/arduino/esp32/system.c @@ -128,19 +128,13 @@ z_result_t _z_condvar_signal(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_s z_result_t _z_condvar_signal_all(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_broadcast(cv)); } -z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime, bool *timeout) { +z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime) { int error = pthread_cond_timedwait(cv, m, abstime); if (error == ETIMEDOUT) { - if (timeout != NULL) { - *timeout = true; - } - return 0; + return Z_ETIMEDOUT; } - if (timeout != NULL) { - *timeout = false; - } _Z_CHECK_SYS_ERR(error); } #endif // Z_FEATURE_MULTI_THREAD == 1 diff --git a/src/system/arduino/opencr/system.c b/src/system/arduino/opencr/system.c index 759a9e7ae..ab49a1e5c 100644 --- a/src/system/arduino/opencr/system.c +++ b/src/system/arduino/opencr/system.c @@ -97,9 +97,7 @@ z_result_t _z_condvar_signal(_z_condvar_t *cv) { return -1; } 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; } -z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime, bool *timeout) { - return -1; -} +z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime) { return -1; } #endif // Z_FEATURE_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ diff --git a/src/system/common/platform.c b/src/system/common/platform.c index f4b2ad9e0..9d042a4f9 100644 --- a/src/system/common/platform.c +++ b/src/system/common/platform.c @@ -61,8 +61,8 @@ z_result_t z_condvar_drop(z_moved_condvar_t *cv) { return _z_condvar_drop(&cv->_ z_result_t z_condvar_signal(z_loaned_condvar_t *cv) { return _z_condvar_signal(cv); } z_result_t z_condvar_wait(z_loaned_condvar_t *cv, z_loaned_mutex_t *m) { return _z_condvar_wait(cv, m); } -z_result_t z_condvar_wait_until(z_loaned_condvar_t *cv, z_loaned_mutex_t *m, const z_clock_t *abstime, bool *timeout) { - return _z_condvar_wait_until(cv, m, abstime, timeout); +z_result_t z_condvar_wait_until(z_loaned_condvar_t *cv, z_loaned_mutex_t *m, const z_clock_t *abstime) { + return _z_condvar_wait_until(cv, m, abstime); } #endif // Z_FEATURE_MULTI_THREAD == 1 diff --git a/src/system/emscripten/system.c b/src/system/emscripten/system.c index aa1399f82..b224c0f6b 100644 --- a/src/system/emscripten/system.c +++ b/src/system/emscripten/system.c @@ -85,7 +85,7 @@ z_result_t _z_condvar_signal_all(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_co z_result_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_cond_wait(cv, m)); } -z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime, bool *timeout) { +z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime) { struct timespec ts; ts.tv_sec = (time_t)(*abstime / 1000); ts.tv_nsec = (long)((*abstime - (ts.tv_sec * 1000)) * 1000000); @@ -93,15 +93,9 @@ z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_ int error = pthread_cond_timedwait(cv, m, &ts); if (error == ETIMEDOUT) { - if (timeout != NULL) { - *timeout = true; - } - return 0; + return Z_ETIMEDOUT; } - if (timeout != NULL) { - *timeout = false; - } _Z_CHECK_SYS_ERR(error); } #endif // Z_FEATURE_MULTI_THREAD == 1 diff --git a/src/system/espidf/system.c b/src/system/espidf/system.c index 2c95afd63..648679a59 100644 --- a/src/system/espidf/system.c +++ b/src/system/espidf/system.c @@ -158,19 +158,13 @@ z_result_t _z_condvar_signal_all(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_co z_result_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_cond_wait(cv, m)); } -z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime, bool *timeout) { +z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime) { int error = pthread_cond_timedwait(cv, m, abstime); if (error == ETIMEDOUT) { - if (timeout != NULL) { - *timeout = true; - } - return 0; + return Z_ETIMEDOUT; } - if (timeout != NULL) { - *timeout = false; - } _Z_CHECK_SYS_ERR(error); } #endif // Z_FEATURE_MULTI_THREAD == 1 diff --git a/src/system/flipper/system.c b/src/system/flipper/system.c index 0498c334f..92c1091ca 100644 --- a/src/system/flipper/system.c +++ b/src/system/flipper/system.c @@ -153,9 +153,7 @@ 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; } -z_result_t _z_condvar_wait_until(_z_condvar_t* cv, _z_mutex_t* m, const z_clock_t* abstime, bool* timeout) { - return -1; -} +z_result_t _z_condvar_wait_until(_z_condvar_t* cv, _z_mutex_t* m, const z_clock_t* abstime) { return -1; } /*------------------ Sleep ------------------*/ z_result_t z_sleep_us(size_t time) { diff --git a/src/system/freertos_plus_tcp/system.c b/src/system/freertos_plus_tcp/system.c index e57873378..c1740843d 100644 --- a/src/system/freertos_plus_tcp/system.c +++ b/src/system/freertos_plus_tcp/system.c @@ -265,7 +265,7 @@ z_result_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return _Z_RES_OK; } -z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime, bool *timeout) { +z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime) { if (!cv || !m) { return _Z_ERR_GENERIC; } @@ -288,10 +288,7 @@ z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_ xSemaphoreTake(cv->mutex, portMAX_DELAY); cv->waiters--; xSemaphoreGive(cv->mutex); - } - - if (timeout != NULL) { - *timeout = timed_out; + return Z_ETIMEDOUT; } return _Z_RES_OK; diff --git a/src/system/mbed/system.cpp b/src/system/mbed/system.cpp index 425fb1e4d..5ab66242c 100644 --- a/src/system/mbed/system.cpp +++ b/src/system/mbed/system.cpp @@ -172,7 +172,7 @@ z_result_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return _Z_RES_OK; } -z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime, bool *timeout) { +z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime) { if (!cv || !m) { return _Z_ERR_GENERIC; } @@ -196,10 +196,7 @@ z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_ cond_var.mutex.lock(); cond_var.waiters--; cond_var.mutex.unlock(); - } - - if (timeout != NULL) { - *timeout = timed_out; + return Z_ETIMEDOUT; } return _Z_RES_OK; diff --git a/src/system/rpi_pico/system.c b/src/system/rpi_pico/system.c index 3f825fefe..26f3b011a 100644 --- a/src/system/rpi_pico/system.c +++ b/src/system/rpi_pico/system.c @@ -207,7 +207,7 @@ z_result_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return _z_mutex_lock(m); } -z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime, bool *timeout) { +z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime) { if (!cv || !m) { return _Z_ERR_GENERIC; } @@ -230,10 +230,7 @@ z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_ xSemaphoreTake(cv->mutex, portMAX_DELAY); cv->waiters--; xSemaphoreGive(cv->mutex); - } - - if (timeout != NULL) { - *timeout = timed_out; + return Z_ETIMEDOUT; } return _Z_RES_OK; diff --git a/src/system/unix/system.c b/src/system/unix/system.c index e8ebe6526..2fcf2b3c8 100644 --- a/src/system/unix/system.c +++ b/src/system/unix/system.c @@ -145,19 +145,13 @@ z_result_t _z_condvar_signal_all(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_co z_result_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_cond_wait(cv, m)); } -z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime, bool *timeout) { +z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime) { int error = pthread_cond_timedwait(cv, m, abstime); if (error == ETIMEDOUT) { - if (timeout != NULL) { - *timeout = true; - } - return 0; + return Z_ETIMEDOUT; } - if (timeout != NULL) { - *timeout = false; - } _Z_CHECK_SYS_ERR(error); } #endif // Z_FEATURE_MULTI_THREAD == 1 diff --git a/src/system/windows/system.c b/src/system/windows/system.c index f7a2b8761..78086a6c0 100644 --- a/src/system/windows/system.c +++ b/src/system/windows/system.c @@ -159,7 +159,7 @@ z_result_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return ret; } -z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime, bool *timeout) { +z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime) { z_clock_t now = z_clock_now(); LARGE_INTEGER frequency; QueryPerformanceFrequency(&frequency); // ticks per second @@ -172,22 +172,15 @@ z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_ double remaining = (double)(abstime->QuadPart - now.QuadPart) / frequency.QuadPart * 1000.0; DWORD block_duration = remaining > 0.0 ? (DWORD)remaining : 0; - z_result_t ret = _Z_RES_OK; if (SleepConditionVariableSRW(cv, m, block_duration, 0) == 0) { if (GetLastError() == ERROR_TIMEOUT) { - if (timeout != NULL) { - *timeout = true; - } - return _Z_RES_OK; + return Z_ETIMEDOUT; } else { - ret = _Z_ERR_GENERIC; + return _Z_ERR_GENERIC; } } - if (timeout != NULL) { - *timeout = false; - } - return ret; + return _Z_RES_OK; } #endif // Z_FEATURE_MULTI_THREAD == 1 diff --git a/src/system/zephyr/system.c b/src/system/zephyr/system.c index f808d908a..bcbb5b912 100644 --- a/src/system/zephyr/system.c +++ b/src/system/zephyr/system.c @@ -128,15 +128,9 @@ z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_ int error = pthread_cond_timedwait(cv, m, abstime); if (error == ETIMEDOUT) { - if (timeout != NULL) { - *timeout = true; - } - return 0; + return Z_ETIMEDOUT; } - if (timeout != NULL) { - *timeout = false; - } _Z_CHECK_SYS_ERR(error); } #endif // Z_FEATURE_MULTI_THREAD == 1