-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
ztimer: add ztimer_stopwatch convenience functions #19343
Conversation
4b5c786
to
7b4498e
Compare
i like how this interface is flexible enough to be written like typedef struct {
ztimer_clock_t *clock; /**< the clock to use */
ztimer zt; /** some timer to keep this working */
uint32_t start_time; /**< start of measurement */
} ztimer_stopwatch_t;
static void _keep_active_cb(void *timer_ptr){
if(!timer_ptr) return;
ztimer_stopwatch_t timer = timer_ptr;
ztimer_set(timer->clock, &timer->zt, 0xfff);
}
static inline void ztimer_stopwatch_init(ztimer_clock_t *clock, ztimer_stopwatch_t *timer)
{
timer->clock = clock;
timer->zt = { .callback=_keep_active_cb, .arg=timer };
}
static inline void ztimer_stopwatch_start(ztimer_stopwatch_t *timer)
{
timer->zt.arg=timer;
ztimer_set(timer->clock, &timer->zt, 0xfff);
timer->start_time = ztimer_now(timer->clock);
}
static inline void ztimer_stopwatch_start_timout(ztimer_stopwatch_t *timer, uint32_t timeout)
{
timer->zt.arg = (void *) 0;
ztimer_set(timer->clock, &timer->zt, timeout);
timer->start_time = ztimer_now(timer->clock);
}
static inline uint32_t ztimer_stopwatch_stop(ztimer_stopwatch_t *timer)
{
uint32_t now = ztimer_now(timer->clock);
ztimer_remove(timer->clock, &timer->zt)
return now - timer->start_time;
} |
Now I'm confused - isn't the purpose of |
You are right this is the purpose of ondemand and this PR is good. |
7b4498e
to
d91c0e8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice API; implementation working great; test provided 👍
bors merge |
Build succeeded: |
Contribution description
Ztimer is often used to measure time, but often without the needed
ztimer_aquire()
.Add some convenience functions that automatically do the right thing and that are easier to use than manually doing the time measuring code, so they will hopefully see more adoption over the error-prone open-coded solution.
Testing procedure
The benchmark function has been converted to use the new API.
tests/bench_runtime_coreapis
still works as before.Issues/PRs references