blob: 2e3e5be70b18cb963ca5e3f0625edd661c39791b [file] [log] [blame]
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +00001/* Copyright (C) 2006 Constantin Kaplinsky. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19//
20// PollingScheduler class. It is used for deciding when to start new
21// polling pass, and how much time it is ok to sleep before starting.
22// PollingScheduler is given a desired polling interval, but it can
23// add time between polling passes if needed for satisfying processor
24// usage limitation.
25//
26
27#ifndef __POLLINGSCHEDULER_H__
28#define __POLLINGSCHEDULER_H__
29
30#include <x0vncserver/TimeMillis.h>
31
32class PollingScheduler {
33
34public:
35
36 PollingScheduler(int interval, int maxload = 50);
37
38 // Set polling parameters.
39 void setParameters(int interval, int maxload = 50);
40
41 // Reset the object into the initial state (no polling performed).
42 void reset();
43
44 // Tell the scheduler that new polling pass is just being started.
45 void newPass();
46
47 // Inform the scheduler about times when we sleep.
48 void sleepStarted();
49 void sleepFinished();
50
51 // This function estimates time remaining before new polling pass.
52 int millisRemaining() const;
53
54 // This function tells if it's ok to start polling pass right now.
55 bool goodTimeToPoll() const;
56
57protected:
58
59 // Parameters.
60 int m_interval;
61 int m_maxload;
62
63 // This boolean flag is true when we do not poll the screen.
64 bool m_initialState;
65
66 // Time stamp saved on starting current polling pass.
67 TimeMillis m_passStarted;
68
69 // Desired duration of current polling pass.
70 int m_ratedDuration;
71
72 // These are for measuring sleep time in current pass.
73 TimeMillis m_sleepStarted;
74 bool m_sleeping;
75 int m_sleptThisPass;
76
77 // Ring buffer for tracking past timing errors.
78 int m_errors[8];
79 int m_errorSum;
80 int m_errorAbsSum;
81
82 // Ring buffer for tracking total pass durations (work + sleep).
83 int m_durations[8];
84 int m_durationSum;
85
86 // Ring buffer for tracking past sleep times.
87 int m_slept[8];
88 int m_sleptSum;
89
90 // Indexer for all ring buffers.
91 int m_idx;
92
93 // Pass counter.
94 int m_count;
95};
96
97#endif // __POLLINGSCHEDULER_H__
98