CComponentsTimer: add support for nano seconds

To enable nano mode, parameter is_nano must set to true.
This commit is contained in:
2016-10-20 23:49:09 +02:00
parent 542de648f5
commit d0dd14040f
2 changed files with 20 additions and 10 deletions

View File

@@ -36,10 +36,11 @@
using namespace std;
CComponentsTimer::CComponentsTimer(const int& interval)
CComponentsTimer::CComponentsTimer(const int& interval, bool is_nano)
{
tm_thread = 0;
tm_interval = interval;
tm_enable_nano = is_nano;
sl_stop_timer = sigc::mem_fun(*this, &CComponentsTimer::stopTimer);
@@ -59,7 +60,10 @@ void CComponentsTimer::runSharedTimerAction()
while(tm_enable && tm_interval > 0) {
tm_mutex.lock();
OnTimer();
if (!tm_enable_nano)
mySleep(tm_interval);
else
usleep((useconds_t)tm_interval);
tm_mutex.unlock();
}
@@ -142,10 +146,11 @@ bool CComponentsTimer::stopTimer()
return false;
}
void CComponentsTimer::setTimerInterval(const int& seconds)
void CComponentsTimer::setTimerInterval(const int& interval, bool is_nano)
{
if (tm_interval == seconds)
if (tm_interval == interval && tm_enable_nano == is_nano)
return;
tm_interval = seconds;
tm_enable_nano = is_nano;
tm_interval = interval;
}

View File

@@ -48,6 +48,8 @@ class CComponentsTimer : public sigc::trackable
///refresh interval in seconds
int tm_interval;
bool tm_enable_nano;
///init function to init shared timer action
static void* initThreadAction(void *arg);
@@ -71,10 +73,12 @@ class CComponentsTimer : public sigc::trackable
* @param[in] interval
* @li int interval in seconds, default value=1 (1 sec)
* If init value for interval > 0, timer starts immediately
* @li bool default = false as seconds mode, true = nano seconds mode
* @see
* setTimerInterval();
*/
CComponentsTimer(const int& interval = 1);
CComponentsTimer(const int& interval = 1, bool is_nano = false);
~CComponentsTimer();
/**Starts timer thread
@@ -105,15 +109,16 @@ class CComponentsTimer : public sigc::trackable
*/
bool isRun() const {return tm_thread;};
/**set interval in seconds
* @param[in] seconds
* @li int
/**set timer interval
* @param[in] interval
* @li int default interval in seconds, if second parameter = true interval is used as nano seconds
* @li bool default = false as seconds mode, true = nano seconds mode
* @return
* void
* @see
* tm_interval
*/
void setTimerInterval(const int& seconds);
void setTimerInterval(const int& interval, bool is_nano = false);
/**Provides a signal handler to receive any function or methode.
* Use this in your class where ever you need time controled actions.