MeVisLabToolboxReference
MeVis/Foundation/Sources/MLProfilingManager/Include/mlTimer.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //----------------------------------------------------------------------------------
00003 // A timer implementation that is thread aware.
00004 //
00005 // \file    mlTimer.h
00006 // \author  Marcus Barann
00007 // \date    6/2010
00008 //
00009 //----------------------------------------------------------------------------------
00010 #ifndef _ML_TIMER_H_
00011 #define _ML_TIMER_H_
00012 
00013 
00014 #include "mlProfilingManagerDllExport.h"
00015 
00016 #include <mlErrorOutput.h>
00017 #include <mlTimeCounter.h>
00018 
00019 
00020 class MLTimeProfile;
00021 
00022 class MLPROFILINGMANAGER_EXPORT MLTimer
00023 {
00024 public:
00025   MLTimer();
00026   ~MLTimer();
00027 
00028   inline void start();
00029   inline void stop();
00030 
00032   inline void start(MLTimer& otherToPause);
00033 
00035   inline void stop(MLTimer& otherToResume);
00036 
00040   long double elapsed() const { return _elapsed; }
00041 
00044   long double consumed() const { return _elapsed - _pausedElapsed; }
00045   unsigned int startCount() const { return _startCount; }
00046 
00047 private:
00048   long double       _startTime;
00049   long double       _pausedStartTime;
00050   long double       _pausedElapsed;
00051   long double       _elapsed;
00052   bool              _isActive;
00053   bool              _isPaused;
00054   unsigned int      _startCount;
00055 
00056   friend class MLTimeProfile;
00057   friend class MLCallGraphNode;
00058 };
00059 
00060 
00061 inline void MLTimer::start()
00062 {
00063   if (!_isActive) {
00064     _elapsed = 0;
00065     _pausedElapsed = 0;
00066     _pausedStartTime = 0;
00067     _startTime = ML_NAMESPACE::TimeCounter().getCurrentValueInSeconds();
00068     _isActive = true;
00069     ++_startCount;
00070   } else {
00071     ML_PRINT_ERROR("MLTimer::start", "Timer is already active", "");
00072   }
00073 }
00074 
00075 
00076 inline void MLTimer::stop()
00077 {
00078   if (_isActive) {
00079     _elapsed = ML_NAMESPACE::TimeCounter().getCurrentValueInSeconds()-_startTime;
00080     _isActive = false;
00081   } else {
00082     ML_PRINT_ERROR("MLTimer::stop", "Timer is not active", "");
00083   }
00084 }
00085 
00086 
00087 inline void MLTimer::stop(MLTimer& otherToResume)
00088 {
00089   long double now = ML_NAMESPACE::TimeCounter().getCurrentValueInSeconds();
00090 
00091   if (otherToResume._isActive) {
00092     if (otherToResume._isPaused) {
00093       otherToResume._pausedElapsed += now-otherToResume._pausedStartTime;
00094       otherToResume._isPaused = false;
00095     } else {
00096       ML_PRINT_ERROR("MLTimer::stop", "Other timer is not paused", "");
00097     }
00098   } else {
00099     ML_PRINT_ERROR("MLTimer::stop", "Other timer is not active", "");
00100   }
00101 
00102   if (_isActive) {
00103     _elapsed = now-_startTime;
00104     _isActive = false;
00105   } else {
00106     ML_PRINT_ERROR("MLTimer::stop", "Timer is not active", "");
00107   }
00108 }
00109 
00110 
00111 inline void MLTimer::start(MLTimer& otherToPause)
00112 {
00113   long double now = ML_NAMESPACE::TimeCounter().getCurrentValueInSeconds();
00114 
00115   if (otherToPause._isActive) {
00116     if (!otherToPause._isPaused) {
00117       otherToPause._pausedStartTime = now;
00118       otherToPause._isPaused = true;
00119     } else {
00120       ML_PRINT_ERROR("MLTimer::start", "Other timer is already paused", "");
00121     }
00122   } else {
00123     ML_PRINT_ERROR("MLTimer::start", "Other timer is not active", "");
00124   }
00125 
00126   if (!_isActive) {
00127     _elapsed = 0;
00128     _pausedElapsed = 0;
00129     _pausedStartTime = 0;
00130     _startTime = now;
00131     _isActive = true;
00132     ++_startCount;
00133   } else {
00134     ML_PRINT_ERROR("MLTimer::start", "Timer is already active", "");
00135   }
00136 }
00137 
00138 
00139 #endif // _ML_TIMER_H