PTLib Version 2.12.9

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
timer.h
Go to the documentation of this file.
1 /*
2 * timer.h
3 *
4 * Real time down counting time interval class.
5 *
6 * Portable Windows Library
7 *
8 * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
9 *
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
14 *
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17 * the License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * The Original Code is Portable Windows Library.
21 *
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23 *
24 * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25 * All Rights Reserved.
26 *
27 * Contributor(s): ______________________________________.
28 *
29 * $Revision: 29407 $
30 * $Author: rjongbloed $
31 * $Date: 2013-04-01 21:10:26 +1100 (Mon, 01 Apr 2013) $
32 */
33
34 #ifndef PTLIB_TIMER_H
35 #define PTLIB_TIMER_H
36
37 #ifdef P_USE_PRAGMA
38 #pragma interface
39 #endif
40
41
42 #include <ptlib/notifier.h>
43 #include <ptclib/threadpool.h>
44
45
46 // To avoid ambiguous operator error we need a one for every integer variant
47 #define PTIMER_OPERATORS(cls) \
48 cls & operator=( int16_t rhs) { this->SetInterval(rhs); return *this; } \
49 cls & operator=(uint16_t rhs) { this->SetInterval(rhs); return *this; } \
50 cls & operator=( int32_t rhs) { this->SetInterval(rhs); return *this; } \
51 cls & operator=(uint32_t rhs) { this->SetInterval(rhs); return *this; } \
52 cls & operator=( int64_t rhs) { this->SetInterval(rhs); return *this; } \
53 cls & operator=(uint64_t rhs) { this->SetInterval(rhs); return *this; } \
54 cls & operator=(const PTimeInterval & rhs) { this->SetInterval(rhs.GetMilliSeconds()); return *this; } \
55
56
70 class PSimpleTimer : public PTimeInterval
71 {
72 PCLASSINFO(PSimpleTimer, PTimeInterval);
73
74 public:
80 PSimpleTimer(
81 long milliseconds = 0,
82 int seconds = 0,
83 int minutes = 0,
84 int hours = 0,
85 int days = 0
86 );
87 PSimpleTimer(
88 const PTimeInterval & time
89 );
90 PSimpleTimer(
91 const PSimpleTimer & timer
92 );
93
100 PSimpleTimer & operator=(
101 DWORD milliseconds
102 );
103 PSimpleTimer & operator=(
104 const PTimeInterval & time
105 );
106 PSimpleTimer & operator=(
107 const PSimpleTimer & timer
108 );
110
127 virtual void SetInterval(
128 PInt64 milliseconds = 0,
129 long seconds = 0,
130 long minutes = 0,
131 long hours = 0,
132 int days = 0
133 );
134
137 void Stop();
138
141 PTimeInterval GetElapsed() const;
142
146 PTimeInterval GetRemaining() const;
147
150 bool IsRunning() const;
151
154 bool HasExpired() const;
155
158 operator bool() const;
160
161 protected:
162 PTimeInterval m_startTick;
163 };
164
165
194 class PTimer : public PTimeInterval
195 {
196 PCLASSINFO(PTimer, PTimeInterval);
197
198 public:
199 typedef unsigned IDType;
200
208 PTimer(
209 long milliseconds = 0,
210 int seconds = 0,
211 int minutes = 0,
212 int hours = 0,
213 int days = 0
214 );
215 PTimer(
216 const PTimeInterval & time
217 );
218 PTimer(
219 const PTimer & timer
220 );
221
228 PTimer & operator=(
229 const PTimer & timer
230 ) { SetInterval(timer.GetMilliSeconds()); return *this; }
231
232 PTIMER_OPERATORS(PTimer);
233
237 virtual ~PTimer();
239
253 virtual void SetInterval(
254 PInt64 milliseconds = 0,
255 long seconds = 0,
256 long minutes = 0,
257 long hours = 0,
258 int days = 0
259 );
260
265 void RunContinuous(
266 const PTimeInterval & time // New time interval for timer.
267 );
268
281 void Stop(
282 bool wait = true
283 );
284
291 PBoolean IsRunning() const;
292
297 void Pause();
298
303 void Resume();
304
310 PBoolean IsPaused() const;
311
314 void Reset();
315
318 const PTimeInterval & GetResetTime() const;
320
335 virtual void OnTimeout();
336
343 const PNotifier & GetNotifier() const;
344
348 void SetNotifier(
349 const PNotifier & func // New notifier function for the timer.
350 );
352
367 static PTimeInterval Tick();
368
377 static unsigned Resolution();
379
384 PInt64 GetMilliSeconds() const;
385
388 PInt64 GetAbsoluteTime() const { return m_absoluteTime; }
390
391 // Internal functions.
392 IDType GetTimerId() const { return m_timerId; }
393 PAtomicInteger::IntegerType GetNextSerialNumber() { return ++m_serialNumber; }
394
395 private:
396 void Construct();
397
398 /* Start or restart the timer from the <code>resetTime</code> variable.
399 This is an internal function.
400 */
401 void StartRunning(
402 PBoolean once // Flag for one shot or continuous.
403 );
404
405 /* Process the timer decrementing it by the delta amount and calling the
406 <code>OnTimeout()</code> when zero. This is used internally by the
407 <code>PTimerList::Process()</code> function.
408 */
409 void Process(
410 PInt64 now // time consider as "now"
411 );
412
413 // Member variables
414
415 // Callback function for expired timers.
416 PNotifier m_callback;
417
418 // The time to reset a timer to when RunContinuous() is called.
419 PTimeInterval m_resetTime;
420
421 // Timer operates once then stops.
422 PBoolean m_oneshot;
423
424 // Timer state.
425 enum TimerState { Stopped, Running, Paused, InTimeout } m_state;
426
427 friend class PTimerList; // needed for Process
428 class PTimerList * m_timerList;
429
430 IDType m_timerId;
431 PAtomicInteger m_serialNumber;
432 PInt64 m_absoluteTime;
433
434 // Include platform dependent part of class
435 #ifdef _WIN32
436 #include "msos/ptlib/timer.h"
437 #else
438 #include "unix/ptlib/timer.h"
439 #endif
440 };
441
442
465 template <
466 class Work_T,
467 class Pool_T = PQueuedThreadPool<Work_T>
468 >
469 class PPoolTimer : public PTimer
470 {
471 PCLASSINFO(PPoolTimer, PTimer);
472 protected:
473 Pool_T & m_pool;
474 public:
475 PPoolTimer(Pool_T & pool)
476 : m_pool(pool)
477 {
478 }
479
480 virtual void OnTimeout()
481 {
482 Work_T * work = CreateWork();
483 if (work != NULL)
484 m_pool.AddWork(work, GetGroup(*work));
485 }
486
487 virtual Work_T * CreateWork() = 0;
488 virtual const char * GetGroup(const Work_T & /*work*/) const { return NULL; }
489
490 PTIMER_OPERATORS(PPoolTimer);
491 };
492
493
495 template <
496 class Work_T,
497 class Base_T = Work_T,
498 class Pool_T = PQueuedThreadPool<Base_T>
499 >
500 class PPoolTimerArg0 : public PPoolTimer<Base_T, Pool_T>
501 {
502 typedef PPoolTimer<Base_T, Pool_T> BaseClass;
503 PCLASSINFO(PPoolTimerArg0, BaseClass);
504 public:
505 PPoolTimerArg0(Pool_T & pool)
506 : BaseClass(pool)
507 {
508 }
509
510 virtual Work_T * CreateWork() { return new Work_T(); }
511
512 PTIMER_OPERATORS(PPoolTimerArg0);
513 };
514
515
517 template <
518 class Work_T,
519 typename Arg1,
520 class Base_T = Work_T,
521 class Pool_T = PQueuedThreadPool<Base_T>
522 >
523 class PPoolTimerArg1: public PPoolTimer<Base_T, Pool_T>
524 {
525 typedef PPoolTimer<Base_T, Pool_T> BaseClass;
526 PCLASSINFO(PPoolTimerArg1, BaseClass);
527 protected:
528 Arg1 m_arg1;
529 public:
530 PPoolTimerArg1(Pool_T & pool, Arg1 arg1)
531 : BaseClass(pool)
532 , m_arg1(arg1)
533 {
534 }
535
536 virtual Work_T * CreateWork() { return new Work_T(m_arg1); }
537
538 PTIMER_OPERATORS(PPoolTimerArg1);
539 };
540
541
543 template <
544 class Work_T,
545 typename Arg1,
546 typename Arg2,
547 class Base_T = Work_T,
548 class Pool_T = PQueuedThreadPool<Base_T>
549 >
550 class PPoolTimerArg2: public PPoolTimer<Base_T, Pool_T>
551 {
552 typedef PPoolTimer<Base_T, Pool_T> BaseClass;
553 PCLASSINFO(PPoolTimerArg2, BaseClass);
554 protected:
555 Arg1 m_arg1;
556 Arg2 m_arg2;
557 public:
558 PPoolTimerArg2(Pool_T & pool, Arg1 arg1, Arg2 arg2)
559 : BaseClass(pool)
560 , m_arg1(arg1)
561 , m_arg2(arg2)
562 {
563 }
564
565 virtual Work_T * CreateWork() { return new Work_T(m_arg1, m_arg2); }
566
567 PTIMER_OPERATORS(PPoolTimerArg2);
568 };
569
570
572 template <
573 class Work_T,
574 typename Arg1,
575 typename Arg2,
576 typename Arg3,
577 class Base_T = Work_T,
578 class Pool_T = PQueuedThreadPool<Base_T>
579 >
580 class PPoolTimerArg3: public PPoolTimer<Base_T, Pool_T>
581 {
582 typedef PPoolTimer<Base_T, Pool_T> BaseClass;
583 PCLASSINFO(PPoolTimerArg3, BaseClass);
584 protected:
585 Arg1 m_arg1;
586 Arg2 m_arg2;
587 Arg3 m_arg3;
588 public:
589 PPoolTimerArg3(Pool_T & pool, Arg1 arg1, Arg2 arg2, Arg3 arg3)
590 : BaseClass(pool)
591 , m_arg1(arg1)
592 , m_arg2(arg2)
593 , m_arg3(arg3)
594 {
595 }
596
597 virtual Work_T * CreateWork() { return new Work_T(m_arg1, m_arg2, m_arg3); }
598
599 PTIMER_OPERATORS(PPoolTimerArg3);
600 };
601
602
603 #endif // PTLIB_TIMER_H
604
605
606 // End Of File ///////////////////////////////////////////////////////////////