/****************************************************************************/
/*	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"

static void	_bx_PoliInsertPinh( BX_WAIT *wait, BX_PROC *proc );
static void	_bx_PoliDeletePinh( BX_WAIT *wait, BX_PROC *proc );

static BX_PROC	*_bx_PoliChangePinh( BX_WAIT *wait, BX_PROC *proc,
									 unsigned *prio );

/*
*	Function table for the priority inheritance policy
*/

const struct _bx_poli _bx_poli_ppin = {

	.ins = _bx_PoliInsertPinh,
	.del = _bx_PoliDeletePinh,
	.chg = _bx_PoliChangePinh,
	.add = _bx_PmutAdd,
	.rem = _bx_PmutRem,
	.mut = _bx_PmutMut
};

/*
*	Insertion onto a waiting queue
*	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

static void	_bx_PoliInsertPinh( BX_WAIT *wait, BX_PROC *proc )
{
	_bx_PoliInsertPrio( wait, proc );
	_bx_PmutIns( (BX_PMUT *) wait, proc->epri );
}

/*
*	Removal from a waiting queue
*	~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

static void	_bx_PoliDeletePinh( BX_WAIT *wait, BX_PROC *proc )
{
BX_PMUT		*pmut = (BX_PMUT *) wait;
unsigned	newp;

	_bx_PoliDelete( wait, proc );

	newp = wait->head ? wait->head->epri : 0;

	if ( pmut->prio != newp ) {

		// The object priority must change

		proc = (BX_PROC *) _bx_PmutMut( pmut, pmut->proc, &newp );
		if ( proc ) _bx_PmutPrio( (BX_PROC *) proc, &newp );
	}
}

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

static BX_PROC	*_bx_PoliChangePinh( BX_WAIT *wait, BX_PROC *proc,
									 unsigned *prio )
{
BX_PMUT	*pmut = (BX_PMUT *) wait;

	_bx_PoliChangePrio( wait, proc, prio );

	if ( pmut->prio != wait->head->epri ) {

		// The object's priority went up or down.

		*prio = wait->head->epri;

		if ( ! pmut->proc ) pmut->prio = *prio;
		return( pmut->proc );
	}
	else {

		return( NULL );
	}
}


