From: Jan Kiszka <jan.kiszka@domain.hid>
To: xenomai@xenomai.org
Cc: Jan Kiszka <jan.kiszka@domain.hid>
Subject: [Xenomai-core] [PATCH 12/13] Ensure mode switch in mutex fast paths
Date: Fri, 17 Oct 2008 17:46:11 +0200 [thread overview]
Message-ID: <20081017154601.403145579@domain.hid> (raw)
In-Reply-To: 20081017154559.420723261@domain.hid
With the help of xeno_get_current_mode(), this patch ensures that shadow
threads in secondary mode will be switched to primary again in case they
acquire an uncontended native or POSIX mutex, thus would otherwise only
pick the user space fast path. This prevents potential priority
inversions that the fast mutex optimizations introduced.
Signed-off-by: Jan Kiszka <jan.kiszka@domain.hid>
---
src/skins/native/mutex.c | 31 ++++++++-------
src/skins/posix/mutex.c | 94 +++++++++++++++++++++++++++--------------------
2 files changed, 71 insertions(+), 54 deletions(-)
Index: b/src/skins/native/mutex.c
===================================================================
--- a/src/skins/native/mutex.c
+++ b/src/skins/native/mutex.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include <nucleus/synch.h>
+#include <nucleus/thread.h>
#include <native/syscall.h>
#include <native/mutex.h>
#include <asm-generic/bits/current.h>
@@ -78,22 +79,24 @@ static int rt_mutex_acquire_inner(RT_MUT
if (!cur)
return -EPERM;
- err = xnsynch_fast_acquire(mutex->fastlock, cur);
- if (likely(!err)) {
- mutex->lockcnt = 1;
- return 0;
- }
-
- if (err == -EBUSY) {
- if (mutex->lockcnt == UINT_MAX)
- return -EAGAIN;
+ if (likely(!(xeno_get_current_mode() & XNRELAX))) {
+ err = xnsynch_fast_acquire(mutex->fastlock, cur);
+ if (likely(!err)) {
+ mutex->lockcnt = 1;
+ return 0;
+ }
+
+ if (err == -EBUSY) {
+ if (mutex->lockcnt == UINT_MAX)
+ return -EAGAIN;
+
+ mutex->lockcnt++;
+ return 0;
+ }
- mutex->lockcnt++;
- return 0;
+ if (timeout == TM_NONBLOCK && mode == XN_RELATIVE)
+ return -EWOULDBLOCK;
}
-
- if (timeout == TM_NONBLOCK && mode == XN_RELATIVE)
- return -EWOULDBLOCK;
#endif /* CONFIG_XENO_FASTSYNCH */
err = XENOMAI_SKINCALL3(__native_muxid,
Index: b/src/skins/posix/mutex.c
===================================================================
--- a/src/skins/posix/mutex.c
+++ b/src/skins/posix/mutex.c
@@ -164,31 +164,33 @@ int __wrap_pthread_mutex_lock(pthread_mu
goto out;
}
- err = xnsynch_fast_acquire(get_ownerp(shadow), cur);
+ if (likely(!(xeno_get_current_mode() & XNRELAX))) {
+ err = xnsynch_fast_acquire(get_ownerp(shadow), cur);
- if (likely(!err)) {
- shadow->lockcnt = 1;
- cb_read_unlock(&shadow->lock, s);
- return 0;
- }
+ if (likely(!err)) {
+ shadow->lockcnt = 1;
+ cb_read_unlock(&shadow->lock, s);
+ return 0;
+ }
- if (err == -EBUSY)
- switch(shadow->attr.type) {
- case PTHREAD_MUTEX_NORMAL:
- break;
+ if (err == -EBUSY)
+ switch(shadow->attr.type) {
+ case PTHREAD_MUTEX_NORMAL:
+ break;
- case PTHREAD_MUTEX_ERRORCHECK:
- err = -EDEADLK;
- goto out;
+ case PTHREAD_MUTEX_ERRORCHECK:
+ err = -EDEADLK;
+ goto out;
- case PTHREAD_MUTEX_RECURSIVE:
- if (shadow->lockcnt == UINT_MAX) {
- err = -EAGAIN;
+ case PTHREAD_MUTEX_RECURSIVE:
+ if (shadow->lockcnt == UINT_MAX) {
+ err = -EAGAIN;
+ goto out;
+ }
+ ++shadow->lockcnt;
+ err = 0;
goto out;
}
- ++shadow->lockcnt;
- err = 0;
- goto out;
}
#endif /* CONFIG_XENO_FASTSYNCH */
@@ -226,32 +228,34 @@ int __wrap_pthread_mutex_timedlock(pthre
goto out;
}
- err = xnsynch_fast_acquire(get_ownerp(shadow), cur);
+ if (likely(!(xeno_get_current_mode() & XNRELAX))) {
+ err = xnsynch_fast_acquire(get_ownerp(shadow), cur);
- if (likely(!err)) {
- shadow->lockcnt = 1;
- cb_read_unlock(&shadow->lock, s);
- return 0;
- }
+ if (likely(!err)) {
+ shadow->lockcnt = 1;
+ cb_read_unlock(&shadow->lock, s);
+ return 0;
+ }
- if (err == -EBUSY)
- switch(shadow->attr.type) {
- case PTHREAD_MUTEX_NORMAL:
- break;
+ if (err == -EBUSY)
+ switch(shadow->attr.type) {
+ case PTHREAD_MUTEX_NORMAL:
+ break;
- case PTHREAD_MUTEX_ERRORCHECK:
- err = -EDEADLK;
- goto out;
+ case PTHREAD_MUTEX_ERRORCHECK:
+ err = -EDEADLK;
+ goto out;
- case PTHREAD_MUTEX_RECURSIVE:
- if (shadow->lockcnt == UINT_MAX) {
- err = -EAGAIN;
+ case PTHREAD_MUTEX_RECURSIVE:
+ if (shadow->lockcnt == UINT_MAX) {
+ err = -EAGAIN;
+ goto out;
+ }
+
+ ++shadow->lockcnt;
goto out;
}
-
- ++shadow->lockcnt;
- goto out;
- }
+ }
#endif /* CONFIG_XENO_FASTSYNCH */
do {
@@ -286,7 +290,17 @@ int __wrap_pthread_mutex_trylock(pthread
if (unlikely(shadow->magic != PSE51_MUTEX_MAGIC)) {
err = -EINVAL;
goto out;
- }
+ }
+
+ if (unlikely(xeno_get_current_mode() & XNRELAX)) {
+ do {
+ err = -XENOMAI_SYSCALL1(__xn_sys_migrate,
+ XENOMAI_XENO_DOMAIN);
+ } while (err == EINTR);
+
+ if (err)
+ goto out;
+ }
err = xnsynch_fast_acquire(get_ownerp(shadow), cur);
next prev parent reply other threads:[~2008-10-17 15:46 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-10-17 15:45 [Xenomai-core] [PATCH 00/13] Generic fast xnsynch support & more Jan Kiszka
2008-10-17 15:46 ` [Xenomai-core] [PATCH 01/13] Generic thread registration Jan Kiszka
2008-10-17 15:46 ` [Xenomai-core] [PATCH 02/13] Handle-based xeno_get_current service Jan Kiszka
2008-10-17 15:46 ` [Xenomai-core] [PATCH 03/13] Remove xnarch_atomic_intptr Jan Kiszka
2008-10-17 15:46 ` [Xenomai-core] [PATCH 04/13] Spread xeno_set_current under all skins Jan Kiszka
2008-10-17 15:46 ` [Xenomai-core] [PATCH 05/13] Factor out xnsynch_acquire/release Jan Kiszka
2008-10-17 15:46 ` [Xenomai-core] [PATCH 06/13] Refactor mutex lockcnt tracking Jan Kiszka
2008-10-17 15:46 ` [Xenomai-core] [PATCH 07/13] Lockless fast path for xnsynch_acquire/release Jan Kiszka
2008-10-17 15:46 ` [Xenomai-core] [PATCH 08/13] Convert POSIX skin to fast xnsynch Jan Kiszka
2008-10-17 15:46 ` [Xenomai-core] [PATCH 09/13] Use fast xnsynch with native skin Jan Kiszka
2008-10-17 15:46 ` [Xenomai-core] [PATCH 10/13] Optionally replace pthread_getspecific with TLS variables Jan Kiszka
2008-10-17 15:46 ` [Xenomai-core] [PATCH 11/13] Report current shadow thread mode to user space Jan Kiszka
2008-10-17 15:46 ` Jan Kiszka [this message]
2008-10-17 15:46 ` [Xenomai-core] [PATCH 13/13] Catch inconsistent SMP feature support Jan Kiszka
2008-10-17 16:11 ` [Xenomai-core] [PATCH 00/13] Generic fast xnsynch support & more Jan Kiszka
2008-10-17 16:41 ` Gilles Chanteperdrix
2008-10-18 18:47 ` 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=20081017154601.403145579@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.