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

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

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

/**
*	Initialises a semaphore.
*
*	@param	sema	Pointer to the semaphore structure
*	@param	value	Initial value (resource count) of the semaphore
*	@param	policy	The semaphore queueing policy. It must be one of
*					BX_POLICY_FIFO, BX_POLICY_PRIO, BX_POLICY_LRQF or
*					BX_POLICY_SRQF.
*
*	@retval	>0				Success
*	@retval	BX_ERR_PARAM	The policy parameter is not valid for a sempaphore
*
*	@attention
*		This function does not suspend the caller.
*		This function should not be called from an ISR or delayed ISR.
*		Note, however, that the function does not check this criteria,
*		to allow initialisation of semaphores before the kernel is
*		started (which must happen in IRQ context). In addition, during
*		the initialisation interrupts are not disabled.
*/

int		BX_SemaInit( BX_SEMA *sema, BX_POLICY policy, unsigned int value )
{
	switch ( policy ) {

		case BX_POLICY_FIFO:

			sema->base.ftab = (struct _bx_poli *) &_bx_poli_fifo;
			break;

		case BX_POLICY_SRQF:

			sema->base.ftab = (struct _bx_poli *) &_bx_poli_srqf;
			break;

		case BX_POLICY_LRQF:

			sema->base.ftab = (struct _bx_poli *) &_bx_poli_lrqf;
			break;

		case BX_POLICY_PRIO:

			sema->base.ftab = (struct _bx_poli *) &_bx_poli_prio;
			break;

		default:

			sema->base.type = 0;
			return( BX_ERR_PARAM );
	}

	sema->base.head = NULL;
	sema->base.tail = NULL;
	sema->base.type = BX_TYPE_SEMA;
	sema->cntr = value;
	return( 1 );
}

/** @} */

