home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / Apple Shared Library Manager / ASLM Examples / Example Tools / Sources / TInterruptSchedulerExample.cp < prev    next >
Encoding:
Text File  |  1996-11-19  |  5.0 KB  |  143 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        TInterruptSchedulerExample.cp
  3.  
  4.     Contains:    This module shows an example use of the TInterruptScheduler class.
  5.  
  6.     Copyright:    © 1993 by Apple Computer, Inc., all rights reserved.
  7.  
  8. */
  9.  
  10. #include "TInitSLM.h"            // the TInitSLM class and SLM include files
  11. #include "TInterruptSchedulerExample.h"
  12.  
  13. #include <Retrace.h>
  14.  
  15. ///————————————————————————————————————————————————————————————————————————————————————
  16. ///    CONSTANTS
  17. ///————————————————————————————————————————————————————————————————————————————————————
  18.  
  19. const short     kInterval = 1;            // number of ticks between calls to the vbl
  20.  
  21. ///————————————————————————————————————————————————————————————————————————————————————
  22. /// VBL RECORD
  23. ///————————————————————————————————————————————————————————————————————————————————————
  24.  
  25. struct myvbl_record {
  26.     VBLTask                myVBLTask;        // our vbl record
  27.     TInterruptScheduler *fScheduler;    // pointer to our interrupt Scheduler        
  28.     TVBLOperation        *fVBLOp;        // my operation to handle vbl tasks
  29. };
  30.  
  31. ///————————————————————————————————————————————————————————————————————————————————————
  32. ///    PROTOTYPES
  33. ///————————————————————————————————————————————————————————————————————————————————————
  34.  
  35. static void do_vbl();
  36. static OSErr install_vbl( struct myvbl_record *myVBL );
  37. static OSErr deinstall_vbl( struct myvbl_record *myVBL );
  38. pascal struct myvbl_record *getvbl_record() = 0x2E88;    // MOVE.L A0, (SP) 
  39.  
  40. /*————————————————————————————————————————————————————————————————————————————————————
  41.     main 
  42.     
  43.     This examples creates an TInterruptScheduler and installs a vbl task. The
  44.     vbl task has access to the InterruptScheduler and the pointer to a TVBLOperation
  45.     object which will handle the processing that is necessary each time the vbl is called.
  46.     When the vbl kicks in, it checks to see if interrupt Scheduler is currenlty running.
  47.     If not, it goes ahead and schedules a the TVBLOperation. When it comes time
  48.     to process the scheduled operation (TVBLOperation), the operation increments its
  49.     fCount field. We allow the vbl to operate in this way for 2 seconds. Since the
  50.     vbl fires every 60th of a second it should end up firing about 120 times.
  51. ————————————————————————————————————————————————————————————————————————————————————*/
  52. main() 
  53. {    
  54.     TInitSLM initLibraryManager;            // initialize the shared library manager
  55.     
  56.     if( initLibraryManager.Failed() )        // If we failed, let's go home
  57.         return 1;
  58.     
  59.     TVBLOperation            *vblOp;
  60.     TInterruptScheduler     *scheduler;
  61.         
  62.     if( scheduler = new TInterruptScheduler )
  63.         if( vblOp = new TVBLOperation() ) {
  64.             vblOp->SetPriority( kNormalPriority );
  65.                                     
  66.             struct myvbl_record myvbl;    // our stack based vbl record
  67.             
  68.             // store the pointer to the scheduler and the operation in the
  69.             // vbl record so the vbl can access them.
  70.             myvbl.fScheduler = scheduler;
  71.             myvbl.fVBLOp = vblOp;
  72.         
  73.             install_vbl( &myvbl );        // go ahead and start the vbl
  74.             
  75.             // Let our vbl run for 2 seconds. This should be enough time
  76.             // for our TVBLOperation to be process about 120 times.
  77.             TStopwatch stopwatch;
  78.             while (stopwatch.ElapsedSeconds() < 2);
  79.             cout <<  "Our process proc was call " << vblOp->GetCount() << " times." << endl;
  80.                             
  81.             deinstall_vbl( &myvbl );    // stop the vbl
  82.                 
  83.             delete vblOp;            // free the space
  84.             delete scheduler;        // free the space
  85.         }
  86.         
  87.     return 0;
  88. }
  89.  
  90. /*————————————————————————————————————————————————————————————————————————————————————
  91.     do_vbl
  92.     
  93.     this is our VBL task.
  94. ————————————————————————————————————————————————————————————————————————————————————*/
  95.  
  96. static void do_vbl()
  97. {
  98.     // This is the routine that's called each time the vbl fires. Since vbl's run
  99.     // with interrupts disabled, we want to defer doing any heavy duty processing
  100.     // by scheduling an operation on an interrupt scheduler to do all the work.
  101.     
  102.     struct myvbl_record *myvbl;
  103.     
  104.     myvbl = getvbl_record();
  105.  
  106.     if( ! InInterruptScheduler() )        // is interrupt Scheduler currently running?
  107.         // now we are going to reschedule the operation
  108.         myvbl->fScheduler->Schedule( myvbl->fVBLOp );
  109.  
  110.     myvbl->myVBLTask.vblCount = kInterval;    // reset the vbl interval
  111. }
  112.  
  113. /*————————————————————————————————————————————————————————————————————————————————————
  114.     install_vbl
  115.     
  116.     initializing and installing a task record. This vbl task is going to schedule our
  117.     cursor animating operation.
  118. ————————————————————————————————————————————————————————————————————————————————————*/
  119.  
  120. static OSErr install_vbl( struct myvbl_record *myVBL )
  121. {
  122.     myVBL->myVBLTask.qType = vType;            // set queue type
  123.     myVBL->myVBLTask.vblAddr = NewVBLProc(do_vbl);        // set address of VBL Task
  124.     myVBL->myVBLTask.vblCount = kInterval;    // set task frequency
  125.     myVBL->myVBLTask.vblPhase = 0;            // no phase
  126.     
  127.     return VInstall( (QElemPtr)&myVBL->myVBLTask );    // install it dude
  128. }
  129.  
  130. /*————————————————————————————————————————————————————————————————————————————————————
  131.     deinstall_vbl
  132.     
  133.     remove our vbl task
  134. ————————————————————————————————————————————————————————————————————————————————————*/
  135. static OSErr deinstall_vbl( struct myvbl_record *myVBL )
  136. {
  137.     OSErr err = VRemove( (QElemPtr)&myVBL->myVBLTask );
  138.     DisposeRoutineDescriptor( (QElemPtr)&myVBL->vblAddr );
  139.     return err;
  140. }
  141.  
  142.  
  143.