📄 table.cpp
字号:
//////////////////////////////////////////////////////////////////////////////// Product: QDPP example// Last Updated for Version: 3.2.04// Date of the Last Update: Nov 23, 2006//// Q u a n t u m L e a P s// ---------------------------// innovating embedded systems//// Copyright (C) 2002-2006 Quantum Leaps, LLC. All rights reserved.//// This software may be distributed and modified under the terms of the GNU// General Public License version 2 (GPL) as published by the Free Software// Foundation and appearing in the file GPL.TXT included in the packaging of// this file. Please note that GPL Section 2[b] requires that all works based// on this software must also be made publicly available under the terms of// the GPL ("Copyleft").//// Alternatively, this software may be distributed and modified under the// terms of Quantum Leaps commercial licenses, which expressly supersede// the GPL and are specifically designed for licensees interested in// retaining the proprietary status of their code.//// Contact information:// Quantum Leaps Web site: http://www.quantum-leaps.com// e-mail: sales@quantum-leaps.com//////////////////////////////////////////////////////////////////////////////#include "qf_port.h"#include "qassert.h"#include "qdpp.h"#include <stdlib.h>Q_DEFINE_THIS_FILE// Public-scope objects ------------------------------------------------------QActive *QDPP_table;// local objects -------------------------------------------------------------class Table : public QActive {private: uint8_t fork_[N]; uint8_t isHungry_[N];public: Table();protected: static void initial(Table *me, QEvent const *e); static QSTATE serving(Table *me, QEvent const *e); #define RIGHT(n_) ((uint8_t)(((n_) + (N - 1)) % N)) #define LEFT(n_) ((uint8_t)(((n_) + 1) % N)) enum { FREE = 0, USED = 1 };private: friend void tableStart(uint8_t prio, QEvent const *qSto[], uint32_t qLen);};//............................................................................void tableStart(uint8_t prio, QEvent const *qSto[], uint32_t qLen){ static Table table; QDPP_table = &table; // initialize the global table.start(prio, qSto, qLen, (void *)0, // no thread-local storage 0, // this thread does not use the FPU (QEvent *)0);}//............................................................................Table::Table() : QActive((QState)&Table::initial) { QDPP_table = this; // initialize the global}//............................................................................void Table::initial(Table *me, QEvent const *) { uint8_t n; me->subscribe(HUNGRY_SIG); me->subscribe(DONE_SIG); me->subscribe(TERMINATE_SIG); for (n = 0; n < N; ++n) { me->fork_[n] = FREE; me->isHungry_[n] = 0; } Q_INIT(&Table::serving);}//............................................................................QSTATE Table::serving(Table *me, QEvent const *e) { uint8_t n, m; TableEvt *pe; switch (e->sig) { case HUNGRY_SIG: { busyDelay(); n = ((TableEvt *)e)->philNum; Q_ASSERT(n < N && !me->isHungry_[n]); displyPhilStat(n, "hungry "); m = LEFT(n); if (me->fork_[m] == FREE && me->fork_[n] == FREE) { me->fork_[m] = me->fork_[n] = USED; pe = Q_NEW(TableEvt, EAT_SIG); pe->philNum = n; QF::publish(pe); displyPhilStat(n, "eating "); } else { me->isHungry_[n] = 1; } return 0; } case DONE_SIG: { busyDelay(); n = ((TableEvt *)e)->philNum; Q_ASSERT(n < N); displyPhilStat(n, "thinking"); me->fork_[LEFT(n)] = me->fork_[n] = FREE; m = RIGHT(n); // check the right neighbor if (me->isHungry_[m] && me->fork_[m] == FREE) { me->fork_[n] = me->fork_[m] = USED; me->isHungry_[m] = 0; pe = Q_NEW(TableEvt, EAT_SIG); pe->philNum = m; QF::publish(pe); displyPhilStat(m, "eating "); } m = LEFT(n); // check the left neighbor n = LEFT(m); if (me->isHungry_[m] && me->fork_[n] == FREE) { me->fork_[m] = me->fork_[n] = USED; me->isHungry_[m] = 0; pe = Q_NEW(TableEvt, EAT_SIG); pe->philNum = m; QF::publish(pe); displyPhilStat(m, "eating "); } return 0; } case TERMINATE_SIG: { QF::exit(); return 0; } } return (QSTATE)&QHsm::top;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -