settimer.c 2.51 KB

/**************************************************************************
 *									  *
 *		 Copyright (C) 1994, Silicon Graphics, Inc.		  *
 *									  *
 *  These coded instructions, statements, and computer programs  contain  *
 *  unpublished  proprietary  information of Silicon Graphics, Inc., and  *
 *  are protected by Federal copyright law.  They  may  not be disclosed  *
 *  to  third  parties  or copied or duplicated in any form, in whole or  *
 *  in part, without the prior written consent of Silicon Graphics, Inc.  *
 *									  *
 **************************************************************************/


#include <rmon.h>
#include "osint.h"
#include "viint.h"

/*
 * This routine initializes the timer structure and starts the timer t.
 * It implements both count-down timer and interval timer.
 * If value is non-zero, it indicates the time to the next timer expiration.
 * If interval is non-zero, it specifies a value to be used in reloading
 * value when the timer expires.
 */
int
osSetTimer(OSTimer *t, OSTime value, OSTime interval,  OSMesgQueue *mq,
	   OSMesg msg)
{

	OSTime tim;
#if 1
	OSTimer *temp;
	u32	count, elapsed_cycles;
	u32 savedMask;
#endif

#ifdef _DEBUG
	/* Check that VI manager is running */

	if (!__osViDevMgr.active) {
	    __osError(ERR_OSSETTIMER, 0);
	    return(0);
	}
#endif
	/* Initialize timer structure */

	t->next = (OSTimer *)NULL;
	t->prev = (OSTimer *)NULL;
	t->interval = interval;
	t->value = (value? value : interval);
	t->mq = mq;
	t->msg = msg;
#if 0
	/* insert timer structure to timer list */

	tim = __osInsertTimer(t);

	/*
	 * If we've just put something at the head of the list,
	 * insure that a timer will go off at the right moment.
	 */

	if (__osTimerList->next == t)
		__osSetTimerIntr(tim);


#else
	savedMask = __osDisableInt();
	/* insert timer structure to timer list */
	if (__osTimerList->next == __osTimerList) {
	    ;
	}
	else {
	    temp = __osTimerList->next;
	    count = osGetCount();
	    elapsed_cycles = count - __osTimerCounter;
	    /* Have enough ticks gone by since timer was set? */
	    
	    if ((OSTime)elapsed_cycles < temp->value) {
		/* not yet ready for next item on list */
		temp->value -= (OSTime)elapsed_cycles;
	    } else {
	      temp->value = (OSTime)1;
	      /* error */
	    }
	    
	}
	tim = __osInsertTimer(t);

	/*
	 * If we've just put something at the head of the list,
	 * insure that a timer will go off at the right moment.
	 */
	__osSetTimerIntr(__osTimerList->next->value);
	__osRestoreInt(savedMask);

#endif
	return(0);
}