-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support waiting on condition variable until a specific time point (#665)
* Add z_clock_advance functions * Add z_condvar_timedwait function * Use monotonic clock in unix port of condvar * Add timeout argument * Fix build on macos * Add missing docstrings * Use POSIX implementation in arduino, espidf and zephyr ports * Add missing errno includes * Fix formatting * Implement z_clock_advance for mbed port * Implement z_clock_advance for freertos_plus_tcp port * Implement z_clock_advance for windows port * Implement z_condvar_wait_until in windows port * Implement z_condvar_wait_until for freertos_plus_tcp and mbed ports * Implement z_condvar_wait_until for rpi_pico port * Add opencv and flipper clock advance implementations * Add emscripten implementation * Return a timeout result code instead of setting timeout variable
- Loading branch information
Showing
14 changed files
with
481 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
|
||
#include <errno.h> | ||
#include <esp_heap_caps.h> | ||
#include <esp_random.h> | ||
#include <stddef.h> | ||
|
@@ -142,7 +143,12 @@ z_result_t _z_mutex_try_lock(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_try | |
z_result_t _z_mutex_unlock(_z_mutex_t *m) { _Z_CHECK_SYS_ERR(pthread_mutex_unlock(m)); } | ||
|
||
/*------------------ Condvar ------------------*/ | ||
z_result_t _z_condvar_init(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_init(cv, NULL)); } | ||
z_result_t _z_condvar_init(_z_condvar_t *cv) { | ||
pthread_condattr_t attr; | ||
pthread_condattr_init(&attr); | ||
pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); | ||
_Z_CHECK_SYS_ERR(pthread_cond_init(cv, &attr)); | ||
} | ||
|
||
z_result_t _z_condvar_drop(_z_condvar_t *cv) { _Z_CHECK_SYS_ERR(pthread_cond_destroy(cv)); } | ||
|
||
|
@@ -151,6 +157,16 @@ 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(_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) { | ||
int error = pthread_cond_timedwait(cv, m, abstime); | ||
|
||
if (error == ETIMEDOUT) { | ||
return Z_ETIMEDOUT; | ||
} | ||
|
||
_Z_CHECK_SYS_ERR(error); | ||
} | ||
#endif // Z_FEATURE_MULTI_THREAD == 1 | ||
|
||
/*------------------ Sleep ------------------*/ | ||
|
@@ -202,6 +218,28 @@ unsigned long z_clock_elapsed_s(z_clock_t *instant) { | |
return elapsed; | ||
} | ||
|
||
void z_clock_advance_us(z_clock_t *clock, unsigned long duration) { | ||
clock->tv_sec += duration / 1000000; | ||
clock->tv_nsec += (duration % 1000000) * 1000; | ||
|
||
if (clock->tv_nsec >= 1000000000) { | ||
clock->tv_sec += 1; | ||
clock->tv_nsec -= 1000000000; | ||
} | ||
} | ||
|
||
void z_clock_advance_ms(z_clock_t *clock, unsigned long duration) { | ||
clock->tv_sec += duration / 1000; | ||
clock->tv_nsec += (duration % 1000) * 1000000; | ||
|
||
if (clock->tv_nsec >= 1000000000) { | ||
clock->tv_sec += 1; | ||
clock->tv_nsec -= 1000000000; | ||
} | ||
} | ||
|
||
void z_clock_advance_s(z_clock_t *clock, unsigned long duration) { clock->tv_sec += duration; } | ||
|
||
/*------------------ Time ------------------*/ | ||
z_time_t z_time_now(void) { | ||
z_time_t now; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.