All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jan Kiszka <jan.kiszka@domain.hid>
To: xenomai-core <xenomai@xenomai.org>
Subject: [Xenomai-core] [RFC][PATCH 9/9] Optimize xnsynch_sleep_on for XN_NONBLOCK
Date: Mon, 01 Sep 2008 11:06:13 +0200	[thread overview]
Message-ID: <48BBB085.7040407@domain.hid> (raw)
In-Reply-To: <48BBA9E6.5070400@domain.hid>

This patch improves the performance of xnsynch_sleep_on when it is
called with (XN_RELATIVE, XN_NONBLOCK) timeout parameters. For this
purpose, it introduces a new thread info XNNONBLCK that is set in case
xnsynch_sleep_on fails while in XN_NONBLOCK mode. Maybe this should be
solved via a dedicated trylock (but that would duplicate quite some
code), hopefully we can avoid it completely by doing lock stealing on
trylock via (generic) fast locks, even in user space.

---
 include/nucleus/thread.h  |    1 +
 ksrc/nucleus/pod.c        |    8 +++++---
 ksrc/nucleus/synch.c      |   20 +++++++++++++++++---
 ksrc/skins/native/mutex.c |   13 +++++++------
 ksrc/skins/posix/mutex.c  |    5 +----
 ksrc/skins/posix/mutex.h  |    8 +++++++-
 6 files changed, 38 insertions(+), 17 deletions(-)

Index: b/include/nucleus/thread.h
===================================================================
--- a/include/nucleus/thread.h
+++ b/include/nucleus/thread.h
@@ -116,6 +116,7 @@
 #define XNROBBED  0x00000020 /**< Robbed from resource ownership */
 #define XNATOMIC  0x00000040 /**< In atomic switch from secondary to primary mode */
 #define XNAFFSET  0x00000080 /**< CPU affinity changed from primary mode */
+#define XNNONBLCK 0x00000100 /**< Non-blocking mode prevented suspension */
 
 /* These information flags are available to the real-time interfaces */
 #define XNTHREAD_INFO_SPARE0  0x10000000
Index: b/ksrc/nucleus/pod.c
===================================================================
--- a/ksrc/nucleus/pod.c
+++ b/ksrc/nucleus/pod.c
@@ -1401,14 +1401,16 @@ void xnpod_suspend_thread(xnthread_t *th
 						thread->name, mask);
 				);
 			xnthread_clear_info(thread,
-					    XNRMID | XNTIMEO | XNROBBED);
+					    XNRMID | XNTIMEO | XNROBBED |
+					    XNNONBLCK);
 			xnthread_set_info(thread, XNBREAK);
 			goto unlock_and_exit;
 		}
 #endif /* CONFIG_XENO_OPT_PERVASIVE */
 
-		xnthread_clear_info(thread, XNRMID | XNTIMEO | XNBREAK |
-					    XNWAKEN | XNROBBED);
+		xnthread_clear_info(thread,
+				    XNRMID | XNTIMEO | XNBREAK | XNWAKEN |
+				    XNROBBED | XNNONBLCK);
 	}
 
 	/* Don't start the timer for a thread indefinitely delayed by
Index: b/ksrc/nucleus/synch.c
===================================================================
--- a/ksrc/nucleus/synch.c
+++ b/ksrc/nucleus/synch.c
@@ -194,7 +194,8 @@ redo:
 
 	if (!owner) {
 		synch->owner = thread;
-		xnthread_clear_info(thread, XNRMID | XNTIMEO | XNBREAK);
+		xnthread_clear_info(thread,
+				    XNRMID | XNTIMEO | XNBREAK | XNNONBLCK);
 		goto unlock_and_exit;
 	}
 
@@ -202,11 +203,18 @@ redo:
 		if (xnthread_test_info(owner, XNWAKEN) && owner->wwake == synch) {
 			/* Ownership is still pending, steal the resource. */
 			synch->owner = thread;
-			xnthread_clear_info(thread, XNRMID | XNTIMEO | XNBREAK);
+			xnthread_clear_info(thread,
+					    XNRMID | XNTIMEO | XNBREAK |
+					    XNNONBLCK);
 			xnthread_set_info(owner, XNROBBED);
 			goto unlock_and_exit;
 		}
 
+		if (timeout == XN_NONBLOCK && timeout_mode == XN_RELATIVE) {
+			xnthread_set_info(thread, XNNONBLCK);
+			goto unlock_and_exit;
+		}
+
 		if (!xnthread_test_state(owner, XNBOOST)) {
 			owner->bprio = owner->cprio;
 			xnthread_set_state(owner, XNBOOST);
@@ -220,8 +228,14 @@ redo:
 		insertpqf(&owner->claimq, &synch->link, thread->cprio);
 		insertpqf(&synch->pendq, &thread->plink, thread->cprio);
 		xnsynch_renice_thread(owner, thread->cprio);
-	} else
+	} else {
+		if (timeout == XN_NONBLOCK && timeout_mode == XN_RELATIVE) {
+			xnthread_set_info(thread, XNNONBLCK);
+			goto unlock_and_exit;
+		}
+
 		insertpqf(&synch->pendq, &thread->plink, thread->cprio);
+	}
 
 	xnpod_suspend_thread(thread, XNPEND, timeout, timeout_mode, synch);
 
