| 1 | 
 | 
|---|
| 2 | /***********************************************************************
 | 
|---|
| 3 | 
 | 
|---|
| 4 |   $Id: $
 | 
|---|
| 5 | 
 | 
|---|
| 6 |   Timer services
 | 
|---|
| 7 | 
 | 
|---|
| 8 |   Copyright (c) 2008 Steven H. Levine
 | 
|---|
| 9 | 
 | 
|---|
| 10 |   05 Jan 08 SHL Baseline
 | 
|---|
| 11 | 
 | 
|---|
| 12 | ***********************************************************************/
 | 
|---|
| 13 | 
 | 
|---|
| 14 | #define INCL_DOS                        // QSV_MS_COUNT
 | 
|---|
| 15 | 
 | 
|---|
| 16 | // #include "errutil.h"                 // DbgMsg // 05 Jan 08 SHL fixme debug
 | 
|---|
| 17 | #include "tmrsvcs.h"
 | 
|---|
| 18 | 
 | 
|---|
| 19 | // static PSZ pszSrcFile = __FILE__;    // 05 Jan 08 SHL fixme debug
 | 
|---|
| 20 | 
 | 
|---|
| 21 | /**
 | 
|---|
| 22 |  * Prepare interval timer descriptor for use
 | 
|---|
| 23 |  * @param pTD point to interval timer descriptor
 | 
|---|
| 24 |  * @param interval_msec is the timer interval in msec or 0 to retain existing value
 | 
|---|
| 25 |  */
 | 
|---|
| 26 | 
 | 
|---|
| 27 | VOID InitITimer(ITIMER_DESC *pitd, UINT interval_msec)
 | 
|---|
| 28 | {
 | 
|---|
| 29 |   if (interval_msec)
 | 
|---|
| 30 |     pitd->interval_msec = interval_msec;
 | 
|---|
| 31 |   pitd->remaining = pitd->estimated;
 | 
|---|
| 32 |   DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &pitd->start_msec, sizeof(pitd->start_msec));
 | 
|---|
| 33 | }
 | 
|---|
| 34 | 
 | 
|---|
| 35 | /**
 | 
|---|
| 36 |  * Check timer interval expired
 | 
|---|
| 37 |  * @return TRUE if expired
 | 
|---|
| 38 |  */
 | 
|---|
| 39 | 
 | 
|---|
| 40 | BOOL IsITimerExpired(ITIMER_DESC *pitd)
 | 
|---|
| 41 | {
 | 
|---|
| 42 |   INT err_msec;
 | 
|---|
| 43 |   ULONG cur_msec;
 | 
|---|
| 44 |   UINT elapsed_msec;
 | 
|---|
| 45 |   INT cnt;
 | 
|---|
| 46 | 
 | 
|---|
| 47 |   if (--pitd->remaining < 0) {
 | 
|---|
| 48 | 
 | 
|---|
| 49 |     DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &cur_msec, sizeof(cur_msec));
 | 
|---|
| 50 |     elapsed_msec = cur_msec - pitd->start_msec;
 | 
|---|
| 51 |     if (elapsed_msec == pitd->interval_msec) {
 | 
|---|
| 52 |       pitd->remaining = pitd->estimated;
 | 
|---|
| 53 |       pitd->start_msec = cur_msec;
 | 
|---|
| 54 |       pitd->misses = 0;
 | 
|---|
| 55 |       return TRUE;                      // Say interval expired
 | 
|---|
| 56 |     }
 | 
|---|
| 57 | 
 | 
|---|
| 58 |     pitd->misses++;
 | 
|---|
| 59 |     err_msec = (cur_msec - pitd->start_msec) - pitd->interval_msec;
 | 
|---|
| 60 |     // Estimate counts per msec
 | 
|---|
| 61 | #if 0 // 05 Jan 08 SHL fixme to be gone when no longer needed for testing
 | 
|---|
| 62 |     DbgMsg(pszSrcFile, __LINE__,
 | 
|---|
| 63 |            "err_msec %d elapsed_msec %d estimated %u misses %u",
 | 
|---|
| 64 |            err_msec, elapsed_msec, pitd->estimated, pitd->misses);
 | 
|---|
| 65 | #endif
 | 
|---|
| 66 |     if (err_msec > 0) {
 | 
|---|
| 67 |       // Late - need to reduce estimated count
 | 
|---|
| 68 |       if (elapsed_msec)
 | 
|---|
| 69 |         pitd->estimated = pitd->estimated * pitd->interval_msec / elapsed_msec;
 | 
|---|
| 70 |       else {
 | 
|---|
| 71 |         // Should not occur
 | 
|---|
| 72 |         if (pitd->estimated)
 | 
|---|
| 73 |           pitd->estimated--;
 | 
|---|
| 74 |       }
 | 
|---|
| 75 |       // Calc counts for next interval
 | 
|---|
| 76 |       pitd->remaining = pitd->estimated -
 | 
|---|
| 77 |                         (pitd->estimated * err_msec / pitd->interval_msec);
 | 
|---|
| 78 |       pitd->start_msec += pitd->interval_msec;
 | 
|---|
| 79 |       return TRUE;
 | 
|---|
| 80 |     }
 | 
|---|
| 81 | 
 | 
|---|
| 82 |     // Early - need to increase estimated count
 | 
|---|
| 83 |     cnt = pitd->estimated * (-err_msec) / pitd->interval_msec;
 | 
|---|
| 84 |     if (!cnt) {
 | 
|---|
| 85 |       if (pitd->estimated)
 | 
|---|
| 86 |         cnt = pitd->estimated * 2;
 | 
|---|
| 87 |       else
 | 
|---|
| 88 |         cnt = 1;
 | 
|---|
| 89 |     }
 | 
|---|
| 90 |     pitd->estimated += cnt;
 | 
|---|
| 91 |     pitd->remaining = cnt;
 | 
|---|
| 92 |   }
 | 
|---|
| 93 |   return FALSE;                         // Keep waiting
 | 
|---|
| 94 | }
 | 
|---|
| 95 | 
 | 
|---|
| 96 | VOID SleepIfNeeded(ITIMER_DESC *pitd, UINT sleepTime)
 | 
|---|
| 97 | {
 | 
|---|
| 98 |   if (IsITimerExpired(pitd)) {
 | 
|---|
| 99 |     DosSleep(sleepTime);
 | 
|---|
| 100 |     InitITimer(pitd, 0);
 | 
|---|
| 101 |   }
 | 
|---|
| 102 | }
 | 
|---|
| 103 | 
 | 
|---|
| 104 | #pragma alloc_text(TMRSVCS,InitITimer,IsITimerExpired,SleepIfNeeded)
 | 
|---|