2019

Committer:
noutram
Date:
Fri Sep 20 14:12:39 2019 +0000
Revision:
0:65e4972b64a9
2019

Who changed what in which revision?

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