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

/****************************************************************************/
/*					Local prototypes										*/
/****************************************************************************/

static void		SemaEval( void *par1, void *par2 );

/****************************************************************************/
/*					Public API functions									*/
/****************************************************************************/

/** @addtogroup bx_sema */
/** @{ */

/**
*	Add resources to a semaphore.
*
*	The counter of the semaphore will be incremented by the amount specified
*	by the parameter. If the increment would overflow the semaphore's counter,
*	then the counter will be set to its maximum value (0xffffffff).
*
*	@param	sema	Pointer to a semaphore
*	@param	value	The number of resources to be added to the semaphore
*
*	@retval	>0				Success, the semaphore count was increased
*	@retval BX_ERR_OBJECT	The semaphore was not initialised.
*
*	@attention
*		This function can be called from user, ISR or delayed ISR context.
*		This function does not suspend the caller but can cause a context
*		switch if a higher priority process is woken up by the call.
*/

int		BX_SemaPut( BX_SEMA *sema, unsigned int value )
{
unsigned int	cpsr;
unsigned int	cntr;

	if ( sema->base.type != BX_TYPE_SEMA ) return( BX_ERR_OBJECT );
	if ( ! value ) return( +1 );

	// Disable interrupts and add the resources to the semaphore

	cpsr = _bx_IrqDis();
	cntr = sema->cntr;

	if ( value > ~cntr )

		cntr += ~cntr;
	else
		cntr += value;

	sema->cntr = cntr;

	if ( ! sema->base.head || sema->base.head->ramt > cntr ) {

		// No process on the semaphore is woken up, nothing more to do.

		_bx_IrqRes( cpsr );
	}
	else {

		// The semaphore must be evaluated, queue a kernel event

		_bx_QueueEvent( cpsr, SemaEval, sema, NULL );
	}

	return( +1 );
}

/** @} */

/****************************************************************************/
/*					Local functions											*/
/****************************************************************************/

/*
*	Evaluates if the semaphore has enough resources to wake up a process
*	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

static void	SemaEval( void *par1, void *par2 )
{
BX_SEMA	*sema = par1;
BX_PROC	*proc;
int		cpsr;

	(void) par2;

	for ( ;; ) {

		// Disable the interrupts for the test

		cpsr = _bx_IrqDis();

		// Let's see if the first process can be started

		if ( ! ( proc = sema->base.head ) || proc->ramt > sema->cntr ) {

			// No process is waiting for the semaphore or the
			// first process can not get the semaphore, we're done.

			break;
		}

		// The next process can be started. Take its request from
		// the semaphore and unlink it from the waiting queue.

		sema->cntr -= proc->ramt;
		_bx_ProcDel( &sema->base, proc );

		// If it has a timeout, we have to deactivate it now

		if ( proc->timo.stat != TIMER_CANCEL &&
			 proc->timo.stat != TIMER_UNUSED ) {

			proc->timo.stat = TIMER_CANCEL;
			_bx_TimerProcess( &proc->timo, (void *) 1 );
		}

		// Add the process to the ready queue. We can enable interrupts now.

		_bx_IrqEnaSys( cpsr );
		_bx_ProcRestart( proc, NULL );
	}
}

