summaryrefslogtreecommitdiff
path: root/lib/common/Timer.h
blob: ba6d71f4553c3071f45b21605fe0a4e36af53172 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// --------------------------------------------------------------------------
//
// File
//		Name:    Timer.h
//		Purpose: Generic timers which execute arbitrary code when
//			 they expire.
//		Created: 5/11/2006
//
// --------------------------------------------------------------------------

#ifndef TIMER__H
#define TIMER__H

#ifdef HAVE_SYS_TIME_H
	#include <sys/time.h>
#endif

#include <vector>

#include "BoxTime.h"

#include "MemLeakFindOn.h"

class Timer;

// --------------------------------------------------------------------------
//
// Class
//		Name:    Timers
//		Purpose: Static class to manage all timers and arrange 
//			 efficient delivery of wakeup signals
//		Created: 19/3/04
//
// --------------------------------------------------------------------------
class Timers
{
	private:
	static std::vector<Timer*>* spTimers;
	static void Reschedule();

	static bool sRescheduleNeeded;
	static void SignalHandler(int iUnused);
	
	public:
	static void Init();
	static void Cleanup();
	static void Add   (Timer& rTimer);
	static void Remove(Timer& rTimer);
	static void RequestReschedule()
	{
		sRescheduleNeeded = true;
	}

	static void RescheduleIfNeeded()
	{
		if (sRescheduleNeeded) 
		{
			Reschedule();
		}
	}
};

class Timer
{
public:
	Timer(size_t timeoutSecs);
	virtual ~Timer();
	Timer(const Timer &);
	Timer &operator=(const Timer &);

public:
	box_time_t   GetExpiryTime() { return mExpires; }
	virtual void OnExpire();
	bool         HasExpired()
	{
		Timers::RescheduleIfNeeded();
		return mExpired; 
	}
	
private:
	box_time_t mExpires;
	bool       mExpired;
};

#include "MemLeakFindOff.h"

#endif // TIMER__H