You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PollingScheduler.h 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* 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. // PollingScheduler class. It is used for deciding when to start new
  20. // polling pass, and how much time it is ok to sleep before starting.
  21. // PollingScheduler is given a desired polling interval, but it can
  22. // add time between polling passes if needed for satisfying processor
  23. // usage limitation.
  24. //
  25. #ifndef __POLLINGSCHEDULER_H__
  26. #define __POLLINGSCHEDULER_H__
  27. #include <x0vncserver/TimeMillis.h>
  28. class PollingScheduler {
  29. public:
  30. PollingScheduler(int interval, int maxload = 50);
  31. // Set polling parameters.
  32. void setParameters(int interval, int maxload = 50);
  33. // Reset the object into the initial state (no polling performed).
  34. void reset();
  35. // Check if the object is active (not in the initial state).
  36. bool isRunning();
  37. // Tell the scheduler that new polling pass is just being started.
  38. void newPass();
  39. // Inform the scheduler about times when we sleep.
  40. void sleepStarted();
  41. void sleepFinished();
  42. // This function estimates time remaining before new polling pass.
  43. int millisRemaining() const;
  44. // This function tells if it's ok to start polling pass right now.
  45. bool goodTimeToPoll() const;
  46. protected:
  47. // Parameters.
  48. int m_interval;
  49. int m_maxload;
  50. // This boolean flag is true when we do not poll the screen.
  51. bool m_initialState;
  52. // Time stamp saved on starting current polling pass.
  53. TimeMillis m_passStarted;
  54. // Desired duration of current polling pass.
  55. int m_ratedDuration;
  56. // These are for measuring sleep time in current pass.
  57. TimeMillis m_sleepStarted;
  58. bool m_sleeping;
  59. int m_sleptThisPass;
  60. // Ring buffer for tracking past timing errors.
  61. int m_errors[8];
  62. int m_errorSum;
  63. int m_errorAbsSum;
  64. // Ring buffer for tracking total pass durations (work + sleep).
  65. int m_durations[8];
  66. int m_durationSum;
  67. // Ring buffer for tracking past sleep times.
  68. int m_slept[8];
  69. int m_sleptSum;
  70. // Indexer for all ring buffers.
  71. int m_idx;
  72. // Pass counter.
  73. int m_count;
  74. };
  75. #endif // __POLLINGSCHEDULER_H__