Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - sched_thread() : (5 Items)
   
sched_thread()  
In ker/nano_sched.c sched_thread() 

Why does a thread goes to the head of the priority queue in case of a lower priority?

if (new_prio != thp->priority) {
	int	lowered = (new_prio < thp->priority);

		adjust_priority(thp, new_prio, thp->dpp, 0);

	// If we lowered priority, ensure we will be head of priority list
	if (lowered && thp->state == STATE_READY) {
		LINK3_REM(DISPATCH_LST(thp->dpp, thp->priority), thp, THREAD);
		LINK3_BEG(DISPATCH_LST(thp->dpp, thp->priority), thp, THREAD);
	}
}

Thanks
Sönke
Re: sched_thread()  
SCHED_SETPRIO is used for pthread_setschedprio() - for some reason this is not doc'd

http://www.opengroup.org/onlinepubs/009695399/functions/pthread_setschedprio.html

The rationale says it all...

RATIONALE

     The pthread_setschedprio() function provides a way for an application to temporarily raise its priority and then 
lower it again, without having the undesired side effect of yielding to other threads of the same priority. This is 
necessary if the 
application is to implement its own strategies for bounding priority inversion, such as priority inheritance or priority
 ceilings. This capability is especially important if the implementation does not support the Thread Priority Protection
 or Thread 
Priority Inheritance options, but even if those options are supported it is needed if the application is to bound 
priority inheritance for other resources, such as semaphores.

     The standard developers considered that while it might be preferable conceptually to solve this problem by 
modifying the specification of pthread_setschedparam(), it was too late to make such a change, as there may be 
implementations that would 
need to be changed. Therefore, this new function was introduced.

Sönke Nielsen wrote:
> In ker/nano_sched.c sched_thread() 
> 
> Why does a thread goes to the head of the priority queue in case of a lower priority?
> 
> if (new_prio != thp->priority) {
> 	int	lowered = (new_prio < thp->priority);
> 
> 		adjust_priority(thp, new_prio, thp->dpp, 0);
> 
> 	// If we lowered priority, ensure we will be head of priority list
> 	if (lowered && thp->state == STATE_READY) {
> 		LINK3_REM(DISPATCH_LST(thp->dpp, thp->priority), thp, THREAD);
> 		LINK3_BEG(DISPATCH_LST(thp->dpp, thp->priority), thp, THREAD);
> 	}
> }
> 
> Thanks
> Sönke
> 
> 
> 
> _______________________________________________
> 
> OSTech
> http://community.qnx.com/sf/go/post36495

-- 
cburgess@qnx.com
RE: sched_thread()  
I've created a PR to get pthread_setschedprio() documented.

Steve Reid (stever@qnx.com)
Technical Editor
QNX Software Systems 
Re: sched_thread()  
Hello, 

Regarding Sönke's specific question:

> Why does a thread goes to the head of the priority queue in case of a lower priority?

Because POSIX wants it so. In process scheduling (2.8.4):

Under the SCHED_FIFO policy, the modification of the definitional thread lists is as follows:
...

8. If a thread whose policy or priority has been modified by pthread_setschedprio() is a running thread or is runnable, 
the effect on its position in the thread list depends on the direction of the modification, as follows:

      a. If the priority is raised, the thread becomes the tail of the thread list.
      b. If the priority is unchanged, the thread does not change position in the thread list.
      c. If the priority is lowered, the thread becomes the head of the thread list.

HTH,
Loïc
Re: sched_thread()  
Thanks for the answers.

Sönke