Jaydeep Shah
/
22_MUTEX_TIME_UNLOCK
MUTEX _TIME BASED LOCK UNLOCK SYSTEM to avoid priority inversion and deadlock or thread stuck issue.
main.cpp@1:4f150e2895bf, 2020-07-26 (annotated)
- Committer:
- radhey04ec
- Date:
- Sun Jul 26 13:18:27 2020 +0000
- Revision:
- 1:4f150e2895bf
- Parent:
- 0:f8e6d9529dbd
FINAL COMMIT
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
radhey04ec | 0:f8e6d9529dbd | 1 | // MUTEX : PART 2 -MUTEX TIME BASED LOCKING |
radhey04ec | 0:f8e6d9529dbd | 2 | |
radhey04ec | 0:f8e6d9529dbd | 3 | /*As we know MUTEX have ownership in locking mechanism. Thread which locked code using mutex flag then only same thread can unblock it |
radhey04ec | 0:f8e6d9529dbd | 4 | If you have not enough knowledge regarding RTOS then it leads some dangerous situation like priority inversion and Deadlock |
radhey04ec | 0:f8e6d9529dbd | 5 | |
radhey04ec | 0:f8e6d9529dbd | 6 | Time based unlocking help us to avoid DEADLOCK. |
radhey04ec | 0:f8e6d9529dbd | 7 | Here mutex automatically unlock the block after ending time period |
radhey04ec | 0:f8e6d9529dbd | 8 | |
radhey04ec | 0:f8e6d9529dbd | 9 | Program created by : Jaydeep shah -radhey04ec@gmail.com |
radhey04ec | 0:f8e6d9529dbd | 10 | Date : 26 July 2020 |
radhey04ec | 0:f8e6d9529dbd | 11 | */ |
radhey04ec | 0:f8e6d9529dbd | 12 | |
radhey04ec | 0:f8e6d9529dbd | 13 | |
radhey04ec | 0:f8e6d9529dbd | 14 | /* Function use |
radhey04ec | 0:f8e6d9529dbd | 15 | bool trylock_for(Kernel::Clock::duration_u32 rel_time); |
radhey04ec | 0:f8e6d9529dbd | 16 | Tries to lock the mutex. If it succeeds within the specified time frame (interval) the mutex is locked and stays locked until unlocked by calling the unlock function. |
radhey04ec | 0:f8e6d9529dbd | 17 | |
radhey04ec | 0:f8e6d9529dbd | 18 | bool trylock_until(Kernel::Clock::time_point abs_time); |
radhey04ec | 0:f8e6d9529dbd | 19 | Tries to lock the mutex. If it succeeds before the given time point is reached it locks the mutex and the mutex stays locked until unlocked by calling the unlock function. |
radhey04ec | 0:f8e6d9529dbd | 20 | |
radhey04ec | 0:f8e6d9529dbd | 21 | */ |
radhey04ec | 0:f8e6d9529dbd | 22 | |
radhey04ec | 0:f8e6d9529dbd | 23 | #include "mbed.h" |
radhey04ec | 0:f8e6d9529dbd | 24 | |
radhey04ec | 0:f8e6d9529dbd | 25 | Mutex Lock; |
radhey04ec | 0:f8e6d9529dbd | 26 | Thread T1,T2; |
radhey04ec | 0:f8e6d9529dbd | 27 | bool V; |
radhey04ec | 0:f8e6d9529dbd | 28 | void common_block(const char* name, int state) |
radhey04ec | 0:f8e6d9529dbd | 29 | { |
radhey04ec | 0:f8e6d9529dbd | 30 | printf("\n Thread arrive %s %d \n", name, state); |
radhey04ec | 1:4f150e2895bf | 31 | if (Lock.trylock_for(50)) { // try to lock the mutex: timeout = 50 ms----------------------------- |
radhey04ec | 0:f8e6d9529dbd | 32 | printf("\n Inside %s state %d \n", name, state); |
radhey04ec | 0:f8e6d9529dbd | 33 | ThisThread::sleep_for(6000); //some how process stuck in wait state |
radhey04ec | 0:f8e6d9529dbd | 34 | printf("\n outside %s state %d \n", name, state); |
radhey04ec | 0:f8e6d9529dbd | 35 | Lock.unlock(); // unlock |
radhey04ec | 0:f8e6d9529dbd | 36 | } |
radhey04ec | 0:f8e6d9529dbd | 37 | else |
radhey04ec | 0:f8e6d9529dbd | 38 | { |
radhey04ec | 1:4f150e2895bf | 39 | //Thread back to other work/next process if fail to acquire Critical section |
radhey04ec | 1:4f150e2895bf | 40 | //Thread not stuck for getting mutex flag it can do other important work also |
radhey04ec | 0:f8e6d9529dbd | 41 | printf("\n %s thread fail to lock state %d",name,state); |
radhey04ec | 0:f8e6d9529dbd | 42 | } |
radhey04ec | 0:f8e6d9529dbd | 43 | } |
radhey04ec | 0:f8e6d9529dbd | 44 | void fun(void const *arg) |
radhey04ec | 0:f8e6d9529dbd | 45 | { |
radhey04ec | 0:f8e6d9529dbd | 46 | while(true) |
radhey04ec | 0:f8e6d9529dbd | 47 | { |
radhey04ec | 0:f8e6d9529dbd | 48 | common_block((const char *)arg,0); //PASS STATE 0 |
radhey04ec | 0:f8e6d9529dbd | 49 | wait(2); |
radhey04ec | 0:f8e6d9529dbd | 50 | common_block((const char *)arg,1);// PASS STATE 1 |
radhey04ec | 0:f8e6d9529dbd | 51 | wait(2); |
radhey04ec | 0:f8e6d9529dbd | 52 | } |
radhey04ec | 0:f8e6d9529dbd | 53 | } |
radhey04ec | 0:f8e6d9529dbd | 54 | |
radhey04ec | 0:f8e6d9529dbd | 55 | int main() |
radhey04ec | 0:f8e6d9529dbd | 56 | { |
radhey04ec | 0:f8e6d9529dbd | 57 | T1.start(callback(fun,(void *)"T1")); |
radhey04ec | 0:f8e6d9529dbd | 58 | T2.start(callback(fun,(void *)"T2")); |
radhey04ec | 0:f8e6d9529dbd | 59 | fun((void *)"main"); |
radhey04ec | 0:f8e6d9529dbd | 60 | } |
radhey04ec | 0:f8e6d9529dbd | 61 |