/****************************************************************************/
/*	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_sigs */
/** @{ */

/**
*	Sends one or more event signals to a process.
*
*	@param	proc	Pointer to the process' descriptor
*	@param	sigs	The event set to send
*
*	@retval >0				Success
*	@retval	BX_ERR_OBJECT	The proc argument is not a valid process descriptor
*
*	@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_EventPost( BX_PROC *proc, int sigs )
{
int		wait;
int		cpsr;

	// Check if the process is really a process

	if ( proc->type != BX_TYPE_PROC ) return( BX_ERR_OBJECT );

	// Delete the always present signal from the set

	sigs &= ~BX_EVENT_ALWAYS;

	// If there's nothing to deliver, we succeed immediately

	if ( ! sigs ) return( +1 );

	// We have to disable the interrupts

	cpsr = _bx_IrqDis();

	// Add the signal(s) to the process' current set and
	// fetch the current wait set mask from the process

	sigs |= proc->sigs;
	proc->sigs = sigs;
	wait  = proc->mask;

	if ( wait && ( wait & sigs ) ) {

		// The process was waiting and it got at least one signal it was
		// waiting for. Indicate that it's not waiting any more and wake
		// it up.

		proc->mask = 0;
		_bx_QueueEvent( cpsr, _bx_ProcRestart, proc, NULL );
	}
	else {

		// We have nothing else to do, just restore the interrupt

		_bx_IrqRes( cpsr );
	}

	return( +1 );
}

/** @} */

