bool QMActive::post_(QEvt const * const e, uint_fast16_t const margin, void const * const sender) #endif { uint_fast16_t nFree; bool status; QF_CRIT_STAT_ QF_CRIT_ENTRY_(); nFree = static_cast<uint_fast16_t>(m_eQueue.maxMsg - m_eQueue.nofMsg); if (nFree > margin) { QS_BEGIN_NOCRIT_(QS_QF_ACTIVE_POST_FIFO, QS::priv_.aoObjFilter, this) QS_TIME_(); // timestamp QS_OBJ_(sender); // the sender object QS_SIG_(e->sig); // the signal of the event QS_OBJ_(this); // this active object (recipient) QS_2U8_(e->poolId_, e->refCtr_); // pool Id & ref Count QS_EQC_(static_cast<QEQueueCtr>(nFree)); // # free entries QS_EQC_(static_cast<QEQueueCtr>(0)); // min # free (unknown) QS_END_NOCRIT_() if (e->poolId_ != static_cast<uint8_t>(0)) { // is it a pool event? QF_EVT_REF_CTR_INC_(e); // increment the reference counter } // posting to the embOS mailbox must succeed, see NOTE3 Q_ALLEGE_ID(710, OS_PutMailCond(&m_eQueue, static_cast<OS_CONST_PTR void *>(&e)) == static_cast<char>(0)); status = true; // return success }
bool GuiQMActive::post_(QEvt const * const e, uint_fast16_t const /*margin*/, void const * const sender) #endif { QF_CRIT_STAT_ QF_CRIT_ENTRY_(); QS_BEGIN_NOCRIT_(QS_QF_ACTIVE_POST_FIFO, QS::priv_.aoObjFilter, this) QS_TIME_(); // timestamp QS_OBJ_(sender); // the sender object QS_SIG_(e->sig); // the signal of the event QS_OBJ_(this); // this active object QS_2U8_(QF_EVT_POOL_ID_(e), /* the poolID of the event */ QF_EVT_REF_CTR_(e)); // the ref Ctr of the event QS_EQC_(0); // number of free entries (not used) QS_EQC_(0); // min number of free entries (not used) QS_END_NOCRIT_() // is it a dynamic event? if (QF_EVT_POOL_ID_(e) != static_cast<uint8_t>(0)) { QF_EVT_REF_CTR_INC_(e); // increment the reference counter } QF_CRIT_EXIT_(); // QCoreApplication::postEvent() is thread-safe per Qt documentation QCoreApplication::postEvent(QApplication::instance(), new QP_Event(e)); return true; }
bool QActive_post_(QActive * const me, QEvt const * const e, uint_fast16_t const margin, void const * const sender) #endif /* Q_SPY */ { uint_fast16_t nFree; bool status; QF_CRIT_STAT_ QF_CRIT_ENTRY_(); nFree = (uint_fast16_t)(me->eQueue.maxMsg - me->eQueue.nofMsg); if (nFree > margin) { QS_BEGIN_NOCRIT_(QS_QF_ACTIVE_POST_FIFO, QS_priv_.aoObjFilter, me) QS_TIME_(); /* timestamp */ QS_OBJ_(sender); /* the sender object */ QS_SIG_(e->sig); /* the signal of the event */ QS_OBJ_(me); /* this active object (recipient) */ QS_2U8_(e->poolId_, e->refCtr_); /* pool Id & ref Count */ QS_EQC_((QEQueueCtr)nFree); /* # free entries available */ QS_EQC_((QEQueueCtr)0); /* min # free entries (unknown) */ QS_END_NOCRIT_() if (e->poolId_ != (uint8_t)0) { /* is it a pool event? */ QF_EVT_REF_CTR_INC_(e); /* increment the reference counter */ } /* posting to the embOS mailbox must succeed, see NOTE3 */ Q_ALLEGE(OS_PutMailCond(&me->eQueue, (OS_CONST_PTR void *)&e) == (char)0); status = true; /* return success */ }
/*..........................................................................*/ QEvt const *QActive_get_(QActive * const me) { QEQueueCtr nFree; QEvt const *e; QF_CRIT_STAT_ QF_CRIT_ENTRY_(); QACTIVE_EQUEUE_WAIT_(me); /* wait for event to arrive directly */ e = me->eQueue.frontEvt; /* always remove event from the front location */ nFree= me->eQueue.nFree + (QEQueueCtr)1; /* get volatile into tmp */ me->eQueue.nFree = nFree; /* upate the number of free */ if (nFree <= me->eQueue.end) { /* any events in the ring buffer? */ /* remove event from the tail */ me->eQueue.frontEvt = QF_PTR_AT_(me->eQueue.ring, me->eQueue.tail); if (me->eQueue.tail == (QEQueueCtr)0) { /* need to wrap the tail? */ me->eQueue.tail = me->eQueue.end; /* wrap around */ } --me->eQueue.tail; QS_BEGIN_NOCRIT_(QS_QF_ACTIVE_GET, QS_priv_.aoObjFilter, me) QS_TIME_(); /* timestamp */ QS_SIG_(e->sig); /* the signal of this event */ QS_OBJ_(me); /* this active object */ QS_2U8_(e->poolId_, e->refCtr_); /* pool Id & ref Count */ QS_EQC_(nFree); /* number of free entries */ QS_END_NOCRIT_() }
//**************************************************************************** /// @description /// This function implements a simple garbage collector for dynamic events. /// Only dynamic events are candidates for recycling. (A dynamic event is one /// that is allocated from an event pool, which is determined as non-zero /// e->poolId_ attribute.) Next, the function decrements the reference counter /// of the event (e->refCtr_), and recycles the event only if the counter /// drops to zero (meaning that no more references are outstanding for this /// event). The dynamic event is recycled by returning it to the pool from /// which it was originally allocated. /// /// @param[in] e pointer to the event to recycle /// /// @note /// QF invokes the garbage collector at all appropriate contexts, when /// an event can become garbage (automatic garbage collection), so the /// application code should have no need to call QP::QF::gc() directly. /// The QP::QF::gc() function is exposed only for special cases when your /// application sends dynamic events to the "raw" thread-safe queues /// (see QP::QEQueue). Such queues are processed outside of QF and the /// automatic garbage collection is **NOT** performed for these events. /// In this case you need to call QP::QF::gc() explicitly. /// void QF::gc(QEvt const * const e) { // is it a dynamic event? if (QF_EVT_POOL_ID_(e) != static_cast<uint8_t>(0)) { QF_CRIT_STAT_ QF_CRIT_ENTRY_(); // isn't this the last reference? if (e->refCtr_ > static_cast<uint8_t>(1)) { QF_EVT_REF_CTR_DEC_(e); // decrement the ref counter QS_BEGIN_NOCRIT_(QS_QF_GC_ATTEMPT, static_cast<void *>(0), static_cast<void *>(0)) QS_TIME_(); // timestamp QS_SIG_(e->sig); // the signal of the event QS_2U8_(e->poolId_, e->refCtr_);// pool Id & refCtr of the evt QS_END_NOCRIT_() QF_CRIT_EXIT_(); } // this is the last reference to this event, recycle it else { uint_fast8_t idx = static_cast<uint_fast8_t>(e->poolId_) - static_cast<uint_fast8_t>(1); QS_BEGIN_NOCRIT_(QS_QF_GC, static_cast<void *>(0), static_cast<void *>(0)) QS_TIME_(); // timestamp QS_SIG_(e->sig); // the signal of the event QS_2U8_(e->poolId_, e->refCtr_);// pool Id & refCtr of the evt QS_END_NOCRIT_() QF_CRIT_EXIT_(); // pool ID must be in range Q_ASSERT_ID(410, idx < QF_maxPool_); #ifdef Q_EVT_VIRTUAL // explicitly exectute the destructor' // NOTE: casting 'const' away is legitimate, // because it's a pool event QF_EVT_CONST_CAST_(e)->~QEvt(); // xtor, #endif // cast 'const' away, which is OK, because it's a pool event QF_EPOOL_PUT_(QF_pool_[idx], QF_EVT_CONST_CAST_(e)); } } }
void QF_publish_(QEvt const * const e, void const * const sender) #endif { QF_CRIT_STAT_ /* make sure that the published signal is within the configured range */ Q_REQUIRE(e->sig < (QSignal)QF_maxSignal_); QF_CRIT_ENTRY_(); QS_BEGIN_NOCRIT_(QS_QF_PUBLISH, (void *)0, (void *)0) QS_TIME_(); /* the timestamp */ QS_OBJ_(sender); /* the sender object */ QS_SIG_(e->sig); /* the signal of the event */ QS_2U8_(e->poolId_, e->refCtr_);/* pool Id & ref Count of the event */ QS_END_NOCRIT_() if (e->poolId_ != (uint8_t)0) { /* is it a dynamic event? */ QF_EVT_REF_CTR_INC_(e); /* increment reference counter, NOTE01 */ } QF_CRIT_EXIT_(); #if (QF_MAX_ACTIVE <= 8) { uint8_t tmp = QF_subscrList_[e->sig].bits[0]; while (tmp != (uint8_t)0) { uint8_t p = QF_LOG2(tmp); tmp &= Q_ROM_BYTE(QF_invPwr2Lkup[p]); /* clear subscriber bit */ Q_ASSERT(QF_active_[p] != (QActive *)0); /* must be registered */ /* QACTIVE_POST() asserts internally if the queue overflows */ QACTIVE_POST(QF_active_[p], e, sender); } } #else { uint_t i = (uint_t)Q_DIM(QF_subscrList_[0].bits); do { /* go through all bytes in the subscription list */ uint8_t tmp; --i; tmp = QF_PTR_AT_(QF_subscrList_, e->sig).bits[i]; while (tmp != (uint8_t)0) { uint8_t p = QF_LOG2(tmp); tmp &= Q_ROM_BYTE(QF_invPwr2Lkup[p]);/*clear subscriber bit */ p = (uint8_t)(p + (uint8_t)(i << 3));/* adjust the priority */ Q_ASSERT(QF_active_[p] != (QActive *)0);/*must be registered*/ /* QACTIVE_POST() asserts internally if the queue overflows */ QACTIVE_POST(QF_active_[p], e, sender); } } while (i != (uint_t)0); } #endif QF_gc(e); /* run the garbage collector, see NOTE01 */ }
//**************************************************************************** //! obtain a message from the private message queue (block if no messages) void const *QXThread::queueGet(uint_fast16_t const nTicks, uint_fast8_t const tickRate) { QEQueueCtr nFree; QEvt const *e; QF_CRIT_STAT_ QF_CRIT_ENTRY_(); QXThread *thr = static_cast<QXThread *>(QXK_attr_.curr); Q_REQUIRE_ID(900, (!QXK_ISR_CONTEXT_()) /* can't block inside an ISR */ /* this must be a "naked" thread (no state) */ && (thr->m_state.act == (QActionHandler)0)); // is the queue empty? -- block and wait for event(s) if (thr->m_eQueue.m_frontEvt == static_cast<QEvt *>(0)) { thr->m_temp.obj = reinterpret_cast<QMState const *>(&thr->m_eQueue); thr->teArm_(static_cast<enum_t>(QXK_QUEUE_SIG), nTicks, tickRate); QXK_attr_.readySet.remove(thr->m_prio); QXK_sched_(); QF_CRIT_EXIT_(); QF_CRIT_EXIT_NOP(); QF_CRIT_ENTRY_(); } // is the queue not empty? if (thr->m_eQueue.m_frontEvt != static_cast<QEvt *>(0)) { e = thr->m_eQueue.m_frontEvt; // always remove from the front // volatile into tmp nFree= thr->m_eQueue.m_nFree + static_cast<QEQueueCtr>(1); thr->m_eQueue.m_nFree = nFree; // update the number of free // any events in the ring buffer? if (nFree <= thr->m_eQueue.m_end) { // remove event from the tail thr->m_eQueue.m_frontEvt = QF_PTR_AT_(thr->m_eQueue.m_ring, thr->m_eQueue.m_tail); if (thr->m_eQueue.m_tail == static_cast<QEQueueCtr>(0)) { thr->m_eQueue.m_tail = thr->m_eQueue.m_end; // wrap } --thr->m_eQueue.m_tail; QS_BEGIN_NOCRIT_(QP::QS_QF_ACTIVE_GET, QP::QS::priv_.aoObjFilter, thr) QS_TIME_(); // timestamp QS_SIG_(e->sig); // the signal of this event QS_OBJ_(&thr); // this active object QS_2U8_(e->poolId_, e->refCtr_); // pool Id & ref Count QS_EQC_(nFree); // number of free entries QS_END_NOCRIT_() }
/*..........................................................................*/ uint8_t QEQueue_post(QEQueue * const me, QEvt const * const e, uint16_t const margin) { QEQueueCtr nFree; /* temporary to avoid UB for volatile access */ uint8_t status; QF_CRIT_STAT_ Q_REQUIRE(e != (QEvt const *)0); /* event must be valid */ QF_CRIT_ENTRY_(); nFree = me->nFree; /* get volatile into the temporary */ if (nFree > (QEQueueCtr)margin) { /* required margin available? */ QS_BEGIN_NOCRIT_(QS_QF_EQUEUE_POST_FIFO, QS_priv_.eqObjFilter, me) QS_TIME_(); /* timestamp */ QS_SIG_(e->sig); /* the signal of this event */ QS_OBJ_(me); /* this queue object */ QS_2U8_(e->poolId_, e->refCtr_); /* pool Id & ref Count */ QS_EQC_(nFree); /* number of free entries */ QS_EQC_(me->nMin); /* min number of free entries */ QS_END_NOCRIT_() if (e->poolId_ != (uint8_t)0) { /* is it a pool event? */ QF_EVT_REF_CTR_INC_(e); /* increment the reference counter */ } --nFree; /* one free entry just used up */ me->nFree = nFree; /* update the volatile */ if (me->nMin > nFree) { me->nMin = nFree; /* update minimum so far */ } if (me->frontEvt == (QEvt const *)0) { /* was the queue empty? */ me->frontEvt = e; /* deliver event directly */ } else { /* queue was not empty, insert event into the ring-buffer */ /* insert event into the ring buffer (FIFO) */ QF_PTR_AT_(me->ring, me->head) = e; /* insert e into buffer */ if (me->head == (QEQueueCtr)0) { /* need to wrap the head? */ me->head = me->end; /* wrap around */ } --me->head; } status = (uint8_t)1; /* event posted successfully */ }
//............................................................................ void QActive::postLIFO(QEvt const * const e) { QF_CRIT_STAT_ QF_CRIT_ENTRY_(); QEQueueCtr nFree = m_eQueue.m_nFree;// tmp to avoid UB for volatile access // the queue must be able to accept the event (cannot overflow) Q_ASSERT(nFree != static_cast<QEQueueCtr>(0)); QS_BEGIN_NOCRIT_(QS_QF_ACTIVE_POST_LIFO, QS::priv_.aoObjFilter, this) QS_TIME_(); // timestamp QS_SIG_(e->sig); // the signal of this event QS_OBJ_(this); // this active object QS_2U8_(e->poolId_, e->refCtr_); // pool Id & refCtr of the evt QS_EQC_(nFree); // number of free entries QS_EQC_(m_eQueue.m_nMin); // min number of free entries QS_END_NOCRIT_() if (e->poolId_ != u8_0) { // is it a dynamic event? QF_EVT_REF_CTR_INC_(e); // increment the reference counter } --nFree; // one free entry just used up m_eQueue.m_nFree = nFree; // update the volatile if (m_eQueue.m_nMin > nFree) { m_eQueue.m_nMin = nFree; // update minimum so far } QEvt const *frontEvt = m_eQueue.m_frontEvt;// read volatile into temporary m_eQueue.m_frontEvt = e; // deliver the event directly to the front if (frontEvt == null_evt) { // is the queue empty? QACTIVE_EQUEUE_SIGNAL_(this); // signal the event queue } else { // queue is not empty, leave event in the ring-buffer ++m_eQueue.m_tail; if (m_eQueue.m_tail == m_eQueue.m_end) { // need to wrap the tail? m_eQueue.m_tail = static_cast<QEQueueCtr>(0); // wrap around } QF_PTR_AT_(m_eQueue.m_ring, m_eQueue.m_tail) = frontEvt; } QF_CRIT_EXIT_(); }
void QF::publish_(QEvt const * const e) { #else void QF::publish_(QEvt const * const e, void const * const sender) { #endif /// @pre the published signal must be within the configured range Q_REQUIRE_ID(100, static_cast<enum_t>(e->sig) < QF_maxSignal_); QF_CRIT_STAT_ QF_CRIT_ENTRY_(); QS_BEGIN_NOCRIT_(QS_QF_PUBLISH, static_cast<void *>(0), static_cast<void *>(0)) QS_TIME_(); // the timestamp QS_OBJ_(sender); // the sender object QS_SIG_(e->sig); // the signal of the event QS_2U8_(e->poolId_, e->refCtr_); // pool Id & refCtr of the evt QS_END_NOCRIT_() // is it a dynamic event? if (e->poolId_ != static_cast<uint8_t>(0)) { QF_EVT_REF_CTR_INC_(e); // increment the reference counter, NOTE01 } QF_CRIT_EXIT_(); QF_SCHED_STAT_TYPE_ lockStat; lockStat.m_lockPrio = static_cast<uint_fast8_t>(0xFF); // uninitialized #if (QF_MAX_ACTIVE <= 8) uint_fast8_t tmp = static_cast<uint_fast8_t>( QF_PTR_AT_(QF_subscrList_, e->sig).m_bits[0]); while (tmp != static_cast<uint8_t>(0)) { uint_fast8_t p = static_cast<uint_fast8_t>(QF_LOG2(tmp)); // clear the subscriber bit tmp &= static_cast<uint_fast8_t>(QF_invPwr2Lkup[p]); // has the scheduler been locked yet? if (lockStat.m_lockPrio == static_cast<uint_fast8_t>(0xFF)) { QF_SCHED_LOCK_(&lockStat, p); } // the priority of the AO must be registered with the framework Q_ASSERT_ID(110, active_[p] != static_cast<QMActive *>(0)); // POST() asserts internally if the queue overflows (void)active_[p]->POST(e, sender); } #else uint_fast8_t i = static_cast<uint_fast8_t>(QF_SUBSCR_LIST_SIZE); // go through all bytes in the subscription list do { --i; uint_fast8_t tmp = static_cast<uint_fast8_t>( QF_PTR_AT_(QF_subscrList_, e->sig).m_bits[i]); while (tmp != static_cast<uint_fast8_t>(0)) { uint_fast8_t p = static_cast<uint_fast8_t>(QF_LOG2(tmp)); // clear the subscriber bit tmp &= static_cast<uint_fast8_t>(QF_invPwr2Lkup[p]); // adjust the priority p += static_cast<uint_fast8_t>(i << 3); // has the scheduler been locked yet? if (lockStat.m_lockPrio == static_cast<uint_fast8_t>(0xFF)) { QF_SCHED_LOCK_(&lockStat, p); } // the priority level be registered with the framework Q_ASSERT(active_[p] != static_cast<QMActive *>(0)); // POST() asserts internally if the queue overflows (void)active_[p]->POST(e, sender); } } while (i != static_cast<uint_fast8_t>(0)); #endif // was the scheduler locked? if (lockStat.m_lockPrio <= static_cast<uint_fast8_t>(QF_MAX_ACTIVE)) { QF_SCHED_UNLOCK_(&lockStat); // unlock the scheduler } // run the garbage collector gc(e); // NOTE: QP::QF::publish_() increments the reference counter to prevent // premature recycling of the event while the multicasting is still // in progress. At the end of the function, the garbage collector step // decrements the reference counter and recycles the event if the // counter drops to zero. This covers the case when the event was // published without any subscribers. }