[Box Backup-commit] COMMIT r1390 - box/chris/general/lib/common

boxbackup-dev@fluffy.co.uk boxbackup-dev@fluffy.co.uk
Sat, 10 Mar 2007 16:53:11 +0000


Author: chris
Date: 2007-03-10 16:53:11 +0000 (Sat, 10 Mar 2007)
New Revision: 1390

Added:
   box/chris/general/lib/common/Timer.h
Log:
Added timer header file


Copied: box/chris/general/lib/common/Timer.h (from rev 1385, box/chris/merge/lib/common/Timer.h)
===================================================================
--- box/chris/general/lib/common/Timer.h	                        (rev 0)
+++ box/chris/general/lib/common/Timer.h	2007-03-10 16:53:11 UTC (rev 1390)
@@ -0,0 +1,84 @@
+// --------------------------------------------------------------------------
+//
+// File
+//		Name:    Timer.h
+//		Purpose: Generic timers which execute arbitrary code when
+//			 they expire.
+//		Created: 5/11/2006
+//
+// --------------------------------------------------------------------------
+
+#ifndef TIMER__H
+#define TIMER__H
+
+#include <sys/time.h>
+
+#include <vector>
+
+#include "MemLeakFindOn.h"
+#include "BoxTime.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