home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / som / include / timer.h < prev    next >
C/C++ Source or Header  |  1999-02-22  |  4KB  |  144 lines

  1. /*
  2.  *   COMPONENT_NAME: somp
  3.  *
  4.  *   ORIGINS: 27
  5.  *
  6.  *
  7.  *    25H7912  (C)  COPYRIGHT International Business Machines Corp. 1992,1996,1996  
  8.  *   All Rights Reserved
  9.  *   Licensed Materials - Property of IBM
  10.  *   US Government Users Restricted Rights - Use, duplication or
  11.  *   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  12.  */
  13. /* @(#) 2.10 src/somp/timer.h, somp, som2.1 12/26/95 17:03:33 [7/30/96 14:47:07] */
  14.  
  15. /*
  16.  */
  17.  
  18. /* File: timer.h */
  19.  
  20. #ifndef TIMER_H
  21. #define TIMER_H 1
  22.  
  23. #include <som.h>
  24.  
  25. #if (defined __OS2__) || (defined _WIN16)
  26.   #define INCL_DOSDATETIME
  27.   #ifdef _WIN16
  28.      #include <dos.h>
  29.      #include <stdio.h>
  30.      #include <string.h>
  31.   #else
  32.      #include <os2.h>
  33.   #endif
  34.  
  35.   /* OS/2 doesn't have the same "gettimeofday" function that AIX has. */
  36.   /* It is simulated (in timer.c) by a call to the OS.                */
  37.  
  38.   struct timeval {
  39.         int             tv_sec;         /* seconds */
  40.         int             tv_usec;        /* microseconds */
  41.   };
  42.  
  43.   struct timezone {
  44.         int             tz_minuteswest; /* minutes west of Greenwich */
  45.         int             tz_dsttime;     /* type of dst correction */
  46.   };
  47.  
  48.   int sompfGetTimeOfDay(struct timeval *tv, struct timezone *tz);
  49.   #define GETTIMEOFDAY sompfGetTimeOfDay
  50. #else
  51.   #include <sys/time.h>
  52.   #define GETTIMEOFDAY gettimeofday
  53. #endif
  54.  
  55. #define SOMPC_TIMER_NAME_LENGTH 30
  56. struct SOMPTimer_type {
  57.   struct timeval startTime;
  58.   struct timeval endTime;
  59.   long int elapsedMicroSec;
  60.   long int timesTurnedOn;
  61.   long int timesTurnedOff;
  62.   char timerName[SOMPC_TIMER_NAME_LENGTH];
  63. };
  64. typedef struct SOMPTimer_type SOMPTimer;
  65.  
  66. void SOMLINK sompfInitTimers(void);
  67. SOMPTimer * SOMLINK sompfGetTimer(char *name);
  68. void SOMLINK sompfPrintTimer(SOMPTimer *thistimer);
  69. void SOMLINK sompfPrintTimers(void);
  70.  
  71. long int SOMLINK sompfGetElapsedMicroSec(SOMPTimer *thisTimer);
  72. long int SOMLINK sompfGetTimesTurnedOn(SOMPTimer *thisTimer);
  73. long int SOMLINK sompfGetTimesTurnedOff(SOMPTimer *thisTimer);
  74. char * SOMLINK sompfGetTimerName(SOMPTimer *thisTimer);
  75.  
  76. /* #define TIMER_ON */
  77.  
  78. #ifdef TIMER_ON
  79.  
  80. #define INITTIMERS  sompfInitTimers();
  81. #define DCLTIMER(t) SOMPTimer *t = (SOMPTimer *)0;
  82. #define DCLSTATICTIMER(t) static SOMPTimer *t = (SOMPTimer *)0;
  83. #define GETTIMER(t,s) if (!t) t = sompfGetTimer(s);
  84. #define PRINTTIMERS sompfPrintTimers();
  85.  
  86. #define STARTTIMER(t) \
  87. { \
  88.   t->timesTurnedOn++; \
  89.   GETTIMEOFDAY(&(t->startTime), &sompgDummyTimeZone); \
  90. }
  91.  
  92. #define sompfStartTimer(thisTimer) \
  93. { \
  94.   thisTimer->timesTurnedOn++; \
  95.   GETTIMEOFDAY(&(thisTimer->startTime), &sompgDummyTimeZone); \
  96. }
  97.  
  98. #define STOPTIMER(t) \
  99. { \
  100.   GETTIMEOFDAY(&(t->endTime), &sompgDummyTimeZone); \
  101.   t->elapsedMicroSec += \
  102.       ((long int) (t->endTime.tv_sec - t->startTime.tv_sec)) * 1000000; \
  103.   if (t->endTime.tv_usec > t->startTime.tv_usec) \
  104.        t->elapsedMicroSec += t->endTime.tv_usec - t->startTime.tv_usec; \
  105.   else t->elapsedMicroSec += \
  106.        (1000000 - t->startTime.tv_usec) + t->endTime.tv_usec - 1000000; \
  107.   t->timesTurnedOff++; \
  108. }
  109.  
  110. #define sompfStopTimer(thisTimer) \
  111. { \
  112.   GETTIMEOFDAY(&(thisTimer->endTime), &sompgDummyTimeZone); \
  113.   thisTimer->elapsedMicroSec += \
  114.       ((long int) (thisTimer->endTime.tv_sec - thisTimer->startTime.tv_sec)) * 1000000; \
  115.   if (thisTimer->endTime.tv_usec > thisTimer->startTime.tv_usec) \
  116.        thisTimer->elapsedMicroSec += thisTimer->endTime.tv_usec - thisTimer->startTime.tv_usec; \
  117.   else thisTimer->elapsedMicroSec += \
  118.        (1000000 - thisTimer->startTime.tv_usec) + thisTimer->endTime.tv_usec - 1000000; \
  119.   thisTimer->timesTurnedOff++; \
  120. }
  121.  
  122. #ifdef _WIN16
  123.   #define sompgDummyTimeZone (*sompgDummyTimeZoneResolve())
  124.   SOMEXTERN struct timezone * SOMLINK  sompgDummyTimeZoneResolve(void);
  125. #else
  126.   SOMEXTERN struct timezone SOMDLINK sompgDummyTimeZone;
  127. #endif
  128.  
  129. #else
  130. #define INITTIMERS
  131. #define DCLTIMER(t)
  132. #define DCLSTATICTIMER(t)
  133. #define GETTIMER(t,s)
  134. #define STARTTIMER(t)
  135. #define STOPTIMER(t)
  136. #define PRINTTIMERS
  137.  
  138. #define sompfStartTimer(thisTimer) {}
  139. #define sompfStopTimer(thisTimer) {}
  140. #endif
  141.  
  142. #endif
  143.  
  144.