Index: b/ksrc/skins/native/mutex.c
===================================================================
--- a/ksrc/skins/native/mutex.c
+++ b/ksrc/skins/native/mutex.c
@@ -388,17 +388,18 @@ int rt_mutex_acquire_inner(RT_MUTEX *mut
 
 	if (unlikely
 	    (xnthread_test_info(thread,
-				XNBREAK | XNRMID | XNROBBED | XNTIMEO))) {
+				XNBREAK | XNNONBLCK | XNRMID | XNROBBED |
+				XNTIMEO))) {
+		if (xnthread_test_info(thread, XNNONBLCK)) {
+			err = -EWOULDBLOCK;	/* Try-to-lock failed. */
+			goto cleanup_and_exit;
+		}
 		if (xnthread_test_info(thread, XNROBBED)) {
 			xnlock_put_irqrestore(&nklock, s);
 			goto retry;
 		}
 		if (xnthread_test_info(thread, XNTIMEO)) {
-			if (timeout_mode == XN_RELATIVE &&
-			    timeout == XN_NONBLOCK)
-				err = -EWOULDBLOCK;
-			else
-				err = -ETIMEDOUT;
+			err = -ETIMEDOUT;
 			goto cleanup_and_exit;
 		}
 		if (xnthread_test_info(thread, XNBREAK)) {
Index: b/ksrc/skins/posix/mutex.c
===================================================================
--- a/ksrc/skins/posix/mutex.c
+++ b/ksrc/skins/posix/mutex.c
@@ -406,13 +406,10 @@ int pthread_mutex_trylock(pthread_mutex_
 				err = 0;
 			}
 		}
-	} else {
+	} else
 		err =
 		    -pse51_mutex_timedlock_internal(cur, shadow, 1,
 						    XN_RELATIVE, XN_NONBLOCK);
-		if (err == ETIMEDOUT)
-			err = EBUSY;
-	}
 
 	cb_read_unlock(&shadow->lock, s);
 
Index: b/ksrc/skins/posix/mutex.h
===================================================================
--- a/ksrc/skins/posix/mutex.h
+++ b/ksrc/skins/posix/mutex.h
@@ -174,7 +174,13 @@ static inline int pse51_mutex_timedlock_
 	xnsynch_sleep_on(&mutex->synchbase, timeout, timeout_mode);
 
 	if (unlikely
-	    (xnthread_test_info(cur, XNBREAK | XNRMID | XNROBBED | XNTIMEO))) {
+	    (xnthread_test_info(cur,
+				XNBREAK | XNNONBLCK | XNRMID | XNROBBED |
+				XNTIMEO))) {
+		if (xnthread_test_info(cur, XNNONBLCK)) {
+			err = -EBUSY;
+			goto error;
+		}
 		if (xnthread_test_info(cur, XNROBBED)) {
 			xnlock_put_irqrestore(&nklock, s);
 			goto retry_lock;



  parent reply	other threads:[~2008-09-01  9:06 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-09-01  8:53 [Xenomai-core] [PATCH 0/9] Fast mutex rework, native support Jan Kiszka
2008-09-01  8:39 ` [Xenomai-core] [PATCH 1/9] Always register threads by their base Jan Kiszka
2008-09-01  8:44 ` [Xenomai-core] [PATCH 2/9] Switch to handle-based fast mutex owners Jan Kiszka
2008-09-01 11:58   ` Gilles Chanteperdrix
2008-09-01 13:48     ` Jan Kiszka
2008-09-01 14:00       ` Gilles Chanteperdrix
2008-09-01 14:03         ` Jan Kiszka
2008-09-01 14:08           ` Jan Kiszka
2008-09-02 12:31           ` Gilles Chanteperdrix
2008-09-05  8:34   ` [Xenomai-core] [PATCH 2/9] Switch to handle-based fast mutex owners - v2 Jan Kiszka
2008-09-05  9:40     ` Gilles Chanteperdrix
2008-09-05  9:46       ` Jan Kiszka
2008-09-05  9:50         ` Gilles Chanteperdrix
2008-09-05  9:55           ` Jan Kiszka
2008-09-05  9:57             ` Gilles Chanteperdrix
2008-09-01  8:45 ` [Xenomai-core] [PATCH 3/9] Remove xnarch_atomic_intptr wrappers Jan Kiszka
2008-09-01  8:47 ` [Xenomai-core] [PATCH 4/9] Spread xeno_set_current Jan Kiszka
2008-09-01  8:54 ` [Xenomai-core] [RFC][PATCH 5/9] Allow lock stealing via pthread_mutex_trylock Jan Kiszka
2008-09-01  9:58   ` Gilles Chanteperdrix
2008-09-01  8:57 ` [Xenomai-core] [RFC][PATCH 6/9] Add XNSYNCH_FWDROB Jan Kiszka
2008-09-01 10:00   ` Gilles Chanteperdrix
2008-09-01  9:01 ` [Xenomai-core] [RFC][PATCH 8/9] Native support for fast mutexes Jan Kiszka
2008-09-05  8:36   ` [Xenomai-core] [RFC][PATCH 8/9] Native support for fast mutexes - v2 Jan Kiszka
2008-09-01  9:06 ` Jan Kiszka [this message]
2008-09-01  9:12 ` [Xenomai-core] [RFC][PATCH 7/9] Switch POSIX mutexes to XNSYNCH_FWDROB Jan Kiszka
2008-09-01 10:01   ` Gilles Chanteperdrix

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=48BBB085.7040407@domain.hid \
    --to=jan.kiszka@domain.hid \
    --cc=xenomai@xenomai.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.