xenomai.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: Richard Weinberger <richard@nod.at>
To: xenomai@lists.linux.dev
Cc: Richard Weinberger <richard@nod.at>
Subject: [PATCH] [RFC] xnthread_set_periodic: Improve period adjusting support
Date: Wed, 20 Dec 2023 20:48:55 +0100	[thread overview]
Message-ID: <20231220194855.8684-1-richard@nod.at> (raw)

Currently, when a periodic task invokes xnthread_set_periodic() multiple times,
the timer is reset each time.
Consequently, the period drifts because the elapsed time is not taken into
consideration.
To improve the situation, account the elapsed time of the already
running time to the new start time if the time is smaller than
the new period when idate is XN_INFINITE.

Alternatively, callers of xnthread_set_periodic() could access
task->ptimer directly and pass an adjusted start time to xnthread_set_periodic().

Signed-off-by: Richard Weinberger <richard@nod.at>
---
 kernel/cobalt/thread.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/kernel/cobalt/thread.c b/kernel/cobalt/thread.c
index ff12f288a..a5304cc8a 100644
--- a/kernel/cobalt/thread.c
+++ b/kernel/cobalt/thread.c
@@ -1335,9 +1335,18 @@ int xnthread_set_periodic(struct xnthread *thread, xnticks_t idate,
 
 	xntimer_set_affinity(&thread->ptimer, thread->sched);
 
-	if (idate == XN_INFINITE)
-		xntimer_start(&thread->ptimer, period, period, XN_RELATIVE);
-	else {
+	if (idate == XN_INFINITE) {
+		xnticks_t start = period;
+
+		if (xntimer_running_p(&thread->ptimer)) {
+			xnticks_t elapsed = xntimer_interval(&thread->ptimer) -
+						__xntimer_get_timeout(&thread->ptimer);
+			if (elapsed < period)
+				start = period - elapsed;
+		}
+
+		xntimer_start(&thread->ptimer, start, period, XN_RELATIVE);
+	} else {
 		if (timeout_mode == XN_REALTIME)
 			idate -= xnclock_get_offset(xntimer_clock(&thread->ptimer));
 		else if (timeout_mode != XN_ABSOLUTE) {
-- 
2.35.3


             reply	other threads:[~2023-12-20 19:49 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-20 19:48 Richard Weinberger [this message]
2024-01-02 13:32 ` [PATCH] [RFC] xnthread_set_periodic: Improve period adjusting support Jan Kiszka
2024-01-02 14:07   ` Richard Weinberger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231220194855.8684-1-richard@nod.at \
    --to=richard@nod.at \
    --cc=xenomai@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).