MUTEX and its use in MBED MUTEX LOCK and Unlock mechanism

Committer:
radhey04ec
Date:
Sat Jul 25 09:46:26 2020 +0000
Revision:
0:b908013d70cd
Child:
1:605f5624661e
MUTEX FINAL COMMIT; NOTE : BUG INSIDE MUTEX CLASS MBED, if you use priority level of thread then MUTEX allow to access CS of two different thread

Who changed what in which revision?

UserRevisionLine numberNew contents of line
radhey04ec 0:b908013d70cd 1 /* 21_ MUTEX a Guard at door - Basic program (All thread with same priority level)
radhey04ec 0:b908013d70cd 2 MUTEX allow only one Thread / Function call inside CS, Mutex have ownership type locking mechanism.
radhey04ec 0:b908013d70cd 3 Procees /or Thread who locked block,only same process /or thread can unlock it.
radhey04ec 0:b908013d70cd 4 */
radhey04ec 0:b908013d70cd 5 //BUGS in MUTEX CLASS
radhey04ec 0:b908013d70cd 6 //NOTE : MUTEX ONLY PROTECT CS from processes which are belonging same priority level -MBED BUG
radhey04ec 0:b908013d70cd 7
radhey04ec 0:b908013d70cd 8 //PROGRAM CREATED BY : JAYDEEP SHAH -- radheec@gmail.com
radhey04ec 0:b908013d70cd 9 //DATE : 25 JULY 20 ,VERSION 1.0
radhey04ec 0:b908013d70cd 10
radhey04ec 0:b908013d70cd 11 //OUTPUT : USE SERIAL TERMINAL 9600 8-N-1
radhey04ec 0:b908013d70cd 12
radhey04ec 0:b908013d70cd 13
radhey04ec 0:b908013d70cd 14 #include "mbed.h" //MBED LIBRARY
radhey04ec 0:b908013d70cd 15
radhey04ec 0:b908013d70cd 16 Mutex M_LOCK; // Create MUTEX OBJECT -class = MUTEX
radhey04ec 0:b908013d70cd 17
radhey04ec 0:b908013d70cd 18 //Create Two Thread
radhey04ec 0:b908013d70cd 19
radhey04ec 0:b908013d70cd 20 Thread t2; //Create Thread with high priority
radhey04ec 0:b908013d70cd 21
radhey04ec 0:b908013d70cd 22 Thread t3; //Create Thread with low priority
radhey04ec 0:b908013d70cd 23
radhey04ec 0:b908013d70cd 24
radhey04ec 0:b908013d70cd 25 //Here below section act as CS critical section
radhey04ec 0:b908013d70cd 26
radhey04ec 0:b908013d70cd 27 void common_function(const char *name, int state) //TWO ARGUMENTS 1) WHO CALLED THIS FUNCTION 2)STATE OF CALLER
radhey04ec 0:b908013d70cd 28 {
radhey04ec 0:b908013d70cd 29 printf("Thread arrive at door %s: %d\n\r", name, state);
radhey04ec 0:b908013d70cd 30
radhey04ec 0:b908013d70cd 31 M_LOCK.lock(); //After arrive lock the code---------------------------LOCK THE BLOCK
radhey04ec 0:b908013d70cd 32
radhey04ec 0:b908013d70cd 33 printf("This Thread lock the code %s: %d\n\r", name, state);
radhey04ec 0:b908013d70cd 34 wait(0.5); //sleep
radhey04ec 0:b908013d70cd 35 M_LOCK.unlock(); //After completing task unlock the code ------------- UNLOCK THE BLOCK
radhey04ec 0:b908013d70cd 36
radhey04ec 0:b908013d70cd 37 printf("Thread cross & unlock %s: %d\n\r", name, state); //OUTSIDE CODE BLOCK ---------
radhey04ec 0:b908013d70cd 38 }
radhey04ec 0:b908013d70cd 39
radhey04ec 0:b908013d70cd 40
radhey04ec 0:b908013d70cd 41
radhey04ec 0:b908013d70cd 42 //Function -- We will connect more than one thread with this function
radhey04ec 0:b908013d70cd 43 //So because of context switching this Function become sharable between multiple process.
radhey04ec 0:b908013d70cd 44 void test_thread(void const *args)
radhey04ec 0:b908013d70cd 45 {
radhey04ec 0:b908013d70cd 46 while (true) {
radhey04ec 0:b908013d70cd 47 common_function((const char *)args, 0);
radhey04ec 0:b908013d70cd 48 ThisThread::sleep_for(500);
radhey04ec 0:b908013d70cd 49 common_function((const char *)args, 1);
radhey04ec 0:b908013d70cd 50 ThisThread::sleep_for(500);
radhey04ec 0:b908013d70cd 51 }
radhey04ec 0:b908013d70cd 52 }
radhey04ec 0:b908013d70cd 53
radhey04ec 0:b908013d70cd 54 int main()
radhey04ec 0:b908013d70cd 55 {
radhey04ec 0:b908013d70cd 56 t2.start(callback(test_thread, (void *)"Th 2"));
radhey04ec 0:b908013d70cd 57 t3.start(callback(test_thread, (void *)"Th 3"));
radhey04ec 0:b908013d70cd 58
radhey04ec 0:b908013d70cd 59 test_thread((void *)"Th 1"); // DIRECT CALL via main thread
radhey04ec 0:b908013d70cd 60 }