![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Solution to the deadlock problem
Fork of Task617Solution-mbedos54 by
main.cpp@7:bd75e7717b58, 2017-10-31 (annotated)
- Committer:
- noutram
- Date:
- Tue Oct 31 15:40:25 2017 +0000
- Revision:
- 7:bd75e7717b58
- Parent:
- 6:d16ce38e9b8a
updated for stage-3
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 | #include "rtos.h" |
noutram | 2:70084af839d3 | 3 | #include "string.h" |
noutram | 2:70084af839d3 | 4 | #include <stdio.h> |
noutram | 2:70084af839d3 | 5 | #include <ctype.h> |
noutram | 0:f916cefba2f4 | 6 | |
noutram | 4:dae8898e55fe | 7 | #define DELAY 200 |
noutram | 0:f916cefba2f4 | 8 | |
noutram | 0:f916cefba2f4 | 9 | DigitalOut onBoardLED(LED1); |
noutram | 7:bd75e7717b58 | 10 | DigitalOut redLED(PE_15); |
noutram | 7:bd75e7717b58 | 11 | DigitalOut yellowLED(PB_10); |
noutram | 7:bd75e7717b58 | 12 | DigitalOut greenLED(PB_11); |
noutram | 0:f916cefba2f4 | 13 | |
noutram | 0:f916cefba2f4 | 14 | DigitalIn onBoardSwitch(USER_BUTTON); |
noutram | 7:bd75e7717b58 | 15 | DigitalIn SW1(PE_12); |
noutram | 7:bd75e7717b58 | 16 | DigitalIn SW2(PE_14); |
noutram | 0:f916cefba2f4 | 17 | |
noutram | 0:f916cefba2f4 | 18 | //Thread ID for the Main function (CMSIS API) |
noutram | 0:f916cefba2f4 | 19 | osThreadId tidMain; |
noutram | 0:f916cefba2f4 | 20 | |
noutram | 2:70084af839d3 | 21 | //Thread sychronisation primatives |
noutram | 4:dae8898e55fe | 22 | Mutex lock1; |
noutram | 4:dae8898e55fe | 23 | Mutex lock2; |
noutram | 4:dae8898e55fe | 24 | unsigned long sw1Count = 0; |
noutram | 4:dae8898e55fe | 25 | unsigned long sw2Count = 0; |
noutram | 2:70084af839d3 | 26 | |
noutram | 6:d16ce38e9b8a | 27 | void thread1() |
noutram | 4:dae8898e55fe | 28 | { |
noutram | 7:bd75e7717b58 | 29 | printf("Entering thread 1\n"); |
noutram | 4:dae8898e55fe | 30 | while (true) { |
noutram | 5:31707531f715 | 31 | yellowLED = 1; |
noutram | 4:dae8898e55fe | 32 | |
noutram | 4:dae8898e55fe | 33 | //Start critical section |
noutram | 4:dae8898e55fe | 34 | lock1.lock(); |
noutram | 4:dae8898e55fe | 35 | |
noutram | 4:dae8898e55fe | 36 | sw1Count++; |
noutram | 4:dae8898e55fe | 37 | printf("\nCount1 = %lu", sw1Count); |
noutram | 4:dae8898e55fe | 38 | |
noutram | 5:31707531f715 | 39 | Thread::wait(1); //1ms |
noutram | 4:dae8898e55fe | 40 | |
noutram | 4:dae8898e55fe | 41 | //End critical section |
noutram | 4:dae8898e55fe | 42 | lock1.unlock(); |
noutram | 5:31707531f715 | 43 | |
noutram | 5:31707531f715 | 44 | if (SW1 == 1) { |
noutram | 5:31707531f715 | 45 | lock2.lock(); |
noutram | 5:31707531f715 | 46 | sw2Count--; |
noutram | 5:31707531f715 | 47 | lock2.unlock(); |
noutram | 5:31707531f715 | 48 | } |
noutram | 4:dae8898e55fe | 49 | |
noutram | 5:31707531f715 | 50 | yellowLED = 0; |
noutram | 4:dae8898e55fe | 51 | Thread::wait(DELAY); |
noutram | 4:dae8898e55fe | 52 | } |
noutram | 2:70084af839d3 | 53 | } |
noutram | 2:70084af839d3 | 54 | |
noutram | 6:d16ce38e9b8a | 55 | void thread2() |
noutram | 0:f916cefba2f4 | 56 | { |
noutram | 7:bd75e7717b58 | 57 | printf("Entering thread 2\n"); |
noutram | 2:70084af839d3 | 58 | while (true) { |
noutram | 5:31707531f715 | 59 | redLED = 1; |
noutram | 2:70084af839d3 | 60 | |
noutram | 4:dae8898e55fe | 61 | //Start critical section |
noutram | 4:dae8898e55fe | 62 | lock2.lock(); |
noutram | 2:70084af839d3 | 63 | |
noutram | 4:dae8898e55fe | 64 | sw2Count++; |
noutram | 4:dae8898e55fe | 65 | printf("\nCount2 = %lu", sw2Count); |
noutram | 4:dae8898e55fe | 66 | |
noutram | 5:31707531f715 | 67 | Thread::wait(1); //1ms |
noutram | 1:4fb27aea76b2 | 68 | |
noutram | 5:31707531f715 | 69 | //End critical section |
noutram | 5:31707531f715 | 70 | lock2.unlock(); |
noutram | 5:31707531f715 | 71 | |
noutram | 5:31707531f715 | 72 | if (SW2 == 1) { |
noutram | 4:dae8898e55fe | 73 | lock1.lock(); |
noutram | 4:dae8898e55fe | 74 | sw1Count--; |
noutram | 4:dae8898e55fe | 75 | lock1.unlock(); |
noutram | 4:dae8898e55fe | 76 | } |
noutram | 5:31707531f715 | 77 | |
noutram | 5:31707531f715 | 78 | redLED = 0; |
noutram | 5:31707531f715 | 79 | Thread::wait(DELAY); |
noutram | 1:4fb27aea76b2 | 80 | |
noutram | 4:dae8898e55fe | 81 | } |
noutram | 0:f916cefba2f4 | 82 | } |
noutram | 0:f916cefba2f4 | 83 | |
noutram | 4:dae8898e55fe | 84 | |
noutram | 0:f916cefba2f4 | 85 | //Main thread |
noutram | 0:f916cefba2f4 | 86 | int main() { |
noutram | 0:f916cefba2f4 | 87 | redLED = 0; |
noutram | 0:f916cefba2f4 | 88 | yellowLED = 0; |
noutram | 2:70084af839d3 | 89 | greenLED = 0; |
noutram | 4:dae8898e55fe | 90 | |
noutram | 0:f916cefba2f4 | 91 | //Main thread ID |
noutram | 0:f916cefba2f4 | 92 | tidMain = Thread::gettid(); |
noutram | 0:f916cefba2f4 | 93 | |
noutram | 4:dae8898e55fe | 94 | //Threads |
noutram | 6:d16ce38e9b8a | 95 | Thread t1, t2; |
noutram | 6:d16ce38e9b8a | 96 | |
noutram | 6:d16ce38e9b8a | 97 | t1.start(thread1); |
noutram | 6:d16ce38e9b8a | 98 | t2.start(thread2); |
noutram | 1:4fb27aea76b2 | 99 | |
noutram | 7:bd75e7717b58 | 100 | printf("Main Thread\n"); |
noutram | 2:70084af839d3 | 101 | while (true) { |
noutram | 4:dae8898e55fe | 102 | Thread::wait(osWaitForever); |
noutram | 0:f916cefba2f4 | 103 | } |
noutram | 0:f916cefba2f4 | 104 | |
noutram | 0:f916cefba2f4 | 105 | } |
noutram | 2:70084af839d3 | 106 | |
noutram | 2:70084af839d3 | 107 |