timer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/unix/private/timer.h
  3. // Purpose: wxTimer for wxBase (unix)
  4. // Author: Lukasz Michalski
  5. // Created: 15/01/2005
  6. // Copyright: (c) Lukasz Michalski
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_UNIX_PRIVATE_TIMER_H_
  10. #define _WX_UNIX_PRIVATE_TIMER_H_
  11. #if wxUSE_TIMER
  12. #include "wx/private/timer.h"
  13. // the type used for milliseconds is large enough for microseconds too but
  14. // introduce a synonym for it to avoid confusion
  15. typedef wxMilliClock_t wxUsecClock_t;
  16. // ----------------------------------------------------------------------------
  17. // wxTimer implementation class for Unix platforms
  18. // ----------------------------------------------------------------------------
  19. // NB: we have to export at least this symbol from the shared library, because
  20. // it's used by wxDFB's wxCore
  21. class WXDLLIMPEXP_BASE wxUnixTimerImpl : public wxTimerImpl
  22. {
  23. public:
  24. wxUnixTimerImpl(wxTimer *timer);
  25. virtual ~wxUnixTimerImpl();
  26. virtual bool IsRunning() const;
  27. virtual bool Start(int milliseconds = -1, bool oneShot = false);
  28. virtual void Stop();
  29. // for wxTimerScheduler only: resets the internal flag indicating that the
  30. // timer is running
  31. void MarkStopped()
  32. {
  33. wxASSERT_MSG( m_isRunning, wxT("stopping non-running timer?") );
  34. m_isRunning = false;
  35. }
  36. private:
  37. bool m_isRunning;
  38. };
  39. // ----------------------------------------------------------------------------
  40. // wxTimerSchedule: information about a single timer, used by wxTimerScheduler
  41. // ----------------------------------------------------------------------------
  42. struct wxTimerSchedule
  43. {
  44. wxTimerSchedule(wxUnixTimerImpl *timer, wxUsecClock_t expiration)
  45. : m_timer(timer),
  46. m_expiration(expiration)
  47. {
  48. }
  49. // the timer itself (we don't own this pointer)
  50. wxUnixTimerImpl *m_timer;
  51. // the time of its next expiration, in usec
  52. wxUsecClock_t m_expiration;
  53. };
  54. // the linked list of all active timers, we keep it sorted by expiration time
  55. WX_DECLARE_LIST(wxTimerSchedule, wxTimerList);
  56. // ----------------------------------------------------------------------------
  57. // wxTimerScheduler: class responsible for updating all timers
  58. // ----------------------------------------------------------------------------
  59. class wxTimerScheduler
  60. {
  61. public:
  62. // get the unique timer scheduler instance
  63. static wxTimerScheduler& Get()
  64. {
  65. if ( !ms_instance )
  66. ms_instance = new wxTimerScheduler;
  67. return *ms_instance;
  68. }
  69. // must be called on shutdown to delete the global timer scheduler
  70. static void Shutdown()
  71. {
  72. if ( ms_instance )
  73. {
  74. delete ms_instance;
  75. ms_instance = NULL;
  76. }
  77. }
  78. // adds timer which should expire at the given absolute time to the list
  79. void AddTimer(wxUnixTimerImpl *timer, wxUsecClock_t expiration);
  80. // remove timer from the list, called automatically from timer dtor
  81. void RemoveTimer(wxUnixTimerImpl *timer);
  82. // the functions below are used by the event loop implementation to monitor
  83. // and notify timers:
  84. // if this function returns true, the time remaining until the next time
  85. // expiration is returned in the provided parameter (always positive or 0)
  86. //
  87. // it returns false if there are no timers
  88. bool GetNext(wxUsecClock_t *remaining) const;
  89. // trigger the timer event for all timers which have expired, return true
  90. // if any did
  91. bool NotifyExpired();
  92. private:
  93. // ctor and dtor are private, this is a singleton class only created by
  94. // Get() and destroyed by Shutdown()
  95. wxTimerScheduler() { }
  96. ~wxTimerScheduler();
  97. // add the given timer schedule to the list in the right place
  98. //
  99. // we take ownership of the pointer "s" which must be heap-allocated
  100. void DoAddTimer(wxTimerSchedule *s);
  101. // the list of all currently active timers sorted by expiration
  102. wxTimerList m_timers;
  103. static wxTimerScheduler *ms_instance;
  104. };
  105. #endif // wxUSE_TIMER
  106. #endif // _WX_UNIX_PRIVATE_TIMER_H_