Skip to content

sys/sema: allow to use ztimer and/or xtimer#15782

Merged
haukepetersen merged 2 commits intoRIOT-OS:masterfrom
haukepetersen:opt_sema_ztimer
Apr 12, 2021
Merged

sys/sema: allow to use ztimer and/or xtimer#15782
haukepetersen merged 2 commits intoRIOT-OS:masterfrom
haukepetersen:opt_sema_ztimer

Conversation

@haukepetersen
Copy link
Copy Markdown
Contributor

Contribution description

On the winding road to get NimBLE running without including xtimer or periph_timer in the build, the sema and event modules are the last ones that pull in these dependencies currently. So I am desperately looking for way to free these modules of xtimer deps.

The code in this PR, still very prototypic, illustrates one possible solution to decouple the sema module from xtimer and at the same time adding ztimer support. However, this might not be the most elegant solution, so I am happy to hear any alternative ideas on this task!

Regarding what IMO should be possible:

  • per default, build with an untouched API, so no existing code ever breaks
  • allow to enable the ztimer API in parallel to the existing API -> allows for mixing legacy code with new ztimer code
  • allow to disable the xtimer support and use the ztimer support only -> allows to throw out xtimer

Testing procedure

not in focus, yet.

Issues/PRs references

none

@haukepetersen haukepetersen added Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation Discussion: RFC The issue/PR is used as a discussion starting point about the item of the issue/PR Area: timers Area: timer subsystems Area: sys Area: System labels Jan 15, 2021
@haukepetersen
Copy link
Copy Markdown
Contributor Author

Forgot to mention: remodeling the event module (event_wait_timeout()) presents the same class of problem, so a unified concept that can be applied to both would be needed here.

@haukepetersen
Copy link
Copy Markdown
Contributor Author

Did another take on this, and I think with the API part I am quite happy now. The new take applies the exact same style than we use for the sys/event module (see also #15789). The sema_post_timed() function(s) are simply only available if the fitting timer backend was compiled in.

The only thing that needs more work is the implementation part. Here we need some smarter approach to prevent code duplication I guess...

@haukepetersen
Copy link
Copy Markdown
Contributor Author

Cleaned up the API part slightly. The more I think about this, I come to the conclusion that having two, although in large parts identical, implementations of the _sema_wait_x() functions seems to be the cleanest solution for now. At some point we will remove the xtimer part anyway, right? Then all we need to do is remove the one code path an map the sema_wait_timed to the ztimer call and we are all good.

@fjmolinas
Copy link
Copy Markdown
Contributor

Cleaned up the API part slightly. The more I think about this, I come to the conclusion that having two, although in large parts identical, implementations of the _sema_wait_x() functions seems to be the cleanest solution for now. At some point we will remove the xtimer part anyway, right? Then all we need to do is remove the one code path an map the sema_wait_timed to the ztimer call and we are all good.

Sure lets got with it as is, seems like there is actually no taste for sema atm no?

Copy link
Copy Markdown
Contributor

@fjmolinas fjmolinas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@haukepetersen there is a sema test in master now, lets keep it simple and simply use the ztimer version in one of the wait scenario:

diff --git a/tests/sema/Makefile b/tests/sema/Makefile
index ce88d28105..0d0af60982 100644
--- a/tests/sema/Makefile
+++ b/tests/sema/Makefile
@@ -1,5 +1,7 @@
 include ../Makefile.tests_common
 
 USEMODULE += sema
+USEMODULE += xtimer
+USEMODULE += ztimer_usec
 
 include $(RIOTBASE)/Makefile.include
diff --git a/tests/sema/main.c b/tests/sema/main.c
index 295c434103..5f0fdd2fa4 100644
--- a/tests/sema/main.c
+++ b/tests/sema/main.c
@@ -26,6 +26,7 @@
 #include "sema.h"
 #include "thread.h"
 #include "xtimer.h"
+#include  "ztimer.h"
 
 #define TEST_TIME_US 1000
 #define TEST_ITERATIONS 4
@@ -48,7 +49,7 @@ static void *second_thread(void *arg)
         printf("THREAD ERROR: sema_wait_timed()");
         *thread_success = -1;
     }
-    if (sema_wait_timed(&test_sema2, TEST_TIME_US) != 0) {
+    if (sema_wait_timed_ztimer(&test_sema2, ZTIMER_USEC, TEST_TIME_US) != 0) {
         printf("THREAD ERROR: sema_wait_timed()");
         *thread_success = -1;
     }

(please rebase to get the test in)

fjmolinas
fjmolinas previously approved these changes Apr 7, 2021
Copy link
Copy Markdown
Contributor

@fjmolinas fjmolinas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some final comments.

@fjmolinas fjmolinas dismissed their stale review April 7, 2021 10:22

wrong button when commenting

@fjmolinas
Copy link
Copy Markdown
Contributor

@haukepetersen there is a sema test in master now, lets keep it simple and simply use the ztimer version in one of the wait scenario:

Actually, maybe its better to simply duplicate the test

@fjmolinas
Copy link
Copy Markdown
Contributor

@haukepetersen there is a sema test in master now, lets keep it simple and simply use the ztimer version in one of the wait scenario:

Actually, maybe its better to simply duplicate the test

With #15782 (comment) in mind I think just the patch in #15782 (review) is enough. So if you remove this newline #15782 (comment) then I think this one is OK!

@haukepetersen
Copy link
Copy Markdown
Contributor Author

done:

  • added call to sema_wait_timed_ztimer() to the test application
  • removed the newline
  • fixed a dependency problem when building without any active timers

@fjmolinas fjmolinas added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Apr 12, 2021
@fjmolinas
Copy link
Copy Markdown
Contributor

Murdock is green please squash @haukepetersen!

Copy link
Copy Markdown
Contributor

@fjmolinas fjmolinas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK!

@haukepetersen
Copy link
Copy Markdown
Contributor Author

squashed

@haukepetersen
Copy link
Copy Markdown
Contributor Author

All green -> go

@haukepetersen haukepetersen merged commit 9926e33 into RIOT-OS:master Apr 12, 2021
@haukepetersen haukepetersen deleted the opt_sema_ztimer branch April 12, 2021 12:44
@kaspar030 kaspar030 added this to the Release 2021.04 milestone Apr 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: sys Area: System Area: timers Area: timer subsystems CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Discussion: RFC The issue/PR is used as a discussion starting point about the item of the issue/PR Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants