Fix a logic error that occur when using the timerfd interface
authorMark Michelson <mmichelson@digium.com>
Fri, 16 Jan 2009 19:54:39 +0000 (19:54 +0000)
committerMark Michelson <mmichelson@digium.com>
Fri, 16 Jan 2009 19:54:39 +0000 (19:54 +0000)
This sequence of events posed a problem

timerfd_timer_open
timerfd_timer_enable_continuous
timerfd_timer_set_rate
timerfd_timer_disable_continuous

The reason was that the timing module was written under the assumption
that timerfd_timer_set_rate would not be called between enabling and
disabling continuous mode. What happened in this situation was that
timerfd_timer_enable_continuous saved off our previously set timer (in this
situation a 0 timer, meaning it never runs out). Then timerfd_timer_disable_continuous
would restore this 0 timer, even though it logically should set the timer to be whatever
was set in timerfd_timer_set_rate.

Now the behavior in timerfd_timer_set_rate is to overwrite the saved timer that may
or may not have been set in timerfd_timer_enable_continuous. Even if
timerfd_timer_enable_continuous has not been previously called, this will not harm the
operation.

Thanks to Terry Wilson for discovering the problem and giving me a really great debug
capture that pointed out the problem clearly

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@168898 65c4cc65-6c06-0410-ace0-fbb531ad65f3

res/res_timing_timerfd.c

index d0ad175..60dc2d5 100644 (file)
@@ -131,13 +131,21 @@ static void timerfd_timer_close(int handle)
 
 static int timerfd_timer_set_rate(int handle, unsigned int rate)
 {
-       struct itimerspec itspec;
-       itspec.it_value.tv_sec = 0;
-       itspec.it_value.tv_nsec = rate ? (long) (1000000000 / rate) : 0L;
-       itspec.it_interval.tv_sec = itspec.it_value.tv_sec;
-       itspec.it_interval.tv_nsec = itspec.it_value.tv_nsec;
+       struct timerfd_timer *our_timer, find_helper = {
+               .handle = handle,
+       };
+
+       if (!(our_timer = ao2_find(timerfd_timers, &find_helper, OBJ_POINTER))) {
+               ast_log(LOG_ERROR, "Couldn't find timer with handle %d\n", handle);
+               return -1;
+       }
+
+       our_timer->saved_timer.it_value.tv_sec = 0;
+       our_timer->saved_timer.it_value.tv_nsec = rate ? (long) (1000000000 / rate) : 0L;
+       our_timer->saved_timer.it_interval.tv_sec = our_timer->saved_timer.it_value.tv_sec;
+       our_timer->saved_timer.it_interval.tv_nsec = our_timer->saved_timer.it_value.tv_nsec;
 
-       return timerfd_settime(handle, 0, &itspec, NULL);
+       return timerfd_settime(handle, 0, &our_timer->saved_timer, NULL);
 }
 
 static void timerfd_timer_ack(int handle, unsigned int quantity)