Module: xenomai-2.6
Branch: master
Commit: d1d00e0acd29bb5f9023494a883a7fa0def40917
URL:    
http://git.xenomai.org/?p=xenomai-2.6.git;a=commit;h=d1d00e0acd29bb5f9023494a883a7fa0def40917

Author: Gilles Chanteperdrix <gilles.chanteperd...@xenomai.org>
Date:   Sat Mar 29 21:40:39 2014 +0100

nucleus/synch: fix rescnt imbalance on slow mutexes

---

 include/nucleus/thread.h    |   16 ++++++++++++++++
 ksrc/skins/rtdm/drvlib.c    |    4 +---
 ksrc/skins/vrtx/mx.c        |    9 +++------
 ksrc/skins/vxworks/semLib.c |    4 +---
 4 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/include/nucleus/thread.h b/include/nucleus/thread.h
index 4a740a0..aca5f90 100644
--- a/include/nucleus/thread.h
+++ b/include/nucleus/thread.h
@@ -436,6 +436,22 @@ struct xnthread *xnthread_lookup(xnhandle_t threadh)
        return (thread && xnthread_handle(thread) == threadh) ? thread : NULL;
 }
 
+static inline int xnthread_try_grab(struct xnthread *thread,
+                                   struct xnsynch *synch)
+{
+       XENO_BUGON(NUCLEUS, xnsynch_fastlock_p(synch));
+
+       if (xnsynch_owner(synch) != NULL)
+               return 0;
+
+       xnsynch_set_owner(synch, thread);
+
+       if (xnthread_test_state(thread, XNOTHER))
+               xnthread_inc_rescnt(thread);
+
+       return 1;
+}
+
 #ifdef __cplusplus
 extern "C" {
 #endif
diff --git a/ksrc/skins/rtdm/drvlib.c b/ksrc/skins/rtdm/drvlib.c
index 391d45f..2e0f14b 100644
--- a/ksrc/skins/rtdm/drvlib.c
+++ b/ksrc/skins/rtdm/drvlib.c
@@ -1538,9 +1538,7 @@ int rtdm_mutex_timedlock(rtdm_mutex_t *mutex, 
nanosecs_rel_t timeout,
        if (unlikely(xnsynch_test_flags(&mutex->synch_base,
                                        RTDM_SYNCH_DELETED)))
                err = -EIDRM;
-       else if (likely(xnsynch_owner(&mutex->synch_base) == NULL))
-               xnsynch_set_owner(&mutex->synch_base, curr_thread);
-       else {
+       else if (!xnthread_try_grab(curr_thread, &mutex->synch_base)) {
                /* Redefinition to clarify XENO_ASSERT output */
                #define mutex_owner xnsynch_owner(&mutex->synch_base)
                XENO_ASSERT(RTDM, mutex_owner != curr_thread,
diff --git a/ksrc/skins/vrtx/mx.c b/ksrc/skins/vrtx/mx.c
index e4c6b72..f40febb 100644
--- a/ksrc/skins/vrtx/mx.c
+++ b/ksrc/skins/vrtx/mx.c
@@ -284,10 +284,8 @@ void sc_mpend(int mid, unsigned long timeout, int *errp)
 
        *errp = RET_OK;
 
-       if (xnsynch_owner(&mx->synchbase) == NULL) {
-               xnsynch_set_owner(&mx->synchbase, cur);
+       if (xnthread_try_grab(cur, &mx->synchbase))
                goto unlock_and_exit;
-       }
 
        if (xnsynch_owner(&mx->synchbase) == cur)
                goto unlock_and_exit;
@@ -330,10 +328,9 @@ void sc_maccept(int mid, int *errp)
                goto unlock_and_exit;
        }
 
-       if (xnsynch_owner(&mx->synchbase) == NULL) {
-               xnsynch_set_owner(&mx->synchbase, xnpod_current_thread());
+       if (xnthread_try_grab(xnpod_current_thread(), &mx->synchbase))
                *errp = RET_OK;
-       } else
+       else
                *errp = ER_PND;
 
 unlock_and_exit:
diff --git a/ksrc/skins/vxworks/semLib.c b/ksrc/skins/vxworks/semLib.c
index aa4db57..06b5068 100644
--- a/ksrc/skins/vxworks/semLib.c
+++ b/ksrc/skins/vxworks/semLib.c
@@ -385,10 +385,8 @@ static STATUS semm_take(wind_sem_t *sem, xnticks_t to)
 {
        xnthread_t *cur = xnpod_current_thread();
 
-       if (xnsynch_owner(&sem->synchbase) == NULL) {
-               xnsynch_set_owner(&sem->synchbase, cur);
+       if (xnthread_try_grab(cur, &sem->synchbase))
                goto grab_sem;
-       }
 
        if (xnsynch_owner(&sem->synchbase) == cur) {
                sem->count++;


_______________________________________________
Xenomai-git mailing list
Xenomai-git@xenomai.org
http://www.xenomai.org/mailman/listinfo/xenomai-git

Reply via email to