4 * PWLib application header file for ptimer
6 * Copyright (c) 2006 Indranet Technologies Ltd.
8 * The contents of this file are subject to the Mozilla Public License
9 * Version 1.0 (the "License"); you may not use this file except in
10 * compliance with the License. You may obtain a copy of the License at
11 * http://www.mozilla.org/MPL/
13 * Software distributed under the License is distributed on an "AS IS"
14 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
15 * the License for the specific language governing rights and limitations
18 * The Original Code is Portable Windows Library.
20 * The Initial Developer of the Original Code is Indranet Technologies Ltd.
22 * Contributor(s): ______________________________________.
25 * Revision 1.4 2007/05/01 03:15:26 dereksmithies
26 * Add a second test, to test the repeated initialisation of some PTimer instances.
28 * Revision 1.3 2006/06/21 03:28:42 csoutheren
29 * Various cleanups thanks for Frederic Heem
31 * Revision 1.2 2006/05/24 02:28:18 dereksmithies
32 * add separate thread to get the timer to start.
33 * Add option to check if the timer has started.
34 * Fix use of the parameters.
36 * Revision 1.1 2006/05/23 04:36:49 dereksmithies
37 * Initial release of a test program to examine the operation of PTimer.
43 #ifndef _PTimer_MAIN_H
44 #define _PTimer_MAIN_H
46 #include <ptlib/pprocess.h>
48 /**A class that does a PTimer functionality. This class runs once. It
49 is started, and on completion of the delay it toggles a flag. At
50 that point, this timer has finished. */
51 class MyTimer
: public PTimer
53 PCLASSINFO(MyTimer
, PTimer
);
58 /**method used to start everything */
59 void StartRunning(PSyncPoint
* _exitFlag
, PINDEX delayMs
);
61 /**Method called when this timer finishes */
62 virtual void OnTimeout();
65 /**The flag to mark the end of this timer */
68 /**The duration we delay for */
69 PTimeInterval delayPeriod
;
71 /**The time at which we started */
77 /////////////////////////////////////////////////////////////////////////////
79 /**This class is a simple simple thread that just creates, waits a
80 period of time, and exits.It is designed to test the PwLib methods
81 for reporting the status of a thread. This class will be created
82 over and over- millions of times is possible if left long
83 enough. If the pwlib thread status functions are broken, a segfault
84 will result. Past enxperience has found a fault in pwlib with the
85 BusyWait option on, with SMP machines and a delay period of 20ms */
86 class DelayThread
: public PThread
88 PCLASSINFO(DelayThread
, PThread
);
91 DelayThread(PINDEX _delay
, BOOL _checkTimer
);
107 /////////////////////////////////////////////////////////////////////////////
108 /**This class turns the ptimer on from a separate thread to the delay thread.
109 Then, DelayThread checks this ptimer, and waits for it to finish.
111 class TimerOnThread
: public PThread
113 PCLASSINFO(TimerOnThread
, PThread
);
116 TimerOnThread(PTimer
& _timer
);
125 ////////////////////////////////////////////////////////////////////////////////
126 /**This thread handles the Users console requests to query the status of
127 the launcher thread. It provides a means for the user to close down this
128 program - without having to use Ctrl-C*/
129 class UserInterfaceThread
: public PThread
131 PCLASSINFO(UserInterfaceThread
, PThread
);
134 UserInterfaceThread()
135 : PThread(10000, NoAutoDeleteThread
)
143 ///////////////////////////////////////////////////////////////////////////////
144 /**This thread launches multiple instances of the BusyWaitThread. Each
145 thread launched is busy monitored for termination. When the thread
146 terminates, the thread is deleted, and a new one is created. This
147 process repeats until segfault or termination by the user */
148 class LauncherThread
: public PThread
150 PCLASSINFO(LauncherThread
, PThread
);
154 : PThread(10000, NoAutoDeleteThread
)
155 { iteration
= 0; keepGoing
= TRUE
; }
159 PINDEX
GetIteration() { return iteration
; }
161 virtual void Terminate() { keepGoing
= FALSE
; }
163 PTimeInterval
GetElapsedTime() { return PTime() - startTime
; }
171 ////////////////////////////////////////////////////////////////////////////////
174 class PTimerTest
: public PProcess
176 PCLASSINFO(PTimerTest
, PProcess
)
182 PINDEX
Delay() { return delay
; }
184 PINDEX
Interval() { return interval
; }
186 BOOL
CheckTimer() { return checkTimer
; }
188 static PTimerTest
& Current()
189 { return (PTimerTest
&)PProcess::Current(); }
191 void TooSoon(PTimeInterval
& elapsed
);
202 /**Code to run the second test supported by this application. */
203 void RunSecondTest();
205 /**First internal timer that we manage */
208 /**Second internal timer that we manage */
212 /**A pwlib callback function which is activated when the first timer
214 void OnFirstTimerExpired(PTimer
&, INT
);
216 /**A pwlib callback function which is activated when the second timer
218 void OnSecondTimerExpired(PTimer
&, INT
);
220 PDECLARE_NOTIFIER(PTimer
, PTimerTest
, OnFirstTimerExpired
);
222 PDECLARE_NOTIFIER(PTimer
, PTimerTest
, OnSecondTimerExpired
);
226 /**This Thread will continually restart the first timer. If
227 there is a bug in pwlib, it will eventually lock up and do no more. At
228 which point, the monitor thread will fire, and say, nothing is
229 happening. This thread sets the value of an atomic integer every time
230 it runs, to indicate activity.*/
231 virtual void RestartFirstTimerMain(PThread
&, INT
);
233 PDECLARE_NOTIFIER(PThread
, PTimerTest
, RestartFirstTimerMain
);
237 /**This Thread will continually restart the second timer. If
238 there is a bug in pwlib, it will eventually lock up and do no more. At
239 which point, the monitor thread will fire, and say, nothing is
240 happening. This thread sets the value of an atomic integer every time
241 it runs, to indicate activity.*/
242 virtual void RestartSecondTimerMain(PThread
&, INT
);
244 PDECLARE_NOTIFIER(PThread
, PTimerTest
, RestartSecondTimerMain
);
247 /**The integer that is set, to indicate activity of the RestartTimer thread */
248 PAtomicInteger restartActivity
;
256 #endif // _PTimer_MAIN_H
259 // End of File ///////////////////////////////////////////////////////////////