Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Question about pthread_setschedprio() implementation in the presence of priority inheritance primitives: (4 Items)
   
Question about pthread_setschedprio() implementation in the presence of priority inheritance primitives  
Hi,
I did a post over the weekend that I don't see in the list.  May be it is being moderated, which is fine.
In case it didn't get submitted for some reason, here is my question again:

pthread_setschedprio() is documented to put a thread at the end of the queue when it is being raised in priority and at 
the front of the queue when being lowered.  This works correctly as far as we can tell using System Profiler.

The question we are investigating is what happens to a thread that currently has its priority raised because of priority
 inheritance to avoid a priority inversion event. The pthread_mutex_lock/pthread_mutex_unlock pair would cause this when
 sharing a lock with two threads at different priority levels.
If pthread_setschedprio() is called by the lower priority thread, while it holds the lock and is has its priority raised
 from priority inheritance, there are two possible scenarios:
1: The priority of the thread drops immediately.
2: Only the base priority of the thread is changed, the effective priority remains the same until it reaches 
pthread_mutex_unlock.

It appears that #1 is implemented.  Is this function-as-designed?  also is this the specified behavior by the POSIX spec
.?

Is there an opportunity to implement #2.  The pseudo code would be:
pthread_setschedprio( new priority)
{
base_priority = new priority
effective priority = MAX (base priority, effective priority)
}

At the end of the priority inheritance event, the thread would then drop to its new base priority.

Any chance this is implemented with a special flag?

Thanks,
David Beberman
www.aicas.com
Re: Question about pthread_setschedprio() implementation in the presence of priority inheritance primitives  
Hi,
Any comments, responses, ideas on this question?

Thanks,
David
Re: Question about pthread_setschedprio() implementation in the presence of priority inheritance primitives  
Here is some pseudo-code and screen captures of the thread traces. Note this is on QNX 6.6 on an ARM/TI BSP, in case 
there is a BSP or CPU interaction involved:

Initial priority of threads:
T2=4, T3=5, T4=6

T2:
{
   // delay loop
   pthread_mutex_lock (&mutex);
   // delay loop
   pthread_setschedprio(pthread_self(), 3);
// delay loop
   pthread_mutex_unlock (&mutex);
   // endless  loop
}

T3:
{
    // endless loop
}

T4:
{
   // delay loop
   pthread_mutex_lock (&mutex);
   // delay loop
   pthread_mutex_unlock (&mutex);
   // endless loop
}


Basically the result is the following:.

1. if we don't change the priority in T2, the priority inheritance works properly. (Mutex_No_SetPrio_154837.png)
2. if we call pthread_setschedprio(3) in T2, then T2 gets blocked and T3 runs forever. (Mutex_SetPrio_155214.png)

Have tried several things such as FIFO, pthread_mutexattr_setprotocol(), etc. None of them can solve the problem.  

From the diagrams, looks like the pthread_setschedprio() is setting the effective priority, not the base priority and 
doing a max(base, effective).
Attachment: Image Mutex_No_SetPrio_154837.png 94.67 KB
Re: Question about pthread_setschedprio() implementation in the presence of priority inheritance primitives  
image with setprio
Attachment: Image Mutex_SetPrio_155214.png 87.25 KB