
Demonstrates data corruption due to a race condition updated for mbed os 5.4
main.cpp@2:7e77fec81f7d, 2017-03-30 (annotated)
- Committer:
- noutram
- Date:
- Thu Mar 30 14:12:06 2017 +0000
- Revision:
- 2:7e77fec81f7d
- Parent:
- 1:8c6ec9de7688
Updated for mbed os 5.4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:f916cefba2f4 | 1 | #include "mbed.h" |
noutram | 0:f916cefba2f4 | 2 | |
noutram | 0:f916cefba2f4 | 3 | #define RED_DONE 1 |
noutram | 0:f916cefba2f4 | 4 | #define YELLOW_DONE 2 |
noutram | 0:f916cefba2f4 | 5 | |
noutram | 0:f916cefba2f4 | 6 | |
noutram | 0:f916cefba2f4 | 7 | //Function declarations |
noutram | 2:7e77fec81f7d | 8 | void countUP(); |
noutram | 2:7e77fec81f7d | 9 | void countDOWN(); |
noutram | 0:f916cefba2f4 | 10 | |
noutram | 0:f916cefba2f4 | 11 | //Digital outputs |
noutram | 0:f916cefba2f4 | 12 | DigitalOut onBoardLED(LED1); |
noutram | 0:f916cefba2f4 | 13 | DigitalOut redLED(D7); |
noutram | 0:f916cefba2f4 | 14 | DigitalOut yellowLED(D6); |
noutram | 0:f916cefba2f4 | 15 | DigitalOut greenLED(D5); |
noutram | 0:f916cefba2f4 | 16 | |
noutram | 0:f916cefba2f4 | 17 | //Digital inputs |
noutram | 0:f916cefba2f4 | 18 | DigitalIn onBoardSwitch(USER_BUTTON); |
noutram | 0:f916cefba2f4 | 19 | DigitalIn SW1(D4); |
noutram | 0:f916cefba2f4 | 20 | DigitalIn SW2(D3); |
noutram | 0:f916cefba2f4 | 21 | |
noutram | 0:f916cefba2f4 | 22 | //Thread ID for the Main function (CMSIS API) |
noutram | 0:f916cefba2f4 | 23 | osThreadId tidMain; |
noutram | 0:f916cefba2f4 | 24 | |
noutram | 0:f916cefba2f4 | 25 | //Stared mutable state |
noutram | 0:f916cefba2f4 | 26 | volatile long long count = 0; |
noutram | 0:f916cefba2f4 | 27 | |
noutram | 0:f916cefba2f4 | 28 | //Threads |
noutram | 2:7e77fec81f7d | 29 | void countUP() |
noutram | 0:f916cefba2f4 | 30 | { |
noutram | 0:f916cefba2f4 | 31 | redLED = 1; |
noutram | 0:f916cefba2f4 | 32 | |
noutram | 0:f916cefba2f4 | 33 | for (unsigned int n=0; n<10000; n++) { |
noutram | 0:f916cefba2f4 | 34 | count++; |
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 | } |
noutram | 0:f916cefba2f4 | 45 | |
noutram | 0:f916cefba2f4 | 46 | redLED = 0; |
noutram | 0:f916cefba2f4 | 47 | osSignalSet(tidMain, RED_DONE); //Signal main thread we are done |
noutram | 0:f916cefba2f4 | 48 | } |
noutram | 0:f916cefba2f4 | 49 | |
noutram | 2:7e77fec81f7d | 50 | void countDOWN() |
noutram | 0:f916cefba2f4 | 51 | { |
noutram | 0:f916cefba2f4 | 52 | yellowLED = 1; |
noutram | 0:f916cefba2f4 | 53 | |
noutram | 0:f916cefba2f4 | 54 | for (unsigned int n=0; n<10000; n++) { |
noutram | 0:f916cefba2f4 | 55 | count--; |
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 | } |
noutram | 0:f916cefba2f4 | 66 | |
noutram | 0:f916cefba2f4 | 67 | osSignalSet(tidMain, YELLOW_DONE); //Signal main thread we are done |
noutram | 0:f916cefba2f4 | 68 | yellowLED = 0; |
noutram | 0:f916cefba2f4 | 69 | } |
noutram | 0:f916cefba2f4 | 70 | |
noutram | 0:f916cefba2f4 | 71 | |
noutram | 0:f916cefba2f4 | 72 | //Main thread |
noutram | 0:f916cefba2f4 | 73 | int main() { |
noutram | 2:7e77fec81f7d | 74 | Thread t1, t2; |
noutram | 2:7e77fec81f7d | 75 | |
noutram | 0:f916cefba2f4 | 76 | redLED = 0; |
noutram | 0:f916cefba2f4 | 77 | yellowLED = 0; |
noutram | 0:f916cefba2f4 | 78 | greenLED = 1; |
noutram | 0:f916cefba2f4 | 79 | |
noutram | 0:f916cefba2f4 | 80 | //Main thread ID |
noutram | 0:f916cefba2f4 | 81 | tidMain = Thread::gettid(); |
noutram | 0:f916cefba2f4 | 82 | |
noutram | 0:f916cefba2f4 | 83 | //Press the switch to run concurrently |
noutram | 0:f916cefba2f4 | 84 | if (onBoardSwitch == 1) { |
noutram | 0:f916cefba2f4 | 85 | printf("Running sequntially\n"); |
noutram | 2:7e77fec81f7d | 86 | countUP(); |
noutram | 2:7e77fec81f7d | 87 | countDOWN(); |
noutram | 0:f916cefba2f4 | 88 | } else { |
noutram | 0:f916cefba2f4 | 89 | printf("Running concurrently\n"); |
noutram | 2:7e77fec81f7d | 90 | t1.start(countUP); |
noutram | 2:7e77fec81f7d | 91 | t2.start(countDOWN); |
noutram | 0:f916cefba2f4 | 92 | |
noutram | 0:f916cefba2f4 | 93 | //Wait for the ALL_ON signal |
noutram | 0:f916cefba2f4 | 94 | Thread::signal_wait(RED_DONE,osWaitForever); |
noutram | 0:f916cefba2f4 | 95 | Thread::signal_wait(YELLOW_DONE,osWaitForever); |
noutram | 0:f916cefba2f4 | 96 | } |
noutram | 0:f916cefba2f4 | 97 | |
noutram | 0:f916cefba2f4 | 98 | printf("Final result = %lld\n", count); |
noutram | 0:f916cefba2f4 | 99 | if (count == 0) { |
noutram | 0:f916cefba2f4 | 100 | greenLED = 0; |
noutram | 0:f916cefba2f4 | 101 | } |
noutram | 0:f916cefba2f4 | 102 | while(true); |
noutram | 0:f916cefba2f4 | 103 | } |