home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / useful / dev / c / rkrm / timer / multiple_timers.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  6KB  |  151 lines

  1. /*
  2.  * Copyright (c) 1992 Commodore-Amiga, Inc.
  3.  * 
  4.  * This example is provided in electronic form by Commodore-Amiga, Inc. for 
  5.  * use with the "Amiga ROM Kernel Reference Manual: Devices", 3rd Edition, 
  6.  * published by Addison-Wesley (ISBN 0-201-56775-X).
  7.  * 
  8.  * The "Amiga ROM Kernel Reference Manual: Devices" contains additional 
  9.  * information on the correct usage of the techniques and operating system 
  10.  * functions presented in these examples.  The source and executable code 
  11.  * of these examples may only be distributed in free electronic form, via 
  12.  * bulletin board or as part of a fully non-commercial and freely 
  13.  * redistributable diskette.  Both the source and executable code (including 
  14.  * comments) must be included, without modification, in any copy.  This 
  15.  * example may not be published in printed form or distributed with any
  16.  * commercial product.  However, the programming techniques and support
  17.  * routines set forth in these examples may be used in the development
  18.  * of original executable software products for Commodore Amiga computers.
  19.  * 
  20.  * All other rights reserved.
  21.  * 
  22.  * This example is provided "as-is" and is subject to change; no
  23.  * warranties are made.  All use is at your own risk. No liability or
  24.  * responsibility is assumed.
  25.  *
  26.  *****************************************************************************
  27.  *
  28.  *  Multiple_Timers.c
  29.  *
  30.  *  This program is designed to do multiple (3) time requests using one
  31.  *  OpenDevice.  It creates a message port - TimerMP, creates an
  32.  *  extended I/O structure of type timerequest named TimerIO[0] and
  33.  *  then uses that to open the device.  The other two time request
  34.  *  structures - TimerIO[1] and TimerIO[2] - are created using AllocMem
  35.  *  and then copying TimerIO[0] into them.  The tv_secs field of each
  36.  *  structure is set and then three SendIOs are done with the requests.
  37.  *  The program then goes into a while loop until all messages are received.
  38.  *
  39.  * Compile with SAS C 5.10  lc -b1 -cfistq -v -y -L
  40.  *
  41.  * Run from CLI only
  42.  */
  43.  
  44. #include <exec/types.h>
  45. #include <exec/memory.h>
  46. #include <devices/timer.h>
  47.  
  48. #include <clib/exec_protos.h>
  49. #include <clib/alib_protos.h>
  50.  
  51. #include <stdio.h>
  52.  
  53. #ifdef LATTICE
  54. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  55. int chkabort(void) { return(0); }  /* really */
  56. #endif
  57.  
  58. VOID main(VOID);
  59.  
  60. void main(void)
  61. {
  62. struct timerequest *TimerIO[3];
  63. struct MsgPort *TimerMP;
  64. struct Message *TimerMSG;
  65.  
  66. ULONG error,x,seconds[3]={4,1,2}, microseconds[3]={0,0,0};
  67.  
  68. int allin = 3;
  69. char *position[]={"last","second","first"};
  70.  
  71. if (TimerMP = CreatePort(0,0))
  72.     {
  73.     if (TimerIO[0] = (struct timerequest *)
  74.                       CreateExtIO(TimerMP,sizeof(struct timerequest)) )
  75.         {
  76.             /* Open the device once */
  77.         if (!(error=OpenDevice( TIMERNAME, UNIT_VBLANK,(struct IORequest *) TimerIO[0], 0L)))
  78.             {
  79.             /* Set command to TR_ADDREQUEST */
  80.             TimerIO[0]->tr_node.io_Command = TR_ADDREQUEST;
  81.  
  82.             if (TimerIO[1]=(struct timerequest *)
  83.                     AllocMem(sizeof(struct timerequest),MEMF_PUBLIC | MEMF_CLEAR))
  84.                 {
  85.                 if (TimerIO[2]=(struct timerequest *)
  86.                        AllocMem(sizeof(struct timerequest),MEMF_PUBLIC | MEMF_CLEAR))
  87.                     {
  88.                     /* Copy fields from the request used to open the timer device */
  89.                     *TimerIO[1] = *TimerIO[0];
  90.                     *TimerIO[2] = *TimerIO[0];
  91.  
  92.                     /* Initialize other fields */
  93.                     for (x=0;x<3;x++)
  94.                         {
  95.                         TimerIO[x]->tr_time.tv_secs   = seconds[x];
  96.                         TimerIO[x]->tr_time.tv_micro  = microseconds[x];
  97.                         printf("\nInitializing TimerIO[%d]",x);
  98.                         }
  99.  
  100.                     printf("\n\nSending multiple requests\n\n");
  101.  
  102.                     /* Send multiple requests asynchronously */
  103.                     /* Do not got to sleep yet...            */
  104.                     SendIO((struct IORequest *)TimerIO[0]);
  105.                     SendIO((struct IORequest *)TimerIO[1]);
  106.                     SendIO((struct IORequest *)TimerIO[2]);
  107.  
  108.                     /* There might be other processing done here */
  109.  
  110.                     /* Now go to sleep with WaitPort() waiting for the requests */
  111.                     while (allin)
  112.                           {
  113.                           WaitPort(TimerMP);
  114.                           /* Get the reply message */
  115.                           TimerMSG=GetMsg(TimerMP);
  116.                           for (x=0;x<3;x++)
  117.                               if (TimerMSG==(struct Message *)TimerIO[x])
  118.                                   printf("Request %ld finished %s\n",x,position[--allin]);
  119.                           }
  120.  
  121.                     FreeMem(TimerIO[2],sizeof(struct timerequest));
  122.                     }
  123.  
  124.                 else
  125.                     printf("Error: could not allocate TimerIO[2] memory\n");
  126.  
  127.                 FreeMem(TimerIO[1],sizeof(struct timerequest));
  128.                 }
  129.  
  130.             else
  131.                 printf("Error could not allocate TimerIO[1] memory\n");
  132.  
  133.             CloseDevice((struct IORequest *) TimerIO[0]);
  134.             }
  135.  
  136.         else
  137.             printf("\nError: Could not OpenDevice\n");
  138.  
  139.         DeleteExtIO((struct IORequest *) TimerIO[0]);
  140.         }
  141.  
  142.     else
  143.         printf("Error: could not create IORequest\n");
  144.  
  145.     DeletePort(TimerMP);
  146.     }
  147.  
  148. else
  149.     printf("\nError: Could not CreatePort\n");
  150. }
  151.