QP/C
qf_tick.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * Product: QF/C
00003 * Last Updated for Version: 4.3.01
00004 * Date of the Last Update:  Jan 06, 2011
00005 *
00006 *                    Q u a n t u m     L e a P s
00007 *                    ---------------------------
00008 *                    innovating embedded systems
00009 *
00010 * Copyright (C) 2002-2011 Quantum Leaps, LLC. All rights reserved.
00011 *
00012 * This software may be distributed and modified under the terms of the GNU
00013 * General Public License version 2 (GPL) as published by the Free Software
00014 * Foundation and appearing in the file GPL.TXT included in the packaging of
00015 * this file. Please note that GPL Section 2[b] requires that all works based
00016 * on this software must also be made publicly available under the terms of
00017 * the GPL ("Copyleft").
00018 *
00019 * Alternatively, this software may be distributed and modified under the
00020 * terms of Quantum Leaps commercial licenses, which expressly supersede
00021 * the GPL and are specifically designed for licensees interested in
00022 * retaining the proprietary status of their code.
00023 *
00024 * Contact information:
00025 * Quantum Leaps Web site:  http://www.quantum-leaps.com
00026 * e-mail:                  info@quantum-leaps.com
00027 *****************************************************************************/
00028 #include "qf_pkg.h"
00029 /* #include "qassert.h" */
00030 
00031 /* Q_DEFINE_THIS_MODULE("qf_tick") */
00032 
00039 /*..........................................................................*/
00040 #ifndef Q_SPY
00041 void QF_tick(void) {                                          /* see NOTE01 */
00042 #else
00043 void QF_tick(void const *sender) {
00044 #endif
00045 
00046     QTimeEvt *t;
00047     QF_CRIT_STAT_
00048 
00049     QF_CRIT_ENTRY_();
00050 
00051     QS_BEGIN_NOCRIT_(QS_QF_TICK, (void *)0, (void *)0)
00052         QS_TEC_((QTimeEvtCtr)(++QS_tickCtr_));          /* the tick counter */
00053     QS_END_NOCRIT_()
00054 
00055     t = QF_timeEvtListHead_;
00056     while (t != (QTimeEvt *)0) {
00057         --t->ctr;
00058         if (t->ctr == (QTimeEvtCtr)0) {     /* is time evt about to expire? */
00059             if (t->interval != (QTimeEvtCtr)0) { /* is it periodic timeout? */
00060                 t->ctr = t->interval;               /* rearm the time event */
00061             }
00062             else { /* one-shot timeout, disarm by removing it from the list */
00063                 if (t == QF_timeEvtListHead_) {
00064                     QF_timeEvtListHead_ = t->next;
00065                 }
00066                 else {
00067                     if (t->next != (QTimeEvt *)0) {  /* not the last event? */
00068                         t->next->prev = t->prev;
00069                     }
00070                     t->prev->next = t->next;
00071                 }
00072                 t->prev = (QTimeEvt *)0;         /* mark the event disarmed */
00073 
00074                 QS_BEGIN_NOCRIT_(QS_QF_TIMEEVT_AUTO_DISARM, QS_teObj_, t)
00075                     QS_OBJ_(t);                   /* this time event object */
00076                     QS_OBJ_(t->act);                   /* the active object */
00077                 QS_END_NOCRIT_()
00078             }
00079 
00080             QS_BEGIN_NOCRIT_(QS_QF_TIMEEVT_POST, QS_teObj_, t)
00081                 QS_TIME_();                                    /* timestamp */
00082                 QS_OBJ_(t);                        /* the time event object */
00083                 QS_SIG_(t->super.sig);         /* signal of this time event */
00084                 QS_OBJ_(t->act);                       /* the active object */
00085             QS_END_NOCRIT_()
00086 
00087             QF_CRIT_EXIT_();/* exit crit. section before calling QF service */
00088 
00089                 /* QACTIVE_POST() asserts internally if the queue overflows */
00090             QACTIVE_POST(t->act, &t->super, sender);
00091         }
00092         else {
00093             static uint8_t volatile dummy;
00094             QF_CRIT_EXIT_();
00095             dummy = (uint8_t)0;   /* execute a few instructions, see NOTE02 */
00096         }
00097 
00098         QF_CRIT_ENTRY_();  /* enter crit. section again to advance the link */
00099         t = t->next;
00100     }
00101     QF_CRIT_EXIT_();
00102 }
00103 
00104 /*****************************************************************************
00105 * NOTE01:
00106 * QF_tick() must always run to completion and never preempt itself.
00107 * In particular, if QF_tick() runs in an ISR, the ISR is not allowed to
00108 * preempt itself. Also, QF_tick() should not be called from two different
00109 * ISRs, which potentially could preempt each other.
00110 *
00111 * NOTE02:
00112 * On many CPUs, the interrupt unlocking takes only effect on the next
00113 * machine instruction, which happens to be here another interrupt lock.
00114 * The assignment of a volatile variable requires a few instructions, which
00115 * the compiler cannot optimize away. This ensures that the interrupts get
00116 * actually unlocked, so that the interrupt latency stays low.
00117 */