QP/C
qk_ext.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * Product: QK/C
00003 * Last Updated for Version: 4.4.02
00004 * Date of the Last Update:  Apr 13, 2012
00005 *
00006 *                    Q u a n t u m     L e a P s
00007 *                    ---------------------------
00008 *                    innovating embedded systems
00009 *
00010 * Copyright (C) 2002-2012 Quantum Leaps, LLC. All rights reserved.
00011 *
00012 * This program is open source software: you can redistribute it and/or
00013 * modify it under the terms of the GNU General Public License as published
00014 * by the Free Software Foundation, either version 2 of the License, or
00015 * (at your option) any later version.
00016 *
00017 * Alternatively, this program may be distributed and modified under the
00018 * terms of Quantum Leaps commercial licenses, which expressly supersede
00019 * the GNU General Public License and are specifically designed for
00020 * licensees interested in retaining the proprietary status of their code.
00021 *
00022 * This program is distributed in the hope that it will be useful,
00023 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00025 * GNU General Public License for more details.
00026 *
00027 * You should have received a copy of the GNU General Public License
00028 * along with this program. If not, see <http://www.gnu.org/licenses/>.
00029 *
00030 * Contact information:
00031 * Quantum Leaps Web sites: http://www.quantum-leaps.com
00032 *                          http://www.state-machine.com
00033 * e-mail:                  info@quantum-leaps.com
00034 *****************************************************************************/
00035 #include "qk_pkg.h"
00036 /*#include "qassert.h"*/
00037 
00038 /*Q_DEFINE_THIS_MODULE("qk_ext")*/
00039 
00046 /*..........................................................................*/
00047 /* NOTE: the QK scheduler is entered and exited with interrupts LOCKED      */
00048 void QK_schedExt_(uint8_t p) {
00049     uint8_t pin = QK_currPrio_;                /* save the initial priority */
00050 #ifdef QK_TLS                                 /* thread-local storage used? */
00051     uint8_t pprev = pin;
00052 #endif
00053     QActive *a;
00054 
00055 #ifdef QK_EXT_SAVE                         /* extended context-switch used? */
00056     if (pin != (uint8_t)0) {       /* no extended context for the idle loop */
00057         a = QF_active_[pin];             /* the pointer to the preempted AO */
00058         QK_EXT_SAVE(a);                        /* save the extended context */
00059     }
00060 #endif
00061     do {
00062         QEvt const *e;
00063         a = QF_active_[p];                  /* obtain the pointer to the AO */
00064 
00065         QK_currPrio_ = p;         /* this becomes the current task priority */
00066 
00067 #ifdef QK_TLS                                 /* thread-local storage used? */
00068         if (p != pprev) {                       /* are we changing threads? */
00069             QK_TLS(a);                   /* switch new thread-local storage */
00070             pprev = p;
00071         }
00072 #endif
00073         QS_BEGIN_NOCRIT_(QS_QK_SCHEDULE, QS_aoObj_, a)
00074             QS_TIME_();                                        /* timestamp */
00075             QS_U8_(p);                            /* the priority of the AO */
00076             QS_U8_(pin);                          /* the preempted priority */
00077         QS_END_NOCRIT_()
00078 
00079         QF_INT_ENABLE();               /* unconditionally enable interrupts */
00080 
00081         e = QActive_get_(a);              /* get the next event for this AO */
00082         QF_ACTIVE_DISPATCH_(&a->super, e);            /* dispatch to the AO */
00083         QF_gc(e);                /* garbage collect the event, if necessary */
00084 
00085         QF_INT_DISABLE();                             /* disable interrupts */
00086 
00087 #if (QF_MAX_ACTIVE <= 8)  /* determine the highest-priority AO ready to run */
00088         QPSet8_findMax(&QK_readySet_, p);
00089 #else
00090         QPSet64_findMax(&QK_readySet_, p);
00091 #endif
00092 #ifdef QK_NO_MUTEX
00093     } while (p > pin);          /* is the new priority higher than initial? */
00094 #else                                /* QK priority-ceiling mutexes allowed */
00095     } while ((p > pin) && (p > QK_ceilingPrio_));
00096 #endif
00097 
00098     QK_currPrio_ = pin;                     /* restore the initial priority */
00099 
00100 #if defined(QK_TLS) || defined(QK_EXT_RESTORE)
00101     if (pin != (uint8_t)0) {       /* no extended context for the idle loop */
00102         a = QF_active_[pin];             /* the pointer to the preempted AO */
00103 #ifdef QK_TLS                                 /* thread-local storage used? */
00104         QK_TLS(a);                              /* restore the original TLS */
00105 #endif
00106 #ifdef QK_EXT_RESTORE                      /* extended context-switch used? */
00107         QK_EXT_RESTORE(a);                  /* restore the extended context */
00108 #endif
00109     }
00110 #endif
00111 }