/****************************************************************************/
/*	Copyright (c) 2009, Bendor Research Pty. Ltd. All rights reserved.		*/
/*	This software is released under the GNU General Public License, version	*/
/*	3, with the exemptions that 1) the header files installed by this 	 	*/
/*	package can be included in closed-source code and 2) the libraries		*/
/*	built by this package can be statically linked into a closed-source 	*/
/*	binary. See the file LICENSE for the details.							*/
/*	This software is provided "AS IS", without any warranty or guarantee.	*/
/*	By using this software you acknowledge that neither Bendor Research nor	*/
/*	any of its personnel or affiliates can be held liable for any damages,	*/
/*	including, but not limited to, loss of business, loss of money, loss of */
/*	life, loss of reputation, arising out of this software being faulty or	*/
/*	not performing as advertised, even if Bendor Research was aware of such	*/
/*	faults in the software. You use this software at your own risk, any and */
/*	all liabilities that arise out of your using this software are strictly	*/
/*	yours.																	*/
/****************************************************************************/

#include "kernel.h"

/*
*	The priority of an object on the queue is to be changed
*	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

BX_PROC	*_bx_PoliChangePrio( BX_WAIT *wait, BX_PROC *proc, unsigned *prio )
{
unsigned	oldp, newp;
BX_PROC		*this;

	// Find the place where the process should be on the waiting queue
	// after the priority change

	oldp = proc->epri;
	newp = *prio;

	if ( oldp == newp ) {

		// No change, do nothing.

		return( NULL );
	}

	// Now move the process to its new position

	if ( oldp < newp ) {

		// New priority is higher, the process moves towards
		// the head of the queue.

		for ( this = proc ;
			  this->prev && this->prev->epri < newp ;
			  this = this->prev );

		if ( this != proc ) {

			// We have to unlink the process and insert
			// it in front of the 'this' pointer

			_bx_ProcDel( wait, proc );
			_bx_ProcInsBefore( wait, proc, this );
		}
	}
	else {

		// New priority is lower, the process moves towards
		// the tail of the queue.

		for ( this = proc ;
			  this->next && this->next->epri > newp ;
			  this = this->prev );

		if ( this != proc ) {

			// We have to unlink the process and insert
			// it behind the 'this' pointer

			_bx_ProcDel( wait, proc );
			_bx_ProcInsAfter( wait, proc, this );
		}
	}

	// Set the priority of the process

	proc->epri = newp;

	// The change on the list doesn't change the priority of the object owner

	return( NULL );
}

