Project Home
Project Home
Trackers
Trackers
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - how works Thread Inversion? : (5 Items)
   
how works Thread Inversion?  
Hello,

I have a little question for which I didn't found an answer in the forum. I hope this is the correct place to ask.

I have very much Threads. Most of them with Prio 10.
I also have:
Thread A with Prio 10
Thread B with Prio 9
 
Thread B has some memory in use.
Thread A gets the CPU and wants to get the memory which is in use by Thread B.
Right now, Thread A sets a "abort"-Flag   for Thread B. 
When Thread B gets the CPU it will soon proof the "abort"-Flag. If it is set, Thread B will free its used memory and 
stop.
The PROBLEM is, that Thread B wont get the CPU if CPU usage is 100%, because its Priority is to low.

May I use mutexes in both threads to activate Priority Inversion what will boost the Thread B Prio to 10.
e.g.:
- "myMutex" will be blocked by Thread B.
- Thread A wants to block "myMutex"
--> QNX will use Priority Inversion to boost Thread B
- Thread B will release memory
- QNX will decrease Thread B Prio again.
- Thread A may block "myMutex" and get the Memory.

Will that work? In the manual is another example and I dont know if it will work unfailing in my case.
I hope I described my question understandable.

Thank you
Greetings
Fabian
Re: how works Thread Inversion?  
Hi Fabian,

the term "priority inversion" describes the effect that -in your case- high priority thread A would effectively be 
'downgraded' to priority 9, waiting for thread B to release the memory.

Mutexes by default implement 'priority inheritance' to address this problem: As soon as thread A tries to lock the mutex
 held by thread B, the latter will temporarily have its prority raised from its own 9 to A's 10 until it unlocks the 
mutex. This way, all processing necessary to let thread A continue is done at its (high) priority.

In fact, you should use a mutex anyway - not merely to prevent priority inversion. The mutex is necessary to ensure only
 one thread will be accessing your "abort-flag", a shared resource, at any one time. Be aware that on an SMP system, 
both threads might well be executing in parallel, although they have different prorities.

Kind regards,
- Thomas
Re: how works Thread Inversion?  
Hello Thomas,

thank you for your explanation. So it seems I used the wrong term.

But if I understood you correct, than it will work the way I described. 
Besides the fact, that I should use mutexes anyway if I use "shared" sources.

I just wondered how QNX knows that Thread B (prio 9) must be bossted. Because QNX cant know that the CPU load would be 
100% for a long time and the Thread B wouldn't get the CPU with the low prio.

Greetings Fabian
Re: how works Thread Inversion?  
Hi Fabian,

yes, it will work the way you described, and yes, whenever accessing shared resources, you should take care that this 
access will take place in a way that guarantees data integrity. Sometimes it can be sufficient to use atomic operations 
(atomic_*()-functions), often you'll need mutexes, sometimes condvars are appropriate, sometimes it even makes sense to 
use message passing for synchronization.

Regarding your question how QNX 'knows' that thread B (low priority) must be boosted - there are two aspects to it:
- How can the OS know it's thread B that must be boosted and
- how can it tell B needs any boosting at all?

For the first aspect, when thread A tries to lock the mutex, the OS knows that thread B currently owns the mutex. It 
also knows that B has a lower effective priority than A and thus needs a boost.

As for the second aspect, we can distinguish two cases:
- Some thread with a priority > B wants the CPU before B releases the mutex
- No higher-priority (than B) thread wants the CPU

The OS cannot know (at the time when A becomes MUTEX-blocked) what will happen - so the best solution is to always boost
. If a third thread with a priority > B and <= A does become READY, priority inversion will be avoided by boosting. If 
no one else wants the CPU, it doesn't matter what priority B is running at, anyway.

Of course, in the latter case we boost 'too much' - we could spare the effort of adjusting B's priority. But the actual 
effort to do so is minimal, much less than determining whether B needs boosting whenever another thread gets READY while
 B still holds the mutex.

Regards,
- Thomas
Re: how works Thread Inversion?  
Hi again.
Thank you very much for the fast informations. 
I think this will be the correct solution for me. I will try it that way.

Greetings Fabian