Skip to content

Commit

Permalink
Merge pull request #13371 from haukepetersen/add_xtimer_settimeoutflag64
Browse files Browse the repository at this point in the history
xtimer: add xtimer_set_timeout_flag64()
  • Loading branch information
kaspar030 authored Feb 13, 2020
2 parents ef6bfbb + 0bcef73 commit fc40b54
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
12 changes: 12 additions & 0 deletions sys/include/xtimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ static inline bool xtimer_less64(xtimer_ticks64_t a, xtimer_ticks64_t b);
*/
int xtimer_mutex_lock_timeout(mutex_t *mutex, uint64_t us);

#if defined(MODULE_CORE_THREAD_FLAGS) || defined(DOXYGEN)
/**
* @brief Set timeout thread flag after @p timeout
*
Expand All @@ -418,6 +419,17 @@ int xtimer_mutex_lock_timeout(mutex_t *mutex, uint64_t us);
*/
void xtimer_set_timeout_flag(xtimer_t *t, uint32_t timeout);

/**
* @brief Set timeout thread flag after @p timeout
*
* See xtimer_set_timeout_flag() for more information.
*
* @param[in] t timer struct to use
* @param[in] timeout timeout in usec
*/
void xtimer_set_timeout_flag64(xtimer_t *t, uint64_t timeout);
#endif

#if defined(MODULE_CORE_MSG) || defined(DOXYGEN)
/**
* @brief Set a timer that sends a message
Expand Down
13 changes: 12 additions & 1 deletion sys/xtimer/xtimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,22 @@ static void _set_timeout_flag_callback(void* arg)
thread_flags_set(arg, THREAD_FLAG_TIMEOUT);
}

void xtimer_set_timeout_flag(xtimer_t *t, uint32_t timeout)
static void _set_timeout_flag_prepare(xtimer_t *t)
{
t->callback = _set_timeout_flag_callback;
t->arg = (thread_t *)sched_active_thread;
thread_flags_clear(THREAD_FLAG_TIMEOUT);
}

void xtimer_set_timeout_flag(xtimer_t *t, uint32_t timeout)
{
_set_timeout_flag_prepare(t);
xtimer_set(t, timeout);
}

void xtimer_set_timeout_flag64(xtimer_t *t, uint64_t timeout)
{
_set_timeout_flag_prepare(t);
xtimer_set64(t, timeout);
}
#endif
8 changes: 8 additions & 0 deletions tests/thread_flags/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ int main(void)
uint32_t diff = xtimer_now_usec() - before;
printf("main: timeout triggered. time passed: %"PRIu32"us\n", diff);

puts("main: setting 100ms timeout (using uint64)...");
uint64_t timeout64 = TIMEOUT;
before = xtimer_now_usec();
xtimer_set_timeout_flag64(&t, timeout64);
thread_flags_wait_any(THREAD_FLAG_TIMEOUT);
diff = xtimer_now_usec() - before;
printf("main: timeout triggered. time passed: %"PRIu32"us\n", diff);

if (diff < (TIMEOUT + THRESHOLD)) {
puts("SUCCESS");
return 0;
Expand Down
2 changes: 2 additions & 0 deletions tests/thread_flags/tests/01-run.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def testfunc(child):
child.expect_exact("thread(): received flags: 0x0008")
child.expect_exact("main: setting 100ms timeout...")
child.expect("main: timeout triggered. time passed: [0-9]{6}us")
child.expect_exact("main: setting 100ms timeout (using uint64)...")
child.expect("main: timeout triggered. time passed: [0-9]{6}us")
child.expect("SUCCESS")


Expand Down

0 comments on commit fc40b54

Please sign in to comment.