QP/C
qf_pspub.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * Product: QF/C
00003 * Last Updated for Version: 4.5.04
00004 * Date of the Last Update:  Feb 04, 2013
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 "qf_pkg.h"
00036 #include "qassert.h"
00037 
00038 Q_DEFINE_THIS_MODULE("qf_pspub")
00039 
00040 
00046 /*..........................................................................*/
00047 #ifndef Q_SPY
00048 void QF_publish(QEvt const * const e)
00049 #else
00050 void QF_publish(QEvt const * const e, void const * const sender)
00051 #endif
00052 {
00053     QF_CRIT_STAT_
00054 
00055       /* make sure that the published signal is within the configured range */
00056     Q_REQUIRE(e->sig < (QSignal)QF_maxSignal_);
00057 
00058     QF_CRIT_ENTRY_();
00059 
00060     QS_BEGIN_NOCRIT_(QS_QF_PUBLISH, (void *)0, (void *)0)
00061         QS_TIME_();                                        /* the timestamp */
00062         QS_OBJ_(sender);                               /* the sender object */
00063         QS_SIG_(e->sig);                         /* the signal of the event */
00064         QS_U8_(QF_EVT_POOL_ID_(e));             /* the pool Id of the event */
00065         QS_U8_(QF_EVT_REF_CTR_(e));           /* the ref count of the event */
00066     QS_END_NOCRIT_()
00067 
00068     if (QF_EVT_POOL_ID_(e) != (uint8_t)0) {       /* is it a dynamic event? */
00069         QF_EVT_REF_CTR_INC_(e);      /* increment reference counter, NOTE01 */
00070     }
00071     QF_CRIT_EXIT_();
00072 
00073 #if (QF_MAX_ACTIVE <= 8)
00074     {
00075         uint8_t tmp = QF_subscrList_[e->sig].bits[0];
00076         while (tmp != (uint8_t)0) {
00077             uint8_t p = QF_LOG2(tmp);
00078             tmp &= Q_ROM_BYTE(QF_invPwr2Lkup[p]);   /* clear subscriber bit */
00079             Q_ASSERT(QF_active_[p] != (QActive *)0);  /* must be registered */
00080 
00081                 /* QACTIVE_POST() asserts internally if the queue overflows */
00082             QACTIVE_POST(QF_active_[p], e, sender);
00083         }
00084     }
00085 #else
00086     {
00087         uint8_t i = (uint8_t)Q_DIM(QF_subscrList_[0].bits);
00088         do {               /* go through all bytes in the subscription list */
00089             uint8_t tmp;
00090             --i;
00091             tmp = QF_PTR_AT_(QF_subscrList_, e->sig).bits[i];
00092             while (tmp != (uint8_t)0) {
00093                 uint8_t p = QF_LOG2(tmp);
00094                 tmp &= Q_ROM_BYTE(QF_invPwr2Lkup[p]);/*clear subscriber bit */
00095                 p = (uint8_t)(p + (uint8_t)(i << 3));/* adjust the priority */
00096                 Q_ASSERT(QF_active_[p] != (QActive *)0);/*must be registered*/
00097 
00098                 /* QACTIVE_POST() asserts internally if the queue overflows */
00099                 QACTIVE_POST(QF_active_[p], e, sender);
00100             }
00101         } while (i != (uint8_t)0);
00102     }
00103 #endif
00104 
00105     QF_gc(e);                      /* run the garbage collector, see NOTE01 */
00106 }
00107 
00108 /*****************************************************************************
00109 * NOTE01:
00110 * QF_publish() increments the reference counter to prevent premature
00111 * recycling of the event while the multicasting is still in progress.
00112 * At the end of the function, the garbage collector step decrements the
00113 * reference counter and recycles the event if the counter drops to zero.
00114 * This covers the case when the event was published without any subscribers.
00115 */