Skip to content

Commit

Permalink
Add timeout argument
Browse files Browse the repository at this point in the history
  • Loading branch information
bjsowa committed Nov 17, 2024
1 parent 24ba4a3 commit c86205f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
4 changes: 2 additions & 2 deletions include/zenoh-pico/system/platform_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,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_timedwait(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime);
z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime, bool *timeout);

/**
* Initializes a condition variable.
Expand Down Expand Up @@ -322,7 +322,7 @@ 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);

z_result_t z_condvar_timedwait(z_loaned_condvar_t *cv, z_loaned_mutex_t *m, const z_clock_t *abstime);
z_result_t z_condvar_wait_until(z_loaned_condvar_t *cv, z_loaned_mutex_t *m, const z_clock_t *abstime, bool *timeout);

/*------------------ Sleep ------------------*/
/**
Expand Down
4 changes: 2 additions & 2 deletions src/system/platform_common.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_timedwait(z_loaned_condvar_t *cv, z_loaned_mutex_t *m, const z_clock_t *abstime) {
return _z_condvar_timedwait(cv, m, abstime);
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);
}

#endif // Z_FEATURE_MULTI_THREAD == 1
17 changes: 15 additions & 2 deletions src/system/unix/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// ZettaScale Zenoh Team, <[email protected]>
//

#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -143,8 +144,20 @@ 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_timedwait(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime) {
_Z_CHECK_SYS_ERR(pthread_cond_timedwait(cv, m, abstime));
z_result_t _z_condvar_wait_until(_z_condvar_t *cv, _z_mutex_t *m, const z_clock_t *abstime, bool *timeout) {
int error = pthread_cond_timedwait(cv, m, abstime);

if (error == ETIMEDOUT) {
if (timeout != NULL) {
*timeout = true;
}
return 0;
}

if (timeout != NULL) {
*timeout = false;
}
_Z_CHECK_SYS_ERR(error);
}
#endif // Z_FEATURE_MULTI_THREAD == 1

Expand Down

0 comments on commit c86205f

Please sign in to comment.