qf_tick.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002 * Product: QF/C
00003 * Last Updated for Version: 4.0.00
00004 * Date of the Last Update:  Apr 07, 2008
00005 *
00006 *                    Q u a n t u m     L e a P s
00007 *                    ---------------------------
00008 *                    innovating embedded systems
00009 *
00010 * Copyright (C) 2002-2008 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 void QF_tick(void) {                                          /* see NOTE01 */
00041     QTimeEvt *t;
00042     QF_INT_LOCK_KEY_
00043 
00044     QF_INT_LOCK_();
00045 
00046     QS_BEGIN_NOLOCK_(QS_QF_TICK, (void *)0, (void *)0)
00047         QS_TEC_(++QS_tickCtr_);                         /* the tick counter */
00048     QS_END_NOLOCK_()
00049 
00050     t = QF_timeEvtListHead_;
00051     while (t != (QTimeEvt *)0) {
00052         if (--t->ctr == (QTimeEvtCtr)0) {   /* is time evt about to expire? */
00053             if (t->interval != (QTimeEvtCtr)0) { /* is it periodic timeout? */
00054                 t->ctr = t->interval;               /* rearm the time event */
00055             }
00056             else { /* one-shot timeout, disarm by removing it from the list */
00057                 if (t == QF_timeEvtListHead_) {
00058                     QF_timeEvtListHead_ = t->next;
00059                 }
00060                 else {
00061                     if (t->next != (QTimeEvt *)0) {  /* not the last event? */
00062                         t->next->prev = t->prev;
00063                     }
00064                     t->prev->next = t->next;
00065                 }
00066                 t->prev = (QTimeEvt *)0;         /* mark the event disarmed */
00067 
00068                 QS_BEGIN_NOLOCK_(QS_QF_TIMEEVT_AUTO_DISARM, QS_teObj_, t)
00069                     QS_OBJ_(t);                   /* this time event object */
00070                     QS_OBJ_(t->act);                   /* the active object */
00071                 QS_END_NOLOCK_()
00072             }
00073 
00074             QS_BEGIN_NOLOCK_(QS_QF_TIMEEVT_POST, QS_teObj_, t)
00075                 QS_TIME_();                                    /* timestamp */
00076                 QS_OBJ_(t);                        /* the time event object */
00077                 QS_SIG_(t->super.sig);         /* signal of this time event */
00078                 QS_OBJ_(t->act);                       /* the active object */
00079             QS_END_NOLOCK_()
00080 
00081             QF_INT_UNLOCK_();/* unlock interrupts before calling QF service */
00082 
00083                /* postFIFO() asserts internally that the event was accepted */
00084             QActive_postFIFO(t->act, (QEvent *)t);
00085         }
00086         else {
00087             static uint8_t volatile dummy;
00088             QF_INT_UNLOCK_();
00089             dummy = (uint8_t)0;   /* execute a few instructions, see NOTE02 */
00090         }
00091 
00092         QF_INT_LOCK_();        /* lock interrupts again to advance the link */
00093         t = t->next;
00094     }
00095     QF_INT_UNLOCK_();
00096 }
00097 
00098 /*****************************************************************************
00099 * NOTE01:
00100 * QF_tick() must always run to completion and never preempt itself.
00101 * In particular, if QF_tick() runs in an ISR, the ISR is not allowed to
00102 * preempt itself. Also, QF_tick() should not be called from two different
00103 * ISRs, which potentially could preempt each other.
00104 *
00105 * NOTE02:
00106 * On many CPUs, the interrupt unlocking takes only effect on the next
00107 * machine instruction, which happens to be here another interrupt lock.
00108 * The assignment of a volatile variable requires a few instructions, which
00109 * the compiler cannot optimize away. This ensures that the interrupts get
00110 * actually unlocked, so that the interrupt latency stays low.
00111 */

Generated on Fri Aug 1 14:15:07 2008 for QP/C by  doxygen 1.5.4