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

Author: Philippe Gerum <r...@xenomai.org>
Date:   Tue Jun 18 14:52:51 2013 +0200

cobalt/kernel/sched: pre-calculate weighted thread priority

Weighted priority is a per-thread priority level which includes the
weight value of the current scheduling class for the thread
being considered.

This patch introduces pre-calculation of this value when a thread
priority or scheduling class changes, which happens much less
frequently in a thread's lifetime than uses of such value, typically
for enforcing PIP on synchronization objects.

---

 include/cobalt/kernel/sched.h  |   26 ++++++--------------------
 include/cobalt/kernel/thread.h |    2 ++
 kernel/cobalt/pod.c            |    4 ++--
 kernel/cobalt/sched.c          |    1 +
 kernel/cobalt/synch.c          |   35 ++++++++++++++++-------------------
 5 files changed, 27 insertions(+), 41 deletions(-)

diff --git a/include/cobalt/kernel/sched.h b/include/cobalt/kernel/sched.h
index 3702217..0101131 100644
--- a/include/cobalt/kernel/sched.h
+++ b/include/cobalt/kernel/sched.h
@@ -361,20 +361,11 @@ static inline void xnsched_requeue(struct xnthread 
*thread)
                sched_class->sched_requeue(thread);
 }
 
-static inline int xnsched_weighted_bprio(struct xnthread *thread)
-{
-       return thread->bprio + thread->sched_class->weight;
-}
-
-static inline int xnsched_weighted_cprio(struct xnthread *thread)
-{
-       return thread->cprio + thread->sched_class->weight;
-}
-
 static inline void xnsched_setparam(struct xnthread *thread,
                                    const union xnsched_policy_param *p)
 {
        thread->sched_class->sched_setparam(thread, p);
+       thread->wprio = thread->cprio + thread->sched_class->weight;
 }
 
 static inline void xnsched_getparam(struct xnthread *thread,
@@ -387,6 +378,7 @@ static inline void xnsched_trackprio(struct xnthread 
*thread,
                                     const union xnsched_policy_param *p)
 {
        thread->sched_class->sched_trackprio(thread, p);
+       thread->wprio = thread->cprio + thread->sched_class->weight;
 }
 
 static inline void xnsched_forget(struct xnthread *thread)
@@ -430,16 +422,6 @@ static inline void xnsched_requeue(struct xnthread *thread)
                __xnsched_rt_requeue(thread);
 }
 
