Demonstrates data corruption due to a race condition updated for mbed os 5.4

Fork of Task614-mbedos54 by Stage-1 Students SoCEM

Committer:
noutram
Date:
Tue Mar 08 12:11:18 2016 +0000
Revision:
1:8c6ec9de7688
Parent:
0:f916cefba2f4
Child:
2:7e77fec81f7d
removed unnecessary thread references

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:f916cefba2f4 1 #include "mbed.h"
noutram 0:f916cefba2f4 2 #include "rtos.h"
noutram 0:f916cefba2f4 3
noutram 0:f916cefba2f4 4 #define RED_DONE 1
noutram 0:f916cefba2f4 5 #define YELLOW_DONE 2
noutram 0:f916cefba2f4 6
noutram 0:f916cefba2f4 7
noutram 0:f916cefba2f4 8 //Function declarations
noutram 0:f916cefba2f4 9 void countUP(void const *args);
noutram 0:f916cefba2f4 10 void countDOWN(void const *args);
noutram 0:f916cefba2f4 11
noutram 0:f916cefba2f4 12 //Digital outputs
noutram 0:f916cefba2f4 13 DigitalOut onBoardLED(LED1);
noutram 0:f916cefba2f4 14 DigitalOut redLED(D7);
noutram 0:f916cefba2f4 15 DigitalOut yellowLED(D6);
noutram 0:f916cefba2f4 16 DigitalOut greenLED(D5);
noutram 0:f916cefba2f4 17
noutram 0:f916cefba2f4 18 //Digital inputs
noutram 0:f916cefba2f4 19 DigitalIn onBoardSwitch(USER_BUTTON);
noutram 0:f916cefba2f4 20 DigitalIn SW1(D4);
noutram 0:f916cefba2f4 21 DigitalIn SW2(D3);
noutram 0:f916cefba2f4 22
noutram 0:f916cefba2f4 23 //Thread ID for the Main function (CMSIS API)
noutram 0:f916cefba2f4 24 osThreadId tidMain;
noutram 0:f916cefba2f4 25
noutram 0:f916cefba2f4 26 //Stared mutable state
noutram 0:f916cefba2f4 27 volatile long long count = 0;
noutram 0:f916cefba2f4 28
noutram 0:f916cefba2f4 29 //Threads
noutram 0:f916cefba2f4 30 void countUP(void const *args)
noutram 0:f916cefba2f4 31 {
noutram 0:f916cefba2f4 32 redLED = 1;
noutram 0:f916cefba2f4 33
noutram 0:f916cefba2f4 34 for (unsigned int n=0; n<10000; n++) {
noutram 0:f916cefba2f4 35 count++;
noutram 0:f916cefba2f4 36 count++;
noutram 0:f916cefba2f4 37 count++;
noutram 0:f916cefba2f4 38 count++;
noutram 0:f916cefba2f4 39 count++;
noutram 0:f916cefba2f4 40 count++;
noutram 0:f916cefba2f4 41 count++;
noutram 0:f916cefba2f4 42 count++;
noutram 0:f916cefba2f4 43 count++;
noutram 0:f916cefba2f4 44 count++;
noutram 0:f916cefba2f4 45 }
noutram 0:f916cefba2f4 46
noutram 0:f916cefba2f4 47 redLED = 0;
noutram 0:f916cefba2f4 48 osSignalSet(tidMain, RED_DONE); //Signal main thread we are done
noutram 0:f916cefba2f4 49 }
noutram 0:f916cefba2f4 50
noutram 0:f916cefba2f4 51 void countDOWN(void const *args)
noutram 0:f916cefba2f4 52 {
noutram 0:f916cefba2f4 53 yellowLED = 1;
noutram 0:f916cefba2f4 54
noutram 0:f916cefba2f4 55 for (unsigned int n=0; n<10000; n++) {
noutram 0:f916cefba2f4 56 count--;
noutram 0:f916cefba2f4 57 count--;
noutram 0:f916cefba2f4 58 count--;
noutram 0:f916cefba2f4 59 count--;
noutram 0:f916cefba2f4 60 count--;
noutram 0:f916cefba2f4 61 count--;
noutram 0:f916cefba2f4 62 count--;
noutram 0:f916cefba2f4 63 count--;
noutram 0:f916cefba2f4 64 count--;
noutram 0:f916cefba2f4 65 count--;
noutram 0:f916cefba2f4 66 }
noutram 0:f916cefba2f4 67
noutram 0:f916cefba2f4 68 osSignalSet(tidMain, YELLOW_DONE); //Signal main thread we are done
noutram 0:f916cefba2f4 69 yellowLED = 0;
noutram 0:f916cefba2f4 70 }
noutram 0:f916cefba2f4 71
noutram 0:f916cefba2f4 72
noutram 0:f916cefba2f4 73 //Main thread
noutram 0:f916cefba2f4 74 int main() {
noutram 0:f916cefba2f4 75 redLED = 0;
noutram 0:f916cefba2f4 76 yellowLED = 0;
noutram 0:f916cefba2f4 77 greenLED = 1;
noutram 0:f916cefba2f4 78
noutram 0:f916cefba2f4 79 //Main thread ID
noutram 0:f916cefba2f4 80 tidMain = Thread::gettid();
noutram 0:f916cefba2f4 81
noutram 0:f916cefba2f4 82 //Press the switch to run concurrently
noutram 0:f916cefba2f4 83 if (onBoardSwitch == 1) {
noutram 0:f916cefba2f4 84 printf("Running sequntially\n");
noutram 0:f916cefba2f4 85 countUP(NULL);
noutram 0:f916cefba2f4 86 countDOWN(NULL);
noutram 0:f916cefba2f4 87 } else {
noutram 0:f916cefba2f4 88 printf("Running concurrently\n");
noutram 0:f916cefba2f4 89 Thread t1(countUP);
noutram 0:f916cefba2f4 90 Thread t2(countDOWN);
noutram 0:f916cefba2f4 91
noutram 0:f916cefba2f4 92 //Wait for the ALL_ON signal
noutram 0:f916cefba2f4 93 Thread::signal_wait(RED_DONE,osWaitForever);
noutram 0:f916cefba2f4 94 Thread::signal_wait(YELLOW_DONE,osWaitForever);
noutram 0:f916cefba2f4 95 }
noutram 0:f916cefba2f4 96
noutram 0:f916cefba2f4 97 printf("Final result = %lld\n", count);
noutram 0:f916cefba2f4 98 if (count == 0) {
noutram 0:f916cefba2f4 99 greenLED = 0;
noutram 0:f916cefba2f4 100 }
noutram 0:f916cefba2f4 101 while(true);
noutram 0:f916cefba2f4 102 }