From 4f6448ed0543868850cc1c3f0c4e786deeac61fb Mon Sep 17 00:00:00 2001 From: MrKevinWeiss Date: Thu, 9 Mar 2023 13:59:50 +0100 Subject: [PATCH 1/4] sys/cpp11-compat: Use ztimer64_usec instead of xtimer --- sys/Makefile.dep | 2 +- sys/cpp11-compat/Kconfig | 3 +-- sys/cpp11-compat/condition_variable.cpp | 27 ++++++++++--------- sys/cpp11-compat/include/riot/chrono.hpp | 7 ++--- .../include/riot/condition_variable.hpp | 26 +++++++++--------- sys/cpp11-compat/thread.cpp | 7 +++-- 6 files changed, 35 insertions(+), 37 deletions(-) diff --git a/sys/Makefile.dep b/sys/Makefile.dep index 28a256232c..5cb8f39945 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -417,7 +417,7 @@ endif ifneq (,$(filter cpp11-compat,$(USEMODULE))) USEMODULE += cpp_new_delete - USEMODULE += xtimer + USEMODULE += ztimer64_usec USEMODULE += timex FEATURES_REQUIRED += cpp FEATURES_REQUIRED += libstdcpp diff --git a/sys/cpp11-compat/Kconfig b/sys/cpp11-compat/Kconfig index 235a3a3ae6..101521da52 100644 --- a/sys/cpp11-compat/Kconfig +++ b/sys/cpp11-compat/Kconfig @@ -15,6 +15,5 @@ config MODULE_CPP11-COMPAT select MODULE_CPP select MODULE_LIBSTDCPP select MODULE_CPP_NEW_DELETE - select MODULE_XTIMER + select ZTIMER64_USEC select MODULE_TIMEX - select MODULE_ZTIMER64_XTIMER_COMPAT if MODULE_ZTIMER_XTIMER_COMPAT diff --git a/sys/cpp11-compat/condition_variable.cpp b/sys/cpp11-compat/condition_variable.cpp index 278f79a67b..3ba39acf4f 100644 --- a/sys/cpp11-compat/condition_variable.cpp +++ b/sys/cpp11-compat/condition_variable.cpp @@ -24,8 +24,8 @@ #include "irq.h" #include "sched.h" #include "thread.h" -#include "timex.h" -#include "xtimer.h" +#include "time_units.h" +#include "ztimer64.h" #include "priority_queue.h" #include "riot/condition_variable.hpp" @@ -99,18 +99,19 @@ void condition_variable::wait(unique_lock& lock) noexcept { cv_status condition_variable::wait_until(unique_lock& lock, const time_point& timeout_time) { - xtimer_t timer; - // todo: use function to wait for absolute timepoint once available - timex_t before; - xtimer_now_timex(&before); - auto diff = timex_sub(timeout_time.native_handle(), before); - xtimer_set_wakeup(&timer, timex_uint64(diff), thread_getpid()); + ztimer64_t timer; + uint64_t total_timeout_time_us = timeout_time.microseconds(); + total_timeout_time_us += timeout_time.seconds() * US_PER_SEC; + + ztimer64_set_wakeup_at(ZTIMER64_USEC, &timer, total_timeout_time_us, + thread_getpid()); wait(lock); - timex_t after; - xtimer_now_timex(&after); - xtimer_remove(&timer); - auto cmp = timex_cmp(after, timeout_time.native_handle()); - return cmp < 1 ? cv_status::no_timeout : cv_status::timeout; + if (ztimer64_now(ZTIMER64_USEC) >= total_timeout_time_us) { + ztimer64_remove(ZTIMER64_USEC, &timer); + return cv_status::timeout; + } + ztimer64_remove(ZTIMER64_USEC, &timer); + return cv_status::no_timeout; } } // namespace riot diff --git a/sys/cpp11-compat/include/riot/chrono.hpp b/sys/cpp11-compat/include/riot/chrono.hpp index bc2d902bf6..03809c13b0 100644 --- a/sys/cpp11-compat/include/riot/chrono.hpp +++ b/sys/cpp11-compat/include/riot/chrono.hpp @@ -12,7 +12,7 @@ * * @file * @brief C++11 chrono drop in replacement that adds the function now based on - * xtimer/timex + * ztimer/timex * @see * std::thread, defined in header thread * @@ -29,7 +29,8 @@ #include #include "time.h" -#include "xtimer.h" +#include "timex.h" +#include "ztimer64.h" namespace riot { @@ -106,7 +107,7 @@ class time_point { */ inline time_point now() { timex_t tp; - xtimer_now_timex(&tp); + tp = timex_from_uint64(ztimer64_now(ZTIMER64_USEC)); return time_point(std::move(tp)); } diff --git a/sys/cpp11-compat/include/riot/condition_variable.hpp b/sys/cpp11-compat/include/riot/condition_variable.hpp index 82b447dd4f..11d386640e 100644 --- a/sys/cpp11-compat/include/riot/condition_variable.hpp +++ b/sys/cpp11-compat/include/riot/condition_variable.hpp @@ -25,7 +25,7 @@ #define RIOT_CONDITION_VARIABLE_HPP #include "sched.h" -#include "xtimer.h" +#include "ztimer64.h" #include "priority_queue.h" #include "riot/mutex.hpp" @@ -171,20 +171,18 @@ cv_status condition_variable::wait_for(unique_lock& lock, if (timeout_duration <= timeout_duration.zero()) { return cv_status::timeout; } - timex_t timeout, before, after; - auto s = duration_cast(timeout_duration); - timeout.seconds = s.count(); - timeout.microseconds - = (duration_cast(timeout_duration - s)).count(); - xtimer_now_timex(&before); - xtimer_t timer; - xtimer_set_wakeup(&timer, timex_uint64(timeout), thread_getpid()); + uint64_t timeout, before, after; + timeout = (duration_cast(timeout_duration)).count(); + before = ztimer64_now(ZTIMER64_USEC); + ztimer64_t timer; + ztimer64_set_wakeup(ZTIMER64_USEC, &timer, timeout, thread_getpid()); wait(lock); - xtimer_now_timex(&after); - xtimer_remove(&timer); - auto passed = timex_sub(after, before); - auto cmp = timex_cmp(passed, timeout); - return cmp < 1 ? cv_status::no_timeout : cv_status::timeout; + after = ztimer64_now(ZTIMER64_USEC); + ztimer64_remove(ZTIMER64_USEC, &timer); + if (after - before >= timeout) { + return cv_status::timeout; + } + return cv_status::no_timeout; } template diff --git a/sys/cpp11-compat/thread.cpp b/sys/cpp11-compat/thread.cpp index 2c6d06c2bf..f19a41101b 100644 --- a/sys/cpp11-compat/thread.cpp +++ b/sys/cpp11-compat/thread.cpp @@ -17,12 +17,10 @@ * * @} */ - -#include "xtimer.h" - #include #include +#include "ztimer64.h" #include "riot/thread.hpp" using namespace std; @@ -73,7 +71,8 @@ namespace this_thread { void sleep_for(const chrono::nanoseconds& ns) { using namespace chrono; if (ns > nanoseconds::zero()) { - xtimer_usleep64(static_cast(duration_cast(ns).count())); + ztimer64_sleep(ZTIMER64_USEC, + static_cast(duration_cast(ns).count())); } } From 8e7407e5f65b005454306c3bbf86a516646dae12 Mon Sep 17 00:00:00 2001 From: MrKevinWeiss Date: Thu, 9 Mar 2023 14:00:31 +0100 Subject: [PATCH 2/4] tests/cpp11_condition_variable: Use ztimer64_usec instead of xtimer --- tests/cpp11_condition_variable/Makefile | 3 +-- .../cpp11_condition_variable/app.config.test | 3 +-- tests/cpp11_condition_variable/main.cpp | 22 ++++++++++--------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/cpp11_condition_variable/Makefile b/tests/cpp11_condition_variable/Makefile index cf2c81d3a9..718ff39e40 100644 --- a/tests/cpp11_condition_variable/Makefile +++ b/tests/cpp11_condition_variable/Makefile @@ -7,7 +7,6 @@ ifneq (,$(filter nucleo-f303k8 nucleo-f334r8,$(BOARD))) endif USEMODULE += cpp11-compat -USEMODULE += xtimer -USEMODULE += timex +USEMODULE += ztimer64_usec include $(RIOTBASE)/Makefile.include diff --git a/tests/cpp11_condition_variable/app.config.test b/tests/cpp11_condition_variable/app.config.test index 709fc283e5..a770787974 100644 --- a/tests/cpp11_condition_variable/app.config.test +++ b/tests/cpp11_condition_variable/app.config.test @@ -1,3 +1,2 @@ CONFIG_MODULE_CPP11-COMPAT=y -CONFIG_MODULE_TIMEX=y -CONFIG_MODULE_XTIMER=y +CONFIG_ZTIMER64_USEC=y diff --git a/tests/cpp11_condition_variable/main.cpp b/tests/cpp11_condition_variable/main.cpp index d97b846233..42e4aa13d7 100644 --- a/tests/cpp11_condition_variable/main.cpp +++ b/tests/cpp11_condition_variable/main.cpp @@ -22,6 +22,8 @@ #include #include +#include "time_units.h" +#include "ztimer64.h" #include "riot/mutex.hpp" #include "riot/chrono.hpp" #include "riot/thread.hpp" @@ -95,13 +97,13 @@ int main() { constexpr unsigned timeout = 1; mutex m; condition_variable cv; - timex_t before, after; + uint64_t before, after; unique_lock lk(m); - xtimer_now_timex(&before); + before = ztimer64_now(ZTIMER64_USEC); cv.wait_for(lk, chrono::seconds(timeout)); - xtimer_now_timex(&after); - auto diff = timex_sub(after, before); - expect(diff.seconds >= timeout); + after = ztimer64_now(ZTIMER64_USEC); + auto diff = after - before; + expect(diff >= timeout * US_PER_SEC); } puts("Done\n"); @@ -111,14 +113,14 @@ int main() { constexpr unsigned timeout = 1; mutex m; condition_variable cv; - timex_t before, after; + uint64_t before, after; unique_lock lk(m); - xtimer_now_timex(&before); + before = ztimer64_now(ZTIMER64_USEC); auto time = riot::now() += chrono::seconds(timeout); cv.wait_until(lk, time); - xtimer_now_timex(&after); - auto diff = timex_sub(after, before); - expect(diff.seconds >= timeout); + after = ztimer64_now(ZTIMER64_USEC); + auto diff = after - before; + expect(diff >= timeout * US_PER_SEC); } puts("Done\n"); From 4795965ac2711991381b0a3ee220904bc42a1b96 Mon Sep 17 00:00:00 2001 From: MrKevinWeiss Date: Thu, 9 Mar 2023 14:00:53 +0100 Subject: [PATCH 3/4] tests/cpp11_thread: Use ztimer64_usec instead of xtimer --- tests/cpp11_thread/Makefile | 2 +- tests/cpp11_thread/app.config.test | 2 +- tests/cpp11_thread/main.cpp | 10 ++++++---- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/cpp11_thread/Makefile b/tests/cpp11_thread/Makefile index 81920c7871..08b3f2f6ec 100644 --- a/tests/cpp11_thread/Makefile +++ b/tests/cpp11_thread/Makefile @@ -1,7 +1,7 @@ include ../Makefile.tests_common USEMODULE += cpp11-compat -USEMODULE += xtimer +USEMODULE += ztimer64_usec USEMODULE += timex include $(RIOTBASE)/Makefile.include diff --git a/tests/cpp11_thread/app.config.test b/tests/cpp11_thread/app.config.test index 709fc283e5..508c15747b 100644 --- a/tests/cpp11_thread/app.config.test +++ b/tests/cpp11_thread/app.config.test @@ -1,3 +1,3 @@ CONFIG_MODULE_CPP11-COMPAT=y CONFIG_MODULE_TIMEX=y -CONFIG_MODULE_XTIMER=y +CONFIG_ZTIMER64_USEC=y diff --git a/tests/cpp11_thread/main.cpp b/tests/cpp11_thread/main.cpp index fc96c9e194..80446d217e 100644 --- a/tests/cpp11_thread/main.cpp +++ b/tests/cpp11_thread/main.cpp @@ -22,6 +22,8 @@ #include #include +#include "timex.h" +#include "ztimer64.h" #include "riot/mutex.hpp" #include "riot/chrono.hpp" #include "riot/thread.hpp" @@ -123,9 +125,9 @@ int main() { puts("Testing sleep_for ..."); { timex_t before, after; - xtimer_now_timex(&before); + before = timex_from_uint64(ztimer64_now(ZTIMER64_USEC)); this_thread::sleep_for(chrono::seconds(1)); - xtimer_now_timex(&after); + after = timex_from_uint64(ztimer64_now(ZTIMER64_USEC)); auto diff = timex_sub(after, before); expect(diff.seconds >= 1); } @@ -136,9 +138,9 @@ int main() { puts("Testing sleep_until ..."); { timex_t before, after; - xtimer_now_timex(&before); + before = timex_from_uint64(ztimer64_now(ZTIMER64_USEC)); this_thread::sleep_until(riot::now() += chrono::seconds(1)); - xtimer_now_timex(&after); + after = timex_from_uint64(ztimer64_now(ZTIMER64_USEC)); auto diff = timex_sub(after, before); expect(diff.seconds >= 1); } From 95c238a97412c2a013d0f7d4a2de222c0c3a98d1 Mon Sep 17 00:00:00 2001 From: MrKevinWeiss Date: Mon, 27 Mar 2023 08:27:08 +0200 Subject: [PATCH 4/4] tests/cpp11_thread: Update BOARD_INSUFFICIENT_MEMORY --- tests/cpp11_thread/Makefile.ci | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/cpp11_thread/Makefile.ci b/tests/cpp11_thread/Makefile.ci index b2969e92a1..84248e69c9 100644 --- a/tests/cpp11_thread/Makefile.ci +++ b/tests/cpp11_thread/Makefile.ci @@ -1,6 +1,10 @@ BOARD_INSUFFICIENT_MEMORY := \ + nucleo-f031k6 \ + nucleo-f042k6 \ nucleo-l011k4 \ + nucleo-l031k6 \ samd10-xmini \ stk3200 \ stm32f030f4-demo \ + stm32g0316-disco \ #