|
| 1 | +capsule Phil { |
| 2 | + [[rt::header_preface]] |
| 3 | + ` |
| 4 | + #include <cstring> |
| 5 | + #include <cstdio> |
| 6 | + #include <cstdlib> |
| 7 | + #include <time.h> |
| 8 | + #include <iostream> |
| 9 | + #include "ForkProt.h" |
| 10 | +` |
| 11 | + [[rt::decl]] |
| 12 | + ` |
| 13 | + public: |
| 14 | + enum PickUpStrategy { |
| 15 | + RANDOM, LEFTFIRST, RIGHTFIRST |
| 16 | + }; |
| 17 | + ` |
| 18 | + [[rt::decl]] |
| 19 | + ` |
| 20 | + PickUpStrategy pickUpStrat = PickUpStrategy::RANDOM; |
| 21 | + const char * myName; |
| 22 | + ` |
| 23 | + service behavior port rightP : ForkProt; |
| 24 | + service behavior port leftP : ForkProt; |
| 25 | + behavior port timerP : Timing; |
| 26 | + behavior port logP : Log; |
| 27 | + statemachine { |
| 28 | + state Thinking { |
| 29 | + entry |
| 30 | + ` |
| 31 | + std::cout << myName << " thinking " << std::flush << std::endl; |
| 32 | + timerP.informIn(RTTimespec(3, 100000000)); |
| 33 | + `; |
| 34 | + }, Wait4Right, Wait4Left, Wait4First, GoLeftWait4Right, GoRightWait4Right, Eating { |
| 35 | + entry |
| 36 | + ` |
| 37 | + std::cout << " " << myName << " eating " << std::flush << std::endl; |
| 38 | + int rnd = rand() % 10; |
| 39 | + timerP.informIn(RTTimespec(6,rnd*10000000)); |
| 40 | + `; |
| 41 | + }; |
| 42 | + choice _junction; |
| 43 | + _initial: initial -> Thinking |
| 44 | + ` |
| 45 | + // what is my name? |
| 46 | + myName = getName(); |
| 47 | + std::cout << myName << " starting up " << std::flush << std::endl; |
| 48 | + std::cout << myName << " using" << std::flush << std::endl; |
| 49 | + |
| 50 | + if (pickUpStrat==PickUpStrategy::RANDOM) |
| 51 | + std::cout << myName << " random" << std::flush << std::endl; |
| 52 | + else if (pickUpStrat==PickUpStrategy::LEFTFIRST) |
| 53 | + std::cout << myName << " left-first" << std::flush << std::endl; |
| 54 | + else if (pickUpStrat==PickUpStrategy::RIGHTFIRST) |
| 55 | + std::cout << myName << " right-first" << std::flush << std::endl; |
| 56 | + |
| 57 | + |
| 58 | + // initialize random number generator |
| 59 | + srand(time(NULL)); |
| 60 | + `; |
| 61 | + gotTimeout: Thinking -> _junction on timerP.timeout; |
| 62 | + left_first: _junction -> Wait4Left when |
| 63 | + ` |
| 64 | + return (pickUpStrat==PickUpStrategy::LEFTFIRST);` |
| 65 | + ` |
| 66 | + leftP.up().send(); |
| 67 | + `; |
| 68 | + right_first: _junction -> Wait4Right when |
| 69 | + ` |
| 70 | + return (pickUpStrat==PickUpStrategy::RIGHTFIRST);` |
| 71 | + ` |
| 72 | + rightP.up().send(); |
| 73 | + `; |
| 74 | + random: _junction -> Wait4First when |
| 75 | + ` |
| 76 | + return (pickUpStrat==PickUpStrategy::RANDOM); |
| 77 | + ` |
| 78 | + ` |
| 79 | + int rnd = rand() % 100; |
| 80 | + if (rnd>50) |
| 81 | + leftP.up().send(); |
| 82 | + else |
| 83 | + rightP.up().send(); |
| 84 | + `; |
| 85 | + random_else: _junction -> Wait4First when `else`; |
| 86 | + gotLeft1: Wait4Left -> GoLeftWait4Right on leftP.ack |
| 87 | + ` |
| 88 | + std::cout << " " << myName << " got left " << std::flush << std::endl; |
| 89 | + rightP.up().send(); |
| 90 | + `; |
| 91 | + gotLeft2: Wait4First -> GoLeftWait4Right on leftP.ack |
| 92 | + ` |
| 93 | + std::cout << " " << myName << " got left " << std::flush << std::endl; |
| 94 | + rightP.up().send(); |
| 95 | + `; |
| 96 | + gotRight1: Wait4Right -> GoRightWait4Right on rightP.ack |
| 97 | + ` |
| 98 | + std::cout << " " << myName << " got right " << std::flush << std::endl; |
| 99 | + leftP.up().send(); |
| 100 | + `; |
| 101 | + gotRight2: Wait4First -> GoRightWait4Right on rightP.ack |
| 102 | + ` |
| 103 | + std::cout << " " << myName << " got right " << std::flush << std::endl; |
| 104 | + leftP.up().send(); |
| 105 | + `; |
| 106 | + gotLeft3: GoRightWait4Right -> Eating on leftP.ack |
| 107 | + ` |
| 108 | + // std::cout << " " << myName << " to eating " << std::flush << std::endl; |
| 109 | + `; |
| 110 | + gotRight3: GoLeftWait4Right -> Eating on rightP.ack |
| 111 | + ` |
| 112 | + // std::cout << " " << myName << " to eating " << std::flush << std::endl; |
| 113 | + `; |
| 114 | + putDownForks: Eating -> Thinking on timerP.timeout |
| 115 | + ` |
| 116 | + leftP.down().send(); |
| 117 | + rightP.down().send(); |
| 118 | + `; |
| 119 | + noNack1: Wait4Left -> Wait4Left on leftP.nack |
| 120 | + ` |
| 121 | + leftP.up().send(); |
| 122 | + `; |
| 123 | + noNeck2: Wait4First -> Wait4First on leftP.nack, rightP.nack |
| 124 | + ` |
| 125 | + RTProtocol * port = msg->sap(); |
| 126 | + if (port != (RTProtocol *) 0) |
| 127 | + ((ForkProt::Base *) port)->up().send(); |
| 128 | + `; |
| 129 | + noNeck3: Wait4Right -> Wait4Right on rightP.nack |
| 130 | + ` |
| 131 | + rightP.up().send(); |
| 132 | + `; |
| 133 | + noNeck4: GoLeftWait4Right -> GoLeftWait4Right on rightP.nack |
| 134 | + ` |
| 135 | + rightP.up().send(); |
| 136 | + `; |
| 137 | + noNeck5: GoRightWait4Right -> GoRightWait4Right on leftP.nack |
| 138 | + ` |
| 139 | + leftP.up().send(); |
| 140 | + `; |
| 141 | + }; |
| 142 | +}; |
0 commit comments