Skip to content

Commit

Permalink
os: Timer's functions now synchronise with timeout()
Browse files Browse the repository at this point in the history
This fixes cleanup problems that when a timer is killed, it may still execute.

Signed-off-by: Peter Soetens <peter@thesourceworks.com>
  • Loading branch information
Peter Soetens committed Nov 22, 2013
1 parent 5859d82 commit 6ce2bc0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion rtt/os/Timer.cpp
Expand Up @@ -89,8 +89,8 @@ namespace RTT {
if (ret == -1) {
// a timer expired
// First: reset/reprogram the timer that expired:
MutexLock locker(m);
{
MutexLock locker(m);
// detect corner case for resize:
if ( next_timer_id < int(mtimers.size()) ) {
// now clear or reprogram it.
Expand Down
29 changes: 28 additions & 1 deletion rtt/os/Timer.hpp
Expand Up @@ -75,7 +75,7 @@ namespace RTT
TimeService* mTimeserv;
base::ActivityInterface* mThread;
Semaphore msem;
mutable Mutex m;
mutable MutexRecursive m;
typedef TimeService::nsecs Time;
/**
* Index in vector is the timer id.
Expand Down Expand Up @@ -115,13 +115,20 @@ namespace RTT
/**
* This function is called each time an armed or periodic timer expires.
* The user must implement this method to catch the time outs.
* @note all other Timer functions synchronise with this function.
* It's allowed to reprogram the timer from within timeout(), but any
* external thread will be blocked until timeout() returns.
* @param timer_id The number of the timer that expired.
*/
virtual void timeout(TimerId timer_id);

/**
* Change the maximum number of timers in this object.
* Any added timer with id >= \a max will be removed.
* @note This function synchronises with timeout().
* In case timeout() is executing, and this function is called from another thread,
* this function will block until timeout() finished executing.
* In case this function is called called from within the timeout() thread, it will not block.
*/
void setMaxTimers(TimerId max);

Expand All @@ -132,6 +139,10 @@ namespace RTT
* @param period The period when the timer should expire.
* This is a floating point number.
* @see killTimer to disable it again.
* @note This function synchronises with timeout().
* In case timeout() is executing, and this function is called from another thread,
* this function will block until timeout() finished executing.
* In case this function is called called from within the timeout() thread, it will not block.
*/
bool startTimer(TimerId timer_id, Seconds period);

Expand All @@ -141,25 +152,41 @@ namespace RTT
* @param wait_time The time in seconds from now, when the
* timer should expire. This is a floating point number.
* @see killTimer to disable it before it fires.
* @note This function synchronises with timeout().
* In case timeout() is executing, and this function is called from another thread,
* this function will block until timeout() finished executing.
* In case this function is called called from within the timeout() thread, it will not block.
*/
bool arm(TimerId timer_id, Seconds wait_time);

/**
* Returns the remaining time before this timer elapses.
* @retval 0.0 if the timer is not armed or has already elapsed.
* @return the remaining time in seconds.
* @note This function synchronises with timeout().
* In case timeout() is executing, and this function is called from another thread,
* this function will block until timeout() finished executing.
* In case this function is called called from within the timeout() thread, it will not block.
*/
TimeService::Seconds timeRemaining(TimerId timer_id) const;

/**
* Check if a given timer id is armed.
* @param timer_id The number of the timer, starting from zero.
* @note This function synchronises with timeout().
* In case timeout() is executing, and this function is called from another thread,
* this function will block until timeout() finished executing.
* In case this function is called called from within the timeout() thread, it will not block.
*/
bool isArmed(TimerId timer_id) const;

/**
* Disable an armed timer.
* @param timer_id The number of the timer, starting from zero.
* @note This function synchronises with timeout().
* In case timeout() is executing, and this function is called from another thread,
* this function will block until timeout() finished executing.
* In case this function is called called from within the timeout() thread, it will not block.
*/
bool killTimer(TimerId timer_id);

Expand Down

0 comments on commit 6ce2bc0

Please sign in to comment.