home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-11-19 | 5.0 KB | 143 lines | [TEXT/MPS ] |
- /*
- File: TInterruptSchedulerExample.cp
-
- Contains: This module shows an example use of the TInterruptScheduler class.
-
- Copyright: © 1993 by Apple Computer, Inc., all rights reserved.
-
- */
-
- #include "TInitSLM.h" // the TInitSLM class and SLM include files
- #include "TInterruptSchedulerExample.h"
-
- #include <Retrace.h>
-
- ///————————————————————————————————————————————————————————————————————————————————————
- /// CONSTANTS
- ///————————————————————————————————————————————————————————————————————————————————————
-
- const short kInterval = 1; // number of ticks between calls to the vbl
-
- ///————————————————————————————————————————————————————————————————————————————————————
- /// VBL RECORD
- ///————————————————————————————————————————————————————————————————————————————————————
-
- struct myvbl_record {
- VBLTask myVBLTask; // our vbl record
- TInterruptScheduler *fScheduler; // pointer to our interrupt Scheduler
- TVBLOperation *fVBLOp; // my operation to handle vbl tasks
- };
-
- ///————————————————————————————————————————————————————————————————————————————————————
- /// PROTOTYPES
- ///————————————————————————————————————————————————————————————————————————————————————
-
- static void do_vbl();
- static OSErr install_vbl( struct myvbl_record *myVBL );
- static OSErr deinstall_vbl( struct myvbl_record *myVBL );
- pascal struct myvbl_record *getvbl_record() = 0x2E88; // MOVE.L A0, (SP)
-
- /*————————————————————————————————————————————————————————————————————————————————————
- main
-
- This examples creates an TInterruptScheduler and installs a vbl task. The
- vbl task has access to the InterruptScheduler and the pointer to a TVBLOperation
- object which will handle the processing that is necessary each time the vbl is called.
- When the vbl kicks in, it checks to see if interrupt Scheduler is currenlty running.
- If not, it goes ahead and schedules a the TVBLOperation. When it comes time
- to process the scheduled operation (TVBLOperation), the operation increments its
- fCount field. We allow the vbl to operate in this way for 2 seconds. Since the
- vbl fires every 60th of a second it should end up firing about 120 times.
- ————————————————————————————————————————————————————————————————————————————————————*/
- main()
- {
- TInitSLM initLibraryManager; // initialize the shared library manager
-
- if( initLibraryManager.Failed() ) // If we failed, let's go home
- return 1;
-
- TVBLOperation *vblOp;
- TInterruptScheduler *scheduler;
-
- if( scheduler = new TInterruptScheduler )
- if( vblOp = new TVBLOperation() ) {
- vblOp->SetPriority( kNormalPriority );
-
- struct myvbl_record myvbl; // our stack based vbl record
-
- // store the pointer to the scheduler and the operation in the
- // vbl record so the vbl can access them.
- myvbl.fScheduler = scheduler;
- myvbl.fVBLOp = vblOp;
-
- install_vbl( &myvbl ); // go ahead and start the vbl
-
- // Let our vbl run for 2 seconds. This should be enough time
- // for our TVBLOperation to be process about 120 times.
- TStopwatch stopwatch;
- while (stopwatch.ElapsedSeconds() < 2);
- cout << "Our process proc was call " << vblOp->GetCount() << " times." << endl;
-
- deinstall_vbl( &myvbl ); // stop the vbl
-
- delete vblOp; // free the space
- delete scheduler; // free the space
- }
-
- return 0;
- }
-
- /*————————————————————————————————————————————————————————————————————————————————————
- do_vbl
-
- this is our VBL task.
- ————————————————————————————————————————————————————————————————————————————————————*/
-
- static void do_vbl()
- {
- // This is the routine that's called each time the vbl fires. Since vbl's run
- // with interrupts disabled, we want to defer doing any heavy duty processing
- // by scheduling an operation on an interrupt scheduler to do all the work.
-
- struct myvbl_record *myvbl;
-
- myvbl = getvbl_record();
-
- if( ! InInterruptScheduler() ) // is interrupt Scheduler currently running?
- // now we are going to reschedule the operation
- myvbl->fScheduler->Schedule( myvbl->fVBLOp );
-
- myvbl->myVBLTask.vblCount = kInterval; // reset the vbl interval
- }
-
- /*————————————————————————————————————————————————————————————————————————————————————
- install_vbl
-
- initializing and installing a task record. This vbl task is going to schedule our
- cursor animating operation.
- ————————————————————————————————————————————————————————————————————————————————————*/
-
- static OSErr install_vbl( struct myvbl_record *myVBL )
- {
- myVBL->myVBLTask.qType = vType; // set queue type
- myVBL->myVBLTask.vblAddr = NewVBLProc(do_vbl); // set address of VBL Task
- myVBL->myVBLTask.vblCount = kInterval; // set task frequency
- myVBL->myVBLTask.vblPhase = 0; // no phase
-
- return VInstall( (QElemPtr)&myVBL->myVBLTask ); // install it dude
- }
-
- /*————————————————————————————————————————————————————————————————————————————————————
- deinstall_vbl
-
- remove our vbl task
- ————————————————————————————————————————————————————————————————————————————————————*/
- static OSErr deinstall_vbl( struct myvbl_record *myVBL )
- {
- OSErr err = VRemove( (QElemPtr)&myVBL->myVBLTask );
- DisposeRoutineDescriptor( (QElemPtr)&myVBL->vblAddr );
- return err;
- }
-
-
-