Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Timers to be cancelled during callback execution #94

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/misc/pv/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class epicsShareClass TimerCallback {
epicsTime timeToRun;
double period;
bool onList;
bool cancelled;
bool processing;
friend class Timer;
struct IncreasingTime;
};
Expand Down
15 changes: 13 additions & 2 deletions src/misc/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ namespace epics { namespace pvData {

TimerCallback::TimerCallback()
: period(0.0),
onList(false)
onList(false),
cancelled(false),
processing(false)
{
}

Expand All @@ -50,6 +52,7 @@ void Timer::addElement(TimerCallbackPtr const & timerCallback)
temp.push_back(timerCallback);

timerCallback->onList = true;
timerCallback->cancelled = false;

// merge sorted lists.
// for us effectively insertion sort.
Expand All @@ -60,6 +63,11 @@ void Timer::addElement(TimerCallbackPtr const & timerCallback)
bool Timer::cancel(TimerCallbackPtr const &timerCallback)
{
Lock xx(mutex);
/* If we're processing, just set the cancel flag */
if (timerCallback->processing) {
timerCallback->cancelled = true;
return true;
}
if(!timerCallback->onList) return false;
if(!alive) {
timerCallback->onList = false;
Expand Down Expand Up @@ -107,6 +115,7 @@ void Timer::run()
TimerCallbackPtr work;
work.swap(queue.front());
work->onList = false;
work->processing = true;
queue.pop_front();

{
Expand All @@ -115,7 +124,9 @@ void Timer::run()
work->callback();
}

if(work->period > 0.0 && alive) {
work->processing = false;

if(work->period > 0.0 && alive && !work->cancelled) {
work->timeToRun += work->period;
addElement(work);
}
Expand Down
Loading