home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / SASC6571.LZX / extras / sprof / timer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-24  |  2.5 KB  |  113 lines

  1. /*-------------------------------------------------------------------*/
  2. /* Copyright (c) 1993 by SAS Institute Inc., Cary NC                 */
  3. /* All Rights Reserved                                               */
  4. /*                                                                   */
  5. /* SUPPORT:    walker - Doug Walker                                  */
  6. /*-------------------------------------------------------------------*/
  7.  
  8. #include <exec/ports.h>
  9. #include <devices/timer.h>
  10. #include <dos/dosextens.h>
  11. #include <proto/exec.h>
  12. #include <proto/dos.h>
  13. #include <string.h>
  14.  
  15. #include "sprofpriv.h"
  16.  
  17. struct TimerPacket 
  18. {
  19.    struct timerequest tm_req;
  20.    struct DosPacket   tm_pkt;
  21. };
  22.  
  23. static struct TimerPacket *timerpkt;
  24. static struct MsgPort *port;
  25. static int ioout, deviceopen;
  26.  
  27. void GetTimerPkt(long repost, int wait)
  28. {
  29.    if(timerpkt != NULL && ioout)
  30.    {
  31.       if(wait || CheckIO((struct IORequest *)&timerpkt->tm_req))
  32.       {
  33.          WaitIO((struct IORequest *)&timerpkt->tm_req);
  34.          ioout = 0;
  35.       }
  36.    }
  37.    if(repost) PostTimerReq(repost);
  38. }
  39.  
  40. void _STDCloseTimer(void)
  41. {
  42.    if(ioout)
  43.    {
  44.       GetTimerPkt(0,1);
  45.       ioout = 0;
  46.    }
  47.    if(timerpkt)
  48.    {
  49.       if(deviceopen)
  50.       {
  51.          CloseDevice((struct IORequest *)&(timerpkt->tm_req));
  52.          deviceopen = 0;
  53.       }
  54.       DeleteExtIO((struct IORequest *)timerpkt);
  55.       timerpkt = NULL;
  56.    }
  57.    if(port)
  58.    {
  59.       DeletePort(port);
  60.       port = NULL;
  61.    }
  62. }
  63.  
  64. long OpenTimer(void)
  65. {
  66.    int error;
  67.  
  68.    if(port || timerpkt) return(0);
  69.  
  70.    if(!(port = CreatePort(NULL, NULL)))
  71.       return(0);
  72.  
  73.    if ((timerpkt = (struct TimerPacket *)
  74.       CreateExtIO(port, sizeof(struct TimerPacket)))== NULL)
  75.    {
  76.       return(0);
  77.    }
  78.  
  79.    timerpkt->tm_req.tr_node.io_Message.mn_Node.ln_Name = 
  80.       (char *)&(timerpkt->tm_pkt);
  81.    timerpkt->tm_pkt.dp_Link = &(timerpkt->tm_req.tr_node.io_Message);
  82.    timerpkt->tm_pkt.dp_Port = port;
  83.  
  84.    error = OpenDevice(TIMERNAME, UNIT_MICROHZ,
  85.                    (struct IORequest *)&(timerpkt->tm_req), 0);
  86.  
  87.    if(error)
  88.    {
  89.       _STDCloseTimer();
  90.       return(0);
  91.    }
  92.    deviceopen = 1;
  93.  
  94.    return(1<<port->mp_SigBit);
  95. }
  96.  
  97. void PostTimerReq(long time)  // time is in thousandths of a second
  98. {
  99.    if(time == 0) return;
  100.  
  101.    if (timerpkt != NULL && !ioout)
  102.    {
  103.       timerpkt->tm_req.tr_node.io_Command = TR_ADDREQUEST;
  104.       timerpkt->tm_req.tr_time.tv_secs = time/1000;
  105.       timerpkt->tm_req.tr_time.tv_micro = (time%1000)*1000;
  106.       timerpkt->tm_pkt.dp_Type = ACTION_TIMER;
  107.  
  108.       SendIO((struct IORequest *)&timerpkt->tm_req);
  109.       ioout = 1;
  110.    }
  111. }
  112.  
  113.