Demonstration of deadlocks by adding a small delay updated for mbed os 5.4

Fork of Task617-mbedos54 by Stage-1 Students SoCEM

Committer:
noutram
Date:
Mon Apr 03 11:34:50 2017 +0000
Revision:
6:bd736256c32d
Parent:
5:d6461300576b
updated for mbed os 5.4

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 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 //Digital outputs
noutram 0:f916cefba2f4 10 DigitalOut onBoardLED(LED1);
noutram 0:f916cefba2f4 11 DigitalOut redLED(D7);
noutram 0:f916cefba2f4 12 DigitalOut yellowLED(D6);
noutram 0:f916cefba2f4 13 DigitalOut greenLED(D5);
noutram 0:f916cefba2f4 14
noutram 2:70084af839d3 15 //Serial Interface
noutram 2:70084af839d3 16 Serial pc(USBTX, USBRX);
noutram 2:70084af839d3 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 2:70084af839d3 26 //Thread sychronisation primatives
noutram 4:dae8898e55fe 27 Mutex lock1;
noutram 4:dae8898e55fe 28 Mutex lock2;
noutram 4:dae8898e55fe 29 unsigned long sw1Count = 0;
noutram 4:dae8898e55fe 30 unsigned long sw2Count = 0;
noutram 2:70084af839d3 31
noutram 6:bd736256c32d 32 void thread1()
noutram 4:dae8898e55fe 33 {
noutram 4:dae8898e55fe 34 pc.printf("Entering thread 1\n");
noutram 4:dae8898e55fe 35 while (true) {
noutram 5:d6461300576b 36 yellowLED = 1;
noutram 4:dae8898e55fe 37
noutram 4:dae8898e55fe 38 //Start critical section
noutram 4:dae8898e55fe 39 lock1.lock();
noutram 4:dae8898e55fe 40
noutram 4:dae8898e55fe 41 sw1Count++;
noutram 4:dae8898e55fe 42 printf("\nCount1 = %lu", sw1Count);
noutram 4:dae8898e55fe 43
noutram 4:dae8898e55fe 44 //Thread::wait(1); //1ms
noutram 1:4fb27aea76b2 45
noutram 4:dae8898e55fe 46 if (SW1 == 1) {
noutram 4:dae8898e55fe 47 lock2.lock();
noutram 4:dae8898e55fe 48 sw2Count--;
noutram 5:d6461300576b 49 lock2.unlock();
noutram 4:dae8898e55fe 50 }
noutram 4:dae8898e55fe 51
noutram 4:dae8898e55fe 52 //End critical section
noutram 4:dae8898e55fe 53 lock1.unlock();
noutram 4:dae8898e55fe 54
noutram 5:d6461300576b 55 yellowLED = 0;
noutram 4:dae8898e55fe 56 Thread::wait(DELAY);
noutram 4:dae8898e55fe 57 }
noutram 2:70084af839d3 58 }
noutram 2:70084af839d3 59
noutram 6:bd736256c32d 60 void thread2()
noutram 0:f916cefba2f4 61 {
noutram 4:dae8898e55fe 62 pc.printf("Entering thread 2\n");
noutram 2:70084af839d3 63 while (true) {
noutram 5:d6461300576b 64 redLED = 1;
noutram 2:70084af839d3 65
noutram 4:dae8898e55fe 66 //Start critical section
noutram 4:dae8898e55fe 67 lock2.lock();
noutram 2:70084af839d3 68
noutram 4:dae8898e55fe 69 sw2Count++;
noutram 4:dae8898e55fe 70 printf("\nCount2 = %lu", sw2Count);
noutram 4:dae8898e55fe 71
noutram 4:dae8898e55fe 72 //Thread::wait(1); //1ms
noutram 1:4fb27aea76b2 73
noutram 4:dae8898e55fe 74 if (SW2 == 1) {
noutram 4:dae8898e55fe 75 lock1.lock();
noutram 4:dae8898e55fe 76 sw1Count--;
noutram 4:dae8898e55fe 77 lock1.unlock();
noutram 4:dae8898e55fe 78 }
noutram 4:dae8898e55fe 79 //End critical section
noutram 4:dae8898e55fe 80 lock2.unlock();
noutram 1:4fb27aea76b2 81
noutram 5:d6461300576b 82 redLED = 0;
noutram 4:dae8898e55fe 83 Thread::wait(DELAY);
noutram 4:dae8898e55fe 84 }
noutram 0:f916cefba2f4 85 }
noutram 0:f916cefba2f4 86
noutram 4:dae8898e55fe 87
noutram 0:f916cefba2f4 88 //Main thread
noutram 0:f916cefba2f4 89 int main() {
noutram 0:f916cefba2f4 90 redLED = 0;
noutram 0:f916cefba2f4 91 yellowLED = 0;
noutram 2:70084af839d3 92 greenLED = 0;
noutram 4:dae8898e55fe 93
noutram 0:f916cefba2f4 94 //Main thread ID
noutram 0:f916cefba2f4 95 tidMain = Thread::gettid();
noutram 0:f916cefba2f4 96
noutram 4:dae8898e55fe 97 //Threads
noutram 6:bd736256c32d 98 Thread t1, t2;
noutram 6:bd736256c32d 99
noutram 6:bd736256c32d 100 t1.start(thread1);
noutram 6:bd736256c32d 101 t2.start(thread2);
noutram 1:4fb27aea76b2 102
noutram 4:dae8898e55fe 103 pc.printf("Main Thread\n");
noutram 2:70084af839d3 104 while (true) {
noutram 4:dae8898e55fe 105 Thread::wait(osWaitForever);
noutram 0:f916cefba2f4 106 }
noutram 0:f916cefba2f4 107
noutram 0:f916cefba2f4 108 }
noutram 2:70084af839d3 109
noutram 2:70084af839d3 110