1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
19436: cpp11-compat: thread::sleep_for in microseconds r=benpicco a=kfessel

### Contribution description

after reviewing #19369  i found that there is a conversion to nanoseconds just to convert it to microseconds some instrunctions later for ztimer64_usec to handle it this removes one of the conversions (convert once direct to microseconds)

### Testing procedure

run the cpp tests

### Issues/PRs references

#19369

Co-authored-by: Karl Fessel <karl.fessel@ovgu.de>
This commit is contained in:
bors[bot] 2023-04-17 18:20:28 +00:00 committed by GitHub
commit 2b6dc6471e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 14 deletions

View File

@ -172,9 +172,9 @@ inline thread_id get_id() noexcept { return thread_id{thread_getpid()}; }
inline void yield() noexcept { thread_yield(); } inline void yield() noexcept { thread_yield(); }
/** /**
* @brief Puts the current thread to sleep. * @brief Puts the current thread to sleep.
* @param[in] ns Duration to sleep in nanoseconds. * @param[in] us Duration to sleep in microseconds.
*/ */
void sleep_for(const std::chrono::nanoseconds& ns); void sleep_for(const std::chrono::microseconds& us);
/** /**
* @brief Puts the current thread to sleep. * @brief Puts the current thread to sleep.
* @param[in] sleep_duration The duration to sleep. * @param[in] sleep_duration The duration to sleep.
@ -182,18 +182,22 @@ void sleep_for(const std::chrono::nanoseconds& ns);
template <class Rep, class Period> template <class Rep, class Period>
void sleep_for(const std::chrono::duration<Rep, Period>& sleep_duration) { void sleep_for(const std::chrono::duration<Rep, Period>& sleep_duration) {
using namespace std::chrono; using namespace std::chrono;
if (sleep_duration > std::chrono::duration<Rep, Period>::zero()) { if (sleep_duration > sleep_duration.zero()) {
constexpr std::chrono::duration<long double> max = nanoseconds::max(); constexpr duration<long double> max = microseconds::max();
nanoseconds ns; microseconds us;
if (sleep_duration < max) { if (sleep_duration < max) {
ns = duration_cast<nanoseconds>(sleep_duration); us = duration_cast<microseconds>(sleep_duration);
if (ns < sleep_duration) { if (us.count() == 0) {
++ns; // wait at least 1
us = microseconds(1);
}
if (us < sleep_duration) {
++us;
} }
} else { } else {
ns = nanoseconds::max(); us = microseconds::max();
} }
sleep_for(ns); sleep_for(us);
} }
} }
/** /**

View File

@ -68,11 +68,10 @@ unsigned thread::hardware_concurrency() noexcept {
namespace this_thread { namespace this_thread {
void sleep_for(const chrono::nanoseconds& ns) { void sleep_for(const chrono::microseconds& us) {
using namespace chrono; using namespace chrono;
if (ns > nanoseconds::zero()) { if (us > microseconds::zero()) {
ztimer64_sleep(ZTIMER64_USEC, ztimer64_sleep(ZTIMER64_USEC, us.count());
static_cast<uint64_t>(duration_cast<microseconds>(ns).count()));
} }
} }