[comp.sys.mac.programmer] Please help: Vertical Retrace Manager.

galetti@uservx.afwl.af.mil (05/08/91)

I'm having problems with the vertical retrace manager.  My code is running on
a Mac IIfx under MPW C.  I'll include the code below and then describe what's
happening.  Here's the code:

<----------------------------- CODE STARTS HERE --------------------------->

/* First I define the following *global* variables: */

VBLTask		plotTask, *pTptr;
QElemPtr	pTaddress;
Ptr		tempptr;

/* Then, I allocate a non-relocatable block to hold the VBLTask structure. */
/* I do this because I don't want this structure moved around. */

tempptr = NewPtr(sizeof(VBLTask));
pTptr = (VBLTask *) tempptr;
pTaddress = (QElemPtr) pTptr;	/* All three of the above variables are
					the same thing, just different types */


/* The lines below initialize the VBLTask structure */

pTptr->qType = vType;
pTptr->vblAddr = (VBLProcPtr) PlotRoutine; /* PlotRoutine() is defined below */
pTptr->vblCount = plotperiod;
pTptr->vblPhase = 0; /* Different values here don't cure the problem */

code = VInstall(pTaddress);	/* Starts PlotRoutine() running on a 
					regular interval. */

/* Rest of code does other things, and then calls VRemove(pTaddress) before
	ending */


void PlotRoutine()
{
	long	myA5, oldA5;

	myA5 = SetCurrentA5();	/* As the MPW OSUtils include file dictates */
	pTptr->vblCount = plotperiod; /* Reinitialize this element of the
						VBLTask structure, otherwise
						this task will be removed
						from the queue. */
	printf("From Sub: %d\n",pTptr->vblCount);
	SysBeep(40);
	oldA5 = SetA5(myA5);	/* Again, according to MPW include file */
}

<----------------------- CODE ENDS HERE --------------------------------->

Here's what's happening:  When VInstall is called, the system starts beeping,
as I expect it to.  But after a few beeps, it stops.  When I call VRemove(),
I find that the VBLTask has ALREADY been removed from the queue.  Also, I
noticed that there is always one less printed output "From Sub:" than there
is the number of beeps that I hear.

What is going on here?  I know that there are certain routines that you can't
call from interrupts, namely routines that move memory.  I don't know if 
printf indirectly calls any of these routines or not, but even without the
printf statement, the task is still inadvertently removed from the queue.

All help will be GREATLY appreciated!

Thank you in advance,

-Ralph Galetti
  ___________________________________________________________________________
 /   Ralph Galetti                  Internet:   galetti@uservx.afwl.af.mil   \
|    PL/LITT                        Interests:  computers, music, computers   |
|    Kirtland AFB, NM 87117-6008                and music, golf, sleep.       |
 \__"No, they couldn't actually prove that it was HIS vomit" - Nigel Tufnel__/