#if 0
/****************************************************************************/
/*	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		LockWait( void *par1, void *par2 );

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

/** @addtogroup bx_lock */
/** @{ */

/**
*	Obtains a lock.
*
*	This function obtains a lock. If the lock is available, then the caller
*	will own the lock. If the lock is not available, then the caller will
*	be suspended and placed on the waiting queue of the lock.
*
*	@param	lock			Pointer to the lock to get
*
*	@param	timeout			Timeout value. If it is 0xffffffff, that is, -1
*							cast to unsigned, then there is not time limit
*							in waiting for the lock. If it is any other value,
*							then it is the timeout in microseconds. If the
*							caller can not get the lock within that time, then
*							the call will return and indicate timeout. If the
*							timeout is 0, then the caller will not be suspended
*							and the function returns immediately if the lock
*							is not available.
*
*	@retval	>0				The lock was obtained by the caller
*	@retval 0				Timeout, the lock was not takem
*	@retval	BX_ERR_ISOWNER	The lock was already owned by the caller
*	@retval	BX_ERR_CONTEXT	The function was called from an ISR context
*	@retval BX_ERR_OBJECT	The passed object is not a lock or it was not
*							initialised.
*	@retval BX_ERR_SUSPEND	The caller should be suspended but can not because
*							its protection levels is higher than BX_PROT_COOP.
*
*	@note
*		Although the timeout value is specified in microseconds, the actual
*		resolution depends on the frequency of calls to the BX_TimerTick()
*		function.
*
*	@attention
*		This function should not be called from an ISR or delayed ISR.
*		This function can suspend the caller.
*/

int		BX_LockObtain( BX_LOCK *lock, unsigned timeout )
{
int		cpsr;
int		retv;
BX_PROC	*self;

	(void) timeout;	// FIXME

	if ( ! ( self = BX_ProcSelf() ) ) {

		// Not a call from userland

		return( BX_ERR_CONTEXT );
	}

	cpsr = _bx_IrqDis();
	retv = _bx_LockTest( lock, self );

	if ( retv ) {

		// We got the lock or an error occured.

		_bx_IrqEnaUsr( cpsr );
		return( retv );
	}

	// We must add ourselves to the lock's waiting list

	if ( self->prot > BX_PROT_COOP ) {

		// We can not suspend

		_bx_IrqEnaUsr( cpsr );
		return( BX_ERR_SUSPEND );
	}

	_bx_QueueEvent( cpsr, LockWait, lock, self );
	return( +1 );
}

/** @} */

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

/*
*	Adds a process (or proxy) to the waiting queue of a lock
*	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*	The lock is par1, the process or proxy is par2. This function is
*	always called from the kernel's event queue handler.
*/

static void		LockWait( void *par1, void *par2 )
{
BX_LOCK		*lock = par1;
BX_PROC 	*proc = par2;

	// We must suspend the process that goes to wait for the lock

	_bx_ProcSuspend( proc, /*NULL*/ (void*)2 );

	// We then add the process to the lock's waiting list.
	// It may cause a priority adjustment cascade.

	(*lock->pmut.wait.ftab->ins)( &lock->pmut.wait, proc );
}
#endif

