⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 philo.c

📁 量子编程源代码 量子编程源代码
💻 C
字号:
/****************************************************************************** Product: QDPP example, DOS, Large model, Turbo C++ 1.01* Last Updated for Version: 3.2.04* Date of the Last Update:  Nov 30, 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_FILEtypedef struct PhilosopherTag Philosopher;struct PhilosopherTag {    QActive super_;    uint8_t num__;                            /* number of this philosopher */    QTimeEvt timeEvt__;                     /* to timeout thining or eating */};void Philosopher_ctor(Philosopher *me);void Philosopher_initial(Philosopher *me, QEvent const *e);QSTATE Philosopher_thinking(Philosopher *me, QEvent const *e);QSTATE Philosopher_hungry(Philosopher *me, QEvent const *e);QSTATE Philosopher_eating(Philosopher *me, QEvent const *e);enum {    THINK_TIME = 7,    EAT_TIME = 5,    /* internal events (not published to the QF) */    TIMEOUT_SIG = MAX_PUB_SIG};/*..........................................................................*/void philosopherStart(uint8_t n, uint8_t prio,                      QEvent const *qSto[], uint32_t qLen){    static Philosopher philo[N];    TableEvt ie;                                    /* initialization event */    Q_REQUIRE(n < N);    Philosopher_ctor(&philo[n]);                      /* explicit Ctor call */    ie.philNum = n;    QActive_start((QActive *)&philo[n], prio,                  qSto, qLen,                  (void *)0,                     /* no thread-local storage */                  QK_FPU_THREAD,               /* this thread uses FPU, say */                  (QEvent *)&ie);}/*..........................................................................*/void Philosopher_ctor(Philosopher *me) {    QActive_ctor_(&me->super_, (QState)&Philosopher_initial);    QTimeEvt_ctor(&me->timeEvt__, TIMEOUT_SIG);}/*..........................................................................*/void Philosopher_initial(Philosopher *me, QEvent const *e) {    me->num__ = ((TableEvt const *)e)->philNum;    QActive_subscribe_((QActive *)me, EAT_SIG);    Q_INIT(&Philosopher_thinking);}/*..........................................................................*/QSTATE Philosopher_thinking(Philosopher *me, QEvent const *e) {    switch (e->sig) {        case Q_ENTRY_SIG: {            QTimeEvt_postIn(&me->timeEvt__, (QActive *)me, THINK_TIME);            return 0;        }        case TIMEOUT_SIG: {            busyDelay();            Q_TRAN(&Philosopher_hungry);            return 0;        }    }    return (QSTATE)&QHsm_top;}/*..........................................................................*/QSTATE Philosopher_hungry(Philosopher *me, QEvent const *e) {    TableEvt *pe;    switch (e->sig) {        case Q_ENTRY_SIG: {            pe = Q_NEW(TableEvt, HUNGRY_SIG);            pe->philNum = me->num__;            QActive_postFIFO(QDPP_table, (QEvent *)pe);            return 0;        }        case EAT_SIG: {            if (((TableEvt *)e)->philNum == me->num__) {                busyDelay();                Q_TRAN(&Philosopher_eating);            }            return 0;        }    }    return (QSTATE)&QHsm_top;}/*..........................................................................*/QSTATE Philosopher_eating(Philosopher *me, QEvent const *e) {    TableEvt *pe;    switch (e->sig) {        case Q_ENTRY_SIG: {            QTimeEvt_postIn(&me->timeEvt__, (QActive *)me, EAT_TIME);            return 0;        }        case TIMEOUT_SIG: {            Q_TRAN(&Philosopher_thinking);            return 0;        }        case Q_EXIT_SIG: {            busyDelay();            pe = Q_NEW(TableEvt, DONE_SIG);            pe->philNum = me->num__;            QF_publish((QEvent *)pe);            return 0;        }    }    return (QSTATE)&QHsm_top;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -