/****************************************************************************/
/*	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 "coop.h"

/** @addtogroup coop_time */
/** @{ */

/**
*	Timer tick function.
*
*	If you use timers, then calling this function informs the timer
*	subsystem that a certain amount of time has elapsed. Please read
*	the explanation @ref res for the BX_TimerTick() function in the
*	pre-emptive kernel, as the behaviour of this function is identical
*	to that one, except for the details on the scheduling aspects of
*	expired timers.
*
*	@param	us		The time elapsed since the last call, in microsecs.
*
*	@attention
*		This call must either be called from an interrupt service routine
*		or with interrupts disabled.
*/

void	BZ_TimerTick( unsigned us )
{
BZ_STAT	*base = &_bz_stat;
BZ_TIME	*this;

	// Update the uptime

	while ( ( base->uptu += us ) > 1000000 ) {

		base->uptu -= 1000000;
		base->upts += 1;
	}

	while ( ( this = base->tptr ) ) {

		if ( this->time > us ) {

			this->time -= us;
			break;
		}

		us -= this->time;

		BZ_EventPost( this->proc, 1 << this->sign );
		base->tptr = this->next;

		if ( this->rept ) {

			this->time = this->rept;
			_bz_TimerAdd( base, this );
		}
	}
}

/** @} */