-static inline int xnsched_weighted_bprio(struct xnthread *thread)
-{
-       return thread->bprio;
-}
-
-static inline int xnsched_weighted_cprio(struct xnthread *thread)
-{
-       return thread->cprio;
-}
-
 static inline void xnsched_setparam(struct xnthread *thread,
                                    const union xnsched_policy_param *p)
 {
@@ -449,6 +431,8 @@ static inline void xnsched_setparam(struct xnthread *thread,
                __xnsched_rt_setparam(thread, p);
        else
                __xnsched_idle_setparam(thread, p);
+
+       thread->wprio = thread->cprio + sched_class->weight;
 }
 
 static inline void xnsched_getparam(struct xnthread *thread,
@@ -471,6 +455,8 @@ static inline void xnsched_trackprio(struct xnthread 
*thread,
                __xnsched_rt_trackprio(thread, p);
        else
                __xnsched_idle_trackprio(thread, p);
+
+       thread->wprio = thread->cprio + sched_class->weight;
 }
 
 static inline void xnsched_forget(struct xnthread *thread)
diff --git a/include/cobalt/kernel/thread.h b/include/cobalt/kernel/thread.h
index e7478a9..379c72e 100644
--- a/include/cobalt/kernel/thread.h
+++ b/include/cobalt/kernel/thread.h
@@ -214,6 +214,8 @@ typedef struct xnthread {
 
        int cprio;                      /* Current priority */
 
+       int wprio;                      /* Weighted priority (dep. scheduling 
class) */
+
        u_long schedlck;                /*!< Scheduler lock count. */
 
        xnpholder_t rlink;              /* Thread holder in ready queue */
diff --git a/kernel/cobalt/pod.c b/kernel/cobalt/pod.c
index 5819e45..1f76554 100644
--- a/kernel/cobalt/pod.c
+++ b/kernel/cobalt/pod.c
@@ -1498,13 +1498,13 @@ int xnpod_set_thread_schedparam(struct xnthread *thread,
 
        xnlock_get_irqsave(&nklock, s);
 
-       old_wprio = xnsched_weighted_cprio(thread);
+       old_wprio = thread->wprio;
 
        ret = xnsched_set_policy(thread, sched_class, sched_param);
        if (ret)
                goto unlock_and_exit;
 
-       new_wprio = xnsched_weighted_cprio(thread);
+       new_wprio = thread->wprio;
 
        trace_mark(xn_nucleus, set_thread_schedparam,
                   "thread %p thread_name %s class %s prio %d",
diff --git a/kernel/cobalt/sched.c b/kernel/cobalt/sched.c
index 65afaf6..6302ebf 100644
--- a/kernel/cobalt/sched.c
+++ b/kernel/cobalt/sched.c
@@ -363,6 +363,7 @@ int xnsched_set_policy(struct xnthread *thread,
        thread->base_class = sched_class;
        xnsched_setparam(thread, p);
        thread->bprio = thread->cprio;
+       thread->wprio = thread->cprio + sched_class->weight;
 
        if (xnthread_test_state(thread, XNREADY))
                xnsched_enqueue(thread);
diff --git a/kernel/cobalt/synch.c b/kernel/cobalt/synch.c
index 4ec285a..a964e8a 100644
--- a/kernel/cobalt/synch.c
+++ b/kernel/cobalt/synch.c
@@ -37,9 +37,6 @@
 #include <cobalt/kernel/clock.h>
 #include <cobalt/kernel/shadow.h>
 
-#define w_bprio(t)     xnsched_weighted_bprio(t)
-#define w_cprio(t)     xnsched_weighted_cprio(t)
-
 /*!
  * \fn void xnsynch_init(struct xnsynch *synch, xnflags_t flags,
  *                       atomic_long_t *fastlock)
@@ -183,7 +180,7 @@ xnflags_t xnsynch_sleep_on(struct xnsynch *synch, xnticks_t 
timeout,
        if (!testbits(synch->status, XNSYNCH_PRIO)) /* i.e. FIFO */
                appendpq(&synch->pendq, &thread->plink);
        else /* i.e. priority-sorted */
-               insertpqf(&synch->pendq, &thread->plink, w_cprio(thread));
+               insertpqf(&synch->pendq, &thread->plink, thread->wprio);
 
        xnpod_suspend_thread(thread, XNPEND, timeout, timeout_mode, synch);
 
@@ -492,7 +489,7 @@ xnflags_t xnsynch_acquire(struct xnsynch *synch, xnticks_t 
timeout,
 
        if (!testbits(synch->status, XNSYNCH_PRIO)) /* i.e. FIFO */
                appendpq(&synch->pendq, &thread->plink);
-       else if (w_cprio(thread) > w_cprio(owner)) {
+       else if (thread->wprio > owner->wprio) {
                if (xnthread_test_info(owner, XNWAKEN) && owner->wwake == 
synch) {
                        /* Ownership is still pending, steal the resource. */
                        synch->owner = thread;
@@ -501,7 +498,7 @@ xnflags_t xnsynch_acquire(struct xnsynch *synch, xnticks_t 
timeout,
                        goto grab_and_exit;
                }
 
-               insertpqf(&synch->pendq, &thread->plink, w_cprio(thread));
+               insertpqf(&synch->pendq, &thread->plink, thread->wprio);
 
                if (testbits(synch->status, XNSYNCH_PIP)) {
                        if (!xnthread_test_state(owner, XNBOOST)) {
@@ -514,11 +511,11 @@ xnflags_t xnsynch_acquire(struct xnsynch *synch, 
xnticks_t timeout,
                        else
                                __setbits(synch->status, XNSYNCH_CLAIMED);
 
-                       insertpqf(&owner->claimq, &synch->link, 
w_cprio(thread));
+                       insertpqf(&owner->claimq, &synch->link, thread->wprio);
                        xnsynch_renice_thread(owner, thread);
                }
        } else
-               insertpqf(&synch->pendq, &thread->plink, w_cprio(thread));
+               insertpqf(&synch->pendq, &thread->plink, thread->wprio);
 
        xnpod_suspend_thread(thread, XNPEND, timeout, timeout_mode, synch);
 
@@ -630,7 +627,7 @@ static void xnsynch_clear_boost(struct xnsynch *synch,
 
        removepq(&owner->claimq, &synch->link);
        __clrbits(synch->status, XNSYNCH_CLAIMED);
-       wprio = w_bprio(owner);
+       wprio = owner->bprio + owner->sched_class->weight;
 
        if (emptypq_p(&owner->claimq)) {
                xnthread_clear_state(owner, XNBOOST);
@@ -641,13 +638,13 @@ static void xnsynch_clear_boost(struct xnsynch *synch,
                h = getheadpq(&hsynch->pendq);
                XENO_BUGON(NUCLEUS, h == NULL);
                target = link2thread(h, plink);
-               if (w_cprio(target) > wprio)
-                       wprio = w_cprio(target);
+               if (target->wprio > wprio)
+                       wprio = target->wprio;
                else
                        target = owner;
        }
 
-       if (w_cprio(owner) != wprio &&
+       if (owner->wprio != wprio &&
            !xnthread_test_state(owner, XNZOMBIE))
                xnsynch_renice_thread(owner, target);
 }
@@ -674,10 +671,10 @@ void xnsynch_requeue_sleeper(struct xnthread *thread)
                return;
 
        removepq(&synch->pendq, &thread->plink);
-       insertpqf(&synch->pendq, &thread->plink, w_cprio(thread));
+       insertpqf(&synch->pendq, &thread->plink, thread->wprio);
        owner = synch->owner;
 
-       if (owner != NULL && w_cprio(thread) > w_cprio(owner)) {
+       if (owner != NULL && thread->wprio > owner->wprio) {
                /*
                 * The new (weighted) priority of the sleeping thread
                 * is higher than the priority of the current owner of
@@ -690,7 +687,7 @@ void xnsynch_requeue_sleeper(struct xnthread *thread)
                         */
                        removepq(&owner->claimq, &synch->link);
                        insertpqf(&owner->claimq, &synch->link,
-                                 w_cprio(thread));
+                                 thread->wprio);
                } else {
                        /*
                         * The resource was NOT claimed, claim it now
@@ -698,7 +695,7 @@ void xnsynch_requeue_sleeper(struct xnthread *thread)
                         */
                        __setbits(synch->status, XNSYNCH_CLAIMED);
                        insertpqf(&owner->claimq, &synch->link,
-                                 w_cprio(thread));
+                                 thread->wprio);
                        if (!xnthread_test_state(owner, XNBOOST)) {
                                owner->bprio = owner->cprio;
                                xnthread_set_state(owner, XNBOOST);
@@ -927,7 +924,7 @@ void xnsynch_forget_sleeper(struct xnthread *thread)
                else {
                        target = link2thread(getheadpq(&synch->pendq), plink);
                        h = getheadpq(&owner->claimq);
-                       if (w_cprio(target) != h->prio) {
+                       if (target->wprio != h->prio) {
                                /*
                                 * Reorder the claim queue, and lower
                                 * the priority to the required
@@ -936,10 +933,10 @@ void xnsynch_forget_sleeper(struct xnthread *thread)
                                 */
                                removepq(&owner->claimq, &synch->link);
                                insertpqf(&owner->claimq, &synch->link,
-                                         w_cprio(target));
+                                         target->wprio);
 
                                h = getheadpq(&owner->claimq);
-                               if (h->prio < w_cprio(owner))
+                               if (h->prio < owner->wprio)
                                        xnsynch_renice_thread(owner, target);
                        }
                }


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

Reply via email to