Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
BroadcastCommunity.qnx.com will be offline from May 31 6:00pm until June 2 12:00AM for upcoming system upgrades. For more information please go to https://community.qnx.com/sf/discussion/do/listPosts/projects.bazaar/discussion.bazaar.topc28418
Forum Topic - Mutex priority inheritance related question: (7 Items)
   
Mutex priority inheritance related question  
Here's a question that came up while teaching priority inheritance w.r.t. mutex locking in class today...

Let's say thread A is RUNNING and B is READY both at priority 21 on a single core system. We also have C at priority 10 
with a mutex locked. Thread A calls pthread_mutex_lock() to lock that mutex. A of course MUTEX blocks and C's priority 
is bumped up to 21. The question is who runs when C unlocks the mutex, A or B? B is next in the list but will A be moved
 to the head of the list and run instead?

Thanks,
Steve
Re: Mutex priority inheritance related question  
My vote is B.

________________________________
From: Steven Dufresne <community-noreply@qnx.com>
Sent: April 22, 2021 2:57:41 PM
To: ostech-core_os
Subject: Mutex priority inheritance related question

Here's a question that came up while teaching priority inheritance w.r.t. mutex locking in class today...

Let's say thread A is RUNNING and B is READY both at priority 21 on a single core system. We also have C at priority 10 
with a mutex locked. Thread A calls pthread_mutex_lock() to lock that mutex. A of course MUTEX blocks and C's priority 
is bumped up to 21. The question is who runs when C unlocks the mutex, A or B? B is next in the list but will A be moved
 to the head of the list and run instead?

Thanks,
Steve



_______________________________________________

OSTech
http://community.qnx.com/sf/go/post121391
To cancel your subscription to this discussion, please e-mail ostech-core_os-unsubscribe@community.qnx.com
Attachment: HTML sf-attachment-mime39966 2.03 KB
Re: Mutex priority inheritance related question  
When the mutex is unlocked, A goes to the end of the READY queue and B becomes running again right away.
Re: Mutex priority inheritance related question  
The effect can be observed when you take a kernel event trace of the attached demo program.
Attachment: Text zzzmutex.c 2.43 KB
Re: Mutex priority inheritance related question  
Thanks Thomas.

I was going to try that this afternoon after class but you beat me to it. Nice test sample.

-Steve
Re: Mutex priority inheritance related question  
Generally speaking a thread that blocks goes to the end of the ready queue when it unblocks (there are a few exceptions)
. Whether it runs after all threads ahead of it in the queue at the same priority level may depend on other factors 
(such as runmasks and APS partitions). Moreover, you can have thread B put into execution on CPU 1, followed by thread A
 put into execution on CPU 2, and almost immediately a higher priority thread preempting B on CPU 1, before B could 
execute a single instruction. 

Reasoning about order of execution is really hard, especially on SMP systems.

--Elad
Re: Mutex priority inheritance related question  
Thanks Elad. Thanks Guys. I passed that on to the customer.