Skip to content

Commit

Permalink
Return a timeout result code instead of setting timeout variable
Browse files Browse the repository at this point in the history
  • Loading branch information
bjsowa committed Dec 16, 2024
1 parent ff7dc08 commit 284e3ba
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 80 deletions.
9 changes: 3 additions & 6 deletions include/zenoh-pico/system/common/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 ------------------*/
/**
Expand Down
1 change: 1 addition & 0 deletions include/zenoh-pico/utils/result.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 2 additions & 8 deletions src/system/arduino/esp32/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions src/system/arduino/opencr/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ------------------*/
Expand Down
4 changes: 2 additions & 2 deletions src/system/common/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 2 additions & 8 deletions src/system/emscripten/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,17 @@ 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);

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
Expand Down
10 changes: 2 additions & 8 deletions src/system/espidf/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions src/system/flipper/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 2 additions & 5 deletions src/system/freertos_plus_tcp/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
7 changes: 2 additions & 5 deletions src/system/mbed/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
7 changes: 2 additions & 5 deletions src/system/rpi_pico/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
10 changes: 2 additions & 8 deletions src/system/unix/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 4 additions & 11 deletions src/system/windows/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
10 changes: 2 additions & 8 deletions src/system/zephyr/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,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
Expand Down

0 comments on commit 284e3ba

Please sign in to comment.