source: trunk/dll/tmrsvcs.c@ 1521

Last change on this file since 1521 was 1521, checked in by Gregg Young, 15 years ago

Minor code clean up mostly remming or removal of DbgMsgs; comments

  • Property svn:keywords set to Author Date Id Revision
File size: 2.9 KB
Line 
1
2/***********************************************************************
3
4 $Id: tmrsvcs.c 1521 2010-05-02 21:48:59Z gyoung $
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 * Call with interval 0 to to reset internal estimators
24 * @param pTD point to interval timer descriptor
25 * @param interval_msec is the timer interval in msec or 0 to retain existing value
26 */
27
28VOID InitITimer(ITIMER_DESC *pitd, UINT interval_msec)
29{
30 if (interval_msec) {
31 // Assume starting new loop at similar rate
32 pitd->interval_msec = interval_msec;
33 pitd->remaining = pitd->estimated;
34 }
35 else {
36 // Assume loop rate is changing to a significantly lower value
37 pitd->remaining = 0;
38 pitd->estimated = 1; // Force rate recalc
39 }
40 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &pitd->start_msec, sizeof(pitd->start_msec));
41}
42
43/**
44 * Check timer interval expired
45 * Attempts to optimize calls to fetch QSV_MS_COUNT
46 * Caller should reinit if processing rate changes
47 * @return TRUE if expired
48 */
49
50BOOL IsITimerExpired(ITIMER_DESC *pitd)
51{
52 INT err_msec;
53 ULONG cur_msec;
54 UINT elapsed_msec;
55 INT cnt;
56
57 if (--pitd->remaining < 0) {
58
59 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &cur_msec, sizeof(cur_msec));
60 elapsed_msec = cur_msec - pitd->start_msec;
61 if (elapsed_msec == pitd->interval_msec) {
62 pitd->remaining = pitd->estimated;
63 pitd->start_msec = cur_msec;
64 pitd->misses = 0;
65 return TRUE; // Say interval expired
66 }
67
68 pitd->misses++;
69 err_msec = (cur_msec - pitd->start_msec) - pitd->interval_msec;
70 // Estimate counts per msec
71 if (err_msec > 0) {
72 // Late - need to reduce estimated count
73 if (elapsed_msec)
74 pitd->estimated = pitd->estimated * pitd->interval_msec / elapsed_msec;
75 else {
76 // Should not occur
77 if (pitd->estimated)
78 pitd->estimated--;
79 }
80 // Calc counts for next interval
81 pitd->remaining = pitd->estimated -
82 (pitd->estimated * err_msec / pitd->interval_msec);
83 pitd->start_msec += pitd->interval_msec;
84 return TRUE;
85 }
86
87 // Early - need to increase estimated count
88 cnt = pitd->estimated * (-err_msec) / pitd->interval_msec;
89 if (!cnt) {
90 if (pitd->estimated)
91 cnt = pitd->estimated * 2;
92 else
93 cnt = 1;
94 }
95 pitd->estimated += cnt;
96 pitd->remaining = cnt;
97 }
98 return FALSE; // Keep waiting
99}
100
101VOID SleepIfNeeded(ITIMER_DESC *pitd, UINT sleepTime)
102{
103 if (IsITimerExpired(pitd)) {
104 DosSleep(sleepTime);
105 InitITimer(pitd, 0);
106 }
107}
108
109#pragma alloc_text(TMRSVCS,InitITimer,IsITimerExpired,SleepIfNeeded)
Note: See TracBrowser for help on using the repository browser.