PTLib  Version 2.18.8
 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 
30 #ifndef PTLIB_TIMER_H
31 #define PTLIB_TIMER_H
32 
33 #ifdef P_USE_PRAGMA
34 #pragma interface
35 #endif
36 
37 #include <ptlib_config.h>
38 
39 #if P_TIMERS
40 
41 #include <ptlib/notifier.h>
42 #include <ptlib/id_generator.h>
43 #include <ptclib/threadpool.h>
44 
45 #include <queue>
46 #include <set>
47 #include <map>
48 
49 
50 // To avoid ambiguous operator error we need a one for every integer variant
51 #define PTIMER_OPERATORS(cls) \
52  cls & operator=( int16_t rhs) { this->SetInterval(rhs); return *this; } \
53  cls & operator=(uint16_t rhs) { this->SetInterval(rhs); return *this; } \
54  cls & operator=( int32_t rhs) { this->SetInterval(rhs); return *this; } \
55  cls & operator=(uint32_t rhs) { this->SetInterval(rhs); return *this; } \
56  cls & operator=( int64_t rhs) { this->SetInterval(rhs); return *this; } \
57  cls & operator=(uint64_t rhs) { this->SetInterval(rhs); return *this; } \
58  cls & operator=(const PTimeInterval & rhs) { this->SetInterval(rhs.GetMilliSeconds()); return *this; } \
59 
60 
74 class PSimpleTimer : public PTimeInterval
75 {
76  PCLASSINFO(PSimpleTimer, PTimeInterval);
77 
78  public:
84  PSimpleTimer(
85  long milliseconds = 0,
86  int seconds = 0,
87  int minutes = 0,
88  int hours = 0,
89  int days = 0
90  );
91  PSimpleTimer(
92  const PTimeInterval & time
93  );
94  PSimpleTimer(
95  const PSimpleTimer & timer
96  );
97 
104  PSimpleTimer & operator=(
105  DWORD milliseconds
106  );
107  PSimpleTimer & operator=(
108  const PTimeInterval & time
109  );
110  PSimpleTimer & operator=(
111  const PSimpleTimer & timer
112  );
114 
119  void Stop();
120 
123  PTimeInterval GetElapsed() const;
124 
128  PTimeInterval GetRemaining() const;
129 
132  bool IsRunning() const;
133 
136  bool HasExpired() const;
137 
140  operator bool() const;
142 
143  protected:
144  virtual void InternalSet(int64_t t);
145  PTimeInterval m_startTick;
146 };
147 
148 
174 class PTimer : public PTimeInterval
175 {
177 
178  public:
179 
187  PTimer(
188  long milliseconds = 0,
189  int seconds = 0,
190  int minutes = 0,
191  int hours = 0,
192  int days = 0
193  );
194  PTimer(
195  const PTimeInterval & time
196  );
197  PTimer(
198  const PTimer & timer
199  );
200 
207  PTimer & operator=(
208  const PTimer & timer
209  );
210 
211  PTIMER_OPERATORS(PTimer);
212 
215  virtual ~PTimer();
217 
223  virtual void PrintOn(
224  ostream & strm
225  ) const;
227 
234  void RunContinuous(
235  const PTimeInterval & time // New time interval for timer.
236  );
237 
255  void Stop(
256  bool wait = true
257  );
258 
265  PBoolean IsRunning() const;
266 
269  void Reset();
270 
273  PTimeInterval GetResetTime() const;
275 
290  virtual void OnTimeout();
291 
298  const PNotifier & GetNotifier() const;
299 
303  void SetNotifier(
304  const PNotifier & func,
305  const PString & threadName = PString::Empty()
306  );
308 
324  static PTimeInterval Tick()
325  );
326 
335  static unsigned Resolution();
337 
338 
339  /* This class defines a list of <code>PTimer</code> objects. It is primarily used
340  internally by the library and the user should never create an instance of
341  it. The <code>PProcess</code> instance for the application maintains an instance
342  of all of the timers created so that it may decrements them at regular
343  intervals.
344  */
345  class List
346  {
347  public:
348  // Create a new timer list
349  List();
350 
351  /* Decrement all the created timers and dispatch to their callback
352  functions if they have expired. The <code>PTimer::Tick()</code> function
353  value is used to determine the time elapsed since the last call to
354  Process().
355 
356  The return value is the number of milliseconds until the next timer
357  needs to be dispatched. The function need not be called again for this
358  amount of time, though it can (and usually is).
359 
360  @return
361  maximum time interval before function should be called again.
362  */
363  PTimeInterval Process();
364 
365  private:
366  bool OnTimeout(PIdGenerator::Handle handle);
367 
368  struct Timeout
369  {
370  PIdGenerator::Handle m_handle;
371  Timeout(PIdGenerator::Handle handle) : m_handle(handle) { }
372  virtual ~Timeout() { }
373  virtual void Work();
374  };
375  PQueuedThreadPool<Timeout> m_threadPool;
376 
377  typedef std::map<PIdGenerator::Handle, PTimer *> TimerMap;
378  TimerMap m_timers;
379  PCriticalSection m_timersMutex;
380 #if PTRACING
381  size_t m_highWaterMark;
382 #endif
383 
384  friend class PTimer;
385  };
386 
387  static List * TimerList();
388 
389 
390  private:
391  virtual int64_t InternalGet() const;
392  virtual void InternalSet(int64_t t);
393  void InternalStart(bool once, int64_t resetTime);
394 
395  // Member variables
396  PNotifier m_callback; // Callback function for expired timers.
397  PString m_threadName;
398  bool m_oneshot; // Timer operates once then stops.
399  PIdGenerator::Handle m_handle;
400  atomic<bool> m_running;
401  PTimeInterval m_absoluteTime;
402  mutable PTimedMutex m_callbackMutex;
403 
404  friend class Emitter;
405 
406 // Include platform dependent part of class
407 #ifdef _WIN32
408 #include "msos/ptlib/timer.h"
409 #else
410 #include "unix/ptlib/timer.h"
411 #endif
412 };
413 
414 
437 template <
438  class Work_T,
439  class Pool_T = PQueuedThreadPool<Work_T>
440 >
441 class PPoolTimer : public PTimer
442 {
443  PCLASSINFO(PPoolTimer, PTimer);
444  protected:
445  Pool_T & m_pool;
446  bool m_stopped;
447  public:
448  PPoolTimer(Pool_T & pool)
449  : m_pool(pool)
450  , m_stopped(false)
451  {
452  }
453 
454  ~PPoolTimer()
455  {
456  m_stopped = true;
457  }
458 
459  virtual void OnTimeout()
460  {
461  if (m_stopped)
462  return;
463  Work_T * work = CreateWork();
464  if (work != NULL)
465  m_pool.AddWork(work, GetGroup(*work));
466  }
467 
468  virtual Work_T * CreateWork() = 0;
469  virtual const char * GetGroup(const Work_T & /*work*/) const { return NULL; }
470 
471  PTIMER_OPERATORS(PPoolTimer);
472 };
473 
474 
476 template <
477  class Work_T,
478  class Base_T = Work_T,
479  class Pool_T = PQueuedThreadPool<Base_T>
480 >
481 class PPoolTimerArg0 : public PPoolTimer<Base_T, Pool_T>
482 {
483  typedef PPoolTimer<Base_T, Pool_T> BaseClass;
484  PCLASSINFO(PPoolTimerArg0, BaseClass);
485  public:
486  PPoolTimerArg0(Pool_T & pool)
487  : BaseClass(pool)
488  {
489  }
490 
491  virtual Work_T * CreateWork() { return new Work_T(); }
492 
493  PTIMER_OPERATORS(PPoolTimerArg0);
494 };
495 
496 
498 template <
499  class Work_T,
500  typename Arg1,
501  class Base_T = Work_T,
502  class Pool_T = PQueuedThreadPool<Base_T>
503 >
504 class PPoolTimerArg1: public PPoolTimer<Base_T, Pool_T>
505 {
506  typedef PPoolTimer<Base_T, Pool_T> BaseClass;
507  PCLASSINFO(PPoolTimerArg1, BaseClass);
508  protected:
509  Arg1 m_arg1;
510  public:
511  PPoolTimerArg1(Pool_T & pool, Arg1 arg1)
512  : BaseClass(pool)
513  , m_arg1(arg1)
514  {
515  }
516 
517  virtual Work_T * CreateWork() { return new Work_T(m_arg1); }
518 
519  PTIMER_OPERATORS(PPoolTimerArg1);
520 };
521 
522 
524 template <
525  class Work_T,
526  typename Arg1,
527  typename Arg2,
528  class Base_T = Work_T,
529  class Pool_T = PQueuedThreadPool<Base_T>
530 >
531 class PPoolTimerArg2: public PPoolTimer<Base_T, Pool_T>
532 {
533  typedef PPoolTimer<Base_T, Pool_T> BaseClass;
534  PCLASSINFO(PPoolTimerArg2, BaseClass);
535  protected:
536  Arg1 m_arg1;
537  Arg2 m_arg2;
538  public:
539  PPoolTimerArg2(Pool_T & pool, Arg1 arg1, Arg2 arg2)
540  : BaseClass(pool)
541  , m_arg1(arg1)
542  , m_arg2(arg2)
543  {
544  }
545 
546  virtual Work_T * CreateWork() { return new Work_T(m_arg1, m_arg2); }
547 
548  PTIMER_OPERATORS(PPoolTimerArg2);
549 };
550 
551 
553 template <
554  class Work_T,
555  typename Arg1,
556  typename Arg2,
557  typename Arg3,
558  class Base_T = Work_T,
559  class Pool_T = PQueuedThreadPool<Base_T>
560 >
561 class PPoolTimerArg3: public PPoolTimer<Base_T, Pool_T>
562 {
563  typedef PPoolTimer<Base_T, Pool_T> BaseClass;
564  PCLASSINFO(PPoolTimerArg3, BaseClass);
565  protected:
566  Arg1 m_arg1;
567  Arg2 m_arg2;
568  Arg3 m_arg3;
569  public:
570  PPoolTimerArg3(Pool_T & pool, Arg1 arg1, Arg2 arg2, Arg3 arg3)
571  : BaseClass(pool)
572  , m_arg1(arg1)
573  , m_arg2(arg2)
574  , m_arg3(arg3)
575  {
576  }
577 
578  virtual Work_T * CreateWork() { return new Work_T(m_arg1, m_arg2, m_arg3); }
579 
580  PTIMER_OPERATORS(PPoolTimerArg3);
581 };
582 
583 
584 #else
585 
586 namespace PTimer
587 {
589  unsigned Resolution();
590 };
591 
592 #endif // P_TIMERS
593 
594 #endif // PTLIB_TIMER_H
595 
596 
597 // End Of File ///////////////////////////////////////////////////////////////
virtual void InternalSet(int64_t t)
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:51
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:2164
Class specialisation for PNotifierTemplate&lt;P_INT_PTR&gt;
This class implements critical section mutexes using the most efficient mechanism available on the ho...
Definition: mutex.h:270
unsigned int Handle
Definition: id_generator.h:46
unsigned Resolution()
PTimeInterval & operator=(const PTimeInterval &other)
High Level (queued work item) thread pool.
Definition: threadpool.h:383
bool PBoolean
Definition: object.h:174
#define PPROFILE_EXCLUDE(func)
Definition: object.h:1259
The character string class.
Definition: pstring.h:108
virtual void PrintOn(ostream &strm) const
Output the time interval to the I/O stream.
This class defines a thread mutual exclusion object.
Definition: mutex.h:101
PTimeInterval Tick()
static const PString & Empty()
Return an empty string.
virtual int64_t InternalGet() const
#define PCLASSINFO_WITH_CLONE(cls, par)
Declare all the standard PTLib class information, plus Clone().
Definition: object.h:2167