University of Plymouth - Stages 1, 2 and 3
/
Task600-ThreadRaces
main.cpp@0:c49a2dd99fc3, 2019-09-20 (annotated)
- Committer:
- noutram
- Date:
- Fri Sep 20 14:06:30 2019 +0000
- Revision:
- 0:c49a2dd99fc3
2019
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:c49a2dd99fc3 | 1 | #include "mbed.h" |
noutram | 0:c49a2dd99fc3 | 2 | void countUp(); |
noutram | 0:c49a2dd99fc3 | 3 | void countDown(); |
noutram | 0:c49a2dd99fc3 | 4 | |
noutram | 0:c49a2dd99fc3 | 5 | #define N 1000000 |
noutram | 0:c49a2dd99fc3 | 6 | #define RELEASED 0 |
noutram | 0:c49a2dd99fc3 | 7 | #define PRESSED 1 |
noutram | 0:c49a2dd99fc3 | 8 | |
noutram | 0:c49a2dd99fc3 | 9 | //Hardware |
noutram | 0:c49a2dd99fc3 | 10 | DigitalOut red_led(PE_15); //CountUp is in its critical section |
noutram | 0:c49a2dd99fc3 | 11 | DigitalOut yellow_led(PB_10); //CountDown is in its critical section |
noutram | 0:c49a2dd99fc3 | 12 | DigitalOut green_led(PB_11); //counter != 0 |
noutram | 0:c49a2dd99fc3 | 13 | DigitalIn button(USER_BUTTON); |
noutram | 0:c49a2dd99fc3 | 14 | |
noutram | 0:c49a2dd99fc3 | 15 | //Additional Threads |
noutram | 0:c49a2dd99fc3 | 16 | Thread t1; |
noutram | 0:c49a2dd99fc3 | 17 | Thread t2; |
noutram | 0:c49a2dd99fc3 | 18 | |
noutram | 0:c49a2dd99fc3 | 19 | //Shared mutable state |
noutram | 0:c49a2dd99fc3 | 20 | volatile long long counter = 0; //Volatile means it must be stored in memory |
noutram | 0:c49a2dd99fc3 | 21 | |
noutram | 0:c49a2dd99fc3 | 22 | //Increment the shared variable |
noutram | 0:c49a2dd99fc3 | 23 | void countUp() |
noutram | 0:c49a2dd99fc3 | 24 | { |
noutram | 0:c49a2dd99fc3 | 25 | //RED MEANS THE COUNT UP FUNCTION IS IN ITS CRITICAL SECTION |
noutram | 0:c49a2dd99fc3 | 26 | red_led = 1; |
noutram | 0:c49a2dd99fc3 | 27 | for (unsigned int n=0; n<N; n++) { |
noutram | 0:c49a2dd99fc3 | 28 | counter++; |
noutram | 0:c49a2dd99fc3 | 29 | counter++; |
noutram | 0:c49a2dd99fc3 | 30 | counter++; |
noutram | 0:c49a2dd99fc3 | 31 | counter++; |
noutram | 0:c49a2dd99fc3 | 32 | counter++; |
noutram | 0:c49a2dd99fc3 | 33 | counter++; |
noutram | 0:c49a2dd99fc3 | 34 | counter++; |
noutram | 0:c49a2dd99fc3 | 35 | counter++; |
noutram | 0:c49a2dd99fc3 | 36 | counter++; |
noutram | 0:c49a2dd99fc3 | 37 | counter++; |
noutram | 0:c49a2dd99fc3 | 38 | } |
noutram | 0:c49a2dd99fc3 | 39 | red_led = 0; |
noutram | 0:c49a2dd99fc3 | 40 | |
noutram | 0:c49a2dd99fc3 | 41 | } |
noutram | 0:c49a2dd99fc3 | 42 | |
noutram | 0:c49a2dd99fc3 | 43 | //Decrement the shared variable |
noutram | 0:c49a2dd99fc3 | 44 | void countDown() |
noutram | 0:c49a2dd99fc3 | 45 | { |
noutram | 0:c49a2dd99fc3 | 46 | //YELLOW MEANS THE COUNT DOWN FUNCTION IS IN ITS CRITICAL SECTION |
noutram | 0:c49a2dd99fc3 | 47 | yellow_led = 1; |
noutram | 0:c49a2dd99fc3 | 48 | for (unsigned int n=0; n<N; n++) { |
noutram | 0:c49a2dd99fc3 | 49 | counter--; |
noutram | 0:c49a2dd99fc3 | 50 | counter--; |
noutram | 0:c49a2dd99fc3 | 51 | counter--; |
noutram | 0:c49a2dd99fc3 | 52 | counter--; |
noutram | 0:c49a2dd99fc3 | 53 | counter--; |
noutram | 0:c49a2dd99fc3 | 54 | counter--; |
noutram | 0:c49a2dd99fc3 | 55 | counter--; |
noutram | 0:c49a2dd99fc3 | 56 | counter--; |
noutram | 0:c49a2dd99fc3 | 57 | counter--; |
noutram | 0:c49a2dd99fc3 | 58 | counter--; |
noutram | 0:c49a2dd99fc3 | 59 | } |
noutram | 0:c49a2dd99fc3 | 60 | yellow_led = 0; |
noutram | 0:c49a2dd99fc3 | 61 | |
noutram | 0:c49a2dd99fc3 | 62 | } |
noutram | 0:c49a2dd99fc3 | 63 | int main() { |
noutram | 0:c49a2dd99fc3 | 64 | |
noutram | 0:c49a2dd99fc3 | 65 | green_led = 1; |
noutram | 0:c49a2dd99fc3 | 66 | |
noutram | 0:c49a2dd99fc3 | 67 | //Start competing threads |
noutram | 0:c49a2dd99fc3 | 68 | t1.start(countUp); |
noutram | 0:c49a2dd99fc3 | 69 | t2.start(countDown); |
noutram | 0:c49a2dd99fc3 | 70 | |
noutram | 0:c49a2dd99fc3 | 71 | //These threads DO exit, so let's wait for BOTH to finish |
noutram | 0:c49a2dd99fc3 | 72 | t1.join(); //Wait for thread t1 to finish |
noutram | 0:c49a2dd99fc3 | 73 | t2.join(); //Wait for thread t2 to finish |
noutram | 0:c49a2dd99fc3 | 74 | |
noutram | 0:c49a2dd99fc3 | 75 | //Did the counter end up at zero? |
noutram | 0:c49a2dd99fc3 | 76 | if (counter == 0) { |
noutram | 0:c49a2dd99fc3 | 77 | green_led = 0; |
noutram | 0:c49a2dd99fc3 | 78 | } |
noutram | 0:c49a2dd99fc3 | 79 | |
noutram | 0:c49a2dd99fc3 | 80 | //Now spin-lock for ever |
noutram | 0:c49a2dd99fc3 | 81 | while(1) { |
noutram | 0:c49a2dd99fc3 | 82 | asm("nop"); |
noutram | 0:c49a2dd99fc3 | 83 | }; |
noutram | 0:c49a2dd99fc3 | 84 | } |
noutram | 0:c49a2dd99fc3 | 85 |